Expert Advisor only runs at first time after compilation (other times it fails)

 

Hello,

I would like to ask for help  with my Expert Advisor that runs only first time after compilation.
Every other attempt to run EA fails with OnInit with runtime error code 5035.

For example:

  1. I make no change in my Expert Advisor mql5 file
  2. I compile EA mql5 file
  3. I run the Single test in Strategy tester - runs ok, no problems
  4. I run the same test again - tester stopped because OnInit returns non-zero code (error 5035)

Is there something happening during the compilation that flushes some variables? or is is something else?

My OnInit function looks like this, during the second run GetLastError returns 5035 even though _LastError variable is reset before OnInit stars

int OnInit()
  {
   Bid = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits);
   Ask = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits);
   is_trade_allowed = false;

   string smbl = Symbol();
   Print("Symbol ", smbl, " ",StringSubstr(smbl,3,3));
   if((StringLen(smbl)!=6)&&(StringLen(smbl)!=8)){
      Alert("Unknown pair: ", smbl);
      return(INIT_FAILED);
    }
    
   string EURXXX = "EUR"+StringSubstr(smbl,3,3);
   Print("EURXXX ", EURXXX);
   if(StringLen(smbl)==8){
      EURXXX+=StringSubstr(smbl,6,2);
   }
   bool select1 = SymbolSelect(EURXXX, true);
   Print("SymbolSelect ", EURXXX, " result: ", select1);
   
   bool select2 = SymbolSelect(smbl, true);
   Print("SymbolSelect ", smbl, " result: ", select2);
   
   double problem = SymbolInfoDouble(EURXXX,SYMBOL_BID);
   int err = GetLastError();
   if((err!=0)||problem==0){
      Alert("Error ", err, " ocurred during addition of pair: ", EURXXX);
      return(INIT_FAILED);
   }

...
...
...

Thanks for any tip

Documentation on MQL5: MQL5 programs / Runtime Errors
Documentation on MQL5: MQL5 programs / Runtime Errors
  • www.mql5.com
Runtime Errors - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
it3oe: I would like to ask for help  with my Expert Advisor that runs only first time after compilation. Every other attempt to run EA fails with OnInit with runtime error code 5035.

For example:

  1. I make no change in my Expert Advisor mql5 file
  2. I compile EA mql5 file
  3. I run the Single test in Strategy tester - runs ok, no problems
  4. I run the same test again - tester stopped because OnInit returns non-zero code (error 5035)

Is there something happening during the compilation that flushes some variables? or is is something else?

My OnInit function looks like this, during the second run GetLastError returns 5035 even though _LastError variable is reset before OnInit stars

You are not supposed to be referencing quote information, or any trade data at all, in the OnInit() event handler, which is for initialisation only.

There is no guarantee that any data will be available at all during the initialisation process, and you should release the event quickly, or you will hold up the event queue.

Your main logic should be in the OnTick() event handler.

 

Forum on trading, automated trading systems and testing trading strategies

Getting the bar opening time (SERIES_LASTBAR_DATE) having the position opening time

Fernando Carreiro, 2023.09.17 00:35

Because the OnInit should be used for initialisation only. There are many times when trade data has not even been loaded yet and not available during the initialisation phase.

All data coming from the trade server should be read in either the OnTick (EAs) or OnCalculate (Indicators) event handlers, not the OnInit.

Create a startup routine in OnTick/OnCalculte for this. Don't do it in OnInit.

Forum on trading, automated trading systems and testing trading strategies

Getting the bar opening time (SERIES_LASTBAR_DATE) having the position opening time

Fernando Carreiro, 2023.09.17 03:16

No! Simply use a static boolean variable to detect the first-time use.

void OnTick() {
   static bool bFirstTime = true;
   if( bFirstTime ) {
      bFirstTime = false; 
      
      // ... carry out a start-up procedure
   };

   // ... do other new tick event handling
};

Forum on trading, automated trading systems and testing trading strategies

Delaying an EA ...

Fernando Carreiro, 2023.04.13 18:23

Don't do any trade related tasks in the OnInit() nor even data feed realted tasks. That is not the purpose of that event handler.

During the execution of OnInit() there may be no connection to the trade server yet, so do it in the OnTick() only after the arrival of the first tick.

And again, don't use a loop with "sleep". That blocks the thread. Use an event driven architecture with a state machine instead.