Download MetaTrader 5

stuck on array out of range error. Please critic my indicator loop.

To add comments, please log in or register
dazamate
575
dazamate  

I am stuck with an array out of range error. I've done much search to try find common coding mistakes and have placed 'safeguards' in to try stop the indicator trying to fetch outside the range.

The indicator works, but sometimes when I flick through the time frames, I will get the array out of range error and the indicator de initializes. 

 

Can anyone spot any issues with my indicator loop?

 

int counted_bars = prev_calculated;
   
   //--- exit if an error has occurred
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   //----
   
   //don't run when there isn't enough bars on the chart
   int nr_of_bars_on_Chart = iBars(Symbol(), Period());
   if( nr_of_bars_on_Chart < 30) { return(rates_total); }
   
   //make sure we start at least 30 bars so we have enought data to look back through if needed
   if(counted_bars<30) counted_bars=30;   
   
   //--- position of the bar from which calculation in the loop starts
   int limit=rates_total-counted_bars-1;
    
   for(int i=limit; i>=0; i--) {  

 

I know this is a common question, I've reached my limit  and don't know what else to do. 

Keith Watford
Moderator
10617
Keith Watford  
dazamate:

I am stuck with an array out of range error. I've done much search to try find common coding mistakes and have placed 'safeguards' in to try stop the indicator trying to fetch outside the range.

The indicator works, but sometimes when I flick through the time frames, I will get the array out of range error and the indicator de initializes. 

 

Can anyone spot any issues with my indicator loop?

 

int counted_bars = prev_calculated;
   
   //--- exit if an error has occurred
   if(counted_bars<0) return(-1);
   //----
   if(counted_bars>0) counted_bars--;
   //----
   
   //don't run when there isn't enough bars on the chart
   int nr_of_bars_on_Chart = iBars(Symbol(), Period());
   if( nr_of_bars_on_Chart < 30) { return(rates_total); }
   
   //make sure we start at least 30 bars so we have enought data to look back through if needed
   if(counted_bars<30) counted_bars=30;   
   
   //--- position of the bar from which calculation in the loop starts
   int limit=rates_total-counted_bars-1;
    
   for(int i=limit; i>=0; i--) {  

 

I know this is a common question, I've reached my limit  and don't know what else to do. 

You don't show your loop and you don't show any calculations using arrays.

How do you expect anyone to help you? 

dazamate
575
dazamate  

The only arrays I am accessing and using are the global price arrays. I don't initialize any of my own. apart from the indicator buffers..

For example... 

for(int i=limit; i>=0; i--) {  

 

if(detect_inside_candles){
            if(isInsideCandle(i) && i>0){
                Buffer0[i] = low[i];
                Buffer1[i] = high[i];
            } 
         }

bool isInsideCandle(int shift) {   
   if((High[shift+1]>High[shift]) && (Low[shift+1]<Low[shift]))
      { return(true); }
   else { return(false); }       
}

 

If I tried to call say Close[0], and the chart has not loaded all the candles yet, will that return an out of array error. because it happens when I flick through the time frames, especially the lower ones. 

I have a fakeout pattern I scan for on each tick...

 if(
   ( Low[0] < Low[1] ) &&
   ( Close[0] >= Low[1] + (breakout_trap_trigger * (High[1]-Low[1])) ) &&
   ( High[0] < High[1] )
)

 

If I try to call these very recent candles while the chart is loading, is that going to cause problems? 

Brian Lillard
1065
Brian Lillard  
dazamate:

The only arrays I am accessing and using are the global price arrays. I don't initialize any of my own. apart from the indicator buffers..

For example... 


    int counted_bars = IndicatorCounted();
   
    if(counted_bars < 0) return(-1);
             
    int e = Bars - counted_bars + period + 1;
   

    if(e > Bars) e = Bars;

    for(x = 0; x < e; x++)

    period of 30?

Keith Watford
Moderator
10617
Keith Watford  

I can't spot anything in your code to cause an array out of range error. It may be elsewhere in your code.

Often when you get the report of the error in the log, it tells you the line and column in the code where the error occurs. That should pinpoint it for you

Keith Watford
Moderator
10617
Keith Watford  
dazamate:

If I try to call these very recent candles while the chart is loading, is that going to cause problems? 

It shouldn't. As long as there is some history loaded. if not up to date, the wrong value may be returned
Keith Watford
Moderator
10617
Keith Watford  
Subgenius:
    int e = Bars - counted_bars + period + 1; 


Did you mean

int e = Bars - (counted_bars + period + 1);  

Brian Lillard
1065
Brian Lillard  
GumRai:

Did you mean

int e = Bars - (counted_bars + period + 1);  


yea i think so since it was illogical to decipher the true bars variable of the code snippet

theres to many variables and not quite a steady range

is it not also a mix of server and local?

Keith Watford
Moderator
10617
Keith Watford  
Subgenius:


yea i think so since it was illogical to decipher the true bars variable of the code snippet

theres to many variables and not quite a steady range

is it not also a mix of server and local?

I have no idea what you are saying.
dazamate
575
dazamate  

 I can't spot anything in your code to cause an array out of range error. It may be elsewhere in your code.

 

Thanks for taking the time to look through it.

 

Often when you get the report of the error in the log, it tells you the line and column in the code where the error occurs. That should pinpoint it for you 

Correct, and I've wondered how to use these numbers for debugging. Here is the msg I get... 

 

So is it saying line 257? what's the second number for?

I can only assume this is the co-ordinates for the compiled machine language file and doesn't directly correlate to the lines in the source code.

Keith Watford
Moderator
10617
Keith Watford  
dazamate:

 

Thanks for taking the time to look through it.

 

Correct, and I've wondered how to use these numbers for debugging. Here is the msg I get... 

 

So is it saying line 257? what's the second number for?

I can only assume this is the co-ordinates for the compiled machine language file and doesn't directly correlate to the lines in the source code.

 

 

The 2nd number is column

Yes, they are the line and column in the source code

If you don't have the numbered lines in your editor

Tools\Options\Genereal and select to show line numbers 

12
To add comments, please log in or register