/// <summary>
        /// Processes the stock data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        private static StockTickerList ProcessStockData(Stream data)
        {
            //var result = new List<StockTickerData>();
            try
            {
                var serializer =
                    new DataContractJsonSerializer(typeof(JsonRoot <StockTickerData>),
                                                   new[]
                {
                    typeof(JsonRoot <StockTickerData>),
                    typeof(JsonContent <StockTickerData>),
                    typeof(StockTickerData),
                    typeof(List <StockTickerData>)
                });
                var result = (JsonRoot <StockTickerData>)(serializer.ReadObject(data));

                DebugManager.LogData("StockDataService -> Requesting stock data completed for " + result.value.items.Count + " items");

                var stockHistory = new StockTickerList();
                foreach (var item in result.value.items)
                {
                    stockHistory.Add(item);
                }

                stockHistory.Sort((x, y) => + (Comparer <DateTime> .Default.Compare(x.Date, y.Date)));

                return(stockHistory);
            }
            catch (Exception ex)
            {
                DebugManager.LogError("StockDataService -> Failed on ProcessStockData(): \n" + ex);
                throw new Exception(SeviceProcessingError, ex);
            }
        }
Пример #2
0
        private void OnRequestStockHistoryCompleted(object sender, OpenReadCompletedEventArgs response)
        {
            var stockToken = new StockToken();

            if (response.UserState != null)
            {
                stockToken = (StockToken)response.UserState;
            }

            var process = "StockQuandlService -> Requesting stock history for " + stockToken.Symbol;

            if (response.Cancelled || response.Error != null)
            {
                Debug.WriteLine(process + " failed: \n   at " + response.Error);
                //ReportServerError(e.Error);
                return;
            }

            Debug.WriteLine(process + " completed");


            var csv          = DataProvider.GetCsvFile(response.Result);
            var culture      = CultureInfo.InvariantCulture;
            var stockHistory = new StockTickerList();

            #region Parse csv data and create stock history
            //      0,    1,    2,   3,     4,      5,        6,           7,
            //   Date, Open, High, Low, Close, Volume, Dividend, Split Ratio, Adj. Open,Adj. High,Adj. Low,Adj. Close,Adj. Volume
            // 2015-09-10,247.23,250.7231,245.33,248.381,2700138.0,0.0,1.0,247.23,250.7231,245.33,248.381,2700138.0
            // 2015-09-09,252.05,254.25,248.303,248.91,3363641.0,0.0,1.0,252.05,254.25,248.303,248.91,3363641.0

            for (var i = 1; i < csv.Count; i++)
            {
                var line  = csv[i];
                var stock = new StockTickerData();
                stock.Date = DateTime.ParseExact(line[0], StockDateFormat, culture);

                stock.Open   = double.Parse(line[1]);
                stock.High   = double.Parse(line[2]);
                stock.Low    = double.Parse(line[3]);
                stock.Close  = double.Parse(line[4]);
                stock.Volume = double.Parse(line[5]);

                //stock.Dividend = double.Parse(line[6]);
                //stock.Split = double.Parse(line[7]);

                stockHistory.Add(stock);

                var day = stock.Date.ToString();
                if (!TradingDates.ContainsKey(day))
                {
                    TradingDates.Add(day, stock.Date);
                }
            }
            #endregion

            stockHistory.Sort((x, y) => + (Comparer <DateTime> .Default.Compare(x.Date, y.Date)));

            // stockHistory.Sort((x, y) => Comparer.Ascending(x.Date, y.Date));

            Debug.WriteLine(process + " data parsed: " + stockHistory.Count);

            if (stockHistory.Count > 0)
            {
                var first = stockHistory.First();
                var count = stockHistory.Count;
                while (first.Date > stockToken.StartDate)
                {
                    var newStock = new StockTickerData {
                        Date = first.Date.AddDays(-1)
                    };
                    if (TradingDates.ContainsKey(newStock.Date.ToString()))
                    {
                        stockHistory.Insert(0, newStock);
                    }

                    first = newStock;
                }
                var delta = stockHistory.Count - count;
                if (delta > 0)
                {
                    Debug.WriteLine("StockQuandlService -> Aligned stock data by adding " + delta + " items");
                }
            }
            Debug.WriteLine(process + " data aligned: " + stockHistory.Count);

            _cache.UpdateCache(stockToken.Symbol, new List <object> {
                stockHistory
            });


            SynchronizationContext.Current.Post(data =>
                                                stockToken.Callback(stockToken.Symbol, (StockTickerList)data), stockHistory);
        }