From a Beginner: 3 EMA Cross Indicator issue

 

Hello Masters,

I just started to learn MT4 Promgram and am doing some practice on my own. And I have an Array out of range issue that I couldnt figure out. Can you guys help me to point out where and why it's wrong?

Goal: 

Draw Up arrow when EMA8 crosses EMA20 from below (golden cross) when price is over EMA50

Draw Down arrow when EMA8 corsses EMA20 from above (dead cross) when price is below EMA50

Here is the original code that I have. Thank you guys very much here first !!!

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3

input int      EMA8=8;
input int      EMA20=20;
input int      EMA50=50;

double         EMA8Buffer[];
double         EMA20Buffer[];
double         EMA50Buffer[];
double         UpBuffer[];
double         DownBuffer[];

int OnInit()
  {

   SetIndexBuffer(0,EMA8Buffer);
   SetIndexBuffer(1,EMA20Buffer);
   SetIndexBuffer(2,EMA50Buffer);
   
   SetIndexLabel(0,"EMA8");
   SetIndexLabel(1,"EMA20");
   SetIndexLabel(2,"EMA50");
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrGold);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clrPeachPuff);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1,clrRed);
   
   SetIndexBuffer(3,UpBuffer);
   SetIndexBuffer(4,DownBuffer);
   
   SetIndexArrow(3,225);
   SetIndexArrow(4,226);
   
   ArraySetAsSeries(EMA8Buffer,true);
   ArraySetAsSeries(EMA20Buffer,true);
   ArraySetAsSeries(EMA50Buffer,true);
   ArraySetAsSeries(UpBuffer,true);
   ArraySetAsSeries(DownBuffer,true);

   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {


   if(rates_total<EMA50 || rates_total<EMA20 || rates_total<EMA8)
   {
      return(0); 
   }

   int limit=rates_total-prev_calculated;

   if(prev_calculated>0) limit++;   
      
   for(int i=0; i<limit; i++)
   {
        EMA8Buffer[i] = iMA(NULL,0,EMA8,0,MODE_EMA,PRICE_CLOSE,i);
        EMA20Buffer[i] = iMA(NULL,0,EMA20,0,MODE_EMA,PRICE_CLOSE,i);
        EMA50Buffer[i] = iMA(NULL,0,EMA50,0,MODE_EMA,PRICE_CLOSE,i);
        
   }
  
   for(int i=0; i<limit; i++)
   {

         if(Close[i]>=EMA50Buffer[i])
         {
            if(EMA8Buffer[i]>EMA20Buffer[i] && EMA8Buffer[i+1]<EMA20Buffer[i+1])
            {
               UpBuffer[i]=EMA8Buffer[i]; //Array out of range.
            }
         }
         
         else
         {
            if(EMA8Buffer[i]<EMA20Buffer[i] && EMA8Buffer[i+1]>EMA20Buffer[i+1])
            {
               DownBuffer[i]=EMA20Buffer[i]; //Array out of range.
            }
         }

   }
 
   return(rates_total);
  }

 

When you get an array out of range error it tells you the line that the error occurs on.

Please highlight the line and help others to help you.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.

 
mnmnmmnnmnmn: And I have an Array out of range issue that I couldnt figure out.

How many indicator buffers to you declare that you use, and how many are you trying to use?

 
Keith Watford:

When you get an array out of range error it tells you the line that the error occurs on.

Please highlight the line and help others to help you.


In future please post in the correct section

I will move your topic to the MQL4 and Metatrader 4 section.

Thank you Keith for moving my question to the right section. But I have no idea how to highlight inside the code.

 
William Roeder:

How many indicator buffers to you declare that you use, and how many are you trying to use?

Hi William,

I declared three buffers, three EMAs and two Arrows.

And in my code above, I used all five of them. Am I doing anything wrong here?

 

Try again. I asked a two part question. Answer my question completely.

 

Thanks William for your tip. I found the mistake and change the buffer 3 to 5. The Out of Range error is now gone :).

But the arrow is not showing up. Can I get the next tip from you? You are so awesome! Thank again first.


#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5
//--- plot EMA8
#property indicator_label1  "EMA8"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot EMA20
#property indicator_label2  "EMA20"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrPeachPuff
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot EMA50
#property indicator_label3  "EMA50"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot UP
#property indicator_label4  "UP"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  C'143,188,139'
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot DOWN
#property indicator_label5  "DOWN"
#property indicator_type5   DRAW_ARROW
#property indicator_color5  clrRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

input int      EMA8=8;
input int      EMA20=20;
input int      EMA50=50;

double         EMA8Buffer[];
double         EMA20Buffer[];
double         EMA50Buffer[];
double         UPBuffer[];
double         DOWNBuffer[];

int OnInit()
  {

   SetIndexBuffer(0,EMA8Buffer);
   SetIndexBuffer(1,EMA20Buffer);
   SetIndexBuffer(2,EMA50Buffer);
   SetIndexBuffer(3,UPBuffer);
   SetIndexBuffer(4,DOWNBuffer);

   PlotIndexSetInteger(3,PLOT_ARROW,159);
   PlotIndexSetInteger(4,PLOT_ARROW,159);
   

   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   if(rates_total<EMA50 || rates_total<EMA20 || rates_total<EMA8)
   {
      return(0); 
   }

   int limit=rates_total-prev_calculated;

   if(prev_calculated>0) limit++;   
      
   for(int i=0; i<limit; i++)
   {
        EMA8Buffer[i] = iMA(NULL,0,EMA8,0,MODE_EMA,PRICE_CLOSE,i);
        EMA20Buffer[i] = iMA(NULL,0,EMA20,0,MODE_EMA,PRICE_CLOSE,i);
        EMA50Buffer[i] = iMA(NULL,0,EMA50,0,MODE_EMA,PRICE_CLOSE,i);
        
        if(Close[i]>=EMA50Buffer[i])
         {
            if(EMA8Buffer[i]>EMA20Buffer[i] && EMA8Buffer[i+1]<EMA20Buffer[i+1])
            {
               UPBuffer[i]=EMA8Buffer[i]; 
            }
         }
         
         else
         {
            if(EMA8Buffer[i]<EMA20Buffer[i] && EMA8Buffer[i+1]>EMA20Buffer[i+1])
            {
               DOWNBuffer[i]=EMA20Buffer[i]; 
            }
         } 
   }
  

   return(rates_total);
  }


 

Oh, I got it. I thought I was smart combined the two for loops together. But I realized it was a mistake. Now it's working yes ! Thank you William !!


Reason: