The distinctive feature of programs intended for operation in the client terminal MetaTrader 4 is their work with constantly updated information in real time. In MQL4 language, this peculiarity is reflected in the form of the three special functions: init(), start(), and deinit().
Special functions are functions with predefined names init(), start() and deinit(), each possessing its 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 a special function call from within 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 the calling conditions (and its own properties).
Special functions are called for execution by the client terminal.
Properties of special functions
Special init() function
The distinct property of the special init() function is its execution at the program initialization. If a program contains the description of the special init() function, it will be called (and executed) at the moment the program starts. If there is no special init() function in a program, no actions will be performed at the program's start.
In EAs, init() is called (and executed) after the client terminal start and uploading of historic data, after changing the security and/or the chart period, after program recompilation in MetaEditor, after changing any input parameters from the EA setup window, and after changing accounts.
In scripts, init() is also called (and executed) immediately after it is attached to a chart.
In custom indicators, init() is called (and executed) immediately after the client terminal start, after changing the security and/or the chart period, after program recompilation in MetaEditor, and after changing any input parameters from the custom indicator setup window.
Special start() function
The distinct properties of the special start() function differ depending on the executable program type.
In EAs, start() is called (and executed) immediately after a new tick comes. If a new tick has come during the execution of start(), this tick will be ignored, that is start() will not be called for execution when such a tick comes. All quotes received during the execution of start() will be ignored. The commencement of start() for execution is performed by the client terminal only, provided the previous operation session has been completed, the control has been returned to the client terminal, and start() is waiting for a new tick.
The possibility to call and execute start() is influenced by the state of "Enable/disable EAs" button. If this button is in the state of disabling EAs, the client terminal will not call for execution 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 start().
The client terminal does not call start() if the EA properties window is open. The EA properties window can be opened only when start() is waiting for a new tick. This window cannot be opened during the execution session of the EA's start() function.
In scripts, start() is called (and executed) only once, immediately after program initialization by init().
In custom indicators, 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), and 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 the security and/or chart period are changed, when an account is changed or a chart is closed, and as a result of client terminal's operation termination. If 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 start() continues its operation for more than the indicated time limit, it will be forcibly terminated by the client terminal.
Special deinit() function
The distinct function of the special deinit() function is its execution at program termination (deinitialization). If a program contains a description of deinit(), it will be called (and executed) at the program's shutdown. If a program does not contain deinit(), no actions will be performed at program shutdown.
The client terminal calls deinit() at the terminal shutdown, when a security window is closed, right before changing a security and/or changing a chart period, at a successful program recompilation in MetaEditor, when changing input parameters, and when an account is changed.
In EAs and scripts, program shutdown with the necessary call of deinit() may happen when attaching to a chart a new program of the same type that replaces the previous one.
In custom indicators, 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 deinit() function is executed longer, the client terminal will forcibly terminate the execution of the special deinit() function 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 descriptions 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 start() function.
Order of using special functions
Developers have presented you with a very convenient tool: when a program starts, init() is executed; after that, the main work is performed by start(); and, when a user finishes his work, deinit() will be started prior to the program shutdown.
The main code of a program must be contained in 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 a custom function is located in outside the descriptions of the special functions, but if a custom function is called for execution, a special function does not terminate its operation. It means control is passed to the custom function for some time, but the custom function itself operates within a special function that has called it. So, in the process of program execution, special functions operate always (in accordance with their own properties), and custom functions are executed when called by special functions.
If you are not going to use any of the special functions, you can choose not to use them in a program. In such a case, the client terminal will not call it. An absolutely normal program is one containing all three of the special functions. A program that does not have init() or deinit(), or either of these functions, is also considered normal.
If none of the special functions is used in a program, this program will not be executed. The client terminal calls only the special functions in accordance with their properties. Custom functions are not called by the client terminal. That is 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() from init() or to perform trade operations from init(), because during initialization values of information environment parameters may be not ready (information about charts, market prices and so on).
Program execution and Examples of implementation contain several practical examples that will help you see some properties of special functions.