Exemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateQuotes(string ticker)
        {
            var company = await _companyService.GetAsync(ticker);

            if (company != null)
            {
                var update = new CompanyQuotesModel
                {
                    Ticker        = company.Ticker,
                    LastUpdated   = company.LastUpdated,
                    HistoryQuotes = _companyService.GetQuotes(ticker)
                };

                if (!update.HistoryQuotes.Any())
                {
                    update.LastUpdated = DateTime.Today.AddYears(-5);
                }

                var request = new GetStockHistoryRequest(update.Ticker, update.LastUpdated);

                var csvQuotes = await _stockClient.GetStockHistory(request);

                var quotes = _fileReader.Read(csvQuotes);
                if (quotes.Any())
                {
                    var qmodels = quotes.Select(q => new QuotesModel(q)).ToList();
                    qmodels = qmodels.Merge(update.HistoryQuotes).Where(q => q.Date > DateTime.Today.AddYears(-1)).ToList();
                    _companyService.UpdateQuotes(new UpdateQuotesRequest(company.Ticker, qmodels));
                }
            }

            return(Ok());
        }
        private IList <IndicatorResult> CalculateIndicators(CompanyQuotesModel company, Indicator indicator)
        {
            var calculator = _processorFactory.Create(indicator);

            return(calculator.Calculate(indicator, company.HistoryQuotes));
        }
Exemplo n.º 3
0
        private void UpdateQuotes(CompanyQuotesModel company)
        {
            var log = new Logger();

            if (!company.HistoryQuotes.Any())
            {
                company.LastUpdated   = DateTime.Today.AddYears(-10);
                company.HistoryQuotes = new List <QuotesModel>();
            }

            var historyRequest = new GetStockHistoryRequest(company.Ticker, company.LastUpdated);

            var quotes       = new List <QuotesModel>();
            var errorMessage = string.Empty;

            try
            {
                var csvQuotes =
                    Task.Run(() => _marketStockClient.GetStockHistory(historyRequest)).Result;

                var squotes = _quotesFileReader.Read(csvQuotes);
                if (squotes.Any())
                {
                    var qmodels = squotes.Select(q => new QuotesModel(q)).ToList();
                    quotes = qmodels.Merge(company.HistoryQuotes);
                }
            }
            catch (AggregateException ex)
            {
                log.Error($"Failed to read Company quotes: {company.Ticker}", ex);
                foreach (var exception in ex.InnerExceptions)
                {
                    errorMessage += exception.Message + " ";
                }
            }
            catch (Exception ex)
            {
                log.Error($"Failed to read Company quotes: {company.Ticker}", ex);
                errorMessage = ex.Message;
            }

            try
            {
                _companyService.UpdateQuotes(new UpdateQuotesRequest(company.Ticker, quotes)
                {
                    ErrorMessage = errorMessage
                });

                //_indicatorProcessor.Calculate(quotes, company.Ticker);

                if (string.IsNullOrEmpty(errorMessage))
                {
                    log.Info($"Company quotes updated successfully: {company.Ticker}");
                }
                else
                {
                    log.Error($"Failed to update Company: {company.Ticker}", new Exception(errorMessage));
                }
            }
            catch (Exception ex)
            {
                errorMessage = $"Failed to update Company: {company.Ticker}";
                log.Error(errorMessage, ex);
            }
        }