public ChartWindow(CandleSeries candleSeries) { if (candleSeries == null) { throw new ArgumentNullException(nameof(candleSeries)); } InitializeComponent(); Title = candleSeries.ToString(); _candleSeries = candleSeries; _connector = MainWindow.Instance.MainPanel.Connector; Chart.ChartTheme = ChartThemes.ExpressionDark; var area = new ChartArea(); Chart.Areas.Add(area); _candleElem = new ChartCandleElement { AntiAliasing = false, UpFillColor = Colors.White, UpBorderColor = Colors.Black, DownFillColor = Colors.Black, DownBorderColor = Colors.Black, }; area.Elements.Add(_candleElem); _connector.CandleReceived += OnCandleReceived; _subscription = _connector.SubscribeMarketData(_candleSeries.ToMarketDataMessage(true)); }
/// <summary> /// To stop the candles receiving subscription, previously created by <see cref="SubscribeCandles"/>. /// </summary> /// <param name="series">Candles series.</param> public virtual void UnSubscribeCandles(CandleSeries series) { var originalTransId = _entityCache.TryGetTransactionId(series); if (originalTransId == 0) { return; } var mdMsg = series.ToMarketDataMessage(false); mdMsg.TransactionId = TransactionIdGenerator.GetNextId(); mdMsg.OriginalTransactionId = originalTransId; UnSubscribeMarketData(series.Security, mdMsg); }
/// <summary> /// Subscribe to receive new candles. /// </summary> /// <param name="series">Candles series.</param> /// <param name="from">The initial date from which you need to get data.</param> /// <param name="to">The final date by which you need to get data.</param> /// <param name="count">Candles count.</param> /// <param name="transactionId">Transaction ID.</param> /// <param name="extensionInfo">Extended information.</param> public virtual void SubscribeCandles(CandleSeries series, DateTimeOffset?from = null, DateTimeOffset?to = null, long?count = null, long?transactionId = null, IDictionary <string, object> extensionInfo = null) { if (series == null) { throw new ArgumentNullException(nameof(series)); } var mdMsg = series.ToMarketDataMessage(true, from, to, count); mdMsg.TransactionId = transactionId ?? TransactionIdGenerator.GetNextId(); mdMsg.ExtensionInfo = extensionInfo; _entityCache.CreateCandleSeries(mdMsg, series); SubscribeMarketData(series.Security, mdMsg); }
public CandleSeriesInfo(CandleSeries series, DateTimeOffset from, DateTimeOffset to, IEnumerable <ICandleBuilderSource> sources, Func <CandleSeriesInfo, IEnumerable <ICandleBuilderSourceValue>, DateTimeOffset> handler, Action <CandleSeries> stopped) { if (series == null) { throw new ArgumentNullException(nameof(series)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (stopped == null) { throw new ArgumentNullException(nameof(stopped)); } Series = series; Message = series.ToMarketDataMessage(true, from, to); _enumerator = new CandleSourceEnumerator <ICandleBuilderSource, IEnumerable <ICandleBuilderSourceValue> >(series, from, to, sources, v => handler(this, v), () => stopped(series)); }
private void LoadData(CandleSeries series) { var msgType = series.CandleType.ToCandleMessageType(); _transactionId = _transactionIdGenerator.GetNextId(); _holder.Clear(); _holder.CreateCandleSeries(_transactionId, series); _candleTransform.Process(new ResetMessage()); _candleBuilder = _builderProvider.Get(msgType.ToCandleMarketDataType()); var storage = new StorageRegistry(); BusyIndicator.IsBusy = true; var path = HistoryPath.Folder; var isBuild = BuildFromTicks.IsChecked == true; var format = Format.SelectedFormat; var maxDays = (isBuild || series.CandleType != typeof(TimeFrameCandle)) ? 2 : 30 * (int)((TimeSpan)series.Arg).TotalMinutes; _mdMsg = series.ToMarketDataMessage(true); Task.Factory.StartNew(() => { var date = DateTime.MinValue; if (isBuild) { foreach (var tick in storage.GetTickMessageStorage(series.Security, new LocalMarketDataDrive(path), format).Load()) { _tradeGenerator.Process(tick); if (_candleTransform.Process(tick)) { var candles = _candleBuilder.Process(_mdMsg, _currCandle, _candleTransform); foreach (var candle in candles) { _currCandle = candle; _updatedCandles.Add((CandleMessage)candle.Clone()); } } _lastTime = tick.ServerTime; if (date != tick.ServerTime.Date) { date = tick.ServerTime.Date; var str = date.To <string>(); this.GuiAsync(() => BusyIndicator.BusyContent = str); maxDays--; if (maxDays == 0) { break; } } } } else { foreach (var candleMsg in storage.GetCandleMessageStorage(msgType, series.Security, series.Arg, new LocalMarketDataDrive(path), format).Load()) { if (candleMsg.State != CandleStates.Finished) { candleMsg.State = CandleStates.Finished; } _currCandle = candleMsg; _updatedCandles.Add(candleMsg); _lastTime = candleMsg.OpenTime; if (candleMsg is TimeFrameCandleMessage) { _lastTime += (TimeSpan)series.Arg; } _tradeGenerator.Process(new ExecutionMessage { ExecutionType = ExecutionTypes.Tick, SecurityId = series.Security.ToSecurityId(), ServerTime = _lastTime, TradePrice = candleMsg.ClosePrice, }); if (date != candleMsg.OpenTime.Date) { date = candleMsg.OpenTime.Date; var str = date.To <string>(); this.GuiAsync(() => BusyIndicator.BusyContent = str); maxDays--; if (maxDays == 0) { break; } } } } _historyLoaded = true; }) .ContinueWith(t => { if (t.Exception != null) { Error(t.Exception.Message); } BusyIndicator.IsBusy = false; Chart.IsAutoRange = false; ModifyAnnotationBtn.IsEnabled = true; NewAnnotationBtn.IsEnabled = true; }, TaskScheduler.FromCurrentSynchronizationContext()); }
private void LoadData(CandleSeries series) { _currCandle = null; _historyLoaded = false; _allCandles.Clear(); _candleTransform.Process(new ResetMessage()); _candleBuilder = series.CandleType.ToCandleMessageType().ToCandleMarketDataType().CreateCandleBuilder(); Chart.Reset(new IChartElement[] { _candleElement }); var storage = new StorageRegistry(); BusyIndicator.IsBusy = true; var path = HistoryPath.Folder; var isBuild = BuildFromTicks.IsChecked == true; var format = Format.SelectedFormat; var maxDays = (isBuild || series.CandleType != typeof(TimeFrameCandle)) ? 5 : 30 * (int)((TimeSpan)series.Arg).TotalMinutes; _mdMsg = series.ToMarketDataMessage(true); Task.Factory.StartNew(() => { var date = DateTime.MinValue; if (isBuild) { foreach (var tick in storage.GetTickMessageStorage(series.Security, new LocalMarketDataDrive(path), format).Load()) { _tradeGenerator.Process(tick); if (_candleTransform.Process(tick)) { var candles = _candleBuilder.Process(_mdMsg, _currCandle, _candleTransform); lock (_lock) { foreach (var candle in candles) { _currCandle = candle; _updatedCandles[candle.OpenTime] = Tuple.Create(candle, candle.State == CandleStates.Finished); } } } _lastTime = tick.ServerTime; if (date != tick.ServerTime.Date) { date = tick.ServerTime.Date; var str = date.To <string>(); this.GuiAsync(() => BusyIndicator.BusyContent = str); maxDays--; if (maxDays == 0) { break; } } } } else { foreach (var candleMsg in storage.GetCandleMessageStorage(series.CandleType.ToCandleMessageType(), series.Security, series.Arg, new LocalMarketDataDrive(path), format).Load()) { lock (_updatedCandles.SyncRoot) { _currCandle = candleMsg; _updatedCandles[candleMsg.OpenTime] = Tuple.Create(candleMsg, true); } _lastTime = candleMsg.OpenTime; if (candleMsg is TimeFrameCandleMessage) { _lastTime += (TimeSpan)series.Arg; } _tradeGenerator.Process(new ExecutionMessage { ExecutionType = ExecutionTypes.Tick, SecurityId = series.Security.ToSecurityId(), ServerTime = _lastTime, TradePrice = candleMsg.ClosePrice, }); if (date != candleMsg.OpenTime.Date) { date = candleMsg.OpenTime.Date; var str = date.To <string>(); this.GuiAsync(() => BusyIndicator.BusyContent = str); maxDays--; if (maxDays == 0) { break; } } } } _historyLoaded = true; }) .ContinueWith(t => { if (t.Exception != null) { Error(t.Exception.Message); } this.GuiAsync(() => { BusyIndicator.IsBusy = false; Chart.IsAutoRange = false; //_areaComb.YAxises.First().AutoRange = false; }); }, TaskScheduler.FromCurrentSynchronizationContext()); }