iMAOnArray returns different results for static array and indicator buffer

[Deleted]  

Hi guys,

Investigating the logic of one indicator I noticed that iMAOnArray() function returns different results for the same values depending on whether they are represented as indicator buffer or static array. I created a simple test that calculates Simple MA of 3 values with the period of 3.

      int N=3;
      MA[2] = 1.0913700000000015;
      MA[1] = 1.0811866666666683;
      MA[0] = 1.0766433333333350;

      double bufferArrayMA=iMAOnArray(MA,0,N,0,MODE_SMA,0);
      Print("BufferArrayMA "+ DoubleToString(bufferArrayMA,16));

      double array[3];
      array[0] = MA[2];
      array[1] = MA[1];
      array[2] = MA[0];
      double staticArrayMA=iMAOnArray(array,0,N,0,MODE_SMA,0);
      Print("StaticArrayMA "+ DoubleToString(staticArrayMA,16));

 I was expected that both results would be absolutely identical, but they were not:

StaticArrayMA 1.0830666666666682
BufferArrayMA 1.0830667368231886

 As you can see the difference is quite small (starting from the seventh digit), but still it's quite strange. 

 Does anybody have a clue what can cause this? Any help is appreciated. 

 The source code of the test is attached to this message.

 Thanks, Anton 

Files:
test.mq4  1 kb
William Roeder  
anikulin Does anybody have a clue what can cause this? Any help is appreciated.

Buffers are series arrays; zero is the latest value. so you get (MA[2]+MA[1]+MA[0])/3

Your array isn't.  So you get (array[-2] + array[-1] + array[0])/3 first 2 are garbage.

ArraySetAsSeries - MQL4 Documentation

[Deleted]  
WHRoeder:

Buffers are series arrays; zero is the latest value. so you get (MA[2]+MA[1]+MA[0])/3

Your array isn't.  So you get (array[-2] + array[-1] + array[0])/3 first 2 are garbage.

ArraySetAsSeries - MQL4 Documentation




But the problem is that iMAOnArray with a series array returns a different value from (MA[2]+MA[1]+MA[0])/3, but iMAOnArray with a static array returns correct one (the one that can be simply calculated as (MA[2]+MA[1]+MA[0])/3).
[Deleted]  
anikulin:
But the problem is that iMAOnArray with a series array returns a different value from (MA[2]+MA[1]+MA[0])/3, but iMAOnArray with a static array returns correct one (the one that can be simply calculated as (MA[2]+MA[1]+MA[0])/3).

I also noticed that the result that iMAOnArray returns depends on the order or elements, however it shouldn't as Simple MA is used.

When I tried to place 3 same values in different ways I got 3 different results (but none of them was correct):

BufferArrayMA 3 1.0830667461140955
BufferArrayMA 2 1.0830667464531798
BufferArrayMA 1 1.0830667368231886

 I also tested iMAOnArrayMQL4 function that is suggested as a replacement for iMAOnArray in MQL5 (https://www.mql5.com/en/articles/81) and its calculation on series array gave the same results as iMAOnArray for static array (1.0830666666666682, correct value).

 Any ideas why iMAOnArray works in a different way and why the order of elements influences the final result? 

Files:
test_1.mq4  2 kb