Simple Code - Unresolvable Error - Array out of range

 

Hi, 

I made a very simple mt4 indicator that shows signals based on volume; if the volume is higher than previous buy/sell as an example.


I run into array out of range error with this code


int limit = MathMax(rates_total - prev_calculated,2);

      for(int i=1;i<(limit);i++)

      {

        

         if (tick_volume[i]>tick_volume[i+1]) // **Out of range code points to this line**

         {

            //do code

         }


//--- return value of prev_calculated for next call

   return(rates_total);

  }


 
lilses:

Hi, 

I made an indicator that shows signals based on volume; if the volume is higher than previous buy/sell as an example.


I run into out of range error with this code


int limit = MathMax(rates_total - prev_calculated,2);

      for(int i=1;i<(limit);i++)

      {

        

         if (tick_volume[i]>tick_volume[i+1]) // **Out of range code points to this line**

         {

            //do code

         }


//--- return value of prev_calculated for next call

   return(rates_total);

  }


When prev_calculated is 0, if i is limit-1 (last iteration), i+1 is  equal to rates_total. Maximum index for tick_volume is rates_total-1.

Adjust your loop to avoid it.

 
Alain Verleyen:

When prev_calculated is 0, if i is limit-1 (last iteration), i+1 is  equal to rates_total. Maximum index for tick_volume is rates_total-1.

Adjust your loop to avoid it.

Alain thank you for replying!


To make sure I understand correctly, my loop should not run if i+1 == rate_total?

int limit = MathMax(rates_total - prev_calculated,2);

      for(int i=1;(i+1!=limit);i++)

      {

...

      }

Also, does this error basically happen when there are no new volume ticks - tick_volume[i+1] == EMPTY because sometimes it'll work and then give an error when I change timeframe for example?

Still can't understand as Open[i+1] would not give any error and they both represent the same candle?

After thinking a little bit more, I'm guessing the solution would look something like this;

if (tick_volume[i+1] == EMPTY){

//do not run

}

I would like some answers to the 2 questions above.

 
 
lilses:

Alain thank you for replying!


To make sure I understand correctly, my loop should not run if i+1 == rate_total?

int limit = MathMax(rates_total - prev_calculated,2);

      for(int i=1;(i+1!=limit);i++)

      {

...

      }

Also, does this error basically happen when there are no new volume ticks - tick_volume[i+1] == EMPTY because sometimes it'll work and then give an error when I change timeframe for example?

Still can't understand as Open[i+1] would not give any error and they both represent the same candle?

After thinking a little bit more, I'm guessing the solution would look something like this;

if (tick_volume[i+1] == EMPTY){

//do not run

}

I would like some answers to the 2 questions above.

You don't understand the problem.

1. tick_volume is an array

2. An array has a size (a number of items). (rates_total items for tick_volume).

3. Each item is identified by an index.

4. First item index is 0, last is size-1. (so for tick_volume between 0 and rates_total-1).

5. When using i+1 as index, if i is greater or equal to rates_total-1, so i+1 greater or equal to rates_total, you have a "out of range" error.

6. So you must be sure i is always lower than rates_total-1.

WHRoeder posted a link with practical example. Please also search about this error if it's still not clear.

Once you understand the problem, it's easy to fix.