Пример #1
0
        private async Task LoadData()
        {
            try
            {
                if (_cancellationTokenSource != null && _cancellationTokenSource.Token.CanBeCanceled &&
                    !_cancellationTokenSource.IsCancellationRequested)
                {
                    _cancellationTokenSource.Cancel();
                    return;
                }

                _cancellationTokenSource = new CancellationTokenSource();

                IsBusy = true;

                await LoadSettings().ConfigureAwait(false);

                SetPortfolio();

                // get list of all saved predictions
                var listBestPredictionsDtos = await PersistenceService.GetBestNetworkDTOs().ConfigureAwait(false);

                Predictions = new ObservableCollection <DashboardPrediction>();

                // for each
                foreach (var dto in listBestPredictionsDtos)
                {
                    if (_cancellationTokenSource.Token.IsCancellationRequested)
                    {
                        return;
                    }

                    var stock = await PersistenceService.GetStockFromId(dto.StockId);

                    stock.HistoricalData = await DownloaderService.GetHistoricalData(stock, _settings.StartDate, DateTime.Now, true).ConfigureAwait(false);

                    var dashboardPrediction = new DashboardPrediction
                    {
                        Symbol  = stock.Symbol, Name = stock.Name, StockId = stock.GetUniqueId(),
                        Country = stock.Country.Name
                    };

                    lock (Locker)
                    {
                        Application.Current.Dispatcher?.Invoke(() => Predictions.Add(dashboardPrediction));
                    }

                    TrainingSession trainingSession;
                    try
                    {
                        trainingSession = await Task.Run(() => new TrainingSession(Portfolio, stock, dto, _settings))
                                          .ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Messenger.Default.Send(new TrainStatusMessage(
                                                   $"Could setup training session for stock {stock.Name}: {ex.Message}", SeverityEnum.Error));
                        trainingSession = new TrainingSession(Portfolio, stock);
                    }

                    dashboardPrediction.TrainingSession  = trainingSession;
                    dashboardPrediction.Close            = (double?)trainingSession.Stock.HistoricalData?.Quotes?.LastOrDefault().Value?.Close ?? 0d;
                    dashboardPrediction.LastTrainingDate = dto.Timestamp;

                    if (trainingSession.Stock.HistoricalData != null)
                    {
                        dashboardPrediction.LastUpdate = trainingSession.Stock.HistoricalData.EndDate;
                    }
                }
            }
            finally
            {
                IsBusy = false;
                _cancellationTokenSource = null;
            }
        }