Array out of range on an Indicator Buffer array

To add comments, please log in or register
Stanislav Ivanov
5510
Stanislav Ivanov  

Hi,

I am having a few unexpected issues assigning previous values on a buffer. I get the array out of range error.

I am sharing the code :

for(int i=1;i<Bars(Symbol(),PERIOD_M1)-1;i++)
        {
        for(int k=ArraySize(info)-2;k>=0;k--)
         {
        // Print("1 ",info[k].symb," 2 ",info[k].ask_volume," 3 ",info[k].bid_volume," 4 ",info[k].tm);
            if(rates[i].time==info[k].tm)
              {
               Print("time ",rates[i].time);
               double total=(info[k].ask_volume)+(info[k].bid_volume);
               if(total>0)
                 {
                  double Ask_p= (info[k].ask_volume/total)*100;
                  double Bid_p= (info[k].bid_volume/total )*100;
                 // Print("i ",i," bars ",Bars(Symbol(),PERIOD_M1)," size ",ArraySize(AskVolume));
                  AskVolume[i]= Ask_p;
                  BidVolume[i]=(-Bid_p);
                 }
              }
        }
      //--- close the file
     }


 This is the buffer :

#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Buy
#property indicator_label1  "Ask Volume"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrLimeGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  7
//--- plot Sell
#property indicator_label2  "Bid Volume"
#property indicator_type2   DRAW_HISTOGRAM
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  7


MqlBookInfo book[];
double Ask,Bid;
double AskVolume[],BidVolume[];
string file_name="VolumeReal"+_Symbol+".txt";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
struct Datefile
  {
   string            symb;
   double            ask_volume;
   double            bid_volume;
   datetime          tm;
  };
Datefile info[];
int inf_size=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   SetIndexBuffer(0,AskVolume,INDICATOR_DATA);
   SetIndexBuffer(1,BidVolume,INDICATOR_DATA);
   MarketBookAdd(_Symbol);
//---
   ReadFunction();
   return(INIT_SUCCEEDED);
  }
Marco vd Heijden
Moderator
12487
Marco vd Heijden  

The compiler also gives the exact line number and cursor position.

Stanislav Ivanov
5510
Stanislav Ivanov  

its this line

AskVolume[i]= Ask_p;
Mehrdad Jeddi
28712
Mehrdad Jeddi  

Hi,

your code is incomplete,

But i think your error is at this line :

if(rates[i].time==info[k].tm)


So you can add this ,but should check the size of the loop array index which should not be more than array size:

if(i>=ArraySize(rates)-1)continue;

or use :

if(i>=ArraySize(AskVolume)-1)continue;
if(i>=ArraySize(BidVolume)-1)continue;

It should be within first loop:

for(int i=1;i<Bars(Symbol(),PERIOD_M1)-1;i++)
        {


So you can add another condition that avoids unexpected exception if it may happen within second loop like:

if(k>=ArraySize(info)-1)continue;

Regards.

Mladen Rakic
148701
Mladen Rakic  
Stanislav Ivanov:

Hi,

I am having a few unexpected issues assigning previous values on a buffer. I get the array out of range error.

I am sharing the code :


 This is the buffer :

If you allowed metatrader 4 to update / upgrade to build 1184 that can happen on a perfectly good code.

In that case revert it (the metatrader)

Stanislav Ivanov
5510
Stanislav Ivanov  
Mehrdad Jeddi:

Hi,

your code is incomplete,

But i think your error is at this line :


So you can add this ,but should check the size of the loop array index which should not be more than array size:

or use :

It should be within first loop:


So you can add another condition that avoids unexpected exception if it may happen within second loop like:

Regards.

Hi thanks for your reply,

I printed out the size of AskBolume its 0. But considering its a buffer, i dont think it can be because of that. Any ideas ?

Mehrdad Jeddi
28712
Mehrdad Jeddi  

So you should array resize it by i index,because the index of i variable is storing data into this array,also for Bid_Volume.

Your code is incomplete,and fixing the code is vague now,

rrocchi
163
rrocchi  
Stanislav Ivanov:

its this line

Check if the return value of 

ArraySize(info);

is correct.

To add comments, please log in or register