[MT4][programming][intro] 2. OnInit() specifications and precautions

Here, let’s check the specifications and precautions regarding OnInit () called from the event handler of mql4.
Please use it as a reference when implementing OnInit ().

Also, for the sample code that will be the format for implementing mql4 and the outline, first see the format file and outline of Experts Advisor.

Contents

OnInit() specifications and precautions

1. specification of OnInit()

Here, we will check the specifications of OnInit (), which is the event handling function of mql4.

1-1. specification of interface

The OnInit() function has two different Interface specifications.

Typereturnfunctionargument
1voidOnInit()void
2intOnInit()void
specification of OnInit() Interface

As mentioned above, there are two patterns with different return values.
When actually implementing it, specify the return type depending on which one you use.

In the case of Type1, every time the OnInit() function is executed, it is internally processed as “initialization successful”.If the initialization is successful, then if there is a Tick event etc., processing such as OnTick () will be called.

In the case of Type2, if the return value is set to non-zero, it will be processed internally as “initialization failure”.Also, if the initialization fails, the OnDeinit() function will be called and the EA will stop working.
If there is an initial condition of EA or a specified condition in the setting Parameter, it is better to implement various parameter check processing using Type2 to prevent malfunction.
After OnDeinit () is called, OnTick () etc. will not be called even if the Tick event occurs.
In addition, it is recommended to use one of the following return values.

Recommended return value:
INIT_SUCCEEDED : Successful initialization
INIT_FAILED : Initialization failed
INIT_PARAMETERS_INCORRECT : This value means the incorrect set of input parameters.

NOTE:Implementation sample of Type1 and Type2 of OnInit()

void OnInit(void)
{
	// Implementation such as initialization processing
	return;
}
int OnInit(void)
{
	int ret = INIT_SUCCEEDED;
	
	if     (/* Some condition */)	{ ret = INIT_FAILED; }
	else if(/* Some condition */)	{ ret = INIT_PARAMETERS_INCORRECT; }
	
	return ret;
}

1-2. Call timing specifications

Next, let’s check the use of OnInit() call timing.
OnInit() is called at the timing shown below.

  1. client terminal loads a program
  2. financial instrument and/or chart timeframe is changed
  3. after a program is recompiled
  4. after input parameters are changed from the setup window of an Expert Advisor
  5. after the account is changed.

What is of concern here is the relationship with global variables.
Is the global variable initialized and OnInit() called, or is it sometimes initialized or not?

If the recognition of the initialization timing of this global variable is different, it will cause an unexpected bug, so please be careful.

2. Precautions when implementing the OnInit() function

The above raised concerns about the timing of OnInit () and global variable initialization, but here we’ll go into a little more detail.

2-1. Relationship between OnInit() and global variables

I couldn’t find the answer even if I referred to MQL4 Reference, which describes the MQL4 specifications, so I will actually create a simple EA and check it.

First, create a simple test EA.
This time, I will confirm the relationship between OnInit() and global variables based on the sample code introduced in the mql4 format sample code.

Sample code

/*****************************/
/* propety setting  ・・・ A */
/*****************************/
#property copyright   "2020-2021, x corp."
#property version     "1.00"
#property strict

/*****************************/
/* Input variables  ・・・ B */
/*****************************/
input double	parameter1		= 1.0;

/******************************/
/* Global variables  ・・・ C */
/******************************/
double 			glovalValue1	= 1.0;

/************************************/
/* Event Handing Functions ・・・ D */
/************************************/
int OnInit(void)
{
	int ret = INIT_SUCCEEDED;
	
	// Output for checking the initialization of global variables and the timing when OnInit () is called
	glovalValue1++;
	Print("OnInit() : ", glovalValue1);
	
	return ret;
}

void OnDeinit(const int reason)
{
	return;
}

In the above sample code, every time OnInit () is called, the process of incrementing the global variable globalValue1 is described and the result is output to the “Expert” tab.

Let’s use this EA to call OnInit() from each call trigger.

client terminal loads a program:
First, check immediately after loading the program.
The result is as follows, and it is in the state where “1” is added to the setting value “1” when the global variable is defined.
Immediately after loading the program, you can see that the OnInit () function is called after the global variables are initialized.

OnInit() : 2.0

financial instrument and/or chart timeframe is changed:
Next, let’s take a look at the call to OnInit() when the timeframe is changed on Chart.
The result is as follows, and it is the state where “1” is added from the state immediately after loading the program.
From this, it can be seen that when the timeframe is changed, OnInit () is called without the global variables being initialized.

OnInit() : 3.0

after a program is recompiled:
Let’s take a look at the others. Next is when the program is recompiled.
The result is as follows, and it is in the state where “1” is added to the setting value “1” when the global variable is defined, as in the case of loading the program.
When you recompile the program, you can see that the OnInit () function is called after the global variables are initialized.

OnInit() : 2.0

after input parameters are changed from the setup window of an Expert Advisor:
Finally, when changed the parameters on the setup screen.(I couldn’t try changing my account because I didn’t own another account.If you have the possibility of changing your account while the EA is running, please try it in the same way.)
The result is as follows, and it is the state where “1” is added from the state immediately after loading the program.
From this, it can be seen that OnInit () is called when the global variable is not initialized when the parameter is changed on the setup screen.

OnInit() : 3.0

2-2. Summary of points to note when implementing the OnInit() function

Finally, this is summarized the calls to OnInit () and the initialization of global variables that we have confirmed so far.

called timing for OnInit()With or without global variable initialization
client terminal loads a programwith initialization
financial instrument and/or chart timeframe is changedwithout initialization
after a program is recompiledwith initialization
after input parameters are changed from the setup window of an Expert Advisorwithout initialization
after the account is changed.– (Please check for yourself)
Summary of the relationship between the call timing of OnInit() and the initialization of global variables

When implementing the initialization process in the OnInit() function, the state of global variables will differ depending on the call timing as described above, so try to implement in consideration of the above and do not embed unexpected bugs.

Next, How to write trade processing based on the implementation example of OnTick().