Is MT4 iMAOnArray function broken? PLEASE verify my simple test code.

 
I want to officially report to MQ that the iMAOnArray function is broken, but before I do, can anyone verify that my test code isn't the problem?
I believe this script when dropped on a chart should return three identical values, but the iMAOnArray method doesn't match.

//+------------------------------------------------------------------+
//|                                                        try.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#include <stdlib.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script "asdfsadf"                                   |
//+------------------------------------------------------------------+
int start()
  {
//----
   double testarray[10];
   double manualSMA=0;
   for (int cnt=1; cnt<10; cnt++)
      {
      testarray[cnt-1]=Close[cnt];
      manualSMA=manualSMA+Close[cnt];
      //Print (testarray[cnt-1]);
      }

   manualSMA=manualSMA/9;

   Print("IMA:         ",DoubleToStr(iMA(NULL,0,9,0,MODE_SMA,PRICE_CLOSE,1),8));
   Print("IMAonarray:  ",DoubleToStr(iMAOnArray(testarray,0,9,0,MODE_SMA,0),8));
   Print("manualSMA:   ",DoubleToStr(manualSMA,8));


   return(0);
  }
//+------------------------------------------------------------------+



The only reason that testarray is declared with 10 elements is that iMAOnArray returns 0 when declared with 9, though 9 elements should be enough for a 9-period SMA right?
iMAOnArray does appear to work correctly when passed an indicator buffer as the array, hence the OSMA code included with MT4 does work properly. But it appears that the function fails miserably if passed a "normal" array.

Or...do I simply not understand the documentation? I just noticed that in the above script if the iMAOnArray parameters are changed from MODE_SMA,0 to MODE_SMA,1, it does work correctly. I did think it was odd that the documentation of the shift parameter says "Shift relative to the current bar" but I figured that was just accidentally left in and that it was meant to say "Shift relative to the start of the array" which I'm quite sure is 0. But what does the current bar have to do with the position of elements in an independent array? Can anyone who understands the usage of this function please clarify this?

 
I had a similar problem recently but haven't gotten an answer:
"Bug, or can anyone explain this please (iMaOnArrray)"

I guess somehow the function is tied to indicator buffers and does produce surprising results on regular arrays. But a clarification from MQ would sure be helpful.


Markus
 
   ...
   double testarray[10];
   double manualSMA=0;
   ArraySetAsSeries(testarray,TRUE);
   for (int cnt=1; cnt<10; cnt++)
   ...


iMaOnArray (and so on) calculates from left to right, but Close[1]->Close[10] is right->left. to avoid this problem use ArraySetAsSeries function

 

The only reason that testarray is declared with 10 elements is that iMAOnArray returns 0 when declared with 9, though 9 elements should be enough for a 9-period SMA right?


There is definitely serious problem while evaluating array by < > and 9, 10.
However, it is intermittent and I cannot coherently report it.

Please Slawa, look into it!
array[n] > 9 gives intermittent false when over or equ 9
array[n] < 10 gives intermittent false when over or equ10
 

iMaOnArray (and so on) calculates from left to right, but Close[1]->Close[10] is right->left. to avoid this problem use ArraySetAsSeries function



Slawa, thanks very much for the explanation.
Could this explanation pleeeeease be added to the documentation for all the functions it affects, which I suppose is generally the "... on buffer" functions under "Technical Indicator calls"? Without this explanation things are really not clear enough especially since it's not needed when used on an indicator buffer.

Or would Metaquotes perhaps consider making these "... on buffer" functions assume that the array used is a series array rather than requiring the array to be set that way? Since iMaOnArray is listed under "Technical Indicator calls" I assumed it did work like this (i.e. starting from 0 and going "backwards" through higher numbers just as if on a chart), since all chart-based indicator calls do work like this, and I think most MT4 users would assume the same.
I realize you all may hesitate to change something like this, but consider that it will probably save a lot of users much frustration and unexpected results (if they even notice their results are incorrect--which can be hard to detect) when they first use the "... on buffer" functions, and existing properly written code (i.e. using ArraySetAsSeries) will still continue to work as expected. Only incorrect code (such as my sample) will no longer work as the user has seen it work before--but that's only because it was done wrong in the first place.

One last thing, I still do think it's a bug that the array must be one element larger than the periods parameter of iMaOnArray. Could this be fixed?

I will submit these requests using the official form unless I get a response here stating that the requests are already noted. Thanks!
 
I second this.
 
 
Hi Slawa:

I have two questions
1) Does IMaOnArray calculates from Close[0] to Close[10] or From Close[1] to Close[10]?
2) Does iEnvelopesOnArray and iEnvelopes calculates also from left to right? Do I have to use ArraySetAsSeries function?

Regards,

Gonzalo


   ...
   double testarray[10];
   double manualSMA=0;
   ArraySetAsSeries(testarray,TRUE);
   for (int cnt=1; cnt<10; cnt++)
   ...


iMaOnArray (and so on) calculates from left to right, but Close[1]->Close[10] is right->left. to avoid this problem use ArraySetAsSeries function

 
Hello Gonzalo
1. no. from Close[9] to Close[0]
2. yes and yes
 
Hello Slawa:

First at all. I appreciatte you quick answer. I have two issues to ask

1) Which is the difference between iEnvelopes and iEnvelopesOn array?
2) I do not understand which is the importance of a "buffer" and if I can avoid it.
3) Can I send the code to open position?, I have tried everything. I have troubles with my code to close and to open positions . However I think the problem is the same because the criteria to close is similar to the criteria to open. So I think I have the same problem for both cases. I have used iEnvelopes and iEnvelopesOnArray, I have used variables and arrays type double. I really do not know what to do. I have search in MQL4.com and in this forum. I HAVE DONE THE HOMEWORK but I don&#180;t know what to do.

Regards,

Gonzalo (From Colombia)


Hello Gonzalo
1. no. from Close[9] to Close[0]
2. yes and yes
 
Hello Slawa:

I forgot to explain you that there is something wrong with the parameters for the function iEnvelopes. When you see the help of MT4, you find the explanation about iEnvelopes has 8 inputs, but when you see the example of MT4 there are only 7 input used, So I am confused about that

Furthermore, I do not understand the difference between parameteres int ma_shift and int shift. I think int shift is useful to expalin you want the envelope for time 0, 1, 3, etc. But which is the function of int ma_shift?

Regards,

Gonzalo




Hello Slawa:

First at all. I appreciatte you quick answer. I have two issues to ask

1) Which is the difference between iEnvelopes and iEnvelopesOn array?
2) I do not understand which is the importance of a "buffer" and if I can avoid it.
3) Can I send the code to open position?, I have tried everything. I have troubles with my code to close and to open positions . However I think the problem is the same because the criteria to close is similar to the criteria to open. So I think I have the same problem for both cases. I have used iEnvelopes and iEnvelopesOnArray, I have used variables and arrays type double. I really do not know what to do. I have search in MQL4.com and in this forum. I HAVE DONE THE HOMEWORK but I don&#180;t know what to do.

Regards,

Gonzalo (From Colombia)


Hello Gonzalo
1. no. from Close[9] to Close[0]
2. yes and yes
[/quote]