sma on MT5 random result

 

Hi, I'm developing an indicator based on sma of a limited set of bars.

Trying different way to calculate sma I obtain different result for the same bar.

I use 4 different way to calculate the sma:

1) using SimpleMAOnBuffer on all available bars (rates_total)

2) using SimpleMA on a limited set of bar (Nbar)

3) using iMA handle over all bars available (rates_total)

4) using SimpleMAOnBuffer on a limited set of bar (Nbar)


Here the result for EURJPY,M1 on a particular bar (2016.11.15 04:31) of the three calculated SMA  with period 420 :

SMA1=116.3047904761912
SMA2=116.3047904761905
SMA3=116.3047904761905
SMA4=116.3047904761901


As you can see there are differences.

My question is why?

Wouldn't be sma always the same indipendently from number of bar and the way you use to calculate it?

Which is the most reliable way to calculate a sma?


I attach the code I wrote.

Files:
prova forum.mq5  11 kb
 
Where is the problem? The 12th position after decimal point is different. This is negligible small and there is no use of the difference.
 
If you use the sma for other calculation (I mean calculation relative to cycle) the difference become significant. This is becouse the error is propagated. Howewer the sma should be identical, why It's not equal?
 
gioooooo:
If you use the sma for other calculation (I mean calculation relative to cycle) the difference become significant. This is becouse the error is propagated. Howewer the sma should be identical, why It's not equal?

It depends how they calculate it ...

SimpleMAOnBuffer() uses the "fast" calculation method (when the previous average is calculated using previous average value - which makes it wrong - try calculating 5 period sma on the following set "1 0 0 0 0 0" and you will not get 0 for the last sma)

iMA() (as far as I am aware of) does the calculation the same way as SimpleMAOnBuffer()

SimpleMA() does that correctly - maybe it will be slower on long periods but it will not make that error from above