[Quantproject-developers] QuantDownloader/Downloader DataBaseImporter.cs,1.6,1.7 TickerDownloader.cs
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2004-05-11 22:06:51
|
Update of /cvsroot/quantproject/QuantDownloader/Downloader In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22169/Downloader Modified Files: DataBaseImporter.cs TickerDownloader.cs WebDownloader.cs Log Message: Code has been cleaned and some methods were renamed. TickerDownloader has been rivisited Index: WebDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/WebDownloader.cs,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** WebDownloader.cs 25 Apr 2004 17:24:49 -0000 1.11 --- WebDownloader.cs 11 May 2004 22:06:40 -0000 1.12 *************** *** 164,168 **** this.dataGrid1.Location = new System.Drawing.Point(328, 8); this.dataGrid1.Name = "dataGrid1"; ! this.dataGrid1.Size = new System.Drawing.Size(304, 456); this.dataGrid1.TabIndex = 1; // --- 164,168 ---- this.dataGrid1.Location = new System.Drawing.Point(328, 8); this.dataGrid1.Name = "dataGrid1"; ! this.dataGrid1.Size = new System.Drawing.Size(432, 456); this.dataGrid1.TabIndex = 1; // *************** *** 369,378 **** this.radioButtonOverWriteYes.Size = new System.Drawing.Size(288, 32); this.radioButtonOverWriteYes.TabIndex = 0; ! this.radioButtonOverWriteYes.Text = "Download all quotes, overwriting the existing ones in database"; // // WebDownloader // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); ! this.ClientSize = new System.Drawing.Size(664, 470); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBoxUpdateDatabaseOptions, --- 369,378 ---- this.radioButtonOverWriteYes.Size = new System.Drawing.Size(288, 32); this.radioButtonOverWriteYes.TabIndex = 0; ! this.radioButtonOverWriteYes.Text = "Download all quotes, deleting all existing ones in database"; // // WebDownloader // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); ! this.ClientSize = new System.Drawing.Size(768, 470); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBoxUpdateDatabaseOptions, *************** *** 388,391 **** --- 388,392 ---- this.Name = "WebDownloader"; this.Text = "Web downloader"; + this.Load += new System.EventHandler(this.WebDownloader_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBoxWebDownloaderOptions.ResumeLayout(false); *************** *** 554,558 **** --- 555,561 ---- new DataColumn( dt.Columns[ "tiTicker" ].ColumnName , dt.Columns[ "tiTicker" ].DataType ) ); this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Columns.Add( "currentState" , System.Type.GetType( "System.String" ) ); + this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Columns.Add( "databaseUpdated" , System.Type.GetType( "System.String" ) ); this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Columns.Add( "adjustedClose" , System.Type.GetType( "System.String" ) ); + this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Columns.Add( "adjCloseToCloseRatio" , System.Type.GetType( "System.String" ) ); this.dataGrid1.DataSource = this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ]; } *************** *** 662,671 **** } ! public bool IsUpdateOptionSelected { get { ! return (this.radioButtonDownloadBeforeMinAndAfterMax.Checked || ! this.radioButtonDownloadOnlyAfterMax.Checked); } } --- 665,680 ---- } ! private void WebDownloader_Load(object sender, System.EventArgs e) ! { ! // this is just used for testing code ! ! ! } ! ! public bool IsBeforeAndAfterSelected { get { ! return this.radioButtonDownloadBeforeMinAndAfterMax.Checked; } } *************** *** 678,681 **** --- 687,712 ---- } } + public bool IsDownloadAllSelected + { + get + { + return (this.radioButtonOverWriteNo.Checked || + this.radioButtonOverWriteYes.Checked); + } + } + public bool IsOverWriteYesSelected + { + get + { + return this.radioButtonOverWriteYes.Checked; + } + } + public bool IsOverWriteNoSelected + { + get + { + return this.radioButtonOverWriteNo.Checked; + } + } } } Index: TickerDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/TickerDownloader.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** TickerDownloader.cs 17 Apr 2004 14:14:41 -0000 1.4 --- TickerDownloader.cs 11 May 2004 22:06:40 -0000 1.5 *************** *** 4,7 **** --- 4,8 ---- using System.Net; using System.Windows.Forms; + using QuantProject.DataAccess; using QuantProject.DataAccess.Tables; using QuantProject.ADT; *************** *** 26,29 **** --- 27,32 ---- private int endYear = DateTime.Now.Year; private int numberOfQuotesInDatabase; + private DataTable downloadedValuesFromSource = new DataTable("quotes"); + private OleDbSingleTableAdapter adapter; public TickerDownloader( WebDownloader myForm, DataRow currentDataTickerRow, string quTicker , int numRows ) *************** *** 35,38 **** --- 38,43 ---- p_numRows = numRows; this.oleDbConnection1 = myForm.OleDbConnection1; + this.adapter = new OleDbSingleTableAdapter("SELECT * FROM quotes WHERE 1=2", + this.downloadedValuesFromSource); } *************** *** 59,68 **** } ! private void updateCurrentStatusAdjustedClose(string stateOfAdjustedCloseValue ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( "tiTicker='" + p_quTicker + "'" ); ! myRows[ 0 ][ "adjustedClose" ] = stateOfAdjustedCloseValue; p_myForm.dataGrid1.Refresh(); } --- 64,93 ---- } ! private void updateCurrentStatusAdjustedClose(string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( "tiTicker='" + p_quTicker + "'" ); ! myRows[ 0 ][ "adjustedClose" ] = status; ! p_myForm.dataGrid1.Refresh(); ! } ! } ! ! private void updateCurrentStatusAdjustedCloseToCloseRatio(string status ) ! { ! lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) ! { ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( "tiTicker='" + p_quTicker + "'" ); ! myRows[ 0 ][ "adjCloseToCloseRatio" ] = status; ! p_myForm.dataGrid1.Refresh(); ! } ! } ! ! private void updateCurrentStatusDatabaseUpdated(string status ) ! { ! lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) ! { ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( "tiTicker='" + p_quTicker + "'" ); ! myRows[ 0 ][ "databaseUpdated" ] = status; p_myForm.dataGrid1.Refresh(); } *************** *** 83,86 **** --- 108,197 ---- } + + private void addNewAdjustedValueInTable( StreamReader streamReader, + DataTable tableToWhichValuesHaveToBeAdded ) + { + string Line; + string[] LineIn; + DataRow row; + + Line = streamReader.ReadLine(); + Line = streamReader.ReadLine(); + + while ( Line != null && ! Line.StartsWith("<")) + { + LineIn=Line.Split(','); + row = tableToWhichValuesHaveToBeAdded.NewRow(); + row[0] = DateTime.Parse( LineIn[0] ); + row[1] = Double.Parse(LineIn[6]); + tableToWhichValuesHaveToBeAdded.Rows.Add(row); + + Line = streamReader.ReadLine(); + } + } + + private DataTable getTableOfNewAdjustedValues( DateTime currBeginDate , DateTime currEndDate ) + { + int a = currBeginDate.Month - 1; + int b = currBeginDate.Day; + int c = currBeginDate.Year; + int d = currEndDate.Month - 1; + int e = currEndDate.Day; + int f = currEndDate.Year; + DataTable table = new DataTable(); + DataColumn date = new DataColumn("quDate", System.Type.GetType("System.DateTime")); + DataColumn adjustedValue = new DataColumn("quAdjustedClose", System.Type.GetType("System.Double")); + table.Columns.Add(date); + table.Columns.Add(adjustedValue); + try + { + HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http:" + "//table.finance.yahoo.com/table.csv?a=" + + a + "&b=" + b + "&c=" + c +"&d=" + d + "&e=" + e + "&f=" + f + "&s=" + p_quTicker + "&y=0&g=d&ignore=.csv"); + Req.Method = "GET"; + Req.Timeout = ConstantsProvider.TimeOutValue; + HttpWebResponse hwr = (HttpWebResponse)Req.GetResponse(); + Stream strm = hwr.GetResponseStream(); + StreamReader sr = new StreamReader(strm); + this.addNewAdjustedValueInTable(sr, table); + sr.Close(); + strm.Close(); + //hwr.Close(); + } + catch (Exception exception) + { + MessageBox.Show( exception.ToString() ); + } + return table; + } + + private void addValuesToTable( StreamReader streamReader ) + { + string Line; + string[] LineIn; + Line = streamReader.ReadLine(); + Line = streamReader.ReadLine(); + + while ( Line != null && ! Line.StartsWith("<")) + { + LineIn=Line.Split(','); + + DataRow myRow =this.downloadedValuesFromSource.NewRow(); + + myRow[ "quTicker" ] = this.p_quTicker; + myRow[ "quDate" ]=DateTime.Parse( LineIn[0] ); + myRow[ "quOpen" ]=Double.Parse( LineIn[1] ); + myRow[ "quHigh" ]=Double.Parse( LineIn[2] ); + myRow[ "quLow" ]=Double.Parse( LineIn[3] ); + myRow[ "quClose" ]=Double.Parse( LineIn[4] ); + myRow[ "quVolume" ]=Double.Parse( LineIn[5] ); + myRow[ "quAdjustedClose" ]=Double.Parse( LineIn[6] ); + + this.downloadedValuesFromSource.Rows.Add(myRow); + + Line = streamReader.ReadLine(); + } + } + + private void importTickerForCurrentTimeFrame( DateTime currBeginDate , DateTime currEndDate ) { *************** *** 106,112 **** StreamReader sr = new StreamReader(strm); ! DataBaseImporter dataBaseImporter = ! new DataBaseImporter( this.oleDbConnection1 , sr, this.p_myForm.radioButtonOverWriteYes.Checked ); ! dataBaseImporter.ImportTicker( p_quTicker ); sr.Close(); strm.Close(); --- 217,225 ---- StreamReader sr = new StreamReader(strm); ! //DataBaseImporter dataBaseImporter = ! //new DataBaseImporter( this.oleDbConnection1 , sr, this.p_myForm.radioButtonOverWriteYes.Checked ); ! //dataBaseImporter.ImportTicker( p_quTicker ); ! this.addValuesToTable(sr); ! sr.Close(); strm.Close(); *************** *** 127,134 **** } ! private void setTimeFrameAndImportTickerForEachTimeFrame(double numDaysOfTimeFrame) { ! if(numDaysOfTimeFrame == 0) ! return; DateTime currBeginDate = new DateTime(); --- 240,246 ---- } ! private DataTable getTableOfDownloadedValues() { ! double numDaysOfTimeFrame = ConstantsProvider.MaxNumDaysDownloadedAtEachConnection; DateTime currBeginDate = new DateTime(); *************** *** 144,147 **** --- 256,261 ---- currBeginDate = currEndDate.AddDays( 1 ); } + return this.downloadedValuesFromSource; + } *************** *** 151,155 **** --- 265,271 ---- newRow[ "tiTicker" ] = p_quTicker; newRow[ "currentState" ] = "Searching ..."; + newRow[ "databaseUpdated" ] = "No"; newRow[ "adjustedClose"] = "..."; + newRow[ "adjCloseToCloseRatio"] = "..."; try { *************** *** 175,179 **** this.endDate = Quotes.GetStartDate(this.p_quTicker); this.resetStartDateIfNecessary(); ! this.setTimeFrameAndImportTickerForEachTimeFrame(200); } --- 291,295 ---- this.endDate = Quotes.GetStartDate(this.p_quTicker); this.resetStartDateIfNecessary(); ! this.checkForNewAdjustedAndContinueOrStop(); } *************** *** 192,196 **** } ! private void checkForNewAdjustedValueFromSource() { try --- 308,312 ---- } ! private void checkForNewAdjustedAndContinueOrStop() { try *************** *** 199,206 **** --- 315,334 ---- { this.updateCurrentStatusAdjustedClose("Changed!"); + if (Quotes.IsAdjustedCloseToCloseRatioChanged(this.p_quTicker, + this.getTableOfNewAdjustedValues(Quotes.GetStartDate(this.p_quTicker), + Quotes.GetEndDate(this.p_quTicker)))) + { + this.updateCurrentStatusAdjustedCloseToCloseRatio("Changed!"); + } + else + { + this.updateCurrentStatusAdjustedCloseToCloseRatio("OK"); + } } else + //download is executed { this.updateCurrentStatusAdjustedClose("OK"); + this.commitDownloadedValuesToDatabase(); } } *************** *** 217,223 **** this.startDate = Quotes.GetEndDate(this.p_quTicker); this.endDate = DateTime.Today; ! this.checkForNewAdjustedValueFromSource(); ! this.setTimeFrameAndImportTickerForEachTimeFrame(200); ! } --- 345,349 ---- this.startDate = Quotes.GetEndDate(this.p_quTicker); this.endDate = DateTime.Today; ! this.checkForNewAdjustedAndContinueOrStop(); } *************** *** 235,243 **** return Single.Parse(LineIn[6]); } public void DownloadTicker() { // update grid in webdownloader form ! addTickerTo_gridDataSet(); this.numberOfQuotesInDatabase = Quotes.GetNumberOfQuotes(this.p_quTicker); if(this.numberOfQuotesInDatabase>0 && p_myForm.IsUpdateOptionSelected) // there are some ticker's quotes in the database and --- 361,425 ---- return Single.Parse(LineIn[6]); } + + private void commitDownloadedValuesToDatabase() + { + Quotes.ComputeCloseToCloseValues(this.downloadedValuesFromSource); + this.adapter.OleDbDataAdapter.ContinueUpdateOnError = true; + if(this.p_myForm.IsOverWriteYesSelected) + Quotes.Delete(this.p_quTicker); + this.adapter.OleDbDataAdapter.Update(this.downloadedValuesFromSource); + this.updateCurrentStatusDatabaseUpdated("YES"); + } + + private void resetAndImportTicker() + { + this.resetStartDateIfNecessary(); + this.commitDownloadedValuesToDatabase(); + } + + + public void DownloadTicker() { // update grid in webdownloader form ! this.downloadedValuesFromSource.Rows.Clear(); ! addTickerTo_gridDataSet(); this.numberOfQuotesInDatabase = Quotes.GetNumberOfQuotes(this.p_quTicker); + if(this.numberOfQuotesInDatabase < 1) + // ticker's quotes are downloaded for the first time + { + this.resetAndImportTicker(); + } + // in all these cases some ticker's quotes are in the database + // and the options choosen by the user in the web downloader form are checked + else if(this.p_myForm.IsOnlyAfterLastQuoteSelected && + this.numberOfQuotesInDatabase >= 1) + { + this.downloadTickerAfterLastQuote(); + } + else if(this.p_myForm.IsBeforeAndAfterSelected && + this.numberOfQuotesInDatabase >= 1) + { + this.downloadTickerBeforeFirstQuote(); + this.downloadTickerAfterLastQuote(); + } + else if(this.p_myForm.IsOverWriteNoSelected && + this.numberOfQuotesInDatabase >= 1) + { + this.resetStartDateIfNecessary(); + this.checkForNewAdjustedAndContinueOrStop(); + } + else if(this.p_myForm.IsOverWriteYesSelected && + this.numberOfQuotesInDatabase >= 1) + { + this.resetAndImportTicker(); + } + + // ticker's quotes are downloaded for the first time or + // the user has chosen to download all quotes + + + /* + if(this.numberOfQuotesInDatabase>0 && p_myForm.IsUpdateOptionSelected) // there are some ticker's quotes in the database and *************** *** 262,267 **** setTimeFrameAndImportTickerForEachTimeFrame(200); } ! //Monitor.Pulse( p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ); ! } public void DownloadTicker(DateTime startingDate) --- 444,450 ---- setTimeFrameAndImportTickerForEachTimeFrame(200); } ! ! */ ! } public void DownloadTicker(DateTime startingDate) Index: DataBaseImporter.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/DataBaseImporter.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** DataBaseImporter.cs 17 Apr 2004 14:16:29 -0000 1.6 --- DataBaseImporter.cs 11 May 2004 22:06:40 -0000 1.7 *************** *** 64,68 **** public void ImportTicker( string ticker ) { ! string Line; string[] LineIn; --- 64,72 ---- public void ImportTicker( string ticker ) { ! //all quotes are deleted if in the web downloader form ! // the radio button "over Write existing record" is checked ! if(this.overWriteExistingRecords) ! Quotes.Delete(ticker); ! string Line; string[] LineIn; *************** *** 100,107 **** myDataSet.Tables["Data"].Rows.Add(myRow);*/ ! //the corresponding record is deleted if in the web downloader form ! // the radio button "over Write existing record" is checked ! if(this.overWriteExistingRecords) ! Quotes.Delete(ticker, DateTime.Parse( LineIn[0] )); Quotes.Add(ticker,DateTime.Parse( LineIn[0] ), Double.Parse( LineIn[1] ), Double.Parse(LineIn[2]), Double.Parse(LineIn[3]), Double.Parse(LineIn[4]), --- 104,108 ---- myDataSet.Tables["Data"].Rows.Add(myRow);*/ ! Quotes.Add(ticker,DateTime.Parse( LineIn[0] ), Double.Parse( LineIn[1] ), Double.Parse(LineIn[2]), Double.Parse(LineIn[3]), Double.Parse(LineIn[4]), |