Download MetaTrader 5

Correct way to calculate an indicator in a for loop

To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
Leo
86
Leo 2015.08.30 18:08 

Hi everyone,

I wish to know if this is the correct way to calculate indicator's value in a for loop (throughout all the available bars):

int OnCalculate(...)

{

//... 

ArraySetAsSeries(SignalLine,false); 

//... 

for(int i=0; i<Bars; i++)

   double ma=iMA(NULL,0,MaPeriod,0,MaMethod,MaPrice,i);

//...

SignalLine[i]=ma;

}

//...

return rates_total

P.S. Logically in this simple case I would like to represent and plot a MA replication by iMA object. But I am not sure of how I set the loop. I get a bit of difference compared to the one calculated with built-in MT4. I cannot figure out why!

Thanks 

Keith Watford
Moderator
9624
Keith Watford 2015.08.30 21:11  
ArraySetAsSeries(SignalLine,false); 
By setting the buffer not to be as series, the iMA calculation for the last bar on the chart will be applied to the first bar on the chart, so back to front.
Leo
86
Leo 2015.08.31 08:27  
Ok thanks! 
To add comments, please log in or register