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 :
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.
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