public async Task Import(CancellationToken cancellationToken) { var lastExpectedDate = _TradingCalendar.PreviousTradingDay(Date.Today.AddDays(-1)); foreach (var stock in _StockQuery.All()) { if (cancellationToken.IsCancellationRequested) { return; } var latestDate = stock.DateOfLastestPrice(); if (latestDate < lastExpectedDate) { var dateRange = new DateRange(latestDate.AddDays(1), lastExpectedDate); var asxCode = stock.Properties.ClosestTo(dateRange.ToDate).AsxCode; _Logger?.LogInformation("Importing closing prices for {0} between {1:d} and {2:d}", asxCode, dateRange.FromDate, dateRange.ToDate); var data = await _DataService.GetHistoricalPriceData(asxCode, dateRange, cancellationToken); var closingPrices = data.Select(x => new Domain.Stocks.StockPrice(x.Date, x.Price)).ToList(); _Logger?.LogInformation("{0} closing prices found", closingPrices.Count); if (closingPrices.Count > 0) { _StockService.UpdateClosingPrices(stock.Id, closingPrices); } } } }
private IEnumerable <Date> GetDates(DateRange dateRange, ValueFrequency frequency) { var firstRequestedDate = _TradingCalendar.NextTradingDay(dateRange.FromDate); var lastRequestedDate = _TradingCalendar.PreviousTradingDay(dateRange.ToDate); IEnumerable <Date> dates; if (frequency == ValueFrequency.Day) { dates = _TradingCalendar.TradingDays(dateRange); } else if (frequency == ValueFrequency.Week) { dates = DateUtils.WeekEndingDays(dateRange); } else if (frequency == ValueFrequency.Month) { dates = DateUtils.MonthEndingDays(dateRange); } else { dates = new Date[] { firstRequestedDate, lastRequestedDate } }; if (firstRequestedDate < dates.First()) { yield return(firstRequestedDate); } Date lastDate = lastRequestedDate; foreach (var date in dates) { yield return(date); lastDate = date; } if (lastDate < lastRequestedDate) { yield return(lastRequestedDate); } } }