private void AppendTick(Security security, ExecutionMessage tick) { var time = tick.ServerTime; var price = tick.TradePrice.Value; if (_candle == null || time >= _candle.CloseTime) { if (_candle != null) { _candle.State = CandleStates.Finished; lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = _candle; _lastPrice = _candle.ClosePrice; _tradeGenerator.Process(tick); } //var t = TimeframeSegmentDataSeries.GetTimeframePeriod(time.DateTime, _timeframe); var bounds = _timeframe.GetCandleBounds(time, security.Board); _candle = new TimeFrameCandle { TimeFrame = _timeframe, OpenTime = bounds.Min, CloseTime = bounds.Max, Security = security, }; _volumeProfile = new CandleMessageVolumeProfile(); _candle.PriceLevels = _volumeProfile.PriceLevels; _candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price; } if (time < _candle.OpenTime) { throw new InvalidOperationException("invalid time"); } if (price > _candle.HighPrice) { _candle.HighPrice = price; } if (price < _candle.LowPrice) { _candle.LowPrice = price; } _candle.ClosePrice = price; _candle.TotalVolume += tick.TradeVolume.Value; _volumeProfile.Update(new TickCandleBuilderSourceValue(tick)); lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = _candle; }
public void UpdateCandle(TimeFrameCandle candle) { candle.OpenPrice = Open; candle.HighPrice = High; candle.LowPrice = Low; candle.ClosePrice = Close; candle.TotalVolume = Volume; }
private void LoadData(string path) { _candle = null; _allCandles.Clear(); Chart.Reset(new IChartElement[] { _candleElement, _activeOrdersElement }); var storage = new StorageRegistry(); var maxDays = 2; BusyIndicator.IsBusy = true; Task.Factory.StartNew(() => { var date = DateTime.MinValue; foreach (var tick in storage.GetTickMessageStorage(_security, new LocalMarketDataDrive(path)).Load()) { AppendTick(_security, tick); if (date != tick.ServerTime.Date) { date = tick.ServerTime.Date; this.GuiAsync(() => { BusyIndicator.BusyContent = date.ToString(); }); maxDays--; if (maxDays == 0) { break; } } } }) .ContinueWith(t => { if (t.Exception != null) { Error(t.Exception.Message); } this.GuiAsync(() => { BusyIndicator.IsBusy = false; Chart.IsAutoRange = false; _area.YAxises.First().AutoRange = false; Chart.Draw(DateTimeOffset.MinValue, _activeOrdersElement, _chartOrders); Log($"Loaded {_allCandles.Count} candles"); }); }, TaskScheduler.FromCurrentSynchronizationContext()); }
private void AppendTick(Security security, ExecutionMessage tick) { var time = tick.ServerTime; var price = tick.TradePrice.Value; if (_candle == null || time >= _candle.CloseTime) { if (_candle != null) { lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = Tuple.Create(_candle, true); _lastPrice = _candle.ClosePrice; _tradeGenerator.Process(tick); } var bounds = _timeframe.GetCandleBounds(time, security.Board); _candle = new TimeFrameCandle { TimeFrame = _timeframe, OpenTime = bounds.Min, CloseTime = bounds.Max, Security = security, }; _volumeProfile = new CandleMessageVolumeProfile(); _candle.PriceLevels = _volumeProfile.PriceLevels; _candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price; } if (time < _candle.OpenTime) { throw new InvalidOperationException("invalid time"); } if (price > _candle.HighPrice) { _candle.HighPrice = price; } if (price < _candle.LowPrice) { _candle.LowPrice = price; } _candle.ClosePrice = price; _candle.TotalVolume += tick.TradeVolume.Value; lock (_volumeProfile.PriceLevels) _volumeProfile.Update(tick.TradePrice.Value, tick.TradeVolume, tick.OriginSide); lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = Tuple.Create(_candle, false); }
private void AppendTick(Security security, ExecutionMessage tick) { var time = tick.ServerTime; var price = tick.TradePrice.Value; if (_candle == null || time >= _candle.CloseTime) { if (_candle != null) { var candle = (TimeFrameCandle)_candle.Clone(); _updatedCandles[candle.OpenTime] = candle; _lastPrice = candle.ClosePrice; } //var t = TimeframeSegmentDataSeries.GetTimeframePeriod(time.DateTime, _timeframe); var tf = TimeSpan.FromMinutes(_timeframe); var bounds = tf.GetCandleBounds(time, _security.Board); _candle = new TimeFrameCandle { TimeFrame = tf, OpenTime = bounds.Min, CloseTime = bounds.Max, }; _volumeProfile = new VolumeProfile(); _candle.PriceLevels = _volumeProfile.PriceLevels; _candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price; _volumeProfile.Update(new TickCandleBuilderSourceValue(security, tick)); } if (time < _candle.OpenTime) { throw new InvalidOperationException("invalid time"); } if (price > _candle.HighPrice) { _candle.HighPrice = price; } if (price < _candle.LowPrice) { _candle.LowPrice = price; } _candle.ClosePrice = price; _candle.TotalVolume += tick.Volume.Value; _volumeProfile.Update(new TickCandleBuilderSourceValue(security, tick)); }
private void DrawCandle(TimeFrameCandle candle) { var data = new ChartDrawData(); var group = data.Group(candle.OpenTime); group.Add(_candleElement, candle); if (_indicatorElement != null) { group.Add(_indicatorElement, _indicator.Process((double)candle.ClosePrice)); } Chart.Draw(data); }
private void AppendTick(Security security, ExecutionMessage tick) { var time = tick.ServerTime; var price = tick.TradePrice.Value; if (_candle == null || time >= _candle.CloseTime) { if (_candle != null) { _candle.State = CandleStates.Finished; lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = _candle; } var tf = TimeSpan.FromMinutes(_timeframe); var bounds = tf.GetCandleBounds(time, _security.Board); _candle = new TimeFrameCandle { TimeFrame = tf, OpenTime = bounds.Min, CloseTime = bounds.Max, Security = security, }; _candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price; } if (time < _candle.OpenTime) { throw new InvalidOperationException("invalid time"); } if (price > _candle.HighPrice) { _candle.HighPrice = price; } if (price < _candle.LowPrice) { _candle.LowPrice = price; } _candle.ClosePrice = price; _candle.TotalVolume += tick.TradeVolume.Value; lock (_updatedCandles.SyncRoot) _updatedCandles[_candle.OpenTime] = _candle; }
private TimeFrameCandle GetCandle(ExecutionMessage tick) { var time = tick.ServerTime; var price = tick.TradePrice.Value; if (_candle == null || time >= _candle.CloseTime) { //var t = TimeframeSegmentDataSeries.GetTimeframePeriod(time.DateTime, _timeframe); var tf = TimeSpan.FromMinutes(_timeframe); var bounds = tf.GetCandleBounds(time, _security.Board); _candle = new TimeFrameCandle { TimeFrame = tf, OpenTime = bounds.Min, CloseTime = bounds.Max, }; _volumeProfile = new VolumeProfile(); _candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price; _volumeProfile.Update(new TickCandleBuilderSourceValue(_security, tick)); _allCandles.Add(_candle); return(_candle); } if (time < _candle.OpenTime) { throw new InvalidOperationException("invalid time"); } if (price > _candle.HighPrice) { _candle.HighPrice = price; } if (price < _candle.LowPrice) { _candle.LowPrice = price; } _candle.ClosePrice = price; _candle.TotalVolume += tick.Volume.Value; _volumeProfile.Update(new TickCandleBuilderSourceValue(_security, tick)); return(_candle); }
private void DrawCandle(TimeFrameCandle candle) { var dict = new Dictionary <IChartElement, object> { { _candleElement, candle }, }; if (_indicatorElement != null) { dict.Add(_indicatorElement, _indicator.Process((double)candle.ClosePrice)); } Chart.Draw(candle.OpenTime, dict); }
private void ChartUpdateTimerOnTick(object sender, EventArgs eventArgs) { if (!_dataIsLoaded || IsRealtime.IsChecked != true || _lastPrice == 0m) { return; } _lastTime += TimeSpan.FromSeconds(10); var numCandles = _candles.Count; AppendTick(new ExecutionMessage { ServerTime = _lastTime, TradePrice = Round(_lastPrice + (decimal)((RandomGen.GetDouble() - 0.5) * 5 * _priceStep), _priceStep), TradeVolume = RandomGen.GetInt(50) + 1 }); TimeFrameCandle candle; var lastLightCandle = _candles[_candles.Count - 1]; if (_candles.Count != numCandles && _lastCandle != null) { _lastCandle.State = CandleStates.Finished; DrawCandle(_lastCandle); } if (_candles.Count != numCandles || _lastCandle == null) { _lastCandle = candle = lastLightCandle.ToCandle(_tfSpan, _security); } else { candle = _lastCandle; lastLightCandle.UpdateCandle(candle); } DrawCandle(candle); }
private void LoadData(Security security) { _candle = null; _lastPrice = 0m; _allCandles.Clear(); 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 ? 2 : 30 * (int)_timeframe.TotalMinutes; Task.Factory.StartNew(() => { var date = DateTime.MinValue; if (isBuild) { foreach (var tick in storage.GetTickMessageStorage(security, new LocalMarketDataDrive(path), format).Load()) { AppendTick(security, tick); _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 candle in storage.GetCandleStorage(typeof(TimeFrameCandle), security, _timeframe, new LocalMarketDataDrive(path), format).Load()) { lock (_updatedCandles.SyncRoot) _updatedCandles[candle.OpenTime] = _candle = (TimeFrameCandle)candle; _lastTime = candle.OpenTime + _timeframe; _lastPrice = _candle.ClosePrice; _tradeGenerator.Process(new ExecutionMessage { ExecutionType = ExecutionTypes.Tick, SecurityId = security.ToSecurityId(), ServerTime = _lastTime, TradePrice = _lastPrice, }); if (date != candle.OpenTime.Date) { date = candle.OpenTime.Date; var str = date.To <string>(); this.GuiAsync(() => BusyIndicator.BusyContent = str); maxDays--; if (maxDays == 0) { break; } } } } }) .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()); }
private void LoadData() { _candle = null; _lastPrice = 0m; _allCandles.Clear(); var id = new SecurityIdGenerator().Split(SecurityId.Text); _security = new Security { Id = SecurityId.Text, PriceStep = _priceStep, Board = _exchangeInfoProvider.GetExchangeBoard(id.BoardCode) }; Chart.Reset(new IChartElement[] { _candleElement1 }); var storage = new StorageRegistry(); var maxDays = 2; BusyIndicator.IsBusy = true; var path = HistoryPath.Folder; Task.Factory.StartNew(() => { var date = DateTime.MinValue; foreach (var tick in storage.GetTickMessageStorage(_security, new LocalMarketDataDrive(path)).Load()) { AppendTick(_security, tick); _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; } } } }) .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()); }
private void InitCharts() { _candle = null; _lastPrice = 0m; _allCandles.Clear(); _chartCombined.ClearAreas(); _chartBindVisibleRange.ClearAreas(); _areaComb = new ChartArea(); _areaBVR1 = new ChartArea(); _areaBVR2 = new ChartArea(); _areaComb.YAxises.Add(new ChartAxis { Id = _chartMainYAxis, AutoRange = false, AxisType = ChartAxisType.Numeric, AxisAlignment = ChartAxisAlignment.Right, }); _areaBVR2.YAxises.Add(new ChartAxis { Id = _chartMainYAxis, AutoRange = false, AxisType = ChartAxisType.Numeric, AxisAlignment = ChartAxisAlignment.Right, }); _chartCombined.AddArea(_areaComb); _chartBindVisibleRange.AddArea(_areaBVR1); _chartBindVisibleRange.AddArea(_areaBVR2); _timeframe = int.Parse((string)((ComboBoxItem)_comboMainTimeframe.SelectedItem).Tag); var step = (decimal)_updownPriceStep.Value.Value; var series = new CandleSeries( typeof(TimeFrameCandle), _security, TimeSpan.FromMinutes(_timeframe)); _candleElement1 = new ChartCandleElement { FullTitle = "Candles", YAxisId = _chartMainYAxis }; _chartCombined.AddElement(_areaComb, _candleElement1, series); _bvElement = new ChartBoxVolumeElement(_timeframe, step) { FullTitle = "BoxVolume", YAxisId = _chartMainYAxis }; _chartCombined.AddElement(_areaComb, _bvElement); _cpElement = new ChartClusterProfileElement(_timeframe, step) { FullTitle = "Cluster profile" }; _chartBindVisibleRange.AddElement(_areaBVR1, _cpElement); _candleElement2 = new ChartCandleElement { FullTitle = "Candles", YAxisId = _chartMainYAxis }; _chartBindVisibleRange.AddElement(_areaBVR2, _candleElement2, series); var ns = typeof(IIndicator).Namespace; var rendererTypes = typeof(Chart).Assembly .GetTypes() .Where(t => !t.IsAbstract && typeof(BaseChartIndicatorPainter).IsAssignableFrom(t)) .ToDictionary(t => t.Name); var indicators = typeof(IIndicator).Assembly .GetTypes() .Where(t => t.Namespace == ns && !t.IsAbstract && typeof(IIndicator).IsAssignableFrom(t)) .Select(t => { var name = t.Name; var p = rendererTypes.TryGetValue(name + "Painter"); if (p == null) { if (t.Name.EndsWith("Indicator")) { name = name.Substring(0, name.Length - "Indicator".Length); } p = rendererTypes.TryGetValue(name + "Painter"); } return(new IndicatorType(t, p)); }) .ToArray(); _chartCombined.IndicatorTypes.AddRange(indicators); _chartBindVisibleRange.IndicatorTypes.AddRange(indicators); }