First of all it is useful to understand what function is in general. For example you in your program perform the same actions for many times. You may write the code which implements these actions in each place. But it is inconvenient and incorrect from several points of view :
- If you need to make changes in this part of the code you will have to make them everywhere where it is used. There is a high probability that you will forget to make changes in some place and the expert advisor will trade with mistakes.
- The code in which a separate function, called in corresponding places, is used for implementation of repeated actions is much more readable. In this case changes (in case of necessity) are made only in one place and readability of such a code is much better.
Thus at any moment you can describe your function which will implement specific actions:
type name ( parameters )
{
actions
}
A function can either return a value of one of the standard types (int, bool, datetime, double, color, string — see the description of types in the article «How to customize parameters of a new expert advisor»), or not to return any value (type void).
The name of the function is any easy-to-understand name which helps at once to remember what this function does. The name may consist of digits (0-9), Roman upper and lower case letters (а — z and А — Z, you should be remembered that letters ’a’ and ’A’ — are completely different characters), the underline character (_). The first symbol can’t be a digit. Also the name of the function mustn’t coincide with a reserved word (i. e. a word that has a special meaning in the MetaQuotes Language 4 — but we will speak about later).
An example of the name of a function: GetConnectionStatus.
Each function can be assigned certain parameters. Parameters are written separated by the comma character in parentheses after the name of the function. For each parameter its type is indicated.
We will give an example of the description of the function that calculates the smallest from two integer numbers:
int GetMinValue(int a1, int a2)
{
if (a1
This function returns the value of the type int (integer number), its name — GetMinValue and it has two input integer parameters: a1 and a2.
To return the value in the calling program the command return (value) is used.
If in some place of our expert advisor it is required to calculate the minimum number from two, then we can call our function in the following way: name (comma separated parameters). For example: GetMinValue(10, 15).
The init(), start() and deinit() functions
The init() function
- This function is called in the following cases:
- after the expert advisor is applied to the chart;
- after MetaTrader 4 is launched and historical data are uploaded;
- after the instrument or the period of the chart is changed;
- after the program is recompiled in MetaEditor;
- after the expert’s settings are changed;
- after the trading account is changed.
The start() function
The start() function is launched on each new tick. As a matter of fact this is the main function of the expert advisor, as it is called on each tick and implements the main work.
Note: If the start() function fails to complete its work before the next tick comes, the next tick is skipped and the function is not called for it.
The deinit() function
With the help of UninitializeReason() we can learn the reason why the deinit() function is called.
- The deinit() function is called in the following cases:
- when MetaTrader 4 completes its work or when the chart is closed (the UninitializeReason() function returns REASON_CHARTCLOSE);
- when the expert advisor is deleted from the chart (REASON_REMOVE);
- before the chart’s instrument or period is changed (REASON_CHARTCHANGE);
- when the program is successfully recompiled in MetaEditor (REASON_RECOMPILE)r;
- when the expert’s parameters are changed (REASON_PARAMETERS);
- in case of switching over to another account (REASON_ACCOUNT).
The UninitializeReason() function returns 0, if the script completes the work by itself .
If within 2.5 seconds the deinit() function doesn’t complete its work it is stopped forcedly.
Next article: "The source code of our first expert advisor"