public Task AddOrUpdateStockPriceHistoryAsync(IStockPriceHistory data)
        {
            IStockPriceHistory priceHistory = StockPriceHistoryTable.FirstOrDefault(a => a.Country == data.Country &&
                                                                                    string.Equals(a.StockId, data.StockId, StringComparison.OrdinalIgnoreCase) &&
                                                                                    a.TradeDateTime.Year == data.TradeDateTime.Year &&
                                                                                    a.TradeDateTime.Month == data.TradeDateTime.Month &&
                                                                                    a.TradeDateTime.Day == data.TradeDateTime.Day);

            switch (priceHistory)
            {
            case null:
                StockPriceHistoryTable.Add(data);
                return(Task.CompletedTask);

            case StockPriceHistory stockPriceHistory:
                stockPriceHistory.ATR         = data.ATR;
                stockPriceHistory.ClosePrice  = data.ClosePrice;
                stockPriceHistory.HighIn20    = data.HighIn20;
                stockPriceHistory.HighPrice   = data.HighPrice;
                stockPriceHistory.LowIn10     = data.LowIn10;
                stockPriceHistory.LowPrice    = data.LowPrice;
                stockPriceHistory.MA120       = data.MA120;
                stockPriceHistory.MA20        = data.MA20;
                stockPriceHistory.MA240       = data.MA240;
                stockPriceHistory.MA60        = data.MA60;
                stockPriceHistory.N20         = data.N20;
                stockPriceHistory.OpenPrice   = data.OpenPrice;
                stockPriceHistory.PriceChange = data.PriceChange;
                stockPriceHistory.PriceRange  = data.PriceRange;
                stockPriceHistory.Volume      = data.Volume;
                break;
            }

            return(Task.CompletedTask);
        }
 public Task <IReadOnlyList <IStockPriceHistory> > GetStockPriceHistoryAsync(CountryKind country, string stockID, DateTime dateBefore, int recordNumber)
 {
     return(Task.FromResult <IReadOnlyList <IStockPriceHistory> >(StockPriceHistoryTable.Where(a => a.Country == country &&
                                                                                               string.Equals(a.StockId, stockID, StringComparison.OrdinalIgnoreCase) &&
                                                                                               a.TradeDateTime < dateBefore)
                                                                  .OrderByDescending(a => a.TradeDateTime)
                                                                  .Take(recordNumber)
                                                                  .ToList()));
 }
        public Task <IStockPriceHistory> GetStockPriceHistoryAsync(CountryKind country, string stockID, DateTime date)
        {
            IStockPriceHistory priceHistory = StockPriceHistoryTable.Find(a => a.Country == country &&
                                                                          string.Equals(a.StockId, stockID, StringComparison.OrdinalIgnoreCase) &&
                                                                          a.TradeDateTime.Year == date.Year &&
                                                                          a.TradeDateTime.Month == date.Month &&
                                                                          a.TradeDateTime.Day == date.Day);

            return(Task.FromResult(priceHistory));
        }
        public Task DeleteStockPriceHistoryAsync(CountryKind country, string stockId)
        {
            IEnumerable <IStockPriceHistory> entries = StockPriceHistoryTable.Where(a => a.Country == country &&
                                                                                    string.Equals(a.StockId, stockId, StringComparison.OrdinalIgnoreCase));

            foreach (IStockPriceHistory item in entries)
            {
                StockPriceHistoryTable.Remove(item);
            }

            return(Task.CompletedTask);
        }
        public Task <IReadOnlyList <IAllPricesEntry> > GetTheLatestStockPriceAsync(CountryKind country, DateTime date)
        {
            IReadOnlyList <IStockPriceHistory> priceHistories = StockPriceHistoryTable.Where(a => a.Country == country && a.TradeDateTime < date)
                                                                .OrderByDescending(a => a.TradeDateTime)
                                                                .ToList();

            if (priceHistories.Count == 0)
            {
                return(Task.FromResult <IReadOnlyList <IAllPricesEntry> >(null));
            }

            List <IAllPricesEntry> list = priceHistories.Select(history => new AllPricesEntry(country,
                                                                                              history.StockId,
                                                                                              history.LowPrice,
                                                                                              history.HighPrice,
                                                                                              history.ClosePrice,
                                                                                              history.OpenPrice,
                                                                                              history.TradeDateTime,
                                                                                              history.YearRange,
                                                                                              history.Volume,
                                                                                              history.ATR,
                                                                                              history.N20,
                                                                                              history.HighIn20,
                                                                                              history.LowIn10,
                                                                                              history.N40,
                                                                                              history.HighIn40,
                                                                                              history.LowIn15,
                                                                                              history.N60,
                                                                                              history.HighIn60,
                                                                                              history.LowIn20,
                                                                                              history.MA20,
                                                                                              history.MA40,
                                                                                              history.MA60,
                                                                                              history.MA120,
                                                                                              history.MA240))
                                          .Cast <IAllPricesEntry>()
                                          .ToList();

            return(Task.FromResult <IReadOnlyList <IAllPricesEntry> >(list));
        }
        public Task <IAllPricesEntry> GetTheLatestStockPriceAsync(CountryKind country, string stockID, DateTime date)
        {
            IStockPriceHistory priceHistory = StockPriceHistoryTable.OrderByDescending(a => a.TradeDateTime)
                                              .FirstOrDefault(a => a.Country == country &&
                                                              string.Equals(a.StockId, stockID, StringComparison.OrdinalIgnoreCase) &&
                                                              a.TradeDateTime < date);

            if (priceHistory == null)
            {
                return(Task.FromResult <IAllPricesEntry>(null));
            }

            AllPricesEntry entry = new AllPricesEntry(country,
                                                      stockID,
                                                      priceHistory.LowPrice,
                                                      priceHistory.HighPrice,
                                                      priceHistory.ClosePrice,
                                                      priceHistory.OpenPrice,
                                                      priceHistory.TradeDateTime,
                                                      priceHistory.YearRange,
                                                      priceHistory.Volume,
                                                      priceHistory.ATR,
                                                      priceHistory.N20,
                                                      priceHistory.HighIn20,
                                                      priceHistory.LowIn10,
                                                      priceHistory.N40,
                                                      priceHistory.HighIn40,
                                                      priceHistory.LowIn15,
                                                      priceHistory.N60,
                                                      priceHistory.HighIn60,
                                                      priceHistory.LowIn20,
                                                      priceHistory.MA20,
                                                      priceHistory.MA40,
                                                      priceHistory.MA60,
                                                      priceHistory.MA120,
                                                      priceHistory.MA240);

            return(Task.FromResult <IAllPricesEntry>(entry));
        }
 public Task <IReadOnlyList <IStockPriceHistory> > GetStockPriceHistoryAsync(CountryKind country, string stockID, DateTime start, DateTime end)
 {
     return(Task.FromResult <IReadOnlyList <IStockPriceHistory> >(StockPriceHistoryTable.Where(a => a.Country == country &&
                                                                                               string.Equals(a.StockId, stockID, StringComparison.OrdinalIgnoreCase) &&
                                                                                               a.TradeDateTime >= start && a.TradeDateTime <= end).ToList()));
 }