Compound operator
In the article «Usage of constants in expert advisors» we learnt to define constants and made it clear what the following lines mean in the code of our first expert advisor:
#define STATE_SQUARE 0
#define STATE_LONG 1
#define STATE_SHORT 2
In the article External variables we learnt how to set parameters of an expert advisor and the following lines became clear for us:
extern int MAPeriod = 13;
extern double LotsNumber = 1.0;
Besides local and static variables we considered global(at the level of one module) variables as well, and as the result we can understand two more lines of our expert:
int CurrentState;
int MyOrderTicket;
Now let’s consider in detail what the init() function does in our expert advisor.
int init()
{
//----
if (iMA(NULL, 0, MAPeriod, 0, MODE_EMA, PRICE_CLOSE, 0) > Close[0])
CurrentState = STATE_SHORT;
else CurrentState = STATE_LONG;
MyOrderTicket = 0;
//----
return(0);
}
As we know from the article «Functions init(), start() and deinit()» this function is run in the following cases:
- after it the expert advisor is attached to a chart;
- after MetaTrader 4 is launched and historical data are uploaded;
- after the instrument or the chart’s period is changed;
- after the program is recompiled in MetaEditor;
- after the expert advisor’s settings are changed;
- after the trading account is changed.
If we throw away comments which begin with double flash (//), we will see that this function consists of several commands (we will call them operators), separated by semicolon (;). When it is required to unite several operators in one well-connected block (for example in a function), we use a construction, which is called a compound operator. In other words we enclose these operators in braces { and }. After the closing brace { there mustn’t be a semicolon.
An example of a compound operator:
if (b==3)
{
b = b + 1;
Print (b);
}
Assignment operator. Arithmetical operations
Assignment operator format:
variable = expression
An array element also may act as the left part of an assignment operator. At first the expression is calculated which then is assigned to the variable.
An example of an assignment operator:
b = b + 1;
Let’s assume that before this operator execution the variable «b» was equal to 7. Then at first the expression in the right part ( 7+1 = 8 ) is calculated, and then the value of this expression (8) is assigned to the variable «b».
Several types of operations may be used in the expression. We will consider the simplest type — arithmetical operations:
| Operation | Example |
| Addition of two arguments | j + k |
| Subtraction of the second argument from the first one | j — k |
| Multiplication of arguments | j * k |
| To get quotient from the division of the first argument by the second one | j / k |
| To get residue of division of the first argument by the second one | j % k |
| The change the sign of the argument | -k |
Beyond expressions (in the form of a separate operator) the following operations can be used:
- Increasing of the argument by one: j++
- Decreasing of the argument by one: j-
Example:
j++; // true expression
k = (j++) + 3; // false expression
Next article: "
Relational operations and logic operations"