Beispiel #1
0
        /// <summary>
        /// Загрузка истории для пустых Bars.
        /// Если Bars не пустой, то история не загружается.
        /// </summary>
        /// <returns></returns>
        public async Task LoadHistoryAsync()
        {
            DateTime start, end;

            if (_tickDispatcher != null)
            {
                end = _tickDispatcher.CurrentDate.AddDays(-1);
            }
            else
            {
                end = _endDate;
            }

            foreach (var psrc in _guid_source.Values)
            {
                if (psrc.Bars.Count > 0)
                {
                    continue;                      // история уже загружена
                }
                if (_tickDispatcher != null)
                {
                    start = _insStoreBL.GetDefaultStartHistoryDate(end, psrc.Bars.Timeframe);
                }
                else
                {
                    start = _startDate;
                }
                await _insStoreBL.LoadHistoryAsync(psrc.Bars, psrc.Instrum.InsID, start, end);

                if (_tickDispatcher != null)
                {
                    psrc.Bars.TickDispatcher = _tickDispatcher;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Асинхронная загрузка исторических данных
        /// </summary>
        /// <param name="toDate">Загрузка по эту дату включительно</param>
        /// <param name="isLastDirty">Данные за последний день неполные</param>
        /// <param name="progress">Объект управления фоновой задачей</param>
        /// <param name="cancel">Отмена длительной операции</param>
        /// <returns>Асинхронная задача загрузки</returns>
        public Task DownloadAllAsync(DateTime toDate, bool isLastDirty, BgTaskProgress progress, CancellationToken cancel)
        {
            return(Task.Run(() =>
            {
                try
                {
                    _logger.LogInformation("DownloadAll to {date}", toDate.ToString("yyyy-MM-dd"));

                    var insStores = _insStoreBL.GetActiveInsStores();
                    int count = insStores.Count(); int idx = 1;
                    if (progress != null)
                    {
                        progress.OnStart(count > 1);
                    }
                    Dictionary <CommonData.InsStore, BgTaskProgress> progresses = new Dictionary <CommonData.InsStore, BgTaskProgress>();
                    if (progress != null)
                    {
                        foreach (var ss in insStores)
                        {
                            string name = "";
                            var instrum = _instrumBL.GetInstrumByID(ss.InsID);
                            if (instrum != null)
                            {
                                name = instrum.ShortName;
                            }
                            var child = progress.AddChildProgress(name);
                            progresses.Add(ss, child);
                        }
                    }

                    foreach (var insStore in insStores)
                    {
                        if (cancel.IsCancellationRequested)
                        {
                            if (progress != null)
                            {
                                progress.OnAbort();
                            }
                            break;
                        }

                        var ssCal = _insStoreBL.GetInsStoreCalendar(insStore.InsStoreID);
                        if (ssCal == null || ssCal.Periods == null)
                        {
                            continue;
                        }

                        DateTime fromDate;
                        if (ssCal.Periods.Count() > 0)
                        {
                            var lastPeriod = ssCal.Periods.Last();
                            fromDate = lastPeriod.IsLastDirty ? lastPeriod.EndDate : lastPeriod.EndDate.AddDays(1);
                        }
                        else
                        {
                            fromDate = _insStoreBL.GetDefaultStartHistoryDate(toDate, insStore.Tf);
                        }

                        var p = progresses.ContainsKey(insStore) ? progresses[insStore] : null;
                        DownloadAsync(insStore, fromDate, toDate, isLastDirty, true, p, cancel).Wait();
                        if (progress != null)
                        {
                            progress.OnProgress((double)idx++ / count * 100);
                        }
                    }
                    if (progress != null)
                    {
                        if (cancel.IsCancellationRequested)
                        {
                            progress.OnAbort();
                        }
                        else
                        {
                            progress.OnComplete();
                        }
                    }

                    _logger.LogInformation("DownloadAll complete.");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "DownloadAll error.");
                    if (progress != null)
                    {
                        progress.OnFault(ex);
                    }
                }
            }));
        }