Download MetaTrader 5

Double Calls to OnInit() and OnDeinit()

To add comments, please log in or register
Unable to find the right application? Order it!
petermitchell
143
petermitchell 2014.03.27 22:27 

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.

ballenoz
80
ballenoz 2014.03.27 22:41  
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.
Bambang Sugianto
10224
Bambang Sugianto 2014.03.28 05:52  

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                                              |
//+------------------------------------------------------------------+ 
ballenoz
80
ballenoz 2014.03.28 06:41  
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!!!
Alain Verleyen
Moderator
30702
Alain Verleyen 2014.03.28 09:23  
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
petermitchell
143
petermitchell 2014.03.31 09:35  

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.

Alain Verleyen
Moderator
30702
Alain Verleyen 2014.03.31 10:35  
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.
petermitchell
143
petermitchell 2014.04.03 11:00  

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

Carl Schreiber
6580
Carl Schreiber 2014.04.03 14:42  

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

Alain Verleyen
Moderator
30702
Alain Verleyen 2014.04.03 14:58  
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 ?
Carl Schreiber
6580
Carl Schreiber 2014.04.03 15:12  

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

12
To add comments, please log in or register