Sections Program Execution and Examples of Implementation contain several practical examples that will help to see some properties of special functions
Page 1 of 5Program Execution - Program in MQL4 - MQL4 Tutorial
Programming skills are better developed if a programmer has a small operating program at his disposal. To understand the whole program, it is necessary to examine thoroughly all its components and trace its operation step-by-step. Please note, special function properties of different application programs (Expert Advisors, scripts, indicators) are different. Now we will analyze how an Expert Advisor operates.
Alert ("Function init() triggered at start");// Alert return; // Exit init() Alert("New tick ",Count," Price = ",Price);// Alert
return; // Exit start() }
//--- int deinit() // Spec. funct. deinit() {
Alert ("Function deinit() triggered at deinitialization"); // Alert return; // Exit deinit() program will start its execution. The program execution starts from the head part. The head part contains only one line:
int Count=0; // Global variable
In this line the global variable Count initialized by zero value is declared. (Local and global variables are analyzed in details in the section Types of Variables. It should be noted here, that algorithm used in this certain program requires declaration of the variable Count as global, that's why it cannot be declared inside a function, it must be declared outside functions description, i.e. in the head part; as a result the value of the global variable Count will be available from any program part.)
2. After the execution of the program head part, the special function init() will be started for execution. Please note, this function call is not contained in a program code. The start of init() execution when an EA is attached to a chart is the function's own property. The client terminal will call init() for execution just because the program code contains its description. In the analyzed program the description of the special function init() is the following:
int init() // Spec. funct. init() {
Alert ("Function init() triggered at start");// Alert return; // Exit init()
2.1 Function Alert() shows an alert window:
2.2 Operator return finishes the operation of the special function init()
As a result of init() execution, an alert will be written. In really used programs such an algorithm is very rare, because such init() usage is of little help. Really, no sense to use a function that informs a trader that it is being executed. Here the algorithm is used only for the visualization of init() execution. Pay attention: the special function init() is executed in a program only once. The function execution takes place at the beginning of program operation after a head part has been processed. When the operator return is executed in the special function init(), the program returns control to the client terminal.
3. The client terminal detected the description of the special function start() in the program:
int start() // Special funct. start() {
double Price = Bid; // Local variable Count++;
Alert("New tick ",Count," Price = ",Price);// Alert
return; // Exit start() }
31. Control is held by the client terminal. The client terminal waits for a new tick and does not start program functions until a new tick comes. It means, for some time the program does not operate, i.e. no actions are performed in it. A pause appears, though there is neither direct nor indirect prompting to perform this pause. The necessity to wait for a tick is the start() function's own property and there is no way of a program influence upon this property (for example, disabling it). The program will be waiting for the control until a new tick comes. When a new tick comes, the client terminal passes control to the program, namely to the special function start() (in this case in accordance with the EA's start() function property). As a result its execution starts.
32 (1). In the line
double Price = Bid; // Local variable
the following actions are performed:
32.1(1). Declaration of a local variable Price (see Types of Variables). Value of this local variables will be available from any part of the special function start().
32.2(1). Execution of the assignment operator. The current Bid price value will be assigned to the variable Price. New price value appears each time when a new tick comes (for example, at the first tick a security price can be equal to 1.2744).
33(1). Then the following line is executed:
Count++;
This not usual record is the full analog of Count=Count+1;
By the moment of passing control to this line the Count variable value is equal to zero. As a result of Count++ execution, the value of Count will be increased by one. So, by the moment of passing control to the next line, Count value will be equal to 1.
34(1). The next line contains Alert() function call:
Alert ("New tick ",Count," Price = ",Price);// Alert
The function will write all constants and variables enumerated in brackets.
At the first execution of the function start() the program will write New tick, then refer to the variable Count for getting its value (at first execution this value is 1), write this value, then will write Price = and refer to the variable Price for getting its value (in our example it is 1.2744) and writing it.
As a result the following line will be written:
35(1). Operator
Function init() triggered at start
New tick 1 Price = 1.2744
Page 2 of 5
Program Execution - Program in MQL4 - MQL4 Tutorial
return; // Exit start()
finishes the operation of the special function start().
36. Control is returned to the client terminal (until a new tick comes).
This is how start() function of an Expert Advisor is executed. When the execution is over, the special function start() returns control to the client terminal and when a new tick comes the client terminal starts its operation once again. This process (starting the execution of the function start() and returning control to the client terminal) can go on for a long time - several days or weeks.
During all this time the special function start() will be executed from time to time. Depending on environment parameters (new prices, time, trade conditions, etc.) in the special function start() different actions like opening or modifying orders can be performed.
37. From the moment of receipt of a new tick, actions of points 32-36 are repeated. However, only the sequence of executed operators is repeated, but variables get new values each time. Let's view differences between the first and the second execution of the special function start().
32 (2). In the line
double Price = Bid; // Local variable
the following actions are performed:
32.1(2). Declaration of a local variable Price (unchanged).
32.2(2). Execution of the Assignment operator. The current Bid price value will be assigned to the variable Price (new price value appears each time when a new tick comes, for example, at the second tick the security price will be equal to 1.2745) (there are changes).
33(2). Then the following line will be executed:
Count++;
At the moment prior to passing control to this line, the value of the variable Count (after the first execution of the function start()) is equal to 1. As a result of Count++ execution, Count value will be increased by one. Thus, at the second execution Count will be equal to 2 (changed).
34(2). Alert() function:
Alert ("New tick",Count," Price = ",Price);// Alert
writes all constants and variables (their new values) enumerated in brackets.
At the second start() execution the program will write New tick, then refer to Count variable for getting its value (in second execution it is equal to 2), write this value, then write Price = and refer to Price variable, get its value (in our example 1.2745) and write it (changed).
As a result the following line will be written:
35(2). Operator
return; // Exit из start()
finishes operation of start() (no changes).
36(2). Control is returned to the client terminal to wait for a new tick.
37(2). Then it is repeated again. In the third start() execution variables will get new values and will be written by the function Alert (), i.e. the program repeats points 32-36(3). And then again and again: 32 - 36(4), 32 - 36(5),..(6)..(7)..(8)... If a user does not take any actions, this process will be repeated endlessly. As a result of start() operation in this program we will see tick history of price change.
New tick 2 Price = 1.2745
Page 3 of 5
Program Execution - Program in MQL4 - MQL4 Tutorial
Next events will happen only when a user decides to terminate the program and forcibly detaches the program from a chart manually.
4. Client terminal passes control to the special function deinit() (in accordance with its properties).
int deinit() // Special funct. deinit() {
Alert ("Function deinit() triggered at exit"); // Alert return; // Exit deinit() }
There are only two operators in the function body.
41. Alert() will write:
42. Operator return finishes operation of deinit().
The function deinit() is started for execution by the client terminal only once, after that the above alert will appear in Alert() window and the program will be removed from a chart.
5. Here the story of Expert Advisor execution ends.
Attach this example program to any chart and start it. The operating program will display a window containing all alerts generated by the function Alert(). By the contentc of alerts it is easy to understand what special function is connected with this or that entry.
Fig. 35. Results of operation of the program simple.mq4.
From this example you can easily see that a program is executed in accordance with special functions properties described in Special Functions. Terminate the program and start it again. Having done this several times, you will get experience of using your first program. It will work both now and the next time. Further programs that you will write yourself will also be constructed in accordance with the described structure and for starting their execution you will also need to attach it to a chart.
Try to understand all concepts and rules and the process of creating programs in MQL4 will be easy and pleasant.
Function deinit() triggered at deinitialization
Page 4 of 5
Program Execution - Program in MQL4 - MQL4 Tutorial
Special Functions Examples of Implementation