Encog.App.Quant.Loader.Yahoo.YahooDownload.LoadAllData C# (CSharp) Method

LoadAllData() public method

Load financial data.
public LoadAllData ( String ticker, String output, CSVFormat outputFormat, System.DateTime from, System.DateTime to ) : void
ticker String The ticker symbol.
output String The output file.
outputFormat Encog.Util.CSV.CSVFormat The output format.
from System.DateTime Starting date.
to System.DateTime Ending date.
return void
        public void LoadAllData(String ticker, String output, CSVFormat outputFormat, DateTime from,
                                DateTime to)
        {
            try
            {
                Uri urlData = BuildURL(ticker, from, to);
                WebRequest httpData = WebRequest.Create(urlData);
                var responseData = (HttpWebResponse) httpData.GetResponse();

                Stream istreamData = responseData.GetResponseStream();
                var csvData = new ReadCSV(istreamData, true, CSVFormat.English);

                TextWriter tw = new StreamWriter(output);
                tw.WriteLine("date,time,open price,high price,low price,close price,volume,adjusted price");

                while (csvData.Next())
                {
                    DateTime date = csvData.GetDate("date");
                    double adjustedClose = csvData.GetDouble("adj close");
                    double open = csvData.GetDouble("open");
                    double close = csvData.GetDouble("close");
                    double high = csvData.GetDouble("high");
                    double low = csvData.GetDouble("low");
                    var volume = (long) csvData.GetDouble("volume");

                    var line = new StringBuilder();
                    line.Append(NumericDateUtil.DateTime2Long(date));
                    line.Append(outputFormat.Separator);
                    line.Append(NumericDateUtil.Time2Int(date));
                    line.Append(outputFormat.Separator);
                    line.Append(outputFormat.Format(open, Precision));
                    line.Append(outputFormat.Separator);
                    line.Append(outputFormat.Format(high, Precision));
                    line.Append(outputFormat.Separator);
                    line.Append(outputFormat.Format(low, Precision));
                    line.Append(outputFormat.Separator);
                    line.Append(outputFormat.Format(close, Precision));
                    line.Append(outputFormat.Separator);
                    line.Append(volume);
                    line.Append(outputFormat.Separator);
                    line.Append(outputFormat.Format(adjustedClose, Precision));
                    tw.WriteLine(line.ToString());
                }

                tw.Close();
            }
            catch (WebException ex)
            {
                throw new QuantError(ex);
            }
        }        
    }

Usage Example

        public void TestYahooDownloadError()
        {
            try
            {
                var yahoo = new YahooDownload();
                yahoo.Precision = 2;
                // load a non-sense ticker, should throw error
                yahoo.LoadAllData("sdfhusdhfuish", OutputName.ToString(), CSVFormat.English,
                                  new DateTime(2000, 01, 01),
                                  new DateTime(2000, 01, 10));

                // bad!
                Assert.IsTrue(false);
            }
            catch (QuantError)
            {
                // good!
            }
        }
All Usage Examples Of Encog.App.Quant.Loader.Yahoo.YahooDownload::LoadAllData