Strange "array out of range" issue

 

Hi everybody !

 

Got an "array out of range" issue, and i don't understand what's wrong. If someone could help it would just be perfect !

 

A big big thaaaaaaaaaaaaanks ! 

 

double MemTop2[];
double MemTop3[];
double MemTop4[];
double MemTop5[];
double BarMemTop1[];
double BarMemTop2[];
double BarMemTop3[];
double BarMemTop4[];
double BarMemTop5[];


//--- indicator buffers
double MemTop1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
 
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MemTop1);
  
 
 
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  
  int start()
  {
   //--- some calculations require no less than N bars (e.g. 100)      
   if (Bars<100) // if less bars are available on a chart (for example on MN timeframe)    
     return(-1); // stop calculation and exit

   //--- the number of bars that have not changed since the last indicator call
   int counted_bars=IndicatorCounted();
   //--- exit if an error has occurred
   if(counted_bars<0) return(-1);
      
   //--- position of the bar from which calculation in the loop starts
   int limit=Bars-counted_bars;

   //--- if counted_bars=0, reduce the starting position in the loop by 1,   
   if(counted_bars==0) 
     {
      limit--;  // to avoid the array out of range problem when counted_bars==0
      //--- we use a shift of 10 bars back in history, so add this shift during the first calculation
      limit-=10;
     }
   else //--- the indicator has been already calculated, counted_bars>0
     {     
      //--- for repeated calls increase limit by 1 to update the indicator values for the last bar
      limit++;
     } 
   //--- the main calculation loop
   for (int i=limit; i>0; i--)
   {

   
   
   
 if (High[i+4]>=High[i+5] && High[i+4]>=High[i+6] && High[i+4]>=High[i+7] && High[i+4]>=High[i+8] && High[i+3]<=High[i+4] && High[i+2]<=High[i+4] && High[i+1]<=High[i+4] && High[i]<=High[i+4]) 
{
MemTop1[i] = High[i+4];
MemTop2[i] = MemTop1[i+1];
MemTop3[i] =  MemTop2[i+1];
MemTop4[i] =  MemTop3[i+1];
MemTop5[i] =  MemTop4[i+1];


BarMemTop1[i] = iBarShift(NULL,0, iTime(0,0,i+4));
BarMemTop2[i] = BarMemTop1[i+1];
BarMemTop3[i] = BarMemTop2[i+1];
BarMemTop4[i] = BarMemTop3[i+1];
BarMemTop5[i] = BarMemTop4[i+1];
}
 
 
   

}
   return(0);
 }
 

You have to define the size of your arrays: MemTop1, MemTop2, ..

Look for 'ArrayResize' and 'note'!!

 
  1. double MemTop1[];
    int init(){
       SetIndexBuffer(0,MemTop1);

    MemTop1 is the only array that will be autosized because it is a buffer. All others have no size.

  2. if (High[i+4]>=High[i+5] && High[i+4]>=High[i+6] && High[i+4]>=High[i+7] && High[i+4]>=High[i+8] && High[i+3]<=High[i+4] && High[i+2]<=High[i+4] && High[i+1]<=High[i+4] && High[i]<=High[i+4])
    Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling back and forth trying to read it. Edit the post with formatted code and you might get additional help.