Help needed with "Array out of range" at indicator execution

 

Hello community,

Before writing here I made sure I looked around to find a solution to my problem. Up to now I always did and I've solved "Array out of range" issues in the past, but know I can't seem to find the problem.

I coded a very simple version of the Stochastic Momentum Index for MT4, all logic seems ok and it compiles right, but I keep getting  "array out of range in 'SMI.mq4' (89,15)" when I run it.

I marked the corresponding line, but the problem is not only in that one, when I omit it it gives the same error to the next line and then the next and so on and so forth.

Any help is appreciated. Attaching code bellow.

Thanks in advanced


#property copyright "Alexandro's Software. Rexal and Trade!"
#property link      "https://www.mql5.com/en/users/seeker_a"
#property version   "1.02"
#property description "Last Date modified: 23/11/2020"
#property strict
#property indicator_separate_window
#property  indicator_buffers 2
#property indicator_maximum 100
#property indicator_minimum -100
#property indicator_label1 "SMI"
#property indicator_label2 "SMI Signal"


bool Send_Alerts=true;
bool Send_Push_Notifications=true;

input int   K_length=5;   //Persent K Length
input int   D_length=3;   //Persent D Length

int Safety_Bars=MathMax(K_length,D_length);
double LLBuffer[],HHBuffer[],HH_LLBuffer[],RelDiffBuffer[],MA_HH_LLBuffer[],MA_RelDiffBuffer[],AvgRelBuffer[],AvgDiffBuffer[],SMIBuffer[],SMI_SignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {


//---
     
   IndicatorBuffers(2);
   SetIndexBuffer(0, SMIBuffer);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Blue);
   SetIndexBuffer(1, SMI_SignalBuffer);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1,Red);

  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 i,limit;
//---
   if(rates_total<=Safety_Bars)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- indi counted in the 1-st buffer
   for(i=0; i<=limit; i++)
     {
     if(i>rates_total-Safety_Bars*2) continue;
     
      LLBuffer[i]=iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i));
      HHBuffer[i]=iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i));
      HH_LLBuffer[i]=iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i))-iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i));
      RelDiffBuffer[i]=iClose(NULL,0,i)-(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i))+iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i)))/2;
      }  
   for(i=0; i<limit; i++)
     {
     if(i>rates_total-Safety_Bars*2) continue;

      MA_HH_LLBuffer[i]=iMAOnArray(HH_LLBuffer,0,D_length,0,MODE_EMA,i);
      MA_RelDiffBuffer[i]=iMAOnArray(RelDiffBuffer,0,D_length,0,MODE_EMA,i);
      
      }  
   for(i=0; i<limit; i++)
     {
     if(i>rates_total-Safety_Bars*2) continue;

      AvgDiffBuffer[i]=iMAOnArray(MA_HH_LLBuffer,0,D_length,0,MODE_EMA,i);
      AvgRelBuffer[i]=iMAOnArray(MA_RelDiffBuffer,0,D_length,0,MODE_EMA,i);
      
      }  
   for(i=0; i<limit; i++)
     {
     if(i>rates_total-Safety_Bars*2) continue;

      if(AvgDiffBuffer[i]!=0)
      SMIBuffer[i]=AvgRelBuffer[i]/(AvgDiffBuffer[i]/2)*100;
      else SMIBuffer[i]=0;
      }  
   for(i=0; i<limit; i++)
     {
     if(i>rates_total-Safety_Bars*2) continue;

      SMI_SignalBuffer[i]=iMAOnArray(SMIBuffer,0,D_length,0,MODE_EMA,i);
      
      }  

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void Alert(string message)
{
if(Send_Alerts) Alert(message);
if(Send_Push_Notifications) SendNotification(message);
}
// string strPeriod = GetTimeFrame(Period();
//+---------------------------------------------------------------------+
//| GetTimeFrame function - returns the textual timeframe               |
//+---------------------------------------------------------------------+
string GetTimeFrame(int lPeriod)
   {
      switch(lPeriod)
      {
      case PERIOD_M1:  return("M1");
      case PERIOD_M2:  return("M2");
      case PERIOD_M3:  return("M3");
      case PERIOD_M4:  return("M4");
      case PERIOD_M5:  return("M5");
      case PERIOD_M6:  return("M6");
      case PERIOD_M10: return("M10");
      case PERIOD_M12: return("M12");
      case PERIOD_M15: return("M15");
      case PERIOD_M20: return("M20");
      case PERIOD_M30: return("M30");
      case PERIOD_H1:  return("H1");
      case PERIOD_H2:  return("H2");
      case PERIOD_H3:  return("H3");
      case PERIOD_H4:  return("H4");
      case PERIOD_H6:  return("H6");
      case PERIOD_H8:  return("H8");
      case PERIOD_H12: return("H12");
      case PERIOD_D1:  return("D1");
      case PERIOD_W1:  return("W1");
      case PERIOD_MN1: return("MN1");
      }
   return IntegerToString(lPeriod);
   }
//+---------------------------------------------------------------------+
 
Alexandros Chontos: I keep getting  "array out of range 

You have two buffers:

   SetIndexBuffer(0, SMIBuffer);
   SetIndexBuffer(1, SMI_SignalBuffer);

Yet you try to post to unsized arrays

      LLBuffer[i]=iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i));
      HHBuffer[i]=iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i));
      HH_LLBuffer[i]=iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i))-iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i));
      RelDiffBuffer[i]=iClose(NULL,0,i)-(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,K_length,i))+iLow(NULL,0,iLowest(NULL,0,MODE_LOW,K_length,i)))/2;
      ⋮
      MA_HH_LLBuffer[i]=iMAOnArray(HH_LLBuffer,0,D_length,0,MODE_EMA,i);
      MA_RelDiffBuffer[i]=iMAOnArray(RelDiffBuffer,0,D_length,0,MODE_EMA,i);
      ⋮
      AvgDiffBuffer[i]=iMAOnArray(MA_HH_LLBuffer,0,D_length,0,MODE_EMA,i);
      AvgRelBuffer[i]=iMAOnArray(MA_RelDiffBuffer,0,D_length,0,MODE_EMA,i);

What do you think the problem is?

 
William Roeder:

You have two buffers:

Yet you try to post to unsized arrays

What do you think the problem is?

Hmmm... point taken. It so happens that up to now I’ve used arrays only as preset buffers in mql4 and thought they automatically set size as buffers do.
 I also set the ArraySetAsSeries to true and all works as a clock! Case closed!
 Thanks friend! 😁
Reason: