How to declare dynamic array

 

I am facing a problem with dynamic array.  My code below :

double ExtMapBuffer5[];

void OnInit()
{
//---- indicator buffers mapping
   
   SetIndexBuffer(4, ExtMapBuffer5, INDICATOR_CALCULATIONS);

   //Set the correct order: 0 is the latest, N - is the oldest
   ArraySetAsSeries(ExtMapBuffer5, true);

}

 

ExtMapBuffer5 is dynamic array.  but when i try to loop with total amount of Bar. its show error below;

"array out of range in "ddd mm".

To find the problem i write a code :   printf(rates_total+"  "+ArrayMaximum(ExtMapBuffer5)); 

then i show  :  Total bar : 342 and Array: 243.

Why Maximum array is 243. How do i solve this problem.  

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
MQL5 programs / Runtime Errors - Documentation on MQL5
 
advali82:

I am facing a problem with dynamic array.  My code below :

 

ExtMapBuffer5 is dynamic array.  but when i try to loop with total amount of Bar. its show error below;

"array out of range in "ddd mm".

To find the problem i write a code :   printf(rates_total+"  "+ArrayMaximum(ExtMapBuffer5)); 

then i show  :  Total bar : 342 and Array: 243.

Why Maximum array is 243. How do i solve this problem.  

Show the code of your loop.

ArrayMaximum gives you the maximum value, what you need is ArraySize. Please read the documentation.

 
angevoyageur:

Show the code of your loop.

ArrayMaximum gives you the maximum value, what you need is ArraySize. Please read the documentation.


 

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[])
{
   int limit;

   int counted_bars = prev_calculated;
   if (counted_bars < 0) return(-1);
   if (counted_bars > 0) counted_bars--;
   if (barsCount > 0) limit = MathMin((rates_total - counted_bars), barsCount);
   else  limit = rates_total - counted_bars;
//----
   int myMA = iMA(NULL, 0, FastLen, 0, MODE_EMA, PRICE_CLOSE);
   if (CopyBuffer(myMA, 0, 0, rates_total, MABuff1) != rates_total) return(0);
   myMA = iMA(NULL, 0, SlowLen, 0, MODE_EMA, PRICE_CLOSE);
   if (CopyBuffer(myMA, 0, 0, rates_total, MABuff2) != rates_total) return(0);

   for (int i = 0; i < limit; i++)
       bbMacd[i] = MABuff1[i] - MABuff2[i];

//----
   CalculateEMA(limit - 1, Length, bbMacd);

   return(rates_total);
}

void CalculateEMA(int begin, int period, const double &price[])
{
   double SmoothFactor = 2.0 / (1.0 + period);
        int start;
        
   //First time
   if (ExtMapBuffer5[ArrayMaximum(ExtMapBuffer5)] <= 0)
   {
        ExtMapBuffer5[begin] = price[begin];
        start = begin - 1;
   }
   else start = begin;

   for(int i = start; i >= 0; i--) ExtMapBuffer5[i] = price[i] * SmoothFactor + ExtMapBuffer5[i + 1] * (1.0 - SmoothFactor);
}
Problem show on yellow Mask line. 
 

Your ExtMapBuffer5 can have index from 0 to rates_total-1. But the value of i can be rates_total-1, so i+1 is rates_total and you get an index out of range error.

What's the purpose of this statement ?

   if (ExtMapBuffer5[ArrayMaximum(ExtMapBuffer5)] <= 0)

This condition is probably always false.

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
MQL5 programs / Runtime Errors - Documentation on MQL5
 
thanks
Reason: