Array Out of Range error while assigning EMPTY_VALUE to the buffer

Arad R
9
Arad R  

Hello 

I am trying to convert an mt4 indicator into an mt5 indicator.
I have some knowledge about mql4 , but its my first time trying out mql5 

I have an indicator in mql4 and I am trying to make it work on mt5 , there were a bunch of errors but somehow got rid of those without removing any of the original code.
even though there aren't any compile time errors , but  it throws the array out of range error where EMPTY_VALUE is assigned to the variable.


Can someone please Point out what the issue could be ?

Highlighted section of the code is throwing the error  :

#property indicator_separate_window

#property indicator_minimum 0.0
#property indicator_maximum 1.0

#property indicator_buffers 3
#property indicator_plots 2

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_HISTOGRAM
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

extern string TimeFrame = "current time frame";
extern int SmoothPeriod = 5;
extern int SmoothPhase = 0;
 
double Buffer1[];
double Buffer2[];
double Buffer3[];
int timeframe_1;
bool check_1 = false;
bool check_2 = false;
string Indi_Name;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, Buffer1,INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   
   SetIndexBuffer(1, Buffer2,INDICATOR_DATA);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   
   SetIndexBuffer(2, Buffer3,INDICATOR_DATA);

   if(TimeFrame == "calculate")
     {
      check_1 = true;
      return (0);
     }
   if(TimeFrame == "getBarsCount")
     {
      check_2 = true;
      return (0);
     }
   timeframe_1 = TF(TimeFrame);
   Indi_Name = MQLInfoString(MQL_PROGRAM_NAME);
   IndicatorSetString(INDICATOR_SHORTNAME,  "fps");
   return (0);
  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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[])
  {

   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);

   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
      ArrayInitialize(Buffer3, EMPTY_VALUE);
     }


   double var_1;
   double var_2;
   double var_3;
  
   int Shift;
   int k;
   int Prev_Cal = prev_calculated;
   
   if(Prev_Cal < 0)
      return (-1);
      
   if(Prev_Cal > 0)
      Prev_Cal--;
      
   int i = MathMin(Bars(Symbol(),PERIOD_CURRENT) - Prev_Cal, Bars(Symbol(),PERIOD_CURRENT)  - 1);
   
   if(check_2)
     {
      Buffer1[0] = i + 1;
      return (0);
     }
   if(check_1 || timeframe_1 == Period())
     {
      for(k = i; k >= 0; k--)
        {
         var_1 = Calculate(High[k + 1], SmoothPeriod, SmoothPhase, k + 1, 0);
         var_2 = Calculate(Low[k + 1], SmoothPeriod, SmoothPhase, k + 1, 10);
         var_3 = Calculate(Close[k], SmoothPeriod, SmoothPhase, k, 20);
         
         Buffer2[k] = EMPTY_VALUE;
         Buffer1[k] = EMPTY_VALUE;
         Buffer3[k] = Buffer3[k + 1];
         
         if(var_3 > var_1)
            Buffer3[k] = -1;
            
         if(var_3 < var_2)
            Buffer3[k] = 1;
            
         if(Buffer3[k] == 1.0)
            Buffer2[k] = 1;
            
         if(Buffer3[k] == -1.0)
            Buffer1[k] = 1;
        }
     
      return (0);
     }


 
William Roeder
26790
William Roeder  
  1. You haven't set your H/L/Close arrays direction (as-series)

  2.          var_1 = Calculate(High[k + 1], SmoothPeriod, SmoothPhase, k + 1, 0);

    Always post all relevant code (using Code button) We can't know what Calculate does.

  3. Your lookback is at least one, but more likely SmoothPhase+1.
              How to do your lookbacks correctly #9#14 & #19
Mladen Rakic
246384
Arad R:

Hello 

I am trying to convert an mt4 indicator into an mt5 indicator.
I have some knowledge about mql4 , but its my first time trying out mql5 

I have an indicator in mql4 and I am trying to make it work on mt5 , there were a bunch of errors but somehow got rid of those without removing any of the original code.
even though there aren't any compile time errors , but  it throws the array out of range error where EMPTY_VALUE is assigned to the variable.


Can someone please Point out what the issue could be ?

Highlighted section of the code is throwing the error  :



 

There is already Gann high low activator that uses Jurik smoothing made for mt5. Use that 

What you are trying to convert is ages old version for mt4 - even for MT4 there were multiple newer versions