Invalid buffer index

 

I have a problem in my EA, strategy tester reports invalid index buffer number error repeatedly throughout the test so I created a simple test EA/Indicator combination to try and find the cause but I still get the same results in tester, I cant see what is wrong with these test programs can anyone shed some light on this ?

test indicator:

//+------------------------------------------------------------------+
//|                                          buffertestindicator.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  Red
#property indicator_color2  Yellow
#property indicator_color3  Pink
#property indicator_color4  Green
#property indicator_color5  Orange
#property indicator_color6  Purple
#property indicator_color7  Blue
#property indicator_color8  White

double zero[];
double one[];
double two[];
double three[];
double four[];
double five[];
double six[];
double seven[];
//----
int buffers = 0;
int drawbegin = 0;
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   initbuffer(zero, "buffertest", DRAW_LINE);
   initbuffer(one,  "buffertest", DRAW_LINE);
   initbuffer(two,  "buffertest", DRAW_LINE);
   initbuffer(three,"buffertest", DRAW_LINE);
   initbuffer(four, "buffertest", DRAW_LINE);
   initbuffer(five, "buffertest", DRAW_LINE);
   initbuffer(six,  "buffertest", DRAW_LINE);
   initbuffer(seven,"buffertest", DRAW_LINE);
   IndicatorBuffers(buffers);
   IndicatorShortName("buffertest");
//----
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int    limit=Bars-counted_bars-1;
//----
   for(int i=limit;i>=0;i--)
    {
     zero[i]   = iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i);
     one[i]    = iMA(NULL,0,2,0,MODE_SMA,PRICE_MEDIAN,i);
     two[i]    = iMA(NULL,0,3,0,MODE_SMA,PRICE_MEDIAN,i);
     three[i]  = iMA(NULL,0,4,0,MODE_SMA,PRICE_MEDIAN,i);
     four[i]   = iMA(NULL,0,5,0,MODE_SMA,PRICE_MEDIAN,i);
     five[i]   = iMA(NULL,0,6,0,MODE_SMA,PRICE_MEDIAN,i);
     six[i]    = iMA(NULL,0,7,0,MODE_SMA,PRICE_MEDIAN,i);
     seven[i]  = iMA(NULL,0,8,0,MODE_SMA,PRICE_MEDIAN,i);
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
void initbuffer(double array[], string label="", int type=DRAW_NONE,
                int arrow=0, int style=EMPTY, int width=EMPTY,
                color clr=CLR_NONE)
{
  SetIndexBuffer(buffers, array);
  SetIndexLabel(buffers, label);
  SetIndexEmptyValue(buffers, EMPTY_VALUE);
  SetIndexDrawBegin(buffers, drawbegin);
  SetIndexShift(buffers, 0);
  SetIndexStyle(buffers, type, style, width);
  SetIndexArrow(buffers, arrow);
  buffers++;
}

test EA:

//+------------------------------------------------------------------+
//|                                                 buffertestea.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

int start()
  {
//----
   double levelone    = iCustom(NULL,0,"buffertestindicator",0,0);
   double leveltwo    = iCustom(NULL,0,"buffertestindicator",1,0);
   double levelthree  = iCustom(NULL,0,"buffertestindicator",2,0);
   double levelfour   = iCustom(NULL,0,"buffertestindicator",3,0);
   double levelfive   = iCustom(NULL,0,"buffertestindicator",4,0);
   double levelsix    = iCustom(NULL,0,"buffertestindicator",5,0);
   double levelseven  = iCustom(NULL,0,"buffertestindicator",6,0);
   double leveleight  = iCustom(NULL,0,"buffertestindicator",7,0);
//----
   return(0);
  }
//+------------------------------------------------------------------+

 

try commenting out this line first....

IndicatorBuffers(buffers);

if get the same err, then try

IndicatorBuffers(buffers+1);
 

Read the Documentation . . .

From here: https://docs.mql4.com/customind/IndicatorBuffers

void IndicatorBuffers (int count)

"Allocates memory for buffers used for custom indicator calculations. The amount of buffers cannot exceed 8 or be less than the value given in the indicator_buffers property."

#property indicator_buffers 8


int buffers = 0;


IndicatorBuffers(buffers);
 

so your saying while the initbuffers() function is initializing the buffers, and before it has initialized all of them the total number of buffers is less than 8 so it is causing the error ? Im going to change this line.

IndicatorBuffers(buffers);

to

IndicatorBuffers(8);
and run the test again.
 

Well Raptor, you pretty much nailed that one to the wall, thanks buddy :)

 
You are welcome. :-)
 

ok this is crazy there must be a bug, I ran that same test a second time with no more changes or modifications and now:

 
int buffers = 0;
int drawbegin = 0;
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   initbuffer(zero, "buffertest", DRAW_LINE); // increments buffers
Each chart change of pair, timeframe, refresh, etc, all indicators and EA goes through a deinit/init cycle. But there is only ONE load (statics and global). On load buffers=0..8. Next init buffers=8..16.(invalid index.) Reset buffers=0 inside init.
 

Oh really ? wow I never would have figured that one out, how do you know all this stuff WHR ? lol

ok I'm going to try that thanks for your help

 

OK that worked I added the line to init()

int init()
  {
//---- indicators
   buffers = 0;
   initbuffer(zero, "buffertest", DRAW_LINE);
   initbuffer(one,  "buffertest", DRAW_LINE);

ran the test three times there was no buffer error, thanks WHR as always your help is greatly appreciated :)

Reason: