iMAOnArray returns different results for static array and indicator buffer

Back to topics list To post a new topic, please log in or register
avatar
3
anikulin 2016.01.14 17:06
 

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 

Attached files:
  test.mq4  (0.75 KB)
avatar
13010
WHRoeder 2016.01.14 17:39 #
 
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




avatar
3
anikulin 2016.01.15 12:25 #
 
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).
avatar
3
anikulin 2016.01.18 17:19 #
 
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? 

Attached files:
  test_1.mq4  (1.22 KB)
Back to topics list  

To add comments, please log in or register