예제 #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());
        }
예제 #2
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);
            }
        }
예제 #3
0
        protected override Task Execute(ScheduledJob job, List <CompanyQuotesModel> companies)
        {
            foreach (var company in companies)
            {
                if (!company.HistoryQuotes.Any())
                {
                    company.LastUpdated   = DateTime.Today.AddYears(-10);
                    company.HistoryQuotes = new List <QuotesModel>();
                }

                var historyRequest = new GetStockHistoryRequest(company.Ticker, company.LastUpdated);
                var errorMessage   = string.Empty;
                var quotes         = new List <QuotesModel>();

                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)
                {
                    errorMessage = ex.Message;

                    foreach (var exception in ex.InnerExceptions)
                    {
                        errorMessage += ". " + exception.Message;
                    }

                    Logger.Error(new ProcessorInfo
                    {
                        JobId       = job.JobId,
                        JobType     = job.JobType,
                        JobState    = JobStatus.Error,
                        ProcessName = JobType.ToString()
                    }, $"Failed to read Company quotes: {company.Ticker}. {errorMessage}", ex);
                }
                catch (Exception ex)
                {
                    Logger.Error(new ProcessorInfo
                    {
                        JobId       = job.JobId,
                        JobType     = job.JobType,
                        JobState    = JobStatus.Error,
                        ProcessName = JobType.ToString()
                    }, $"Failed to read Company quotes: {company.Ticker}. {ex.Message}", ex);
                }

                try
                {
                    CompanyService.UpdateQuotes(new UpdateQuotesRequest(company.Ticker, quotes)
                    {
                        ErrorMessage = errorMessage
                    });
                }
                catch (Exception ex)
                {
                    Logger.Error(new ProcessorInfo
                    {
                        JobId       = job.JobId,
                        JobType     = job.JobType,
                        JobState    = JobStatus.Error,
                        ProcessName = JobType.ToString()
                    }, $"Failed to update Company: {company.Ticker}. {ex.Message}", ex);
                }
            }

            return(Task.FromResult(0));
        }