/// <summary> /// Writes a ticker symbol's downloaded data to a file /// </summary> /// <param name="tradingDate">The date for the trades</param> /// <param name="buffer">the csv data to be written</param> /// <param name="directory">the base folder to write the data to. Generally this will be the exchange folder and single letter folder</param> /// <param name="ticker">the symbol to write. If a folder for the symbols in NYSE\A\{symbol} does not exist it will be created</param> /// <returns>A generic task for the async call. There is not actual result, but Async/Await does not allow voids.</returns> public async Task WriteStreamAsync(DateTime tradingDate, string buffer, string directory, string ticker) { if (directory.Length == 0) { directory = Config.GetDefaultDownloadDirectory(); } if (!directory.EndsWith(@"\")) { directory += @"\"; } if (ticker.Length == 0) { throw new NullReferenceException("Ticker symbol is null"); } // Add the ticker symbol directory to the \exchange\singleletter\ directory and make sure it exists. string folder = directory + ticker + @"\"; if (!System.IO.Directory.Exists(folder)) { System.IO.Directory.CreateDirectory(folder); } // Create the dated filename string filename = FilenameDateFormatter.FormatDt(tradingDate) + "_trade.csv"; // Create the final path for writing the stream string filepath = folder + filename; // No point if writing if there is no data in the buffer // For example, on a bank holiday if (buffer.Length > 0) { // Check to see if the trading day is today, // and if so overwrite the current day's file. // If we download during the trading day, we will get the latest day's trades // up to the amount of delay Google builds into their quote mechanism. (I think 15 minutes) // If the current hour is after say 4:45 GMT-5 (East Coast time) we will get all of the day's prices if (tradingDate.Day == DateTime.Now.Day) { // If today is Saturday or Sunday, the equity markets are closed. if (!(DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)) { await WriteFileAsync(buffer, filepath, filename); } } // If we write a file in the above if block, we do not need to write it again // If we have a file in the folder already for a date, we do not need to write it again if (!File.Exists(filename)) { await WriteFileAsync(buffer, filepath, filename); } } }
/// <summary> /// Builds the output file name by check the form's check boxes /// </summary> /// <returns>A filename</returns> private string BuildOutputFilename() { StringBuilder sb = new StringBuilder(); //if (textBoxExchange.Text.Length > 0) // sb.Append(textBoxExchange.Text + "_"); sb.Append(textBoxTicker.Text); if (checkBoxRawData.Checked) { sb.Append("RawData"); } else if (radioButtonMinutes.Checked) { if (checkBoxDateTime.Checked) { sb.Append("Milliseconds"); } else { sb.Append("DateTime"); } } else if (radioButtonAllData.Checked) { //sb.Append("AllData"); } else if (radioButtonSince.Checked) { sb.Append("Since"); DateTime dt = System.Convert.ToDateTime(dateTimePickerSinceDate.Text); sb.Append(FilenameDateFormatter.FormatDt(dt)); sb.Append("-"); sb.Append(FilenameDateFormatter.FormatDt(DateTime.Now)); } if (radioButtonLastQuoute.Checked) { sb.AppendFormat("_LastQuote_{0}", FilenameDateFormatter.FormatDt(DateTime.Now)); } sb.Append(".csv"); return(sb.ToString()); }