/// <summary>
        /// Gets teh Url from the Builder
        /// </summary>
        /// <returns>string - the url calculated in the Builder</returns>
        private string getDownloadURI()
        {
            string ticker = textBoxTicker.Text;
            string exchange = textBoxExchange.Text;

            if (String.IsNullOrEmpty(ticker))
            {
                return string.Empty;
            }

            DownloadURIBuilder uriBuilder = new DownloadURIBuilder(exchange, ticker);

            if (radioButtonAllData.Checked)
            {
                return uriBuilder.GetGetPricesUrlToDownloadAllData(DateTime.Now);
            }
            else if (radioButtonLastQuoute.Checked)
            {
                return uriBuilder.GetGetPricesUrlForLastQuote();
            }
            else if (radioButtonSince.Checked)
            {
                DateTime startDate = dateTimePickerSinceDate.Value.Date,
                    endDate = DateTime.Now.Date;
                if (endDate < startDate)
                { //It's impossible to download data from the future. That's why no URL is returned in this case.
                    return string.Empty;
                }
                else
                {
                    return uriBuilder.GetGetPricesUrlForRecentData(startDate, endDate);
                }
            }
            else if (radioButtonMinutes.Checked)
            {
                return uriBuilder.GetGetPricesUrlForLastNumberOfDays(15);
            }
            else
            {
                return string.Empty;
            }
        }
Example #2
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);
            }
        }