Download MetaTrader 5

iMAonArray on simple array

To add comments, please log in or register
elektor
34
elektor  

Hi all,

Was trying to get my head around the iMAonArray function. I noticed that if you use a simple array then the iMAonArray function reads from lower to upper bound of the array variable whereas if the array is an indicator buffer array then the iMAOnArray function reads from upper to lower bound of the array. It took me some time to realise that I cannot use a custom indicator in an EA.

For example here is my EA code to check the simple array version

int init()
{
   double myEMA_Exp1[10]; //This is a simple output array
   double myArray[10]; // This is a simple input array
   
   int Handle, QntSymbol;
   string FileName = "EMAVersion2.txt";

   Handle = FileOpen(FileName, FILE_CSV|FILE_WRITE|FILE_READ,",");

   if (Handle == -1)
   {
      Alert("An error while opening the file. ",// Error message              
            "May be the file is busy by the other applictiom");      
      PlaySound("Bzrrr.wav");          // Sound accompaniment      
      return;                          // Exir start()
   }

   //double myArray[10];
   int x = 0;
   
   for(int i = 9; i >= 0; i--)
   {
      myArray[x] = i;
      Print(myArray[x]);
      x++;
   }

   /***************************************************
  
    From page 84 of MQL4 Manual
   
   
    double iMAOnArray(double array[], int total, int period, int ma_shift, int ma_method,int shift)

    Calculates the Moving average counted on buffer and returns its value.

    Parameters
    array[] - Array with data.
    total - The number of items to be counted. 0 means whole array.
    period - Number of periods for calculation.
    ma_shift - MA shift. Indicator bars shifting back
    ma_method - MA method. It can be any of the Moving Average method enumeration value.
    shift - Shift relative to the current bar (number of periods back), where the input data should be taken from.
    Sample
    double macurrent=iMAOnArray(ExtBuffer,0,5,0,MODE_LWMA,0);
    double macurrentslow=iMAOnArray(ExtBuffer,0,10,0,MODE_LWMA,0);  
    double maprev=iMAOnArray(ExtBuffer,0,5,0,MODE_LWMA,1);
    double maprevslow=iMAOnArray(ExtBuffer,0,10,0,MODE_LWMA,1);

    if(maprev<maprevslow && macurrent>=macurrentslow)
    Alert("crossing up");
   
  ******************************************************/
  
   //Define EMA Period
   int EMA_Period = 2;   
   
   for(i = x - 1; i  >= 0; i--)
   {   
   
      //Simple EMA calculation; no shift; get output data back 1 time point
      myEMA_Exp1[i] = iMAOnArray(myArray, 0, EMA_Period, 0, MODE_EMA, i);
   
      //FileSeek(Handle, 0, SEEK_END);
      QntSymbol = FileWrite(Handle, "array = " + myArray[i], " i = " + i, " myEMA_Exp1 = " + myEMA_Exp1[i]);
   
   
       //File error handling
       if (QntSymbol < 0)
       {
          Alert("An error while opening the file. ",// Error message              
          "May be the file is busy by the other applictiom");      
          PlaySound("Bzrrr.wav");          // Sound accompaniment      
          return;                          // Exir start()    
       }   
   }
   FileClose(Handle);
   
   return(0);

}
int start()
  {
  //int    counted_bars=IndicatorCounted();
   return(0);
  }
//+------------------------------------------------------------------+

The output is like below

array = 0.00000000, i = 9, myEMA_Exp1 = 9.00000000
array = 1.00000000, i = 8, myEMA_Exp1 = 8.33333333
array = 2.00000000, i = 7, myEMA_Exp1 = 7.44444444
array = 3.00000000, i = 6, myEMA_Exp1 = 6.48148148
array = 4.00000000, i = 5, myEMA_Exp1 = 5.49382716
array = 5.00000000, i = 4, myEMA_Exp1 = 4.49794239
array = 6.00000000, i = 3, myEMA_Exp1 = 3.49931413
array = 7.00000000, i = 2, myEMA_Exp1 = 2.49977138
array = 8.00000000, i = 1, myEMA_Exp1 = 1.49992379
array = 9.00000000, i = 0, myEMA_Exp1 = 0.49997460

The excel calculation is attached in the file FOREX.2013.DEC.03.1. Please see calculation in column Q.

elektor
34
elektor  

Looks like I cannot attach excels. Here are the EMA calculations anyway

O P Q



EMA_Period=2/(2+1)

DataEMA calc
9
=9
8=8*EMA_Period+9*(1-EMA_Period)
7=7*EMA_Period+8*(1-EMA_Period)
6=6*EMA_Period+7*(1-EMA_Period)
5=5*EMA_Period+6*(1-EMA_Period)
4=4*EMA_Period+5*(1-EMA_Period)
3=3*EMA_Period+4*(1-EMA_Period)
2=2*EMA_Period+3*(1-EMA_Period)
1=1*EMA_Period+2*(1-EMA_Period)
0=0*EMA_Period+1*(1-EMA_Period)
Ian Venner
2584
Ian Venner  
cablegui:

Hi all,

Was trying to get my head around the iMAonArray function. I noticed that if you use a simple array then the iMAonArray function reads from lower to upper bound of the array variable whereas if the array is an indicator buffer array then the iMAOnArray function reads from upper to lower bound of the array. It took me some time to realise that I cannot use a custom indicator in an EA.

For example here is my EA code to check the simple array version

The output is like below

The excel calculation is attached in the file FOREX.2013.DEC.03.1. Please see calculation in column Q.

The difference between the two arrays is because the buffer array is SetAsSeries(). You can use the values from a custom indicator in an EA by calling iCustom.

elektor
34
elektor  

Briliant thanks for the info SDC.

To add comments, please log in or register