array out of range problem mql5

 

Why is it when i counts up the Version 1 code works but when i counts down as shown below in Version 2 code it gives array out of range error?

Version 1 with i counting forwards

//--- indicator settings
#property indicator_chart_window

#property indicator_buffers 2 
#property indicator_plots 1       

#property indicator_type1 DRAW_LINE  
#property indicator_color1 Yellow                                          
#property indicator_width1 3  

//--- Indicator buffers
double MiddleBandArray[];
double BollDrawMiddleArray[];

//--- Global variables 
int BollingerBandsDefinition;

//+------------------------------------------------------------------+
//| Rate of Change initialization function                           |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,MiddleBandArray,INDICATOR_DATA);
   SetIndexBuffer(1,BollDrawMiddleArray,INDICATOR_DATA);

//--- Set handles
   BollingerBandsDefinition = iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);   
}

int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
{
   int limit;
   if(prev_calculated==0) limit=0;
    else limit=prev_calculated-1;
 
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) 
   {
      to_copy=rates_total;     
   }
   else
   {
      to_copy=rates_total-prev_calculated;   // At 00:00 its 6258 then on each new bar change it goes 1 then after its back to 0   

      if(prev_calculated>0) 
      {
         to_copy++;    // At 00:00 its 2 then 1 then on each new bar its 2 then 1 etc    
      }
   }

   // Copy price info into arrays
   CopyBuffer(BollingerBandsDefinition,0,0,to_copy,MiddleBandArray);

      //--- the main loop of calculations
   for(int i=limit; i<rates_total && !IsStopped() ; i++)  
   {
      double myMiddleBandValue = MiddleBandArray[i];
      BollDrawMiddleArray[i] = myMiddleBandValue;
   }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}   


Version 2 with i counting backwards

//--- indicator settings
#property indicator_chart_window

#property indicator_buffers 2 
#property indicator_plots 1       

#property indicator_type1 DRAW_LINE  
#property indicator_color1 Yellow                                          
#property indicator_width1 3  

//--- Indicator buffers
double MiddleBandArray[];
double BollDrawMiddleArray[];

//--- Global variables 
int BollingerBandsDefinition;

//+------------------------------------------------------------------+
//| Rate of Change initialization function                           |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,MiddleBandArray,INDICATOR_DATA);
   SetIndexBuffer(1,BollDrawMiddleArray,INDICATOR_DATA);

//--- Set handles
   BollingerBandsDefinition = iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);   
}

int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
{
   int limit;
   if(prev_calculated==0) limit=rates_total;    // limit = 9255 at 00:00 then 1 at all other times   
    else limit=1;
 
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) 
   {
      to_copy=rates_total;     
   }
   else
   {
      to_copy=rates_total-prev_calculated;   // At 00:00 its 6258 then on each new bar change it goes 1 then after its back to 0   

      if(prev_calculated>0) 
      {
         to_copy++;    // At 00:00 its 2 then 1 then on each new bar its 2 then 1 etc    
      }
   }

   // Copy price info into arrays
   CopyBuffer(BollingerBandsDefinition,0,0,to_copy,MiddleBandArray);

      //--- the main loop of calculations
   for(int i=limit; i>=1 && !IsStopped() ; i--)  
   {
      double myMiddleBandValue = MiddleBandArray[i];
      BollDrawMiddleArray[i] = myMiddleBandValue;
   }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
}

 
 //if(prev_calculated==0) limit=rates_total;
 //replace
 if(prev_calculated==0) limit=rates_total-1;

Well done for highlighting the relevant code.

If only everyone would do that, it makes it easier for others to locate the problem.

for(int i=limit; i>=1 && !IsStopped() ; i--)  
Do you not want to calculate for the [0] bar?
 
   BollingerBandsDefinition = iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);

You can't get results for the first 20 bars.
          How to do your lookbacks correctly.

 
Stephen Reynolds:

Why is it when i counts up the Version 1 code works but when i counts down as shown below in Version 2 code it gives array out of range error?

Version 1 with i counting forwards


Version 2 with i counting backwards

if(prev_calculated==0) limit=rates_total-1;
 
Mladen Rakic:

This works! Thanks.

I see if i make i count 9254-1 rather than 9255-1 it works. 

Cant understand exactly why it works though?   

 
Stephen Reynolds:

This works! Thanks.

I see if i make i count 9254-1 rather than 9255-1 it works. 

Cant understand exactly why it works though?   

When you take into account the type of the error you get, isn't that obvious?
Reason: