Please help - Array out of range error.

 

Hi everyone,

I have been giving myself a headache over this for several days and I really need some guidance.

The code in question is a custom indicator that draws 4 lines on a chart. Two for high/low price of the last x number of bars, and two for stops placed a multiple of the ATR below the high/low (ie Trailing ATR stops). 

I create an array[] that stores the values of the high - ATR and Low + ART for each bar. Then I try to create another array[] that stores the ArrayMaximum() of the first array (ie I want the highest (High - ATR) for the last 10 bars).

The code compiles fin but running it gets me  "array out of range in 'BreakOutCI.mq4' (125,19)" in my experts tab. WHICH ARRAY?

I know the bit of code that gets the 'highest (High - ATR) for the last 10 bars' works because I can check it with the Print() function. 

Can anyone can spot what is wrong, and why my array is out of range?

Is this enough of the code, or would all of it be helpful? I didn't want to bombard you with code.

//Function to populate the ATR value and stop arrays
void CalculateATRStops()
{
for(int count=0;count<Bars;count++)
      {
      BuyATRValueArray[count]  = High[count]-(iATR(NULL,PERIOD_CURRENT,ATR_Period,count)*ATR_Multiple);
      SellATRValueArray[count] = Low[count]+(iATR(NULL,PERIOD_CURRENT,ATR_Period,count)*ATR_Multiple);
      }
      
BuyATRValueArray[0] = BuyATRValueArray[1]; 
SellATRValueArray[0] = SellATRValueArray[1];

//--****************** THIS IS WHERE I THINK THE PROBLEM IS *****************
for(int count=0;count<Bars;count++)
   {
//I think problem here V     (this bit works fine) V
   BuyATRStopArray[count] = BuyATRValueArray[ArrayMaximum(BuyATRValueArray,ATR_Stop_Decay,count)];
   SellATRStopArray[count] = SellATRValueArray[ArrayMinimum(SellATRValueArray,ATR_Stop_Decay,count)];
   }
//--*************************************************************************
}
 

The code compiles fin but running it gets me  "array out of range in 'BreakOutCI.mq4' (125,19)" in my experts tab. WHICH ARRAY? 

 

So you know the line and indent (125,19). That should tell you which array.

Are you sizing the array(s) correctly? 

 

Forex_cellence,

You may read this?

 

Thanks for the responses.

I think my question was unclear. I know which array  (the BuyStopARTArray at (125,19)) is not working. I still don't know why not.

I am not sizing my arrays at all. I am assuming that they are 'dynamic' arrays that size themselves? The other arrays seem to auto size themselves ok.

Would it be anything to do with one being 'populated' by an the built in iATR function but this one being derived from another array?

I'll try setting the size of the array. See if that work.

Thanks

*** Yup, setting the array size using ArrayResize() has fixed the problem. I still don't understand why some arrays seemed to work fine without setting their size. ***

 
Indicator buffers are special arrays that re-size automatically. Other arrays have to be given a size
Reason: