Esempio n. 1
0
        public ActionResult <List <StockResponse> > Get([FromQuery] string query, [FromQuery] DateTime?date, [FromQuery] DateTime?fromDate, [FromQuery] DateTime?toDate)
        {
            IEnumerable <Stock> stocks = null;
            Date resultDate;

            if ((date != null) && (fromDate != null) && (toDate != null))
            {
                return(BadRequest("Cannot specify a date and a date range"));
            }

            if (query == null)
            {
                if (date != null)
                {
                    resultDate = DateFromParameter(date);
                    stocks     = _StockQuery.All(resultDate);
                }
                else if ((fromDate != null) || (toDate != null))
                {
                    var dateRange = DateRangeFromParameter(fromDate, toDate);
                    resultDate = dateRange.ToDate;

                    stocks = _StockQuery.All(dateRange);
                }
                else
                {
                    stocks     = _StockQuery.All();
                    resultDate = Date.Today;
                }
            }
            else
            {
                if (date != null)
                {
                    resultDate = DateFromParameter(date);
                    stocks     = _StockQuery.Find(resultDate, x => MatchesQuery(x, query));
                }
                else if ((fromDate != null) || (toDate != null))
                {
                    var dateRange = DateRangeFromParameter(fromDate, toDate);
                    resultDate = dateRange.ToDate;

                    stocks = _StockQuery.Find(dateRange, x => MatchesQuery(x, query));
                }
                else
                {
                    stocks     = _StockQuery.Find(x => MatchesQuery(x, query));
                    resultDate = Date.Today;
                }
            }

            return(Ok(stocks.Select(x => x.ToResponse(resultDate)).ToList()));
        }
        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);
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task Import(CancellationToken cancellationToken)
        {
            var asxCodes = _StockQuery.All(Date.Today).Select(x => x.Properties[Date.Today].AsxCode);

            var stockQuotes = await _DataService.GetMultiplePrices(asxCodes, cancellationToken);

            foreach (var stockQuote in stockQuotes)
            {
                if (stockQuote.Date == Date.Today)
                {
                    var stock = _StockQuery.Get(stockQuote.AsxCode, stockQuote.Date);
                    if (stock != null)
                    {
                        _Logger?.LogInformation("Updating current price for {0}: {1}", stockQuote.AsxCode, stockQuote.Price);

                        _StockService.UpdateCurrentPrice(stock.Id, stockQuote.Price);
                    }
                }
            }
        }