Exemplo n.º 1
0
		private TimeFrameCandle GetCandle(DateTimeOffset time, decimal price, decimal vol)
		{
			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,
				};

				_candle.OpenPrice = _candle.HighPrice = _candle.LowPrice = _candle.ClosePrice = price;
				_candle.VolumeProfileInfo.Update(new InputTick(time, price, vol));

				_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 += vol;

			_candle.VolumeProfileInfo.Update(new InputTick(time, price, vol));

			return _candle;
		}
Exemplo n.º 2
0
		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);
		}
Exemplo n.º 3
0
		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;
		}
Exemplo n.º 4
0
		private void CandleBuilderOnCandle(TimeFrameCandle candle)
		{
			var sub = _candleSubscriptions.TryGetValue(new SubscriptionKey(candle.Security.Id, (TimeSpan) candle.Arg));
			var candles = new [] {candle};

			sub?.Subscribers.ForEach(s => Candles?.Invoke(s, candles));
		}
Exemplo n.º 5
0
		private bool CheckSameCandle(TimeFrameCandle candle, TimeFrameCandleMessage msg)
		{
			return candle.Arg == msg.Arg && candle.OpenTime == msg.OpenTime;
		}
Exemplo n.º 6
0
		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.TradeVolume.Value;

			_volumeProfile.Update(new TickCandleBuilderSourceValue(security, tick));
		}
Exemplo n.º 7
0
		private void LoadData()
		{
			_candle = null;
			_lastPrice = 0m;
			_allCandles.Clear();

			var id = new SecurityIdGenerator().Split(SecurityId.Text);

			_security = new Security
			{
				Id = SecurityId.Text,
				PriceStep = 5,
				Board = ExchangeBoard.GetBoard(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;

						this.GuiAsync(() =>
						{
							BusyIndicator.BusyContent = date.ToString();
						});

						maxDays--;

						if (maxDays == 0)
							break;
					}
				}
			})
			.ContinueWith(t =>
			{
				if (t.Exception != null)
					Error(t.Exception.Message);

				BusyIndicator.IsBusy = false;
			}, TaskScheduler.FromCurrentSynchronizationContext());
		}