The distinctive feature of programs intended for operation in the client terminal MetaTrader 4 is their work with constantly updated information in real-time mode. In MQL4 language this peculiarity is reflected in the form of three special functions: init(), start() and deinit().
Special functions are functions with predefined names init(), start() and deinit() possessing own special properties.
Properties of Special Functions
Common Property of Special Functions
The main property of all special functions is their execution in a program under certain conditions without using special function call from the program. Special functions are called for execution by the client terminal. If a program contains the description of a special
function, it will be called (and executed) in accordance with calling conditions (own properties).
Special functions are called for execution by the client terminal.
Own Properties of Special Functions Special Function init().
The own property of the special function init() is its execution at the program initialization. If a program contains the description of the special function init(), it will be called (and
executed) at the moment the program starts. If there is no special function init() in a program, no actions will be performed at a program start.
In Expert Advisors the special function init() is called (and executed) after the client
terminal start and uploading of historic data, after changing security and/or chart period, after program recompilation in MetaEditor, after changing input parameters from EA setup
window and after account changing.
In scripts the special function init() is also called (and executed) immediately after it is to a chart.
In custom indicators the special function init() is called (and executed) immediately after the client terminal start, after changing security and/or chart period, after program
recompilation in MetaEditor and after changing input parameters from the custom indicator setup window.
Special Function start().
The own properties of the special function start() differ depending on the executable program type.
In Expert Advisors the special function start() is called (and executed) immediately after a new tick comes. If a new tick has come during the execution of the special function start(), this tick will be ignored, i.e. the special function start() will not be called for execution when such a tick comes. All quotes received during the execution of the special function start() will be ignored. Start of the special function start() for execution is performed by the client terminal only provided that the previous operation session has been completed, the control has been returned to the client terminal and the special function start() is waiting for a new tick.
The possibility to call and execute the special function start() is influenced by the state of
"Enable/disable Expert Advisors" button. If this button is in the state of disabling EAs, the client terminal will not call for execution the special function start() irrespective of whether new quotes come or not. However, changing the button state from enabled to disabled does not terminate the current operation session of the special function start().
The special function start() is not called by the client terminal if EA properties window is open. The EA properties window can be opened only when the special function start() is waiting for a new tick. This window cannot be opened during the execution session of the EA's special function start().
In scripts the special function start() is called (and executed) once immediately after program initialization in the special function init().
In custom indicators the special function start() is called (and executed) immediately after a new tick comes, immediately after being attached to a chart, when changing a security window size, when switching from one security to another, when starting the client terminal (if during the previous session an indicator was attached to a chart), after changing a symbol and period of a current chart irrespective of the fact whether new quotes come or not.
Termination of a current session of start() execution for all program types can be performed when a program is removed from a chart, when security and/or chart period are changed, when an account is changed/chart is closed and as a result of client terminal's operation termination. If the special function start() was executed during the shutdown command, time available for the terminal to complete execution of the function is 2.5 seconds. If after the shutdown command the special function start() continues its operation for more than the indicated time limit, it will be forcibly terminated by the client terminal.
Special Function deinit().
The own function of the special function deinit() is its execution at program termination (deinitialization). If a program contains description of the special function deinit(), it will be called (and executed) at a program shutdown. If a program does not contain th especial function deinit(), no actions will be performed at program shutdown.
The special function deinit() is also called for execution by the client terminal at the terminal shutdown, when a security window is closed, right before changing a security and/or chart period, at a successful program recompilation in MetaEditor, when changing input
parameters, as well as when an account is changed.
In Expert Advisors and scripts program shutdown with the necessary call of the special function deinit() may happen when attaching to a chart a new program of the same type that replaces the previous one.
In custom indicators the special function deinit() is not executed when a new indicator is attached to a chart. Several indicators can operate on a security window, that is why the attachment of a new indicator to a chart does not result in the shutdown of other indicators with deinit() call.
The time of deinit() execution is limited to 2.5 seconds. If the code of the special function deinit() is executed longer, the client terminal will forcibly terminate the execution of the special function deinit() and operation of the program.
Requirements for Special Functions
Special functions init() and deinit() can be absent in a program. The order of special functions description in a program does not matter. Special functions can be called from any program part in accordance with the general order of function calls.
Special functions may have parameters. However, when these functions are called by the client terminal, no parameters will be sent from outside, only default values will be used.
Special functions init() and deinit() must finish their operation maximally quickly and in no case run into a cycle path trying to start the whole operation before calling the function start().
Order of Using Special Functions
Developers have presented to programmers a very convenient tool: when a program starts, first of all init() is executed, after that the main work is performed with the help of the function start(), and when a user finishes his work, the function deinit() will be started prior to the program shutdown.
The main code of a program must be contained in the function start(). All operators, built-in and custom functions calls and all necessary calculations should be performed inside this function. At the same time one must correctly understand the role of custom functions. The description of custom functions is located in a program code outside the description of special functions, but if a custom function is called for execution, a special function does not
terminate its operation. It means control fr some time is passed to the custom function, but the custom function itself operates within a special function that has called it. So, in the process of a program execution special function operate always (in accordance with their own properties) and custom function are executed when called by special functions.
If a programmer is not going to use any of the special functions, he can refuse from using it in a program. In such a case the client terminal will not call it. Absolutely normal program is one containing all the three special functions. A program that does not have init() or deinit() or both these functions is also considered normal.
If none of the special functions is used in a program, this program will not be executed.
Client terminal calls for execution only special function in accordance with their properties.
Custom functions are not called by the client terminal. That's why if a program does not contain special functions at all (and contains only custom functions), it will be never called for execution.
It is not recommended to call start() function from the special function init() or perform trade operations from init(), because during initialization values of information environment parameters may be not ready (information about charts, market prices, etc.).
Sections Program Execution and Examples of Implementation contain several practical examples that will help to see some properties of special functions.