Candles creating by instrument basket
Click or drag to resize

Candles creating by instrument basket

To create ContinuousSecurity, WeightedIndexSecurity or ExpressionIndexSecurity candles the same procedure used as for creating the Security candles.

For example, create a 1 min candle for APM5 - ESM5 spread:

C#
private OpenECryTrader _trader;

private Security _instr1;
private Security _instr2;
private WeightedIndexSecurity _indexInstr;

private const string _secCode1 = "APM5";
private const string _secCode2 = "ESM5";

private CandleManager _candleManager;
private CandleSeries _indexSeries;

readonly TimeSpan _timeFrame = TimeSpan.FromMinutes(1);

private ChartArea _area;
private ChartCandleElement _candlesElem;

...

_area = new ChartArea();
_chart.Areas.Add(_area);

_candlesElem = new ChartCandleElement();
_area.Candles.Add(_candlesElem);

...

_candleManager = new CandleManager(_trader);
_candleManager.Processing += DrawCandles;
....
ConfigManager.RegisterService<ISecurityProvider>(_trader);
...

_indexInstr = new WeightedIndexSecurity() { Board = ExchangeBoard.Nyse, Id = "IndexInstr" };
_indexInstr.Weights.Add(_instr1, 1);
_indexInstr.Weights.Add(_instr2, -1);
_indexSeries = new CandleSeries(typeof(TimeFrameCandle), _indexInstr, _timeFrame)
{
    BuildCandlesMode = BuildCandlesModes.Build,
};
...

_candleManager.Start(_indexSeries);

After that, candles will come to the event handler Processing - DrawCandles. These candles can be displayed on the chart:

C#
private void DrawCandles(CandleSeries series, Candle candle)
{
    if (series == _indexSeries) 
       {
          var chartData = new ChartDrawData();
          chartData.Group(candle.OpenTime).Add(_candleElement, candle);
          _chart.Draw(chartData);
       }
}
See Also