changing moving average period from code

Created at 12 Apr 2023, 10:56
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!
YA

yaghouti

Joined 19.12.2019

changing moving average period from code
12 Apr 2023, 10:56


Hi

i have a moving average like:

param1 = Indicators.MovingAverage(param2, period1, MovingAverageType.Simple);

which is in the initialize

i want to change the period1 from inside code during Calculate function but how much i change period1 from inside the code nothing change.


@yaghouti
Replies

firemyst
13 Apr 2023, 03:44

It won't change unless you create a new MovingAverage with the new setting or overwrite the previous one.

So after you change "period1", you should do this again:

param1 = Indicators.MovingAverage(param2, period1, MovingAverageType.Simple);

The Initialize method will only run again if you display the parameters window and change it that way.

If you programmatically change values, then you need to programmatically create new movingaverage objects with those new parameters.


@firemyst

yaghouti
13 Apr 2023, 09:19

RE:

i want to check some parameters on each tick and change the period1 from calculate,

would you mind please explain me how to do this task?

 

 

firemyst said:

It won't change unless you create a new MovingAverage with the new setting or overwrite the previous one.

So after you change "period1", you should do this again:

param1 = Indicators.MovingAverage(param2, period1, MovingAverageType.Simple);

The Initialize method will only run again if you display the parameters window and change it that way.

If you programmatically change values, then you need to programmatically create new movingaverage objects with those new parameters.

 


@yaghouti

firemyst
13 Apr 2023, 10:17

RE: RE:

yaghouti said:

i want to check some parameters on each tick and change the period1 from calculate,

would you mind please explain me how to do this task?

 

 

firemyst said:

It won't change unless you create a new MovingAverage with the new setting or overwrite the previous one.

So after you change "period1", you should do this again:

param1 = Indicators.MovingAverage(param2, period1, MovingAverageType.Simple);

The Initialize method will only run again if you display the parameters window and change it that way.

If you programmatically change values, then you need to programmatically create new movingaverage objects with those new parameters.

 

 

Not knowing everything you want to do, it's not that complicated. In a nutshell, it'll be similar to the following:

protected override void Calculate(int index)
{
// do some stuff or whatever you want to do

//check and change
if ( [whatever condition] == true)
{
  //set the new period1
  period1 = 12; //or whatever

  //update the MA
  param1 = Indicators.MovingAverage(param2, period1, MovingAverageType.Simple);
}

//do whatever else you need to

}

 

 


@firemyst