Array Out Of Range error on my custon indicator at line "BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0]; "

 

Dear All,

after a deep reading of the MQL reference I've started to program and translate an existing indicator from another system (realtimepro)

This indicato get an ok from the Compile, but fail when attached to a chart.

the fail message is Array Out Of Range, and from the debugger I see is at the line of this assignment:

BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0];

http://s16.postimg.org/tecvtc6gl/Array_Out_Of_Range.png

this is the full code and I'm trying to find the error, can you indicate me what to search ?

//--------------------------------------------------------------------
// myseparatewindowindicator.mq4 
// The code should be used for educational purpose only.
//--------------------------------------------------------------------
#property indicator_separate_window // Drawing in a separate window
#property indicator_buffers 1       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line
 
extern int History  =50;            // Amount of bars in calculation history
extern int Aver_Bars=5;             // Amount of bars for calculation
extern int d=31;
extern int e=31;
double Buf_0[];                     // Declaring an indicator array
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
   SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// line style
   return (0);                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
   int i,                           // Bar index
   n,                               // Formal parameter
   Counted_bars;                    // Number of counted bars
   double
   min,
   max,
   R,
   M,
   Sum_H,                           // Sim of High values for period
   Sum_L;                           // Sum of low values for period
   double 
   BU[],
   PB[];
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   if (i>History-1)                 // If too many bars ..
      i=History-1;                  // ..calculate for specific amount.
   while(i>=0)                      // Loop for uncounted bars
     {
  //    Sum_H=0;                      // Nulling at loop beginning
  //    Sum_L=0;                      // Nulling at loop beginning
  //    for(n=i;n<=i+Aver_Bars-1;n++) // Loop of summing values 
  //      {
  //       Sum_H=Sum_H + High[n];     // Accumulating maximal values sum
  //       Sum_L=Sum_L + Low[n];      // Accumulating minimal values sum
  
     if (Bars<2) {
   BU[0]=Close[0];
   }
   else {
   BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0];
   }
   
   //M=Average[d](close)    
   M=iMA(NULL,0,d,0,MODE_SMA,PRICE_TYPICAL,0);
   
   PB[0]=MathAbs(BU[0]-M);
   
   min=ArrayMinimum(PB,e,0);
   max=ArrayMaximum(PB,e,0);
   R=100*(PB[0]-min)/(max-min);
  
  
   }
  //    Buf_0[i]=(Sum_H-Sum_L)/Aver_Bars;// Value of 0 buffer on i bar
      i--;                          // Calculating index of the next bar
     
//--------------------------------------------------------------------
   return (0);                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------

Regards,

Giuseppe

 

this is the code that I've translated in an indicator for mql4, but I get the error from the array range

regards

Giuseppe

 

I do not see your local arrays (BU[], PB[];) getting a size before being used.

giuseppeloddo:

this is the code that I've translated in an indicator for mql4, but I get the error from the array range

regards

Giuseppe

 
ubzen:

I do not see your local arrays (BU[], PB[];) getting a size before being used.



now I've changed the code as follow, but no output if I set the arrays to value 31, the output is a fixed flat line that doesn't change.

What about the use of the method resize? I've also doubt for the ArrayMinimum ArrayMaximum methods use

//+------------------------------------------------------------------+
//|                                                     Momentum.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "DorseyCorinnah"
#property strict

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//--- input parameter
//input int InpMomPeriod=31;  // Momentum Period
input int d=31;
input int e=31;
//--- buffers
double 
ExtMomBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;

//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMomBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="Mom("+IntegerToString(d)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input parameter
  if(d<=0)
     {
     Print("Wrong input parameter Momentum Period=",d);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,d);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Momentum                                                         |
//+------------------------------------------------------------------+
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,min,max;
    double R,M;
   double BU[31];
   double PB[31];
//--- check for bars count and input parameter
   if(rates_total<=d || d<=0)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtMomBuffer,false);
   ArraySetAsSeries(close,false);
//--- initial zero
   if(prev_calculated<=0)
     {
      for(i=0; i<d; i++)
         ExtMomBuffer[i]=0.0;
      limit=d;
     }
   else
      limit=prev_calculated-1;
//--- the main loop of calculations
   for(i=limit; i<rates_total; i++){
  //    ExtMomBuffer[i]=close[i]*100/close[i-InpMomPeriod];
   if (Bars<2) {
   BU[0]=Close[0];
   }
   else {
   BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0];
   }
   
   //M=Average[d](close)    
   M=iMA(NULL,0,d,0,MODE_SMA,PRICE_TYPICAL,0);
   
   PB[0]=MathAbs(BU[0]-M);
   
   min=ArrayMinimum(PB,e,0);
   max=ArrayMaximum(PB,e,0);
   R=100*(PB[0]-PB[min])/(PB[max]-PB[min]);
  ExtMomBuffer[i]=R;
  }
  
      
      
      
      
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
giuseppeloddo:


now I've changed the code as follow, but no output if I set the arrays to value 31, the output is a fixed flat line that doesn't change.

What about the use of the method resize? I've also doubt for the ArrayMinimum ArrayMaximum methods use

IndicatorBuffers() is ? ?

You are creating an Indicator so you use buffers not arrays, Buffers are series automatically so you don't need ArraySetAsSeries() for the buffer . . .

 
   double BU[31];
   double PB[31];
:
   BU[0]=BU[1]-BU[2]/3.414+(1/3.414)*Close[0];
:
   PB[0]=MathAbs(BU[0]-M);

You only set BU[0] never any other element. If it was a buffer then the values would shift on a new bar. They're arrays so they don't.
Reason: