/// <summary> /// Reads the ticker symbol/exchange symbol info from a csv file /// </summary> /// <param name="symbolFileInfo">FileInfo - Points to the symbol file</param> /// <returns>bool - true if the exchanges were in the file, false if any blank second column was found</returns> public bool ReadSymbolFileIntoDictionary(FileInfo symbolFileInfo) { SymbolList = new Dictionary <string, string>(); bool foundNoBlankExchangeColumn = true; using (StreamReader sr = new StreamReader(symbolFileInfo.FullName)) { try { //string buffer = sr.ReadLine(); //if (buffer != null && buffer.Split(',')[0].ToLower().Contains("symbol")) // buffer = sr.ReadLine(); while (!sr.EndOfStream) { string buffer = sr.ReadLine(); if (buffer != null && !buffer.Contains(",")) { buffer += ","; } string[] columns = buffer.Split(','); string symbol = columns[0]; { if (columns[1].Length == 0) { foundNoBlankExchangeColumn = false; ExchangeLookup lookup = new ExchangeLookup(symbol); columns[1] = lookup.GetExchangeForSymbol(symbol); } symbol = symbol.Replace("\"", ""); if (symbol.Contains(@"^")) { continue; } // Skip duplicate symbols if (!SymbolList.ContainsKey(symbol)) { SymbolList.Add(symbol, columns[1]); linelist.Add(ColumnJoiner.JoinColumns(columns)); } } } } catch (Exception e) { Console.WriteLine(e); } } return(foundNoBlankExchangeColumn); }
/// <summary> /// Loops through a symbol list, looking up the exchange where the security is traded /// and writing the files to an {exchange}\{firstletter}\{symbol} folder /// </summary> /// <param name="symbolList">A list of symbols</param> /// <returns>Task (void)</returns> private async Task LoopSymbolList(Dictionary <string, string> symbolList) { foreach (string ticker in symbolList.Keys) { //if (System.String.Compare(ticker, "QCOM", System.StringComparison.Ordinal) <= 0) // continue; //DirectoryInfo exchangeDirectoryInfo; string symbol = ticker.Replace("^", "-").Trim(); if (symbol.Contains(@"\") || symbol.Contains(@"/")) { continue; } // Look up the exchange on GoogleFinance if it is not in the symbolList item string exchange; var kvpair = symbolList.FirstOrDefault(s => s.Key == ticker); if (kvpair.Value == null) { // Look up the exchange from GoogleFinance ExchangeLookup exchangeLookup = new ExchangeLookup(); exchange = exchangeLookup.GetExchangeForSymbol(ticker); } else { exchange = kvpair.Value; } // used in debugging //if (ticker == "ABEV") // Debug.WriteLine("here"); DirectoryInfo outputFolder; if (OutputDirectory != null || OutputDirectory.Length > 0) { if (!OutputDirectory.EndsWith(@"\")) { OutputDirectory += @"\"; } outputFolder = new DirectoryInfo(OutputDirectory); // the factory adds the "minute"; } else { outputFolder = new DirectoryInfo(Config.GetDefaultDownloadDirectory()); } DirectoryInfo symbolDirectoryInfo = SymbolDirectoryFactory.Create(MinuteDirectoryFactory.Create(outputFolder), symbol); // find out if files have been downloaded to this OutputDirectory before. // If not get the max available from Google Finance (15 days) // Otherwise get the files that have not been downloaded. var files = symbolDirectoryInfo.GetFiles().OrderBy(f => f.Name); int numberOfDays; if (!files.Any()) { numberOfDays = 15; } else { var lastfile = files.LastOrDefault(); numberOfDays = NumberOfDaysSinceLastDownload(lastfile); //numberOfDays = 7; } _uriBuilder.SetTickerName(symbol); _uriBuilder.SetExchangeName(exchange); var uri = _uriBuilder.GetGetPricesUrlForLastNumberOfDays(numberOfDays); // download Data Ticker = ticker; // this assignment is superflous because the ticker is returned in the header // Set the QueryString in the Header to the symbol and OutputDirectory // so they will be returned in the DownloadDataCompleted event handler NameValueCollection myQueryStringCollection = new NameValueCollection { { "symbol", ticker }, { "OutputDirectory", symbolDirectoryInfo.FullName } }; _wClient.QueryString = myQueryStringCollection; // Get the data async await _wClient.DownloadDataTaskAsync(uri); } }
/// <summary> /// Reads the ticker symbol/exchange symbol info from a csv file /// </summary> /// <param name="symbolFileInfo">FileInfo - Points to the symbol file</param> /// <returns>bool - true if the exchanges were in the file, false if any blank second column was found</returns> public bool ReadSymbolFileIntoDictionary(FileInfo symbolFileInfo) { SymbolList = new Dictionary<string, string>(); bool foundNoBlankExchangeColumn = true; using (StreamReader sr = new StreamReader(symbolFileInfo.FullName)) { try { //string buffer = sr.ReadLine(); //if (buffer != null && buffer.Split(',')[0].ToLower().Contains("symbol")) // buffer = sr.ReadLine(); while(!sr.EndOfStream) { string buffer = sr.ReadLine(); if (buffer != null && !buffer.Contains(",")) { buffer += ","; } string[] columns = buffer.Split(','); string symbol = columns[0]; { if (columns[1].Length == 0) { foundNoBlankExchangeColumn = false; ExchangeLookup lookup = new ExchangeLookup(symbol); columns[1] = lookup.GetExchangeForSymbol(symbol); } symbol = symbol.Replace("\"", ""); if (symbol.Contains(@"^")) { continue; } // Skip duplicate symbols if (!SymbolList.ContainsKey(symbol)) { SymbolList.Add(symbol, columns[1]); linelist.Add(ColumnJoiner.JoinColumns(columns)); } } } } catch (Exception e) { Console.WriteLine(e); } } return foundNoBlankExchangeColumn; }
/// <summary> /// Loops through a symbol list, looking up the exchange where the security is traded /// and writing the files to an {exchange}\{firstletter}\{symbol} folder /// </summary> /// <param name="symbolList">A list of symbols</param> /// <returns>Task (void)</returns> private async Task LoopSymbolList(Dictionary<string, string> symbolList) { foreach (string ticker in symbolList.Keys) { //if (System.String.Compare(ticker, "QCOM", System.StringComparison.Ordinal) <= 0) // continue; //DirectoryInfo exchangeDirectoryInfo; string symbol = ticker.Replace("^", "-").Trim(); if (symbol.Contains(@"\") || symbol.Contains(@"/")) continue; // Look up the exchange on GoogleFinance if it is not in the symbolList item string exchange; var kvpair = symbolList.FirstOrDefault(s => s.Key == ticker); if (kvpair.Value == null) { // Look up the exchange from GoogleFinance ExchangeLookup exchangeLookup = new ExchangeLookup(); exchange = exchangeLookup.GetExchangeForSymbol(ticker); } else { exchange = kvpair.Value; } // used in debugging //if (ticker == "ABEV") // Debug.WriteLine("here"); DirectoryInfo outputFolder; if (OutputDirectory != null || OutputDirectory.Length > 0) { if (!OutputDirectory.EndsWith(@"\")) { OutputDirectory += @"\"; } outputFolder = new DirectoryInfo(OutputDirectory); // the factory adds the "minute"; } else { outputFolder = new DirectoryInfo(Config.GetDefaultDownloadDirectory()); } DirectoryInfo symbolDirectoryInfo = SymbolDirectoryFactory.Create(MinuteDirectoryFactory.Create(outputFolder), symbol); // find out if files have been downloaded to this OutputDirectory before. // If not get the max available from Google Finance (15 days) // Otherwise get the files that have not been downloaded. var files = symbolDirectoryInfo.GetFiles().OrderBy(f => f.Name); int numberOfDays; if (!files.Any()) { numberOfDays = 15; } else { var lastfile = files.LastOrDefault(); numberOfDays = NumberOfDaysSinceLastDownload(lastfile); //numberOfDays = 7; } _uriBuilder.SetTickerName(symbol); _uriBuilder.SetExchangeName(exchange); var uri = _uriBuilder.GetGetPricesUrlForLastNumberOfDays(numberOfDays); // download Data Ticker = ticker; // this assignment is superflous because the ticker is returned in the header // Set the QueryString in the Header to the symbol and OutputDirectory // so they will be returned in the DownloadDataCompleted event handler NameValueCollection myQueryStringCollection = new NameValueCollection { {"symbol", ticker}, {"OutputDirectory", symbolDirectoryInfo.FullName} }; _wClient.QueryString = myQueryStringCollection; // Get the data async await _wClient.DownloadDataTaskAsync(uri); } }