OnTick Critical Error

 

Hello,

I keep getting the runtime OnTick Critical error when I try to test my ea. Here is part of the code;

double cci[];
double rsi[];
double prev_volume, prev_AvD, prev_holdthese, prev_cci;
double volume, AvD, holdthese;


int OnInit()
 {
MqlRates rates[];
int totalbars=Bars(_Symbol,_Period);
ArraySetAsSeries(rates, true);
CopyRates(_Symbol,_Period,0,totalbars,rates);

ArraySetAsSeries(rsi,true);
int rsiHandle=iRSI(_Symbol,_Period,21,PRICE_CLOSE);
CopyBuffer(rsiHandle,0,0,totalbars,rsi);


ArraySetAsSeries(cci,true);
int cciHandle=iCustom(_Symbol,_Period,"Examples\\CCI");
if (cciHandle<0) Alert ("CCI indicator not initialized");
CopyBuffer(cciHandle,0,0,totalbars,cci);


//---
   return(INIT_SUCCEEDED);
  }


void OnTick()
   {

int i=0;
int a=4;
int x=1;

do
{
double currentrsi=rsi[i];
double previousrsi=rsi[x];
double previousbutonersi=rsi[x+1];
double currentdifference=MathAbs(currentrsi-previousrsi);
double prev_difference=MathAbs(previousrsi-previousbutonersi);
holdthese=holdthese+currentdifference;
prev_holdthese=prev_holdthese+prev_difference;


currentcci=cci[i];
double prevcci=cci[x];
double prevbutonecci=cci[x+1];


if (x>i+4)
{
...
...
...
...
i don't think this part of the code is relevant
...
...
...
        
a++;
}         
x++;         
i++;
}
while (a<=30);
}


I understand that it is caused by the array being out of range but I have tried everything I can think of but I can't get anywhere. Any help will be highly appreciated. Thanks.

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 
int OnInit(){
MqlRates rates[];
int totalbars=Bars(_Symbol,_Period);
ArraySetAsSeries(rates, true);
CopyRates(_Symbol,_Period,0,totalbars,rates);

ArraySetAsSeries(rsi,true);
int rsiHandle=iRSI(_Symbol,_Period,21,PRICE_CLOSE);
     CopyBuffer(rsiHandle,0,0,totalbars,rsi);
  1. don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  2. Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  3. Check your return codes

 
Murray Mbugua:

This is the MQL4 section, but your code appears to be MQL5.
Please confirm what it is intended to be.

 
Keith Watford:

This is the MQL4 section, but your code appears to be MQL5.
Please confirm what it is intended to be.

Sorry about that. I thought I was in the right place. I must've missed a step while finding the MQL5 forum

 
William Roeder:
  1. don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  2. Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  3. Check your return codes

Thank you for your quick and very clear explanation.

I have made the necessary changes as you have recommended as below.

int OnInit()
 {
ArraySetAsSeries(rsi,true);
int rsiHandle=iRSI(_Symbol,_Period,21,PRICE_CLOSE);
if (rsiHandle<0) Alert ("RSI indicator not initialized");

ArraySetAsSeries(cci,true);
int cciHandle=iCustom(_Symbol,_Period,"Examples\\CCI");
if (cciHandle<0) Alert ("CCI indicator not initialized");


//---
   return(INIT_SUCCEEDED);
  }


void OnTick()
{
//---
MqlRates rates[];
int totalbars=Bars(_Symbol,_Period);
ArraySetAsSeries(rates, true);

int ratescopied=CopyRates(_Symbol,_Period,0,totalbars,rates);
if (ratescopied<0) Alert ("Rates not copied");

int rsicopied=CopyBuffer(rsiHandle,0,0,totalbars,rsi);
if (rsicopied<0) Alert ("Rsi not copied");

int ccicopied=CopyBuffer(cciHandle,0,0,totalbars,cci);
if (ccicopied<0) Alert ("CCI not copied");


I am still getting the same Critical Error but after checking the CopyBuffer return codes, it turns out the problem is the indicators are not copying the values. What could I be doing wrong and what could be the solution? Thanks in advance.

 
It looks like the problem wasn't that the indicators weren't copying values. I have solved that but still get the same OnTick Critical Error.
Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
The executing subsystem of the client terminal has an opportunity to save the error code in case it occurs during a MQL5 program run. There is a predefined variable _LastError for each executable MQL5 program. Before starting the OnInit function, the _LastError variable is reset...
 
Murray Mbugua:
It looks like the problem wasn't that the indicators weren't copying values. I have solved that but still get the same OnTick Critical Error.

Use ArrayResize(), to give a count size your cci and rsi buffers with value of totalbars size.

 
Roberto Jacobs: Use ArrayResize(),
  1. ArraySize
  2. No need, OP already knows the size in the returned values (e.g. rsicopied.)
Reason: