Array out of range in function with for loop

 

I try to build function which will calculate pivots for specified price and put it in array of MqlPivots structures. in src is code of function. when i call it in a script whit this:

MqlPivots pivots[];

Print(GetPivots(_Symbol, PERIOD_D1, 0, 3, pivots)); 

 I get critical error Array out of range. Why?

 

When I put  number 3 in MqlPivots pivots[3]; then it works fine?

 

struct MqlPivots {
datetime time;
double pivot;
double R1, R2, R3; //Resistance 1, 2, 3
double S1, S2, S3; //Support 1, 2, 3
};


int GetPivots (string symbol_name, ENUM_TIMEFRAMES timeframe, int start_pos, int count, MqlPivots &pivots_array[]) {

int counter = 0;


for(int i = start_pos; i < count; i++) {



double high = iHigh(symbol_name, timeframe, i);
double low = iLow(symbol_name, timeframe, i);
double close = iClose(symbol_name, timeframe, i);


pivots_array[i].time = iTime(symbol_name, timeframe, i);
pivots_array[i].pivot = (high + low + close)/3;

pivots_array[i].R1 = (2 * pivots_array[i].pivot) - low;
pivots_array[i].R2 = pivots_array[i].pivot + (high - low);
pivots_array[i].R3 = high + 2 * (pivots_array[i].pivot - low);

pivots_array[i].S1 = (2 * pivots_array[i].pivot) - high;
pivots_array[i].S2 = pivots_array[i].pivot - (high - low);
pivots_array[i].S3 = low - 2 * (high - pivots_array[i].pivot);

counter++;

}


return(counter);

} ;
 
Well read about arrays - you'll find all you need!
 
chevanton1988:

MqlPivots pivots[];

Print(GetPivots(_Symbol, PERIOD_D1, 0, 3, pivots)); 

 I get critical error Array out of range. Why?

  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You told GetPivots to fill pivots[0] through pivots[2] via "0,3,pivots." Do those array elements exist?
 
gooly:
Well read about arrays - you'll find all you need!

gooly,

 

I put  ArrayResize in function before start of the "for loop", and now it works. I can't understand why it is necessary.  when I declare array with unknown dimension, and when i put it as a parameter in function, why i need to ArreyResize that unknown array in function. I put count variable as a parametar of ArrayResize as a new size (number of bars which i want to calculate). What is reserve size value (excess) in ArrayResize, can you explain? 

void OnStart()
  {




MqlPivots pivots[];



Print(GetPivots(_Symbol, PERIOD_D1, 0, 3, pivots));

}
struct MqlPivots {
datetime time;
double pivot;
double R1, R2, R3; //Resistance 1, 2, 3
double S1, S2, S3; //Support 1, 2, 3
};


int GetPivots (string symbol_name, ENUM_TIMEFRAMES timeframe, int start_pos, int count, MqlPivots &pivots_array[]) {

int counter = 0;

ArrayResize(pivots_array, count, 0);

for(int i = start_pos; i < count; i++) {



double high = iHigh(symbol_name, timeframe, i);
double low = iLow(symbol_name, timeframe, i);
double close = iClose(symbol_name, timeframe, i);


pivots_array[i].time = iTime(symbol_name, timeframe, i);
pivots_array[i].pivot = (high + low + close)/3;

pivots_array[i].R1 = (2 * pivots_array[i].pivot) - low;
pivots_array[i].R2 = pivots_array[i].pivot + (high - low);
pivots_array[i].R3 = high + 2 * (pivots_array[i].pivot - low);

pivots_array[i].S1 = (2 * pivots_array[i].pivot) - high;
pivots_array[i].S2 = pivots_array[i].pivot - (high - low);
pivots_array[i].S3 = low - 2 * (high - pivots_array[i].pivot);

counter++;

}


return(counter);

} ;
 
chevanton1988:

gooly,

 

I put  ArrayResize in function before start of the "for loop", and now it works. I can't understand why it is necessary.  when I declare array with unknown dimension, and when i put it as a parameter in function, why i need to ArreyResize that unknown array in function. I put count variable as a parametar of ArrayResize as a new size (number of bars which i want to calculate). What is reserve size value (excess) in ArrayResize, can you explain? 

 

ArrayResize is necessary because you are declaring it as a dynamic array. This means that the number of elements in the array is likely to change. The computer is incapable of guessing the arraysize that you require, so you have to include it in your code.

As far as I understand, the reserve means that memory is allocated to the array, but not necessarily used. If you have a lot of code that increases the array size by 1 at a time, the memory has already been allocated and so it saves on processing time. 

 
GumRai:

ArrayResize is necessary because you are declaring it as a dynamic array. This means that the number of elements in the array is likely to change. The computer is incapable of guessing the arraysize that you require, so you have to include it in your code.

As far as I understand, the reserve means that memory is allocated to the array, but not necessarily used. If you have a lot of code that increases the array size by 1 at a time, the memory has already been allocated and so it saves on processing time. 

Is it possible in debugging mode see how much is big array, because I build and indicator which include all data (bars) on chart?
 
chevanton1988:
Is it possible in debugging mode see how much is big array, because I build and indicator which include all data (bars) on chart?

I don't know, I have never used debugging mode.

If an indicator, why don't you just use a buffer?  They are automatically resized. 

 
GumRai:

I don't know, I have never used debugging mode.

If an indicator, why don't you just use a buffer?  They are automatically resized. 

I had the same problem with Buffers, too. When I used SimpleMAOnBuffer() function, also i got an error for Array out of range. I resolve also this problem with ArrayResize. I think in MQL4 Buffers aren't automatically resized.

double         PivotBuffer[];
double         Pivot[];



int OnInit()
  {
   SetIndexBuffer(0,PivotBuffer, INDICATOR_DATA);
   SetIndexBuffer(0,Pivot, INDICATOR_CALCULATIONS);

   return(INIT_SUCCEEDED);
  }




int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

//izracun pivot tocaka po baru
for(int i = 0; i < rates_total; i++) {

Pivot[i] = (high[i] + low[i] + close[i])/3;  

}; 


ArrayResize(PivotBuffer, rates_total, 0);


SimpleMAOnBuffer(rates_total, prev_calculated, 0, period, Pivot, PivotBuffer);

}
 
chevanton1988:

I had the same problem with Buffers, too. When I used SimpleMAOnBuffer() function, also i got an error for Array out of range. I resolve also this problem with ArrayResize. I think in MQL4 Buffers aren't automatically resized.

Possibly because you bind both with index 0. So PivotBuffer is not a buffer and has no size

   SetIndexBuffer(0,PivotBuffer, INDICATOR_DATA);
   SetIndexBuffer(0,Pivot, INDICATOR_CALCULATIONS);
Reason: