public override void RequestUpdates(List <string> symbols, DateTime startDate, DateTime endDate, BarScale scale, int barInterval, IUpdateRequestCompleted requestCompleted) { List <Task> tasks = new List <Task>(); BarDataScale dataScale = new BarDataScale(scale, barInterval); foreach (string updSymbol in symbols) { SymbolDescription symbolDescription = GetSymbolDescription(updSymbol); if (symbolDescription != null) { tasks.Add(Task.Factory.StartNew((object updateRequiredSymbol) => { string symbol = (string)updateRequiredSymbol; DateTime currentDate = DateTime.Now; Bars bars = new Bars(symbol, scale, barInterval); try { string suffix = GetSuffix(dataScale); int corrections = 0; bars.AppendWithCorrections(GetHistory(dataScale, symbol, suffix), out corrections); if (bars.Count > 0 && bars.Date[bars.Count - 1] > currentDate) { bars.Delete(bars.Count - 1); } requestCompleted.UpdateCompleted(bars); } catch (Exception exception) { logger.Error(exception); requestCompleted.UpdateError(symbol, exception); } }, updSymbol)); } else { requestCompleted.UpdateError(updSymbol, new Exception("В формате имени инструмента была допущена ошибка")); } } if (tasks.Count > 0) { Task.WaitAll(tasks.ToArray()); } requestCompleted.ProcessingCompleted(); }
public override Bars RequestData(DataSource ds, string symbol, DateTime startDate, DateTime endDate, int maxBars, bool includePartialBar) { Bars bars = new Bars(symbol, ds.Scale, ds.BarInterval); List <SymbolDescription> symbolDescriptions = SymbolDescription.DeserializeList(ds.DSString); SymbolDescription description = symbolDescriptions.Find(x => x.FullCode == symbol); if (description != null && OnDemandUpdate) { _dataStore.LoadBarsObject(bars); DateTime currentDate = DateTime.Now; try { string suffix = GetSuffix(ds.BarDataScale); int correction = 0; bars.AppendWithCorrections(GetHistory(ds.BarDataScale, description.FullCode, suffix), out correction); } catch (Exception exception) { logger.Error(exception); MessageBox.Show(string.Format("[{0}] {1}", symbol, exception.Message), "Внимание! Произошла ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (bars.Count > 0 && bars.Date[bars.Count - 1] > currentDate && !includePartialBar) { bars.Delete(bars.Count - 1); } if (!base.IsStreamingRequest) { lock (_locker) _dataStore.SaveBarsObject(bars); } } else { _dataStore.LoadBarsObject(bars, startDate, endDate, maxBars); } return(bars); }
public override void UpdateDataSource(DataSource ds, IDataUpdateMessage dataUpdateMsg) { _cancelTokenSource = new CancellationTokenSource(); dataUpdateMsg.ReportUpdateProgress(0); if (string.IsNullOrWhiteSpace(ds.DSString)) { List <SymbolDescription> symbolDescriptions = (from symbol in ds.Symbols select GetSymbolDescription(symbol)).ToList(); ds.DSString = SymbolDescription.SerializeList(symbolDescriptions); } List <SymbolDescription> updateRequired = (from description in SymbolDescription.DeserializeList(ds.DSString) where UpdateRequired(description, ds.BarDataScale) select description).ToList(); dataUpdateMsg.DisplayUpdateMessage(string.Format("Количество инструментов требующих обновления: {0}", updateRequired.Count)); if (updateRequired.Count > 0) { dataUpdateMsg.DisplayUpdateMessage("Запуск обновления инструментов:"); Task[] tasks = new Task[updateRequired.Count]; for (int i = 0; i < updateRequired.Count; i++) { dataUpdateMsg.DisplayUpdateMessage(string.Format("[START] Инструмент: {0} - Обновление запущено", updateRequired[i].FullCode)); tasks[i] = Task.Factory.StartNew((object updateRequiredSymbol) => { string symbol = (string)updateRequiredSymbol; DateTime currentDate = DateTime.Now; Bars bars = new Bars(symbol, ds.Scale, ds.BarInterval); _dataStore.LoadBarsObject(bars); try { string suffix = GetSuffix(ds.BarDataScale); int corrections = 0; bars.AppendWithCorrections(GetHistory(ds.BarDataScale, symbol, suffix), out corrections); if (bars.Count > 0 && bars.Date[bars.Count - 1] > currentDate) { bars.Delete(bars.Count - 1); } lock (_locker) _dataStore.SaveBarsObject(bars); dataUpdateMsg.DisplayUpdateMessage(string.Format("[COMPLETE] Инструмент: {0} - Обновление завершено", symbol)); } catch (Exception exception) { logger.Error(exception); dataUpdateMsg.DisplayUpdateMessage(string.Format("[ERROR] Инструмент {0} - {1}", symbol, exception.Message)); } }, updateRequired[i].FullCode); } try { Task.WaitAll(tasks); } catch (AggregateException exception) { exception.Handle((inner) => { if (inner is OperationCanceledException) { return(true); } else { logger.Error(inner); return(false); } }); } } }