array out of range - Top/Bottom

 

I have a problem.

I want to put in my EA as Stop the last bottom, or the previous one, or the same for the top.

But when I run the EA, it gives the error "array out of range".

what am I doing wrong?

double top =0;
double bottom=0;
double lasttop =0;
double lastbotton =0;
MqlRates candle[];
ArraySetAsSeries(candle,true);
int copied = CopyRates(_Symbol,0,0,500,candle);


bool topBottom()
  {
   top=0;		
   bottom=0;
   lasttop=0;
   lastBottom=0;
   for(int i=3; i<500; i++)
     {
      if(top==0)
        {
            if(candle[i].high>=candle[i+4].high
            && candle[i].high>=candle[i+3].high
            && candle[i].high>=candle[i+2].high
            && candle[i].high>=candle[i+1].high
            && candle[i].high>=candle[i-1].high
            && candle[i].high>=candle[i-2].high) //The error appears in this highlighted part.
           {
            top=candle[i].high;	//If the high of candle 3 is higher than the next 1.2 and the previous 4,5,6,7, then it marks as a top.
            i=i+4;		//advance 4
           }
        }
      else
         if(lasttop==0)		//and starts looking for the previous top if it found the last one.
           {
               if(candle[i].high>=candle[i+4].high
               && candle[i].high>=candle[i+3].high
               && candle[i].high>=candle[i+2].high
               && candle[i].high>=candle[i+1].high
               && candle[i].high>=candle[i-1].high
               && candle[i].high>=candle[i-2].high)
              {
               lasttop=candle[i].high;
               i=i+4;
              }
           }
      if(bottom==0)
        {
            if(candle[i].low<=candle[i+4].low
            && candle[i].low<=candle[i+3].low
            && candle[i].low<=candle[i+2].low
            && candle[i].low<=candle[i+1].low
            && candle[i].low<=candle[i-1].low
            && candle[i].low<=candle[i-2].low)
           {
            bottom=candle[i].low;
            i=i+4;
           }
        }
      else
         if(lastBottom==0)
           {
               if(candle[i].low<=candle[i+4].low
               && candle[i].low<=candle[i+3].low
               && candle[i].low<=candle[i+2].low
               && candle[i].low<=candle[i+1].low
               && candle[i].low<=candle[i-1].low
               && candle[i].low<=candle[i-2].low)
              {
               lastBottom=candle[i].low;
               i=i+4;
              }
           }
         if(top>0		//exits when it has found values for all.
         && bottom>0
         && lastBottom>0
         && lasttop>0)
        {
         Print("T=",top," LT=",lasttop," B=",bottom," LB=",lastBottom);
         break;
        }
     }
   return(true);
  }
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 in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Marco Antonio Colognese:

I have a problem.

I want to put in my EA as Stop the last bottom, or the previous one, or the same for the top.

But when I run the EA, it gives the error "array out of range".

what am I doing wrong?

which line does it report the error on

where is your definition of the array

you need to post all the relevent code

 
Marco Antonio Colognese:

I have a problem.

I want to put in my EA as Stop the last bottom, or the previous one, or the same for the top.

But when I run the EA, it gives the error "array out of range".

what am I doing wrong?

Hi, you use array candle. But at your code I dont see where you create this arrray and its size.

 
I showed the requested in the code! Excuse me. I'm starting! 😅
 
Marco Antonio Colognese #:
I showed the requested in the code! Excuse me. I'm starting! 😅
You have not.  You need to answer my questions or nobody can  help sorry
 
Paul Anscombe #:
You have not.  You need to answer my questions or nobody can  help sorry

I highlighted in the code where the error in question appears. 

 
Marco Antonio Colognese #:

I highlighted in the code where the error in question appears. 

How many rates are getting returned in the copy

What is the value of i  when it errors.

500 rates = 0 to 499  not 500

your loop of 3-500  means 500+4 = 504 if you have only copied 500 then it would fail. so your loop should be 499-4 = 495 


 
Marco Antonio Colognese:

I have a problem.

I want to put in my EA as Stop the last bottom, or the previous one, or the same for the top.

But when I run the EA, it gives the error "array out of range".

what am I doing wrong?

If i =13 candle corresponding to yesterday date, i+1(would be 14 corresponding to 2 days ago) and (i-1) would be corresponding to today's date, you are getting out of range because (i-2) which is suppose to be tomorrow's date is not yet having a candle.


Check that 500 is not greater than the (total number of bars -4 && START+2) in the TF, you are looking at 

Reason: