Download MetaTrader 5

Array out of range exception when debugging

To add comments, please log in or register
PegasoVT
10
PegasoVT  

Hi I'm getting an "array out of range" exception for a very simple indicator within the Meta Editor debugger.

The initial values after the indicator starts are the following:


Bars: 218

countedBars=0 


In effect the code try to address the rsi[218] element but the arrays in MT4 are zero-based. The code should address rsi[217] element. 

I think that the error is correctly raised but when I launch the indicator directly inside MT4 (without the debugger) it works fine... (#@#?#). 

The same problem arises in other sections of the code and every time I'm getting an "array out of range" exception. Again when the indicator is used directly from within MT4 (without the debugger) it works fine. I spent a lot of time with no results, but I am very very new with MT4-MT5, so probably there is some thing I have to understand yet.. can you help me?

//+-------------------------------
// code sample used with the debugger
//+----------------------------- 

double rsi[]; 

void OnStart()
{

    int countedBars = IndicatorCounted();
    if(countedBars < 0) countedBars=0;
    CalculateIndicator(countedBars);
 } 

 

void CalculateIndicator(int countedBar)
{
   for (int i = Bars - countedBars; i >= 0; i--)
  { 

rsi[i] = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, i);   // Bars==218  - countedBars==0  i==218-- Here is the Out of range exception
   }              

Alain Verleyen
Moderator
32814
Alain Verleyen  

I think that the error is correctly raised but when I launch the indicator directly inside MT4 (without the debugger) it works fine... (#@#?#).

Because when running the debugger "#property strict" is implied.

Anyway, fix your code.

PegasoVT
10
PegasoVT  
Alain Verleyen:

Because when running the debugger "#property strict" is implied.

Anyway, fix your code.

Many thanks for your reply, I will correct the indicator to avoid the exceptions.
Maurizio
Bernhard Schweigert
12785
Bernhard Schweigert  

try add

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

To add comments, please log in or register