Click or drag to resize

Indicator adding to the chart

The following example shows adding of indicator to draw on the chart:

C#
private RithmicTrader _trader;
private Security _security;

private CandleManager _candleManager;
private CandleSeries _candleSeries;
private SimpleMovingAverage _sma;

readonly TimeSpan _timeFrame = TimeSpan.FromMinutes(1);

private ChartArea _area;
private ChartCandleElement _candlesElem;
private ChartIndicatorElement _longMaElem;

...

// _chart - StockSharp.Xaml.Charting.Chart
// new chart area
_area = new ChartArea();
_chart.Areas.Add(_area);

// Special chart element to draw a candles
_candlesElem = new ChartCandleElement();
_area.Elements.Add(_candlesElem);

// Special chart element to draw an indicator
_longMaElem = new ChartIndicatorElement
{
    Title = "Long"
};
_area.Elements.Add(_longMaElem);
...

_sma = new SimpleMovingAverage() { Length = 80 };
_candleManager = new CandleManager(_trader);
_candleManager.Processing += Draw;

...

_candleSeries = new CandleSeries(typeof(TimeFrameCandle), _security, _timeFrame);
_candleManager.Start(_candleSeries);

...

private void Draw(CandleSeries series, Candle candle)
{
   if (candle.State != CandleStates.Finished)
       return;

    var longValue = _sma.Process(candle);
    var data = new ChartDrawData();

    data
        .Group(candle.OpenTime)
            .Add(_candlesElem, candle)
            .Add(_longElem, longValue);

    _chart.Draw(data);

}
indicators chart
See Also

Other Resources