array out of range problem

 

Hi, in the following code i fail to see why im getting an array out of range error?

//--- indicator settings
#property indicator_separate_window

#property indicator_buffers 1 
#property indicator_plots 1

// Buffers
double MAOsc[];

// Global variables 
int fastMA;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Indicator buffers mapping
   SetIndexBuffer(0,MAOsc,INDICATOR_DATA);

   // Array Set As Series 
   ArraySetAsSeries(MAOsc,true);
  
   fastMA = iMA(_Symbol,_Period,3,0,MODE_SMA,PRICE_CLOSE);

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,const int begin,const double &price[])
{
//---- calculation of limit starting bar index
   int limit;
   if(prev_calculated>rates_total || prev_calculated<=0)// checking of first call
   {
      limit=rates_total-1; // 9255-1=9254 so limit = 9254 at 00:00
      
   }
   else limit=rates_total-prev_calculated; // So should fluctuate between 0 and 1  because of the way prev_calculated is equal to after each hour at 00:59 always 3 times
   

//--- Amount of data to copy within CopyBuffer()    
   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 from indicator iMA() into arrays
   CopyBuffer(fastMA,0,0,to_copy,MAOsc);

   while(limit >= 0)
   {       
      for(int a = 0; a < 2; a++)
      {   
         int i = limit + a;  
         
         Print(MAOsc[i]);         
      }

      limit--; 
   }
 
   // Return value of prev_calculated for next call
   return(rates_total);
}

On the parts outlined why when i make  a < 1  it works okay        

but when i make  a < 2  it gives me the array out of range error?

 
  1.       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 from indicator iMA() into arrays
       CopyBuffer(fastMA,0,0,to_copy,MAOsc);
    
       while(limit >= 0)
       {       
          for(int a = 0; a < 2; a++)
          {   
             int i = limit + a;  
             Print(MAOsc[i]);
    You return rates_total. So after the initial run, limit is zero, to_copy is one, the array has one element and limit+a is beyond the array for non-zero a.

  2. Your look back here is one (a < 2).
              How to do your lookbacks correctly #9#14 & #19

Reason: