In the previous section we analyzed an example of special functions execution in a simple Expert Advisor simple.mq4. For better practice let's analyze some more modifications of this program.
Example of a correct program structure
As a rule, function descriptions are indicated in the same sequence as they are called for execution by the client terminal, namely first goes the description of the special function init(), then start() and the last one is deinit(). However, special functions are called for execution by the client terminal in accordance with their own properties, that is why the location of a description in a program does not matter. Let us change the order of descriptions and see the result (Expert Advisor possible.mq4).
Alert("New tick ",Count," Price = ",Price);// Alert
return; // exit start() }
//---int init() // Special funct. init() {
Alert ("Function init() triggered at start");// Alert
return; // Exit init() }
//---int deinit() // Special funct. deinit() {
Alert ("Function deinit() triggered at exit");// Alert
return; // Exit deinit() }
//---Starting this Expert Advisor you will see that the execution sequence of special functions in a program does not depend on the order of descriptions in a program. You may change the positions of function descriptions in a a source code and the result will be the same as in the execution of the Expert Advisor simple.mq4.
Example of incorrect program structure
But the program will behave in a different way if we change the head part position. In our example we will indicate start() earlier than the head part (Expert Advisri incorrect.mq4):
Alert ("New tick ",Count," Price = ",Price);// Alert
return; // Exit start()
{
Alert ("Function init() triggered at start");// Alert
return; // Exit init() }
//---int deinit() // Special funct. deinit() {
Alert ("Function deinit() triggered at exit");// Alert
return; // Exit deinit() }
//---When trying to compile this Expert Advisor, MetaEditor will show an error message:
Fig. 36. Error message during incorrect.mq4 program compilation.
In this case the line
int Count=0; // Global variable
is written outside all functions, but is not at the very beginning of a program, but somewhere in the middle of it.
The defining moment in the program structure is that the declaration of the global variable Count is done after function declaring (in our case - special function start()). In this section we will not discuss details of using global variables; types of variables and usage rules are described in the section Variables. It should be noted here that any global variable must be declared earlier (earlier in text) than the first call to it (in our case it is in the function start()).
In the analyzed program this rule was violated and the compiler displayed an error message.
Example of using a custom function
Now let's see how the program behaves in relation to custom functions. For this purpose let's upgrade the code described in the example of a simple Expert Advisor simple.mq4 and then analyze it. A program with a custom function will look like this (Expert Advisor
userfunction.mq4):
//---// userfunction.mq4
// To be used as an example in MQL4 book.
//---int Count=0; // Global variable //---int init() // Special funct. init() {
Alert ("Function init() triggered at start");// Alert
return; // Exit init() }
//---int start() // Special funct. start() {
double Price = Bid; // Local variable My_Function(); // Custom funct. call Alert("New tick ",Count," Price = ",Price);// Alert
return; // Exit start() }
//---int deinit() // Special funct. deinit() {
Alert ("Function deinit() triggered at exit");// Alert
return; // Exit deinit()
//---First of all let's see what has chaned and what has remained unchanged.
Unchanged parts:
Alert ("Function init() triggered at start");// Aler
return; // Exit init() }
3. Special function deinit() is unchanged.
int deinit() // Special funct. deinit() {
Alert("Function deinit() triggered at exit"); // Alert
return; // Exit deinit()
2. The code of the special function start() has also changed: now it contains the custom function call, but there is no Count variable calculation line now.
int start() // Special funct. start() {
double Price = Bid; // Local variable
My_Function(); // Custom function call
Alert("New tick ",Count," Price = ",Price);// Alert
return; // Exit start() }
In the section Program Execution we analyzed the order of init() and deinit() execution. In this example these functions will be executed the same way, so we will not dwell on their operation. Let's analyze the execution of the special function start() and the custom function My_Function(). The custom function description is located outside all special functions as it must be. The custom function call is indicated in start() code, which is also correct.
After init() is executed, the program will be executed so:
31.The special function start() is waiting to be started by the client terminal. When a new tick comes, the terminal will start this function for execution. As a result the following actions will be performed:
32 (1). In the line
double Price = Bid; // Local variable
the same actions are performed:
32.1(1). Local variable Price is initialized (see Types of Variables). Value of this local variable will be available from any part of the special function start().
32.2(1). Assignment operator is executed. The last available Bid price will be assigned to the variable Price (for example at the first tick it is equal to 1.2744).
33(1). Next comes My_Function() call:
My_Function(); // Custom function call
This line will be executed within the start() operation. The result of this code part
implementation (custom function call) is passing control to the function body (description) with further returning it to the call place.
34(1). There is only one operator in the custom function description:
Count++;
At the first custom function call Count is equal to zero. As the result of Count++ operator execution the value of Count will be increased by one. Having executed this operator (the only and the last one) the custom function finishes its operation and returns control to the place, from which it has been called.
It should be noted here that custom functions may be called only from special functions (or from other custom functions that are called from special functions). That is why the following statement is correct: at any current moment one of the special functions is operating (or start() is waiting for a new tick to be then started by the client terminal) and custom functions are executed only inside special functions.
In this case control is returned to the special function start() that is being executed, i.e. to the line following the function call operator:
35(1). This line contains Alert() call:
Alert ("New tick ",Count," Price = ",Price);// Alert
The function Alert() will show in a window all constant and variable enumerated in brackets:
New tick 1 Price = 1.2744 36(1). Operator
return; // Exit start()
finishes start() operation.
37. Control is passed to the client terminal waiting for a new tick.
At further start() executions variables will get new values and messages by Alert() will be shown, i.e. the program will perform points 32 - 36. At each start() execution (at each tick) call to the custom function My_Function will be performed and this function will be
executed. The execution of start() will continue until a user decides to terminate the program operation. In this case the special function deinit() will be executed and the program will stop operating.
The program userfunction.ех4 started for execution will show a window containing messages by Alert(). Note, the result of the program operation will be the same as the result of a simple Expert Advisor simple.mq4 operation. It is clear that userfunction.mq4 structure is made up in accordance with a usual order of functional blocks location. If another acceptable order is used, the result will be the same.
Operators
This section deals with the rules of formatting and execution of operators used in MQL4.
Each section includes simple examples that demonstrate the execution of operators. To digest the material in full, it is recommended to compile and launch for execution all exemplary programs. This will also help you to consolidate skills in working with MetaEditor.
• Assignment Operator.
This is the simplest and the most intuitive operator. We all know the as-signment operation from school maths: The name of a variable is located to the left of the equality sign, the value to be assigned to it is to the right of the equality sign.
• Conditional Operator "if-else" .
It is often necessary to guide the program in one or another direction re-garding certain conditions. In these cases, the operator "if-else" is very helpful.
• Cycle Operator "while".
The processing of large one-type data arrays usually requires multiple re-petitions of the same operations. You can organize a loop of such opera-tions in the cycle operator "while". Each one execution of operaopera-tions in a cycle is called iteration.
• Cycle Operator "for" .
The operator "for" is also a cycle operator. However, unlike the operator
"while", we usually specify in it the initial and the final value of a certain condition for execution of iterations.
• Operator "break" .
If you want to interrupt the working of a cycle operator without execution of the resting iterations, you need the operator "break". It is used only in the operators "while", "for", "switch", nowhere else.
• Operator "continue".
One more very helpful operator - the operator of going to the next itera-tion within a cycle. It allows the program to skip all the resting operators in the current iteration and go to the next one.
• Operator "switch" .
This operator is a "toggle" that allows the program to choose one of many possible alternatives. For each alternative, its predefined constant is de-scribed that is the case for this alternative.
• Function Call .
We understand under function call that the function to be called will ex-ecute some operations. The function may return a value of the predefined type. The amount of parameters passed into the function may not exceed 64.
• Function Description and Operator "return" .
Before to call a user-defined function, you should describe it first. The function description is specifying its type, name and the list of parameters.
Besides, the body of the function contains executable operators. The work of a function is ended in execution of the operator "return".