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 <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));
        }
示例#3
0
        private async Task <bool> UpdatePriceInStorageAsync(DateTime currentDate)
        {
            IStockPriceHistory item = await _memoryDbOperations.GetStockPriceHistoryAsync(_baseData.Country, _stockId, currentDate).ConfigureAwait(false);

            if (item == null)
            {
                return(false);
            }

            ICurrentPrice value = new CurrentPriceItem(item.ClosePrice, item.TradeDateTime, item.HighPrice, item.LowPrice);

            _storage.AddOrUpdateItem(_baseData.Country, _stockId, value);

            return(true);
        }
        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));
        }
示例#5
0
        public async Task InsertToDatabase(IStockQuoteFromDataSource stockData)
        {
            // get previous close price , if none, it's the first record and then insert to db
            IReadOnlyList <IStockPriceHistory> previousPrices = await _database.GetStockPriceHistoryAsync(stockData.Country.ConvertToTT2Country(), stockData.StockId, stockData.TradeDateTime, 1).ConfigureAwait(false);

            IStockPriceHistory previousStockPriceHistory = previousPrices?.FirstOrDefault();

            if (previousStockPriceHistory == null)
            {
                //string msg = $"The stock data {GetStockFullID(todayPriceFromDataSource.StockId)} from data sourceKind doesn't have the previous closed price, so it will NOT be added to database";
                //WriteToErrorLog(msg);
                IStockPriceHistory priceHist = new StockPriceHistory(stockData.Country.ConvertToTT2Country(), stockData.StockId, stockData.TradeDateTime)
                {
                    OpenPrice  = stockData.OpenPrice,
                    ClosePrice = stockData.ClosePrice,
                    HighPrice  = stockData.HighPrice,
                    LowPrice   = stockData.LowPrice,
                    Volume     = stockData.Volume
                };

                await _database.AddOrUpdateStockPriceHistoryAsync(priceHist).ConfigureAwait(false);

                return;
            }

            // get the max days of SystemN
            IReadOnlyList <IStockPriceHistory> recentStockPriceHistory = await _database.GetStockPriceHistoryAsync(stockData.Country.ConvertToTT2Country(), stockData.StockId, stockData.TradeDateTime, _maxCalculationDay - 1).ConfigureAwait(false);

            // if there exists previous closed price, then calculate today's ATR
            decimal todayATR = _baseData.CalculateTodayATR(stockData.HighPrice, stockData.LowPrice, previousStockPriceHistory.ClosePrice);

            (decimal? N20, decimal? HighIn20, decimal? LowIn10) = DailyPriceCalculationSystem(BuySellStrategyType.N20, recentStockPriceHistory, todayATR, stockData);
            (decimal? N40, decimal? HighIn40, decimal? LowIn15) = DailyPriceCalculationSystem(BuySellStrategyType.N40, recentStockPriceHistory, todayATR, stockData);
            (decimal? N60, decimal? HighIn60, decimal? LowIn20) = DailyPriceCalculationSystem(BuySellStrategyType.N60, recentStockPriceHistory, todayATR, stockData);
            decimal?MA20  = CalculateMovingAverage(20, recentStockPriceHistory, stockData);
            decimal?MA40  = CalculateMovingAverage(40, recentStockPriceHistory, stockData);
            decimal?MA60  = CalculateMovingAverage(60, recentStockPriceHistory, stockData);
            decimal?MA120 = CalculateMovingAverage(120, recentStockPriceHistory, stockData);
            decimal?MA240 = CalculateMovingAverage(240, recentStockPriceHistory, stockData);

            IStockPriceHistory priceHistory = new StockPriceHistory(stockData.Country.ConvertToTT2Country(), stockData.StockId, stockData.TradeDateTime)
            {
                OpenPrice  = stockData.OpenPrice,
                ClosePrice = stockData.ClosePrice,
                HighPrice  = stockData.HighPrice,
                LowPrice   = stockData.LowPrice,
                Volume     = stockData.Volume,
                ATR        = todayATR,
                N20        = N20,
                N40        = N40,
                N60        = N60,
                HighIn20   = HighIn20,
                LowIn10    = LowIn10,
                HighIn40   = HighIn40,
                LowIn15    = LowIn15,
                HighIn60   = HighIn60,
                LowIn20    = LowIn20,
                MA20       = MA20,
                MA40       = MA40,
                MA60       = MA60,
                MA120      = MA120,
                MA240      = MA240
            };

            await _database.AddOrUpdateStockPriceHistoryAsync(priceHistory).ConfigureAwait(false);
        }
示例#6
0
        // add records into database
        private async Task BackTestFirstBuyOperationAsync(decimal buyPrice)
        {
            IReadOnlyList <IStockPriceHistory> items = await DatabaseOperations.GetStockPriceHistoryAsync(_memberStock.Country, _memberStock.StockId, BaseData.CurrentTime, 1).ConfigureAwait(false);

            if (items == null || items.Count == 0)
            {
                if (!_testStatus)
                {
                    _ = BaseData.GetLogger().WriteToErrorLogAsync(BaseData.Country, BaseData.CurrentTime, "TurtleBuyStrategy", new Exception($"BuyStockRunner: No stock price history data on previous business day of {BaseData.CurrentTime:yyyy-MM-dd}"));
                }

                return;
            }

            IStockPriceHistory item = items.FirstOrDefault();

            if (item == null)
            {
                return;
            }

            decimal N = -1m;

            // TODO : refactor
            switch (_memberStock.Strategy)
            {
            case BuySellStrategyType.N20:
                if (item.N20 != null)
                {
                    N = item.N20.Value;
                }

                break;

            case BuySellStrategyType.N40:
                if (item.N40 != null)
                {
                    N = item.N40.Value;
                }

                break;

            case BuySellStrategyType.N60:
                if (item.N60 != null)
                {
                    N = item.N60.Value;
                }

                break;
            }

            if (N == -1m)
            {
                return;
            }

            await DatabaseOperations.AddMemberBuyStockAsync(_memberStock.MemberEmail,
                                                            _memberStock.Country,
                                                            _memberStock.StockId,
                                                            buyPrice,
                                                            N,
                                                            _memberStock.Strategy,
                                                            BaseData.CurrentTime).ConfigureAwait(false);
        }
示例#7
0
 public void SetPriceHistory(IStockPriceHistory stockPriceHistory)
 {
     _StockPriceHistory = stockPriceHistory;
 }