Download MetaTrader 5

EA Codeing i get error 4007 i want to get MA Values

To add comments, please log in or register
Have you published a program in the Market? Place it in your blog as well - the widget will help!
Trading
13
Trading 2013.11.27 21:45 

Hello,

I am trying to make somethink simple but i dont know what i have wrong in my Code.

I am trying to get the last 2 Moving Average Values with the following code:

 

 double ma[2000];
   if(CopyBuffer(ExtHandle,0,0,10,ma)!=1)
     {
      Print("CopyBuffer from MA failed, no data,error: ",GetLastError());
      return;
     }

 i did think that i can get then from the Array ma[] the last 10 Moving average values, for example ma[0] should have the current bar values and ma[1] the last closed and ma[2] the values from bar 2, but it is not working, i just get error 4007.µCan somebody tell me how i must write this code that it work and that i get the values wich i want?

Alain Verleyen
Moderator
30743
Alain Verleyen 2013.11.27 23:07  

A lot of errors here :

 double ma[2000];
  • You are copying 10 values, so you don't need an array with 2000 values
 if(CopyBuffer(ExtHandle,0,0,10,ma)!=1)
  • CopyBuffer returns the number of copied values, so as you are asking 10 values, you only get an error if CopyBuffer returns a value less than 10, not !=1.
  • In mql5, array are indexed in a standard way from 0 (the oldest value) to n (the newest value), you have to use ArraySetAsSeries the change this indexation if you need it.
  • If you need 3 values, use an array of 3 values or use a dynamic array :
   double ma[3];
   if(CopyBuffer(ExtHandle,0,0,3,ma)!=3)
     {
      Print("CopyBuffer from MA failed, no data,error: ",GetLastError());
      return;
     }
   ArraySetAsSeries(ma,true);
   Print("Value of current bar= ",ma[0]);
   Print("Value of last closed bar= ",ma[1]);
   Print("Value close bar(2)= ",ma[2]);
Trading
13
Trading 2013.11.28 09:56  

Thank you for this example, it was very helpfull.

One little thinks i making wrong, i get the message that ArraySetAsSeries() cannot be use for static arrays, how can i create another array type? 

Alain Verleyen
Moderator
30743
Alain Verleyen 2013.11.28 13:45  
Trading:

Thank you for this example, it was very helpfull.

One little thinks i making wrong, i get the message that ArraySetAsSeries() cannot be use for static arrays, how can i create another array type? 

You are right, it's my fault. Simply declare ma array as :

   double ma[];
To add comments, please log in or register