Add 2 values and create a Moving Average from the result in a cBot

Created at 28 Jun 2016, 21:57
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
CT

ctid233655

Joined 28.06.2016

Add 2 values and create a Moving Average from the result in a cBot
28 Jun 2016, 21:57


Hi,

I have very little experience in codding so I don't even know if this is possible but I want to add the value of 2 indicators in a cbot and then create a moving average 8 periods from the final result.

Like this in a cbot:

Ind1 = xpto1_indicator;

Ind2 = xpto2_indicator;

 

Ind3 = Ind1 + Ind2;

MA8p_Ind3 = ....


@ctid233655
Replies

ctid233655
29 Jun 2016, 13:16

This is the IndicatorDataSeries platform example:

//  The following example is the calculation of the simple moving average
//  of the median price


[Output("Result")]
public IndicatorDataSeries Result { get; set; }
private IndicatorDataSeries _dataSeries;
private SimpleMovingAverage _simpleMovingAverage;


protected override void Initialize()
{
    _dataSeries = CreateDataSeries();
    _simpleMovingAverage = Indicators.SimpleMovingAverage(_dataSeries, 14);
}
public override void Calculate(int index)
{
    _dataSeries[index] = (MarketSeries.High[index] + MarketSeries.Low[index])/2;
    Result[index] = _simpleMovingAverage.Result[index];
}
 

This works well in an indicator but it doesn't seem to work in a cBot.


@ctid233655

ctid233655
30 Jun 2016, 03:28

Problem solved.

The previous code works on a cbot as well.

The problem is that you need to initialize the previous values in the _dataSeries up to the number of periods in the Moving average. Otherwise you'll have to wait until the number of periods have gone by which in higher timeframe charts might be a very long time.

 

 


@ctid233655