Array out of range when running indicator

 

Hello,

I have an indicator that show me 3 bar fractals. I'm drawing lines on OHLC of a candle and that's where it gives me some problems.


It works fine as long a i keep the number below i+4 in the line below - if i change it to i+5 i get the error "array out of range xxxx.mq4 (253,53)

ObjectCreate(strObjectNameLow, OBJ_TREND, 0, Time[i-5], value, Time[i+5], value);

Here is the code i'm working on

#property indicator_chart_window
#property indicator_buffers 2

#include <WinUser32.mqh>

extern int     Fractal_Timeframe                = 0;
extern int     Maxbar                           = 2000;
extern color   Up_Fractal_Color                 = Red;
extern int     Up_Fractal_Symbol                = 108;
extern color   Down_Fractal_Color               = Blue;
extern int     Down_Fractal_Symbol              = 108;
extern bool    Extend_Line                      = false;
extern bool    Extend_Line_to_Background        = true;
extern bool    Show_Validation_Candle           = true;
extern color   Up_Fractal_Extend_Line_Color     = Red;
extern int     Up_Fractal_Extend_Width          = 0;
extern int     Up_Fractal_Extend_Style          = 2;
extern color   Down_Fractal_Extend_Line_Color   = Blue;
extern int     Down_Fractal_Extend_Width        = 0;
extern int     Down_Fractal_Extend_Style        = 2;
extern double  DistanceFromCandle               = 25;

double UpBuffer[], DoBuffer[], refchk, tempref, level, level_1, level_2;
int barc;

//+------------------------------------------------------------------+
//|                                                             INIT |
//+------------------------------------------------------------------+
int init()
  {

   SetIndexBuffer(0,UpBuffer);
   SetIndexStyle(0,DRAW_ARROW, DRAW_ARROW, 0, Up_Fractal_Color);
   SetIndexArrow(0,Up_Fractal_Symbol);
   SetIndexBuffer(1,DoBuffer);
   SetIndexStyle(1,DRAW_ARROW, DRAW_ARROW, 0, Down_Fractal_Color);
   SetIndexArrow(1,Down_Fractal_Symbol);

   return(0);
  }

//+------------------------------------------------------------------+
//|                                                           DEINIT |
//+------------------------------------------------------------------+
int deinit()
  {
   for(int i = ObjectsTotal(); i >= 0; i--)
     {
      if(StringSubstr(ObjectName(i),0,12) == "MTF_Fractal_")
        {
         ObjectDelete(ObjectName(i));
        }
     }
   
   for(int i = ObjectsTotal(); i >= 0; i--)
     {
      if(StringSubstr(ObjectName(i),0,12) == "Example line")
        {
         ObjectDelete(ObjectName(i));
        }
     }
     
   Comment("");
   return(0);
  }
  
//+------------------------------------------------------------------+
//|                                                            START |
//+------------------------------------------------------------------+
int start()
  {
   // Check if correctly timeframe is selected. Only show dots on D1
   if((Period() == 1440))
   {  

      int c, dif;
      int i;
      
      tempref =   iHigh(Symbol(), Fractal_Timeframe, 1) +
                  iHigh(Symbol(), Fractal_Timeframe, 51) +
                  iHigh(Symbol(), Fractal_Timeframe, 101);

      if(barc != Bars || IndicatorCounted() < 0 || tempref != refchk)
        {
         barc = Bars;
         refchk = tempref;
        }
      else
         return(0);

      deinit();

      if(Fractal_Timeframe <= Period())
         Fractal_Timeframe = Period();

      dif = Fractal_Timeframe/Period();

      if(Maxbar > Bars)
         Maxbar = Bars-10;
         
      int CountHigh = 1;
      int CountLow = 1;
      

      
      for(i = 0; i < Maxbar; i++)
      {   
         if(iBarShift(NULL,Fractal_Timeframe,Time[i]) < 3)
         {
            UpBuffer[i] = 0;
            DoBuffer[i] = 0;
            continue;
         }
         
         // Add a dot above the candle that is the high fractal
         UpBuffer[i] = iFractals(NULL,Fractal_Timeframe,1,iBarShift(NULL,Fractal_Timeframe,Time[i]));
         
         if(CountHigh<2)
         {
            if(UpBuffer[i] > 0)
            {
               DrawFractalOHLCHigh(i, "M", High[i]);
               DrawFractalOHLCHigh(i, "N", Low[i]);
               DrawFractalOHLCHigh(i, "O", Open[i]);
               DrawFractalOHLCHigh(i, "P", Close[i]);
               CountHigh = CountHigh + 1;
            }
         }
         //level_1 = iFractals(NULL,Fractal_Timeframe,1,iBarShift(NULL,Fractal_Timeframe,Time[i]));
         //UpBuffer[i] = level_1+DistanceFromCandle*10*Point;
         
         // Add a dot below the candle that is the low fractal
         DoBuffer[i] = iFractals(NULL,Fractal_Timeframe,2,iBarShift(NULL,Fractal_Timeframe,Time[i]));         
         if(CountLow<2)
         {
            if(DoBuffer[i] > 0)
            {
               DrawFractalOHLCLow(i, "Q", High[i]);
               DrawFractalOHLCLow(i, "R", Low[i]);
               DrawFractalOHLCLow(i, "S", Open[i]);
               DrawFractalOHLCLow(i, "T", Close[i]);
               CountLow = CountLow + 1;
            }
         }

         //level_2 = DoBuffer[i] = iFractals(NULL,Fractal_Timeframe,2,iBarShift(NULL,Fractal_Timeframe,Time[i]));
         //DoBuffer[i] = level_2-DistanceFromCandle*10*Point;
      }         
               
      //if(Extend_Line)
      if(Show_Validation_Candle)
      {
         for(i = 0; i < 30; i++)
         {
            if(UpBuffer[i] > 0)
            {
               level = UpBuffer[i];
               for(c = i; c > 0; c--)
               {
                   if((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level))
                      break;
                   if(Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level)
                      break;
                   if(Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level)
                      break;
               }
               
               if(Extend_Line)
               {               
                  DrawLine ("H", i, c, level, Extend_Line_to_Background, Up_Fractal_Extend_Line_Color, Up_Fractal_Extend_Width, Up_Fractal_Extend_Style);
               }
               //if(Show_Validation_Candle)
               UpBuffer[i-2*dif] = level;
               i += dif;
            }
         }
      }
   
      if(Show_Validation_Candle)
      {
         for(i = 0; i < 30; i++)
         {
            if(DoBuffer[i] > 0)
            {
               level = DoBuffer[i];
               for(c = i; c > 0; c--)
               {
                  if((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level))
                     break;
                  if(Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level)
                     break;
                  if(Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level)
                     break;
               }
               
               if(Extend_Line)
               {
                  DrawLine ("L", i, c, level, Extend_Line_to_Background, Down_Fractal_Extend_Line_Color, Down_Fractal_Extend_Width, Down_Fractal_Extend_Style);
               }
               //if(Show_Validation_Candle)
               DoBuffer[i-2*dif] = level;
               i += dif;
            }
         }
      }
   }
      else
   {
      Comment("Invalid timeframe selected. Change timeframe to D1.");
   }
   return(0);
  }


