[Quantproject-developers] QuantDownloader/Downloader TickerDownloader.cs, 1.21, 1.22 WebDownloader.
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-11-30 22:52:09
|
Update of /cvsroot/quantproject/QuantDownloader/Downloader In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv28981/Downloader Modified Files: TickerDownloader.cs WebDownloader.cs Log Message: Downloading tickers from Yahoo has been moved to a separate thread: so, the downloader form can be explored by the user also during downloading. The downloader form has been enriched with other information. Only the parameters showed by default by the form have been succesfully tested. Index: WebDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/WebDownloader.cs,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** WebDownloader.cs 2 Jul 2006 20:04:15 -0000 1.21 --- WebDownloader.cs 30 Nov 2008 22:52:04 -0000 1.22 *************** *** 23,27 **** { public OleDbConnection OleDbConnection1 = ConnectionProvider.OleDbConnection; - private System.Windows.Forms.Button button1; public System.Windows.Forms.DataGrid dataGrid1; private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; --- 23,26 ---- *************** *** 30,53 **** private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; [...1240 lines suppressed...] ! void WebDownloaderPaint(object sender, PaintEventArgs e) ! { ! if( this.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] != null) ! this.webDownloaderPaint_refreshTableForGrid(); ! } ! ! void WebDownloaderFormClosing(object sender, FormClosingEventArgs e) ! { ! if (this.downloadingInProgress) ! { ! e.Cancel = true; ! MessageBox.Show("You can't close the form if downloading is still in progress!"); ! } ! else ! { ! e.Cancel = false; ! } ! } ! } } Index: TickerDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/TickerDownloader.cs,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** TickerDownloader.cs 3 Sep 2006 13:45:57 -0000 1.21 --- TickerDownloader.cs 30 Nov 2008 22:52:04 -0000 1.22 *************** *** 1,3 **** --- 1,4 ---- using System; + using System.Threading; using System.Data; using System.IO; *************** *** 17,24 **** private System.Data.OleDb.OleDbConnection oleDbConnection1; private WebDownloader p_myForm; ! private DataRow p_currentDataTickerRow; ! private string p_quTicker; ! private int p_numRows; ! private DateTime INITIAL_DATE = ConstantsProvider.InitialDateTimeForDownload; private DateTime startDate; private DateTime endDate = DateTime.Today; --- 18,33 ---- private System.Data.OleDb.OleDbConnection oleDbConnection1; private WebDownloader p_myForm; ! private int numOfTickersToDownload; ! private bool checkBoxIsDicotomicSearchActivated; ! private bool isCheckCloseToCloseSelected; ! private bool isOverWriteSelected; ! private bool isComputeCloseToCloseRatioSelected; ! private bool isOnlyAfterLastQuoteSelected; ! private bool isOnlyAddMissingSelected; ! private bool isOverWriteYesSelected; ! private bool isSingleQuoteSelected; ! private DateTime selectedDateForSingleQuote; ! private string currentTicker; ! private DateTime INITIAL_DATE; private DateTime startDate; private DateTime endDate = DateTime.Today; *************** *** 27,43 **** private int endYear = DateTime.Now.Year; private int numberOfQuotesInDatabase; ! private DataTable downloadedValuesFromSource = new DataTable("quotes"); private OleDbSingleTableAdapter adapter; private Stream stream; private StreamReader streamReader; ! public TickerDownloader( WebDownloader myForm, DataRow currentDataTickerRow, string quTicker , int numRows ) { ! this.startDate = this.INITIAL_DATE; ! p_myForm = myForm; ! p_currentDataTickerRow = currentDataTickerRow; ! p_quTicker = quTicker; ! p_numRows = numRows; this.oleDbConnection1 = myForm.OleDbConnection1; this.adapter = new OleDbSingleTableAdapter("SELECT * FROM quotes WHERE 1=2", this.downloadedValuesFromSource); --- 36,69 ---- private int endYear = DateTime.Now.Year; private int numberOfQuotesInDatabase; ! private DataTable downloadedValuesFromSource; private OleDbSingleTableAdapter adapter; private Stream stream; private StreamReader streamReader; + private HttpWebResponse httpWebResponse; ! ! private void tickerDownloader_copyPropertiesFromForm( WebDownloader myForm ) { ! this.startDate = myForm.StartingDate; ! numOfTickersToDownload = this.p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Rows.Count; ! checkBoxIsDicotomicSearchActivated = myForm.checkBoxIsDicotomicSearchActivated.Checked; ! isCheckCloseToCloseSelected = myForm.IsCheckCloseToCloseSelected; ! isOverWriteSelected = myForm.IsOverWriteYesSelected; ! isComputeCloseToCloseRatioSelected = myForm.IsComputeCloseToCloseRatioSelected; ! isOnlyAfterLastQuoteSelected = myForm.IsOnlyAfterLastQuoteSelected; ! isOnlyAddMissingSelected = myForm.IsOnlyAddMissingSelected; ! isOverWriteYesSelected = myForm.IsOverWriteYesSelected; ! isSingleQuoteSelected = myForm.IsSingleQuoteSelected; ! selectedDateForSingleQuote = myForm.SelectedDateForSingleQuote; ! } ! ! ! public TickerDownloader( WebDownloader myForm ) ! { ! this.INITIAL_DATE = ConstantsProvider.InitialDateTimeForDownload; ! p_myForm = myForm; ! this.tickerDownloader_copyPropertiesFromForm( myForm ); this.oleDbConnection1 = myForm.OleDbConnection1; + this.downloadedValuesFromSource = new DataTable("quotes"); this.adapter = new OleDbSingleTableAdapter("SELECT * FROM quotes WHERE 1=2", this.downloadedValuesFromSource); *************** *** 63,103 **** lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( columnName + "='" + p_quTicker + "'" ); ! myRows[ 0 ][ "currentState" ] = newState; ! p_myForm.dataGrid1.Refresh(); } } ! private void updateCurrentStatusAdjustedClose(string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( columnName + "='" + p_quTicker + "'" ); ! myRows[ 0 ][ "adjustedClose" ] = status; ! p_myForm.dataGrid1.Refresh(); } } ! private void updateCurrentStatusAdjustedCloseToCloseRatio(string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select(columnName + "='" + p_quTicker + "'" ); ! myRows[ 0 ][ "adjCloseToCloseRatio" ] = status; ! p_myForm.dataGrid1.Refresh(); } } private void updateCurrentStatusDatabaseUpdated(string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select(columnName + "='" + p_quTicker + "'" ); ! myRows[ 0 ][ "databaseUpdated" ] = status; ! p_myForm.dataGrid1.Refresh(); } } --- 89,146 ---- lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! p_myForm.CurrentStateForCurrentUpdatingTicker = newState; ! p_myForm.Invalidate(); ! // string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! // DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( columnName + "='" + p_quTicker + "'" ); ! // myRows[ 0 ][ "currentState" ] = newState; ! // p_myForm.dataGrid1.Refresh(); } } ! private void updateCurrentStatusAdjustedClose( string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! p_myForm.AdjustedCloseInfoForCurrentUpdatingTicker = status; ! p_myForm.Invalidate(); ! // string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! // DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select( columnName + "='" + p_quTicker + "'" ); ! // myRows[ 0 ][ "adjustedClose" ] = status; ! // p_myForm.dataGrid1.Refresh(); } } ! private void updateCurrentStatusAdjustedCloseToCloseRatio( string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! p_myForm.AdjCloseToCloseRatioInfoForCurrentUpdatingTicker = status; ! p_myForm.Invalidate(); ! // string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! // DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select(columnName + "='" + p_quTicker + "'" ); ! // myRows[ 0 ][ "adjCloseToCloseRatio" ] = status; ! // p_myForm.dataGrid1.Refresh(); } } + private void updateCurrentLastQuoteInDB(string status ) + { + lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) + { + p_myForm.LastQuoteInDBForCurrentUpdatingTicker = status; + p_myForm.Invalidate(); + } + } + private void updateCurrentStatusDatabaseUpdated(string status ) { lock( p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ) { ! p_myForm.DatabaseUpdatedInfoForCurrentUpdatingTicker = status; ! p_myForm.Invalidate(); ! // string columnName = p_myForm.DsTickerCurrentlyDownloaded.Tables["Tickers"].Columns[0].ColumnName; ! // DataRow[] myRows = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Select(columnName + "='" + p_quTicker + "'" ); ! // myRows[ 0 ][ "databaseUpdated" ] = status; ! // p_myForm.dataGrid1.Refresh(); } } *************** *** 109,113 **** System.Data.OleDb.OleDbCommand odc = new System.Data.OleDb.OleDbCommand(); odc.CommandText = "insert into faultyTickers ( ftTicker , ftDateTime ) " + ! "values ( '" + p_quTicker + "' , #" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + --- 152,156 ---- System.Data.OleDb.OleDbCommand odc = new System.Data.OleDb.OleDbCommand(); odc.CommandText = "insert into faultyTickers ( ftTicker , ftDateTime ) " + ! "values ( '" + currentTicker + "' , #" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + *************** *** 200,204 **** DataRow myRow =this.downloadedValuesFromSource.NewRow(); ! myRow[ "quTicker" ] = this.p_quTicker; myRow[ "quDate" ]=DateTime.Parse( LineIn[0] ); myRow[ "quOpen" ]=Double.Parse( LineIn[1] ); --- 243,247 ---- DataRow myRow =this.downloadedValuesFromSource.NewRow(); ! myRow[ "quTicker" ] = this.currentTicker; myRow[ "quDate" ]=DateTime.Parse( LineIn[0] ); myRow[ "quOpen" ]=Double.Parse( LineIn[1] ); *************** *** 220,234 **** try { - this.p_myForm.Refresh(); this.setStreamsFromYahoo(currBeginDate, currEndDate); this.addCurrentStreamToTable(); if(this.streamReader!=null) this.streamReader.Close(); else return; ! this.updateCurrentStatus(currEndDate.ToShortDateString()); ! ! //this.updateCurrentStatus( d + "/" + e + "/" + f ); ! } catch (Exception exception) { --- 263,278 ---- try { this.setStreamsFromYahoo(currBeginDate, currEndDate); this.addCurrentStreamToTable(); + this.updateCurrentStatus(currEndDate.ToShortDateString()); if(this.streamReader!=null) + { this.streamReader.Close(); + this.stream.Close(); + this.httpWebResponse.Close(); + } else return; ! } catch (Exception exception) { *************** *** 285,373 **** } ! private void addTickerTo_gridDataSet() ! { ! DataRow newRow = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].NewRow(); ! newRow[ 0 ] = p_quTicker; ! newRow[ "currentState" ] = "Searching ..."; ! newRow[ "databaseUpdated" ] = "No"; ! newRow[ "adjustedClose"] = "..."; ! newRow[ "adjCloseToCloseRatio"] = "..."; ! try ! { ! p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows.Add( newRow ); ! p_myForm.labelNumberOfTickersToDownload.Text = ! Convert.ToString(Convert.ToInt16(p_myForm.labelNumberOfTickersToDownload.Text) - 1); ! } ! catch (Exception ex) ! { ! string notUsed = ex.ToString(); ! //MessageBox.Show( ex.ToString() ); ! } ! p_myForm.dataGrid1.Refresh(); ! } private void resetStartDateIfNecessary() { ! if(this.p_myForm.checkBoxIsDicotomicSearchActivated.Checked == true) this.startDate = firstAvailableDateOnYahoo(this.INITIAL_DATE, this.endDate); } ! private void downloadTickerBeforeFirstQuote() { ! this.endDate = Quotes.GetFirstQuoteDate(this.p_quTicker); this.resetStartDateIfNecessary(); this.checkForNewAdjustedAndContinueOrStop(); } ! private bool getResponseForRepeatedChecks(int numberOfRepeatedChecks) { bool response = false; QuantProject.Data.DataTables.Quotes tickerQuotes = ! new QuantProject.Data.DataTables.Quotes(this.p_quTicker); ! for(int i = 1; i< this.numberOfQuotesInDatabase; i += this.numberOfQuotesInDatabase/numberOfRepeatedChecks) { DateTime dateToCheck = tickerQuotes.GetPrecedingDate(this.startDate, i); ! response = ! Quotes.IsAdjustedCloseChanged(this.p_quTicker, dateToCheck, ! this.adjustedCloseFromSource(dateToCheck)); } return response; } private void checkForNewAdjustedAndContinueOrStop() { try { ! if(this.getResponseForRepeatedChecks(ConstantsProvider.NumberOfCheckToPerformOnAdjustedValues)) { this.updateCurrentStatusAdjustedClose("Changed!"); ! if (Quotes.IsAdjustedCloseToCloseRatioChanged(this.p_quTicker, ! this.getTableOfDownloadedValues(Quotes.GetFirstQuoteDate(this.p_quTicker), ! Quotes.GetLastQuoteDate(this.p_quTicker))) && ! this.p_myForm.IsCheckCloseToCloseSelected) { ! this.updateCurrentStatusAdjustedCloseToCloseRatio("Changed at " + Quotes.DateWithDifferentCloseToClose); ! //stop } ! else { ! if(this.p_myForm.IsCheckCloseToCloseSelected) ! this.updateCurrentStatusAdjustedCloseToCloseRatio("OK"); ! else ! this.updateCurrentStatusAdjustedCloseToCloseRatio("Not Checked"); ! this.updateAdjustedClose(); ! this.updateCurrentStatusAdjustedClose("Updated!"); ! this.downloadedValuesFromSource = this.getTableOfDownloadedValues(Quotes.GetLastQuoteDate(this.p_quTicker), ! DateTime.Now); ! this.commitDownloadedValuesToDatabase(); ! } } ! else ! //download is executed { ! this.downloadedValuesFromSource = this.getTableOfDownloadedValues(); ! this.commitDownloadedValuesToDatabase(); ! this.updateCurrentStatusAdjustedClose("OK"); } } --- 329,464 ---- } ! // private void addTickerTo_gridDataSet() ! // { ! // ! // DataRow newRow = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].NewRow(); ! // newRow[ 0 ] = p_quTicker; ! // newRow[ "currentState" ] = "Searching ..."; ! // newRow[ "databaseUpdated" ] = "No"; ! // newRow[ "adjustedClose"] = "..."; ! // newRow[ "adjCloseToCloseRatio"] = "..."; ! // try ! // { ! // p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows.Add( newRow ); ! // p_myForm.labelNumberOfTickersToDownload.Text = ! // Convert.ToString(Convert.ToInt16(p_myForm.labelNumberOfTickersToDownload.Text) - 1); ! // } ! // catch (Exception ex) ! // { ! // string notUsed = ex.ToString(); ! // //MessageBox.Show( ex.ToString() ); ! // } ! // p_myForm.dataGrid1.Refresh(); ! // } private void resetStartDateIfNecessary() { ! if(this.checkBoxIsDicotomicSearchActivated == true) this.startDate = firstAvailableDateOnYahoo(this.INITIAL_DATE, this.endDate); } ! private void downloadTickers_downloadTickerBeforeFirstQuote() { ! this.endDate = Quotes.GetFirstQuoteDate(this.currentTicker); this.resetStartDateIfNecessary(); this.checkForNewAdjustedAndContinueOrStop(); } ! private bool isAdjustedCloseChanged(int numberOfRepeatedChecks) { bool response = false; + QuantProject.Data.DataTables.Quotes tickerQuotes = ! new QuantProject.Data.DataTables.Quotes(this.currentTicker); ! // for( ! int i = 1; ! // i < this.numberOfQuotesInDatabase && response == false; ! // i += this.numberOfQuotesInDatabase/numberOfRepeatedChecks) { DateTime dateToCheck = tickerQuotes.GetPrecedingDate(this.startDate, i); ! float adjustedCloseFromSource = float.MaxValue; ! try{ ! adjustedCloseFromSource = this.getAdjustedCloseFromSource(dateToCheck); ! this.httpWebResponse.Close(); ! } ! catch(Exception ex){ ! string notUsed = ex.ToString(); ! } ! response = ! Quotes.IsAdjustedCloseChanged( this.currentTicker, dateToCheck, ! adjustedCloseFromSource ); } + return response; } + private void checkForNewAdjustedAndContinueOrStop_dummy() + { + System.Threading.Thread.Sleep(500); + this.updateCurrentStatusAdjustedCloseToCloseRatio("OK"); + this.updateCurrentStatusAdjustedClose("Updated!"); + this.updateCurrentStatusAdjustedClose("OK"); + this.updateCurrentStatus("Yes"); + } + private void checkForNewAdjustedAndContinueOrStop_writeNoQuoteFoundMessageIfTheCase() + { + if(this.downloadedValuesFromSource.Rows.Count == 0) + this.updateCurrentStatus("No quote found"); + } + + private void checkForNewAdjustedAndContinueOrStop_downloadNewQuotesAndCommit() + { + DateTime lastQuoteDateInDB = Quotes.GetLastQuoteDate(this.currentTicker); + this.updateCurrentLastQuoteInDB(lastQuoteDateInDB.ToShortDateString()); + this.downloadedValuesFromSource = + this.getTableOfDownloadedValues( lastQuoteDateInDB , + DateTime.Now ); + this.checkForNewAdjustedAndContinueOrStop_writeNoQuoteFoundMessageIfTheCase(); + this.commitDownloadedValuesToDatabase(); + } + private void checkForNewAdjustedAndContinueOrStop() { try { ! bool adjCloseToCloseRatioChanged = false; ! if ( this.isCheckCloseToCloseSelected ) ! adjCloseToCloseRatioChanged = Quotes.IsAdjustedCloseToCloseRatioChanged( this.currentTicker, ! this.getTableOfDownloadedValues(Quotes.GetFirstQuoteDate(this.currentTicker), ! Quotes.GetLastQuoteDate(this.currentTicker))); ! if(this.isAdjustedCloseChanged(ConstantsProvider.NumberOfCheckToPerformOnAdjustedValues)) { this.updateCurrentStatusAdjustedClose("Changed!"); ! if( !this.isCheckCloseToCloseSelected ) ! //it is necessary to download values first for past adjusted values to be updated ! this.downloadedValuesFromSource = this.getTableOfDownloadedValues(Quotes.GetFirstQuoteDate(this.currentTicker), ! Quotes.GetLastQuoteDate(this.currentTicker)); ! //else values have been already downloaded for checking adjCloseToCloseRatios ! if (this.isCheckCloseToCloseSelected && adjCloseToCloseRatioChanged ) { ! this.updateCurrentStatusAdjustedCloseToCloseRatio("Changed at " + Quotes.DateWithDifferentCloseToClose); ! //stop downloading ticker if the close to close ratio has changed } ! else if ( this.isCheckCloseToCloseSelected && !adjCloseToCloseRatioChanged ) ! //adjusted close changed, but the close to close ratio ! // has not been changed { ! this.updateCurrentStatusAdjustedCloseToCloseRatio("OK"); ! this.updateAdjustedClose(); ! this.checkForNewAdjustedAndContinueOrStop_downloadNewQuotesAndCommit(); ! } ! else if ( !isCheckCloseToCloseSelected ) ! { ! this.updateCurrentStatusAdjustedCloseToCloseRatio("Not Checked"); ! this.updateAdjustedClose(); ! this.checkForNewAdjustedAndContinueOrStop_downloadNewQuotesAndCommit(); ! } } ! else//adjusted close has not been changed ! //download is executed directly { ! this.updateCurrentStatusAdjustedClose("Not changed"); ! this.checkForNewAdjustedAndContinueOrStop_downloadNewQuotesAndCommit(); } } *************** *** 383,391 **** foreach(DataRow row in this.downloadedValuesFromSource.Rows) { ! Quotes.UpdateAdjustedClose(this.p_quTicker, (DateTime)row[Quotes.Date], (float)row[Quotes.AdjustedClose]); } } ! private void downloadTickerForTheSelectedDate(DateTime date) { this.startDate = date; --- 474,483 ---- foreach(DataRow row in this.downloadedValuesFromSource.Rows) { ! Quotes.UpdateAdjustedClose(this.currentTicker, (DateTime)row[Quotes.Date], (float)row[Quotes.AdjustedClose]); } + this.updateCurrentStatusAdjustedClose("Updated"); } ! private void downloadTickers_downloadTickerForTheSelectedDate(DateTime date) { this.startDate = date; *************** *** 394,405 **** } ! private void downloadTickerAfterLastQuote() { ! this.startDate = Quotes.GetLastQuoteDate(this.p_quTicker); this.endDate = DateTime.Today; this.checkForNewAdjustedAndContinueOrStop(); } ! private float adjustedCloseFromSource(DateTime adjustedCloseDate) { string Line; --- 486,502 ---- } ! private void downloadTickers_downloadTickerAfterLastQuote() { ! this.startDate = Quotes.GetLastQuoteDate(this.currentTicker); ! this.updateCurrentLastQuoteInDB(this.startDate.ToShortDateString()); this.endDate = DateTime.Today; + //this.checkForNewAdjustedAndContinueOrStop_dummy(); + // this.resetStartDateIfNecessary(); + // this.downloadedValuesFromSource = this.getTableOfDownloadedValues(); + // this.commitDownloadedValuesToDatabase(); this.checkForNewAdjustedAndContinueOrStop(); } ! private float getAdjustedCloseFromSource(DateTime adjustedCloseDate) { string Line; *************** *** 417,436 **** private void commitDownloadedValuesToDatabase() { ! if(this.p_myForm.IsOverWriteYesSelected) { foreach(DataRow row in this.downloadedValuesFromSource.Rows) ! Quotes.Delete(this.p_quTicker, (DateTime)row["quDate"]); } ! if(this.p_myForm.IsComputeCloseToCloseRatioSelected) QuantProject.DataAccess.Tables.Quotes.ComputeCloseToCloseValues(this.downloadedValuesFromSource); this.adapter.OleDbDataAdapter.ContinueUpdateOnError = true; int rowsUpdated = this.adapter.OleDbDataAdapter.Update(this.downloadedValuesFromSource); if(rowsUpdated > 0) ! this.updateCurrentStatusDatabaseUpdated("YES"); else ! this.updateCurrentStatus("Not found updatable quotes"); } ! private void resetAndImportTicker() { this.resetStartDateIfNecessary(); --- 514,550 ---- private void commitDownloadedValuesToDatabase() { ! if(this.isOverWriteYesSelected) { foreach(DataRow row in this.downloadedValuesFromSource.Rows) ! Quotes.Delete(this.currentTicker, (DateTime)row["quDate"]); } ! if(this.isComputeCloseToCloseRatioSelected) QuantProject.DataAccess.Tables.Quotes.ComputeCloseToCloseValues(this.downloadedValuesFromSource); this.adapter.OleDbDataAdapter.ContinueUpdateOnError = true; int rowsUpdated = this.adapter.OleDbDataAdapter.Update(this.downloadedValuesFromSource); if(rowsUpdated > 0) ! this.updateCurrentStatusDatabaseUpdated("Added " + rowsUpdated.ToString() + " rows"); else ! this.updateCurrentStatusDatabaseUpdated("No row added"); } ! private void downloadTickers_refreshIndexOfCurrentTicker( int indexOfCurrentUpdatingTicker ) ! { ! lock(p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ]) ! { ! p_myForm.IndexOfCurrentUpdatingTicker = indexOfCurrentUpdatingTicker; ! } ! } ! ! private void downloadTickers_refreshCurrentTicker( int indexOfCurrentUpdatingTicker ) ! { ! lock(p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ]) ! { ! this.currentTicker = ! (string)p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows[ indexOfCurrentUpdatingTicker ][0]; ! } ! } ! ! private void downloadTickers_resetAndImportTicker() { this.resetStartDateIfNecessary(); *************** *** 438,520 **** this.commitDownloadedValuesToDatabase(); } ! ! public void DownloadTicker() { ! // update grid in webdownloader form ! Cursor.Current = Cursors.WaitCursor; ! 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(); ! } ! else if(this.p_myForm.IsSingleQuoteSelected) ! { ! if(Quotes.GetTickerQuotes(this.p_quTicker, ! this.p_myForm.SelectedDateForSingleQuote, ! this.p_myForm.SelectedDateForSingleQuote).Rows.Count == 0) ! //there's no quote for the ticker at the given date ! this.downloadTickerForTheSelectedDate(this.p_myForm.SelectedDateForSingleQuote); ! } ! Cursor.Current = Cursors.Default; ! ! // ticker's quotes are downloaded for the first time or ! // the user has chosen to download all quotes ! ! ! } ! public void DownloadTicker(DateTime startingDate) { ! this.INITIAL_DATE = startingDate; ! this.DownloadTicker(); } ! ! private bool isAtLeastOneDateAvailable(StreamReader streamReader, int daysToBeTested) { string Line; ! if(streamReader == null) ! return false; ! Line = streamReader.ReadLine(); ! // column headers are read ! Line = streamReader.ReadLine(); ! //actual values are read ! bool isOneDateAvailableInNextDaysToBeTested = false; ! int numDays = 1; ! while(numDays < daysToBeTested) ! { ! if(Line != null) ! { ! isOneDateAvailableInNextDaysToBeTested = true; ! numDays = daysToBeTested + 1; ! } ! numDays++; } ! return isOneDateAvailableInNextDaysToBeTested; } --- 552,637 ---- this.commitDownloadedValuesToDatabase(); } ! ! private void downloadTickers_setInfoCurrentStatusToDefault() { ! this.updateCurrentLastQuoteInDB("..."); ! this.updateCurrentStatus("..."); ! this.updateCurrentStatusDatabaseUpdated("..."); ! this.updateCurrentStatusAdjustedClose("..."); ! this.updateCurrentStatusAdjustedCloseToCloseRatio("..."); ! } ! ! ! public void DownloadTickers() { ! for( int i = 0; i < this.numOfTickersToDownload; i++ ) ! { ! this.downloadTickers_setInfoCurrentStatusToDefault(); ! this.downloadTickers_refreshCurrentTicker( i ); ! this.downloadTickers_refreshIndexOfCurrentTicker( i ); ! this.numberOfQuotesInDatabase = Quotes.GetNumberOfQuotes(this.currentTicker); ! if(this.numberOfQuotesInDatabase < 1) ! // ticker's quotes are downloaded for the first time ! { ! this.downloadTickers_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.isOnlyAfterLastQuoteSelected && ! this.numberOfQuotesInDatabase >= 1) ! { ! this.downloadTickers_downloadTickerAfterLastQuote(); ! } ! else if(this.isOnlyAddMissingSelected && ! this.numberOfQuotesInDatabase >= 1) ! { ! this.resetStartDateIfNecessary(); ! this.checkForNewAdjustedAndContinueOrStop(); ! } ! else if(this.isOverWriteYesSelected && ! this.numberOfQuotesInDatabase >= 1) ! { ! this.downloadTickers_resetAndImportTicker(); ! } ! else if(this.isSingleQuoteSelected) ! { ! if(Quotes.GetTickerQuotes(this.currentTicker, ! this.selectedDateForSingleQuote, ! this.selectedDateForSingleQuote).Rows.Count == 0) ! //there's no quote for the ticker at the given date ! this.downloadTickers_downloadTickerForTheSelectedDate(this.p_myForm.SelectedDateForSingleQuote); ! } ! p_myForm.Invalidate(); ! Thread.Sleep(200); ! } ! this.p_myForm.DownloadingInProgress = false; } ! private bool isAtLeastOneDateAvailable(StreamReader streamReader, int daysToBeTested) { + bool returnValue = false; string Line; ! try{ ! Line = streamReader.ReadLine(); ! // column headers are read ! Line = streamReader.ReadLine(); ! //actual values are read ! ! int numDays = 1; ! while(numDays < daysToBeTested) ! { ! if(Line != null) ! { ! returnValue = true; ! numDays = daysToBeTested + 1; ! } ! numDays++; ! } } ! catch (Exception exception) ! { ! string notUsed = exception.ToString(); ! } ! return returnValue; } *************** *** 575,586 **** int f = endDate.Year; HttpWebRequest Req; - HttpWebResponse hwr; string url; url = "http:" + "//ichart.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"; ! //url = "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"; int numTrials = 1; ! while(numTrials < 5) { try --- 692,703 ---- int f = endDate.Year; HttpWebRequest Req; string url; url = "http:" + "//ichart.yahoo.com/table.csv?a=" ! + a + "&b=" + b + "&c=" + c +"&d=" + d + "&e=" + e + "&f=" + f + "&s=" + currentTicker + "&y=0&g=d&ignore=.csv"; ! // url = "http:" + "//table.finance.yahoo.com/table.csv?a=" ! // + a + "&b=" + b + "&c=" + c +"&d=" + d + "&e=" + e + "&f=" + f + "&s=" + currentTicker + "&y=0&g=d&ignore=.csv"; int numTrials = 1; ! int maxNumTrials = 3; ! while( numTrials < maxNumTrials ) { try *************** *** 589,604 **** Req.Method = "GET"; Req.Timeout = ConstantsProvider.TimeOutValue; ! hwr = (HttpWebResponse)Req.GetResponse(); ! this.stream = hwr.GetResponseStream(); this.streamReader = new StreamReader(this.stream); ! numTrials = 6; } - catch (Exception exception) { string notUsed = exception.ToString(); numTrials++; ! if(numTrials==5) ! FaultyTickers.AddOrUpdate(this.p_quTicker, DateTime.Now.Date); // to add faulty tickers to database table //throw new Exception("It has not been possible to set streams from Yahoo: \n\n" + --- 706,720 ---- Req.Method = "GET"; Req.Timeout = ConstantsProvider.TimeOutValue; ! this.httpWebResponse = (HttpWebResponse)Req.GetResponse(); ! this.stream = this.httpWebResponse.GetResponseStream(); this.streamReader = new StreamReader(this.stream); ! numTrials = maxNumTrials; } catch (Exception exception) { string notUsed = exception.ToString(); numTrials++; ! if( numTrials == maxNumTrials ) ! FaultyTickers.AddOrUpdate(this.currentTicker, DateTime.Now.Date); // to add faulty tickers to database table //throw new Exception("It has not been possible to set streams from Yahoo: \n\n" + *************** *** 616,619 **** --- 732,741 ---- bool response = false; response = this.isAtLeastOneDateAvailable(this.streamReader,daysOfTheTimeWindow ); + if( response == true ) + this.updateCurrentStatus("Found quotes from " + initialDateOfTheTimeWindow.ToShortDateString() + + " to " + initialDateOfTheTimeWindow.AddDays(daysOfTheTimeWindow).ToShortDateString() ); + else // response == false + this.updateCurrentStatus("No quote found"); + return response; } |