[Quantproject-developers] QuantDownloader/Downloader TickerDownloader.cs,1.2,1.3
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2004-03-21 19:40:38
|
Update of /cvsroot/quantproject/QuantDownloader/Downloader In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5757/Downloader Modified Files: TickerDownloader.cs Log Message: Useless code has been deleted and some methods renamed; added methods to perform dicotomic search on tickers'quotes Index: TickerDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/TickerDownloader.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TickerDownloader.cs 30 Nov 2003 22:38:51 -0000 1.2 --- TickerDownloader.cs 21 Mar 2004 19:30:28 -0000 1.3 *************** *** 17,27 **** private string p_quTicker; private int p_numRows; ! private DateTime startDate = new DateTime( 2000 , 1 , 1 ); private DateTime endDate = DateTime.Today; private int endDay = DateTime.Now.Day; private int endMonth = DateTime.Now.Month; private int endYear = DateTime.Now.Year; public TickerDownloader( WebDownloader myForm, DataRow currentDataTickerRow, string quTicker , int numRows ) { p_myForm = myForm; p_currentDataTickerRow = currentDataTickerRow; --- 17,29 ---- private string p_quTicker; private int p_numRows; ! private DateTime startDate; private DateTime endDate = DateTime.Today; private int endDay = DateTime.Now.Day; private int endMonth = DateTime.Now.Month; private int endYear = DateTime.Now.Year; + private DateTime INITIAL_DATE = new DateTime(1980, 1, 1); public TickerDownloader( WebDownloader myForm, DataRow currentDataTickerRow, string quTicker , int numRows ) { + this.startDate = this.INITIAL_DATE; p_myForm = myForm; p_currentDataTickerRow = currentDataTickerRow; *************** *** 30,58 **** this.oleDbConnection1 = myForm.OleDbConnection1; } - private void addTickerTo_gridDataSet() - { - - DataRow newRow = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].NewRow(); - newRow[ "tiTicker" ] = p_quTicker; - newRow[ "currentState" ] = "Start"; - //lock( p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows ) - //{ - try - { - //MessageBox.Show( (p_myForm == null).ToString() ); - //MessageBox.Show( (p_myForm.dsTickerCurrentlyDownloaded == null).ToString() ); - //MessageBox.Show( (p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ] == null).ToString() ); - //MessageBox.Show( (p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows == null).ToString() ); - //MessageBox.Show( (newRow == null).ToString() ); - p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].Rows.Add( newRow ); - } - catch (Exception ex) - { - MessageBox.Show( ex.ToString() ); - } - //} - p_myForm.dataGrid1.Refresh(); - } private void removeTickerFrom_gridDataSet() { --- 32,37 ---- this.oleDbConnection1 = myForm.OleDbConnection1; } + /* private void removeTickerFrom_gridDataSet() { *************** *** 65,68 **** --- 44,48 ---- p_myForm.dataGrid1.Refresh(); } + */ private void updateCurrentStatus( string newState ) { *************** *** 89,93 **** odc.ExecuteNonQuery(); } ! private void writeFile_tickerCsv_forNextTimeFrame( DateTime currBeginDate , DateTime currEndDate ) { int a = currBeginDate.Month - 1; --- 69,74 ---- odc.ExecuteNonQuery(); } ! ! private void importTickerForCurrentTimeFrame( DateTime currBeginDate , DateTime currEndDate ) { int a = currBeginDate.Month - 1; *************** *** 99,103 **** int numTrials = 1; - //updateCurrentStatus( " 1 " ); while (numTrials < 5) { --- 80,83 ---- *************** *** 107,139 **** 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 = 10000; ! HttpWebResponse hwr = (HttpWebResponse)Req.GetResponse(); - - //updateCurrentStatus( " 2 " ); Stream strm = hwr.GetResponseStream(); - //updateCurrentStatus( " 3 " ); StreamReader sr = new StreamReader(strm); ! // StreamWriter sw=new StreamWriter( "C:\\Documents and Settings\\Glauco\\My Documents\\Visual Studio Projects\\QuantProject\\csvFiles\\" + p_quTicker + ".csv"); ! // sw.Write(myString); ! // sw.Close(); DataBaseImporter dataBaseImporter = ! new DataBaseImporter( this.oleDbConnection1 , sr ); dataBaseImporter.ImportTicker( p_quTicker ); sr.Close(); strm.Close(); ! ! ! // import_tickerCsv_into_quotesCsv(); ! updateCurrentStatus( d + "/" + e + "/" + f ); numTrials = 6 ; - //updateCurrentStatus( " scritto file! " ); } catch (Exception exception) { MessageBox.Show( exception.ToString() ); ! updateCurrentStatus( "Tentativo: " + numTrials ); numTrials++; if (numTrials > 5) --- 87,110 ---- 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 = 25000; HttpWebResponse hwr = (HttpWebResponse)Req.GetResponse(); Stream strm = hwr.GetResponseStream(); 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(); ! hwr.Close(); ! ! updateCurrentStatus( d + "/" + e + "/" + f ); numTrials = 6 ; } catch (Exception exception) { MessageBox.Show( exception.ToString() ); ! updateCurrentStatus( "Trial: " + numTrials ); numTrials++; if (numTrials > 5) *************** *** 142,147 **** } } ! private void writeFile_tickerCsv() { DateTime currBeginDate = new DateTime(); currBeginDate = startDate; --- 113,122 ---- } } ! ! private void setTimeFrameAndImportTickerForEachTimeFrame(double numDaysOfTimeFrame) { + if(numDaysOfTimeFrame == 0) + return; + DateTime currBeginDate = new DateTime(); currBeginDate = startDate; *************** *** 149,188 **** { DateTime currEndDate = new DateTime(); ! if ( DateTime.Compare( DateTime.Today , currBeginDate.AddDays( 200 ) ) < 0 ) currEndDate = DateTime.Today; else ! currEndDate = currBeginDate.AddDays( 200 ); ! writeFile_tickerCsv_forNextTimeFrame( currBeginDate , currEndDate ); currBeginDate = currEndDate.AddDays( 1 ); } } ! private void import_tickerCsv_into_quotesCsv() { ! string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ! "C:\\Documents and Settings\\Glauco\\My Documents\\Visual Studio Projects\\QuantProject\\csvFiles\\" + ! ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\""; ! ! System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(sConnectionString); ! objConn.Open(); ! System.Data.OleDb.OleDbCommand odCommand = new System.Data.OleDb.OleDbCommand(); ! odCommand.Connection = objConn; try { ! odCommand.CommandText = "insert into quotes.csv SELECT '" + p_quTicker + "' as Ticker, * FROM " + p_quTicker + ".csv"; ! odCommand.ExecuteNonQuery(); ! } catch (Exception ex) { MessageBox.Show( ex.ToString() ); } ! objConn.Close(); } ! public void downloadTicker() ! { { ! addTickerTo_gridDataSet(); ! writeFile_tickerCsv(); //Monitor.Pulse( p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ); } } } --- 124,288 ---- { DateTime currEndDate = new DateTime(); ! if ( DateTime.Compare( DateTime.Today , currBeginDate.AddDays( numDaysOfTimeFrame ) ) < 0 ) currEndDate = DateTime.Today; else ! currEndDate = currBeginDate.AddDays( numDaysOfTimeFrame ); ! this.importTickerForCurrentTimeFrame ( currBeginDate , currEndDate ); currBeginDate = currEndDate.AddDays( 1 ); } } ! ! private void addTickerTo_gridDataSet() { ! DataRow newRow = p_myForm.DsTickerCurrentlyDownloaded.Tables[ "Tickers" ].NewRow(); ! newRow[ "tiTicker" ] = p_quTicker; ! newRow[ "currentState" ] = "Searching ..."; 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) { MessageBox.Show( ex.ToString() ); } ! p_myForm.dataGrid1.Refresh(); } ! ! public void DownloadTicker() { ! // update grid in webdownloader form ! addTickerTo_gridDataSet(); ! /* if(tickerIsInDatabase && p_myForm.UpdateFlagYes) ! { ! // try to import ticker before the first quote ! ! // try to import ticker after the last quote ! } ! else ! // tickers'quotes are downloaded for the first time ! {*/ ! if(this.p_myForm.checkBoxIsDicotomicSearchActivated.Checked == true) ! this.startDate = firstAvailableDateOnYahoo(this.INITIAL_DATE, this.endDate); ! setTimeFrameAndImportTickerForEachTimeFrame(200); ! ! //} ! //Monitor.Pulse( p_myForm.dsTickerCurrentlyDownloaded.Tables[ "Tickers" ] ); } + public void DownloadTicker(DateTime startingDate) + { + this.INITIAL_DATE = startingDate; + this.DownloadTicker(); + + } + + + private bool isAtLeastOneDateAvailable(StreamReader streamReader, int daysToBeTested) + { + string Line; + 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; + + } + + + private bool getResponseForTimeWindow( DateTime initialDateOfTheTimeWindow, + int daysOfTheTimeWindow ) + { + int a = initialDateOfTheTimeWindow.Month - 1; + int b = initialDateOfTheTimeWindow.Day; + int c = initialDateOfTheTimeWindow.Year; + DateTime endDateOfTheTimeWindow = initialDateOfTheTimeWindow.AddDays(daysOfTheTimeWindow); + int d = endDateOfTheTimeWindow.Month - 1; + int e = endDateOfTheTimeWindow.Day; + int f = endDateOfTheTimeWindow.Year; + HttpWebRequest Req; + HttpWebResponse hwr; + Stream strm; + StreamReader sr; + bool response = false; + int numTrials = 1; + while(numTrials < 5) + { + try + { + 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 = 20000; + hwr = (HttpWebResponse)Req.GetResponse(); + strm = hwr.GetResponseStream(); + sr = new StreamReader(strm); + response = this.isAtLeastOneDateAvailable(sr, daysOfTheTimeWindow ); + sr.Close(); + strm.Close(); + hwr.Close(); + numTrials = 6; + } + catch (Exception exception) + { + MessageBox.Show( exception.ToString() + "\n\n for: " + initialDateOfTheTimeWindow.ToString()); + numTrials++; + } + } + return response; + } + private DateTime getMiddleDate(DateTime startingDate, + DateTime endingDate) + { + DateTime middleDate; + double differenceInDays = endingDate.Subtract(startingDate).Days; + middleDate = startingDate.AddDays(differenceInDays / 2); + return middleDate; + } + + + private DateTime firstAvailableDateOnYahoo(DateTime startingDate, + DateTime endingDate) + { + + if(startingDate == this.INITIAL_DATE) + // at the first call, when the actual parameters are INITIAL_DATE and now + // the dicotomic search may be useless ... + if(getResponseForTimeWindow(startingDate, 10) == true) + { + return startingDate; + } + + DateTime middleDate = getMiddleDate(startingDate, endingDate); + + if(endingDate.Subtract(startingDate).Days <= 10) + { + return startingDate; + } + else + { + if (getResponseForTimeWindow(middleDate, 10) == true) + { + return firstAvailableDateOnYahoo(startingDate, middleDate); + } + else + { + return firstAvailableDateOnYahoo(middleDate, endingDate); + } + } + } } |