//+------------------------------------------------------------------+
//|                                                        DRAW LINE |
//+------------------------------------------------------------------+
void DrawLine (string dir, int i, int c, double lev, bool back, color col, int width, int style)
{
   ObjectCreate("MTF_Fractal_"+dir+i,OBJ_TREND,0,0,0,0,0);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME1,iTime(Symbol(),Period(),i));
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE1,lev);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME2,iTime(Symbol(),Period(),c));
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE2,lev);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_RAY,0);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_BACK,back);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_COLOR,col);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_WIDTH,width);
   ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_STYLE,style);
}

void DrawFractalOHLCHigh(int i, string dir, double value)
{
   string strObjectNameHigh = "Example line"+dir+i;
   ObjectCreate(strObjectNameHigh, OBJ_TREND, 0, Time[i-10], value, Time[i+10], value);
   ObjectSet(strObjectNameHigh, OBJPROP_RAY, false);
}

void DrawFractalOHLCLow(int i, string dir, double value)
{
   string strObjectNameLow = "Example line"+dir+i;
   ObjectCreate(strObjectNameLow, OBJ_TREND, 0, Time[i-5], value, Time[i+5], value);
   ObjectSet(strObjectNameLow, OBJPROP_RAY, false);
}

I know i have to use ArrayResize, but i cannot find out to put it in the code correctly so it removes the array out of range error.


This is how it looks like when the number is i+4



And
when the number is changed to i+5 it looks like this


The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks, as a small error in the expert or indicator logic can cause losses on the trading account. That is why we have developed a series of basic checks to ensure the required quality level of the Market products. If any errors are identified by the Market...
 
Eagle_Eye:

It works fine as long a i keep the number below i+4 in the line below - if i change it to i+5 i get the error "array out of range xxxx.mq4 (253,53)

Help people to help you by highlighting the line of code that is causing the problem.

 
Keith Watford:

Help people to help you by highlighting the line of code that is causing the problem.

Ok done thx.

 
Eagle_Eye:

Ok done thx.

I found out what the problem was, i'm trying to give it a higher number than i have candles to the right, the line cannot show in future so to speak.

At least it has to be done in another way.

And i don't know how to do that

 

Add this line to the top:

#property strict

Then you need to make sure that i-5 will never be below 0 (otherwise you get that error) and that i+5 will never be larger than Bars-1 like so:

if(i-5>=0 && i+5<Bars)
  {
   string strObjectNameLow = "Example line"+dir+i;
   ObjectCreate(strObjectNameLow, OBJ_TREND, 0, Time[i-5], value, Time[i+5], value);
   ObjectSet(strObjectNameLow, OBJPROP_RAY, false);   
  }

You need to complete it yourself for the other places where i-10 or i+10 is used.

By the way the indicator code is outdated, it should be migrated to the actual MT4 release.

 
lippmaje:

Add this line to the top:

Then you need to make sure that i-5 will never be below 0 (otherwise you get that error) and that i+5 will never be larger than Bars-1 like so:

You need to complete it yourself for the other places where i-10 or i+10 is used.

By the way the indicator code is outdated, it should be migrated to the actual MT4 release.

#property strict
It's already in the code, just forgot to copy/paste that in as well. My mistake.

Thank you for the code example.

I'm not very skilled coder and do not code very often so i wasn't aware of that. I have to look into the actual MT4 release

Thx.
Reason: