Array out of Range with My Grid Line indicator

 

Good day Guys, can any one help me fix this problem? I'm trying to build a Grid Line Indicator and its saying Array is out of Range. Here is the source Code :

#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 clrWhite
extern double   RSMAX=2.0;
extern double   RSMIN=1.0;
extern double   RS_STEP=10.0;
double pips;
double TickSize;
double RS_Lines[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
 //---
      TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
     if(TickSize ==0.00001 || Point == 0.001)
      pips= TickSize * 10;
      else pips= TickSize;
//--
//------ Indicator Buffer Mapping
  SetIndexBuffer(0,RS_Lines);
  SetIndexStyle(0,DRAW_LINE);
  SetIndexLabel(0,"Draw Lines");
//-------
   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[])
  {
//---
   GridLines();
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void GridLines()
{

int Grid_index = (RSMAX / 0.00001);
double GridStep = MathRound(RS_STEP * pips);
double P = RSMIN; 
for(int i=0; i<Grid_index; i++)
{  
  RS_Lines[i]+=(i * GridStep)+ P;
     
     }
  
 
 }


 I will Realy appreciate if this indicator is properly debbuged By you guys. Thanks in advance

 

Please use the SRC button when you post code - it makes it much easier to read! I've edited your post.

RS_Lines is a buffer, which means if there are a 1000 bars there are 1000 elements in the RS_Lines array.

The problem you have is that Grid_index could be a larger number than the amount of bars on the chart. If that happens, you will get an "Array out of range" error.

One way to address the problem is to make sure you don't go beyond the number of bars in the chart

void GridLines()
{

int Grid_index = (RSMAX / 0.00001);
double GridStep = MathRound(RS_STEP * pips);
double P = RSMIN; 
for(int i=0; i<fmin(Bars,Grid_index); i++)
{  
  RS_Lines[i]+=(i * GridStep)+ P;
     
     }
 
  1. Buffer exceeded, already answered.
  2.  RS_Lines[i]+=(i * GridStep)+ P;
    What you will see (If you just assigned to buffer,) is one line dropping RS_STEP pips per bar. Quite useless. But each tick you add to the buffer so that line will move up the chart until it's gone.
  3. Why did you post in the Root / MT5 General section instead of the MQL4 section (bottom of the Root page?)
 
honest_knave:

Please use the SRC button when you post code - it makes it much easier to read! I've edited your post.

RS_Lines is a buffer, which means if there are a 1000 bars there are 1000 elements in the RS_Lines array.

The problem you have is that Grid_index could be a larger number than the amount of bars on the chart. If that happens, you will get an "Array out of range" error.

One way to address the problem is to make sure you don't go beyond the number of bars in the chart

Still not drawing any Grid lines on the Chart,pls try and check it out for me. Thanks in advance...

 

Of course not - you haven't changed anything. You can't do what you want with one buffer, as already answered. (RSMAX-RSMIN)/(RS_STEP*Pip) is one hundred.

See 'Horizontal Grid Lines on Chart' indicator by 'MOOSE1' for MetaTrader 4



 
whroeder1:

Of course not - you haven't changed anything. You can't do what you want with one buffer, as already answered. (RSMAX-RSMIN)/(RS_STEP*Pip) is one hundred.

See 'Horizontal Grid Lines on Chart' indicator by 'MOOSE1' for MetaTrader 4



thanks whroeder1
 
whroeder1:

Of course not - you haven't changed anything. You can't do what you want with one buffer, as already answered. (RSMAX-RSMIN)/(RS_STEP*Pip) is one hundred.

See 'Horizontal Grid Lines on Chart' indicator by 'MOOSE1' for MetaTrader 4



Thanks honest knave....