예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        /// <summary>
        /// Reads JJs symbols.txt and checks to see if the zip files exist.
        /// </summary>
        /// <param name="symbolFileInfo"></param>
        /// <returns>true if they all do</returns>
        public static bool CheckJJList(FileInfo symbolFileInfo)
        {
            List <string> noexchange = new List <string>();
            Dictionary <string, string> symbolDictionary = new Dictionary <string, string>();
            //SymbolListBuilder builder = new SymbolListBuilder();


            string symbols = string.Empty;

            using (StreamReader sr = new StreamReader(symbolFileInfo.FullName))
            {
                try
                {
                    while (!sr.EndOfStream)
                    {
                        string symbol = sr.ReadLine();
                        if (!string.IsNullOrEmpty(symbol))
                        {
                            symbolDictionary.Add(symbol, "");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(ex.Message);
                }
            }
            var uriBuilder = new DownloadURIBuilder("NYSE", "AAMRQ");

            foreach (var ticker in symbolDictionary)
            {
                uriBuilder.SetTickerName(ticker.Key);
                uriBuilder.SetExchangeName(ticker.Value);
                var    uri     = uriBuilder.GetGetPricesUrlToDownloadAllData(DateTime.Now);
                var    wClient = new WebClient();
                byte[] buf     = wClient.DownloadData(uri);
                string rstring = Encoding.Default.GetString(buf);
                bool   found   = false;
                if (rstring.Length > 0)
                {
                    string[] lines = rstring.Split('\n');
                    foreach (string line in lines)
                    {
                        if (line.StartsWith("a"))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        noexchange.Add(ticker.Key);
                    }
                }
            }

            // deletes any old bad csv files and writes new ones
            SaveBadSymbolList(new FileInfo(symbolFileInfo.FullName.Replace("txt", "csv")), noexchange);
            if (noexchange.Count > 0)
            {
                System.Diagnostics.Debug.WriteLine("There were bad symbols in JJs List");
                return(false);
            }
            return(true);
        }