Double Calls to OnInit() and OnDeinit()

 

I have noticed that MT4 Build 600+ makes a double call to OnInit() and OnDeinit() when you change the parameters by clicking on the smiley face. In the Experts log you will observe the sequence:


Expert EURUSD,M30: initialized

Expert EURUSD,M30: uninit reason 5

Expert EURUSD,M30: inputs: ..............

Expert EURUSD,M30: initialized

Expert EURUSD,M30: uninit reason 5

Expert EURUSD,M30: inputs: ..............


While this may go unnoticed in many EAs which will just perform some functions twice, it is a serious problem for several of my EAs as the OnInit() function is used to prompt the user on what type of trade to select. So now they are prompted twice to enter the trade. I've coded a crude workaround to ignore the second call to OnInit() but this is a horrible way to do it. This is such a fundamental operation of MT4 that this needs to be fixed properly by MetaQuotes.

 
The code posted at https://www.mql5.com/en/forum/150009 could serve as a work-around while we wait a few years for MetaQuotes to address the issue.
 

maybe you should separate your function from init

//+------------------------------------------------------------------+
//|                                                          teS.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
bool myInithasRun=0;
int OnInit()
  {
//---
    myInithasRun=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if( myInithasRun==0){MY_INIT();return ;}
  }
//+------------------------------------------------------------------+
//| MY INIT function                                                  |
//+------------------------------------------------------------------+
void MY_INIT()
  {Alert(" running my init");  myInithasRun=1;
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+ 
 
Yes, that would work SOMETIMES [ not when you want init to run before market open for example ]. However, this misses the point that none of us should need to reverse engineer and work around the sloppy code MQ has introduced. It is surely THEIR job to sort issues when they are reported, or preferably before!!!
 
ballenoz:
The code posted at https://www.mql5.com/en/forum/150009 could serve as a work-around while we wait a few years for MetaQuotes to address the issue.


https://www.mql5.com/en/forum/150009/page2#937576
 

Thanks for the coding suggestions. I did something similar by using a flag to ignore the second OnInit() call until a tick has been received. However you are quite right that we should not have to be coding around basic problems in MT4.

I made this post because this is such a fundamental error. I don't know how to escalate this with MetaQuotes as you seem to get redirected to this forum from their web site for client support. My hope is that someone from MetaQuotes will read this post and implement a fix.

 
petermitchell:

Thanks for the coding suggestions. I did something similar by using a flag to ignore the second OnInit() call until a tick has been received. However you are quite right that we should not have to be coding around basic problems in MT4.

I made this post because this is such a fundamental error. I don't know how to escalate this with MetaQuotes as you seem to get redirected to this forum from their web site for client support. My hope is that someone from MetaQuotes will read this post and implement a fix.



Please read other's posts, it's already fixed in build 628.
 

Thanks for the update angevoyageur. I currently have build 625, so I look forward to trying out 628.

 

hmm, b628 has another problem now with OnInit().

If an EA is started by the Strategy-Tester either Opti, or a single backt-test OnInit() is performed as if it were loaded at a chart of a chart:

You have inthe Expert-Tab (not the Journal-Tab of the Tester) the line with the setup: 2014.04.03 13:09:52.315 xN EURJPY,M1 inputs:...

If you have Alerts or Print lines they are executed as well :(

Even if the EA is not applied to any chart - quite confusing at its first appearance.

Gooly

 
gooly:

hmm, b628 has another problem now with OnInit().

If an EA is started by the Strategy-Tester either Opti, or a single backt-test OnInit() is performed as if it were loaded at a chart of a chart:

You have inthe Expert-Tab (not the Journal-Tab of the Tester) the line with the setup: 2014.04.03 13:09:52.315 xN EURJPY,M1 inputs:...

If you have Alerts or Print lines they are executed as well :(

Even if the EA is not applied to any chart - quite confusing at its first appearance.

Gooly

I can't understand what you are talking about. What has changed on build 628 ?
 

1) Does an EA prints its setup into the Expert-Tab when started by the Strategy-Tester?

2) Are Alerts executed when an Optimization in the Strat.-Tester is started?

Never recognized that before b628.

Gooly

Reason: