iMA issues - values do not match Moving Averages indicator

 

I'm having some issues with iMA... it seems to give different results than the Moving Averages indicator for long periods.

I'm also calculating the EMA using SQL - the SQL and the indicator agree on values (taking rounding into account), but the iMA function gives a different value...

I would hope the indicator and iMA are using the same code, but I can't find the iMA code anywhere....

Any ideas?

eg

indicator sql iMa
maFast (15) 148.726 148.726100583001 148.72610058 all agree on value for short period
maSlow(350) 148.504 148.503793562465 148.50205960 iMA gives a different value for longer period

iMA code as follows:

maSlow = iMA(NULL,0,350,1,MODE_EMA,PRICE_CLOSE,0);
maFast = iMA(NULL,0,15,1,MODE_EMA,PRICE_CLOSE,0);

... I also tried...

maSlow = iMA(NULL,0,350,0,MODE_EMA,PRICE_CLOSE,1);
maFast = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,1);

...as I wasn't clear of the difference between the two shift indices... same result....

 
mctaff:

I'm having some issues with iMA... it seems to give different results than the Moving Averages indicator for long periods.

I'm also calculating the EMA using SQL - the SQL and the indicator agree on values (taking rounding into account), but the iMA function gives a different value...

I would hope the indicator and iMA are using the same code, but I can't find the iMA code anywhere....

Any ideas?

I'm pretty sure that MT4 uses the standard definition of an EMA given here: http://www.iexplain.org/exponential-moving-average-defined/.


The fun is that this definition is recursive: one of the inputs to the calculation is the previous EMA. What most software does is to take a simple average of the first N data points, and use this as the input into the first EMA calculation. MT4 does something very similar, but simply takes the price of the very first bar as its average and uses that as the input for the calculation for the subsequent bar. I can exactly replicate the result of iMA() - to 8 decimal places - in Excel using this method (on data exported from MT4).


The interesting thing about this method is that values more than N bars ago have a bearing - albeit a tiny one - on the current EMA value and, correspondingly, the value can be very slightly different depending on the total length of the bar history being used in the calculation, not just the values of the last N bars. Strictly speaking, the period parameter to an EMA calculation refers to a weighting factor, not actually to the number of bars to use.

Reason: