In an indicator, is it possible for OnCalculate's "rates_total" to come bigger then the indicator's buffer's sizes?

 

Hi!

I just got an "array out of range" in the following code:

      int evalPos = 0;
      
      if (checkByTimer)
         evalPos = glAlertInfo.lastTickBarPos;
      else if (checkByNewTickIsOfNewBar)
         evalPos = glAlertInfo.lastTickBarPos;
      
                if (evalPos > 0)
                {
                        if (glIndData.lastHigh >= PriorDayBuffer[evalPos]  //array out of range in accessing this buffer
                        //...

As you can see in the code above, the "array out of range" must have come from evalPos receiving a bigger than 0 value from "glAlertInfo.lastTickBarPos". So which value this variable had?

Well, "glAlertInfo.lastTickBarPos" has its value changed only twice in my code: first in OnInit where it receives 0 and at the very end of OnCalculate, where it gets the position value of the last processed tick:

OnCalculate(...)
{
   //...

   //
   glAlertInfo.lastTickBarPos = rates_total - 1;
   
   //
   return rates_total;
}

So since evalPos couldn't had 0 as value where the array out of range happened, the only other option is that it had "rates_total -1", which means the array PriorDayBuffer had its buffer size smaller then rates_total (ArraySize(PriorDayBuffer) < rates_total). The problem is that this is the main buffer of the indicator, configured as such for it:

//--- indicator buffers
double PriorDayBuffer[];

//...

OnInit()
{
//...

//--- indicator buffers mapping
   SetIndexBuffer(0,PriorDayBuffer,INDICATOR_DATA);

//...
}

And well, I allways thought this "main indicator buffers" always had their size adjusted to be compatible with OnCalculate's rates_total by MT5 itself. Could this be a bug in MT5? :| What am I missing?

 
Martin Bittencourt:

Hi!

I just got an "array out of range" in the following code:

As you can see in the code above, the "array out of range" must have come from evalPos receiving a bigger than 0 value from "glAlertInfo.lastTickBarPos". So which value this variable had?

Well, "glAlertInfo.lastTickBarPos" has its value changed only twice in my code: first in OnInit where it receives 0 and at the very end of OnCalculate, where it gets the position value of the last processed tick:

So since evalPos couldn't had 0 as value where the array out of range happened, the only other option is that it had "rates_total -1", which means the array PriorDayBuffer had its buffer size smaller then rates_total (ArraySize(PriorDayBuffer) < rates_total). The problem is that this is the main buffer of the indicator, configured as such for it:

And well, I allways thought this "main indicator buffers" always had their size adjusted to be compatible with OnCalculate's rates_total by MT5 itself. Could this be a bug in MT5? :| What am I missing?

It's a bug in your code.

evalPos is set to rates_total-1 at the end of OnCalculate. But when you are using it BEFORE that, it can have a value higher than the CURRENT rates_total (-1) and so you can get an AOR.

A buffer size can (and so rates_total) DECREASE between OnCalculate() calls.

 
Alain Verleyen #:

A buffer size can (and so rates_total) DECREASE between OnCalculate() calls.

Thanks for the reply; you nailed it! The buffer is indeed getting smaller inbetween the calling of the code where the AOR happens and when glAlertInfo.lastTickBarPos is filled:

Error messages

I suppose this is due to the asset, B3's WIN, constantly recieving a "dead last candle" some minutes after the closing of the market, dead candle that is erased in the following morning:

Dead candle

So I fixed the problem by abandoning the use of the variable "glAlertInfo.lastTickBarPos" and by filling evalPos with "ArraySize(PriorDayBuffer) - 1".

Thanks for the help!