Exemple #1
0
        /// <summary>
        /// Gets just the past days stock history
        /// </summary>
        public IList <StockHistoryModel> GetLastCloseStockHistory()
        {
            // Create a new array to store randomized apiKeys and copy the apiKeys array into the new array
            string[] randomApiKeys = new string[apiKeys.Length];

            Array.ConstrainedCopy(apiKeys, 0, randomApiKeys, 0, apiKeys.Length);

            // Randomize the order of the api keys so we hopefully never max any of them out
            Random rng = new Random();
            int    n   = randomApiKeys.Length;

            while (n > 1)
            {
                int    k    = rng.Next(n--);
                string temp = randomApiKeys[n];
                randomApiKeys[n] = randomApiKeys[k];
                randomApiKeys[k] = temp;
            }

            // Get a cached list of dates reflecting the next missing entry in the stock history table to be used for the api query
            IDictionary <string, string> nextDate = stockDao.GetNextMissingHistoryEntryAllStocksFormatted();

            // List of stock history models to store the results from the external API query
            List <StockHistoryApiModel> historyResults = new List <StockHistoryApiModel>();

            // Initialize counters and a new WebClient object to query the API
            int       currentStockIndex  = 0;
            int       currentApiKeyIndex = 0;
            WebClient client             = new WebClient();

            // Loop over the randomized apiKeys array and store our position in the counters so if a request fails we can pick back up where it failed
            for (; currentApiKeyIndex < randomApiKeys.Length; currentApiKeyIndex++)
            {
                try
                {
                    for (; currentStockIndex < stocks.Length; currentStockIndex++)
                    {
                        if (nextDate.ContainsKey(stocks[currentStockIndex]))
                        {
                            string apiRequest = $"https://marketdata.websol.barchart.com/getHistory.json?apikey={randomApiKeys[currentApiKeyIndex]}&type=daily&symbol={stocks[currentStockIndex]}&startDate={nextDate[stocks[currentStockIndex]]}";
                            string response   = client.DownloadString(apiRequest);
                            historyResults.Add(JsonConvert.DeserializeObject <StockHistoryApiModel>(response));
                        }
                    }
                    break;
                }
                catch
                {
                    continue;
                }
            }

            List <StockHistoryModel> models = new List <StockHistoryModel>();

            foreach (StockHistoryApiModel historyResult in historyResults)
            {
                Result[] results = null;

                if (historyResult.results != null && historyResult.results.Length != 0)
                {
                    results = historyResult.results;
                }
                else
                {
                    results    = new Result[1];
                    results[0] = null;
                }

                //Add the stock details required for stock to our stock model
                foreach (Result result in results)
                {
                    if (result != null)
                    {
                        StockHistoryModel newStock = new StockHistoryModel();
                        newStock.StockSymbol = result.symbol;
                        newStock.TradingDay  = Convert.ToDateTime(result.tradingDay);
                        newStock.OpenPrice   = result.open;
                        newStock.DailyHigh   = result.high;
                        newStock.DailyLow    = result.low;
                        newStock.ClosePrice  = result.close;
                        newStock.Volume      = result.volume;

                        models.Add(newStock);
                    }
                }
            }

            return(models);
        }