myeatrade

MQL4 Reference

MQL4 Basics:Operations & Expressions

Some characters and character sequences are of a special importance. These are so-called operation symbols, for example:

+ - * / %     symbols of arithmetic operations
&& ||         symbols of logic operations
= += *=       symbols of assignment operations

Operation symbols are used in expressions and have sense when appopriate operands are given them

Punctuation marks are emphasized, as well. These are parentheses, braces, comma, colon, and semicolon.

Operation symbols, punctuation marks, spaces are used to separate language elements from each other.

Expressions

An expression consists of one or more operands and operation characters. An expression can be written in several lines.

Examples:

a++; b = 10;
x = (y * z) /
    (w + 2) + 127;

An expression that ends with a semicolon (;) is an operator.

Arithmetical operations

Arithmetical operations include additive and multiplicative operations:

Sum of values                           i = j + 2;
Difference of values                    i = j - 3;
Changing the operation sign             x = - x;
Product of values                       z = 3 * x;
Division quotient                       i = j / 5;
Division remainder                      minutes = time % 60;
Adding 1 to the variable value          i++;
Subtracting 1 from the variable value   k--;

The operations of adding/subtracting 1 (increment/decrement) cannot be used in expressions.

Examples:

int a=3;
a++;              // valid expression
int b=(a++)*3;    // invalid expression

Assignment operation

The value of the expression that includes the given operation is the value of the left operand after assignment.

Assigning the y value to the x variable                   y = x;

The following operations unite arithmetic or bitwise operations with operation of assignment:

Adding x to the y variable                               y += x;
Subtracting x from the y variable                        y -= x;
Multiplying the y variable by x                          y *= x;
Dividing the y variable by x                             y /= x;
Module x value of y                                      y %= x;
Logical shift of y representation to the right by x bit  y >>= x;
Logical shift of y representation to the left by x bit   y <<= x;
Bitwise operation AND                                    y &= x;
Bitwise operation OR                                     y |= x;
Bitwise operation exclusive OR
of x and y binary notations                              y ^= x;

There can be only one operation of assignment in an expression. Bitwise operations are performed with integer numbers only. The logical shift operation uses values of x less than 5 binary digits. The greater digits are rejected, so the shift is for the range of 0 to 31 bit. By %= operation (y value by module of x), the result sign is equal to the sign of divided number.

Operations of relation

The logical value FALSE is represented with an integer zero value, while the logical value TRUE is represented with any value differing from zero.

The value of expressions containing operations of relation or logical operations is 0 (FALSE) or 1 (TRUE).

True if a equals b                        a == b;
True if a does not equal b                a != b;
True if a is less than b                  a < b;
True if a is greater than b               a > b;
True if a is less than or equals b        a <= b;
True if a is greater than or equals b     a >= b;

Two unnormalized floating-point numbers cannot be linked by == or != operations. That is why it is necessary to subtract one from another, and the normalized outcome needs to be compared to null.

Boolean operations

Operand of logical negation (operation NOT displayed as exclamation mark) must be of arithmetic type. The result equals TRUE (1) if the operand value is FALSE (0); and it equals FALSE (0) if the operand differs from FALSE (0).

if(!a) Print("not 'a'");

Logical operation OR (||) of x and y values. The expression value is TRUE (1) if x or y value is true (not null). Otherwise, it is FALSE (0). Logical expressions are calculated completely, i.e., the so-called "short estimate" method does not apply to them.

if(x<0 || x>=max_bars) Print("out of range");

Logical operation AND (&&) of x and y values. The expression value is TRUE (1) if both x and y values are ture (not null). Otherwise, it is FALSE (0).

if(p!=x && p>y) Print("TRUE");

Bitwise operations

Complement of the variable value up to one. The value of the expression contains 1 in all digits where n contains 0, and it contains 0 in all digits where n contains 1.

b = ~n;

Binary-coded representation of x is shifted to the right by y digits. The right shift is a logical one, i.e., the freed left-side bits will be filled with zeros.

x = x >> y;

The binary-coded representation of x is shifted to the right by y digits, the freed digits on the right will be filled with zeroes.

x = x << y;

Bitwise operation AND of binary-coded x and y representations. The value of the expression contains 1 (TRUE) in all digits where both x and y do not contain zero; and it contains 0 (FALSE) in all other digits.

b = ((x & y) != 0);

Bitwise operation OR of binary-coded x and y representations. The expression value contains 1 in all digits where x or y is not equal to 0, and it contains 0 in all other digits.

b = x | y;

Bitwise operation EXCLUSIVE OR of binary-coded x and y representations. The expression value contains 1 in all digits where x and y have different binary values, and it contains 0 in all other digits.

b = x ^ y;

Bitwise operations are executed with integers only.

Other operations

Indexing

At addressing to the i-th element of array, the expression value is the value of the variable with serial number of i.

Example:

array[i] = 3; //Assign the value of 3 to the i-th element of the array.

Only an integer can be index of an array. Four-dimensional and below arrays are allowed. Each measurement is indexed from 0 to measurement size-1. In particular case, for a one-dimensional array consisting of 50 elements, the reference to the first element will look like array[0], that to the the last element will array[49].

At access beyond the array, the executing subsystem will generate the error of ERR_ARRAY_INDEX_OUT_OF_RANGE that can be got through the GetLastError() function.

Call for function with x1,x2,…,xn arguments.

Each argument can represent a constant, a variable, or an expression of the corresponding type. The arguments passed are separated by commas and must be inside of parentheses, the opening parenthesis to follow the name of the called function.

The expression value is the value returned by the function. If the returned value is of the void type, such function call cannot be placed to the right in the assignment operation. Please make sure that the expressions x1,x2,…,xn are executed exactly in this order.

Example:

double SL=Bid-25*Point;
int    ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,SL,Ask+25*Point,"My comment",123,0,Red);

Comma operation

Expressions separated by commas are executed from left to right. All side effects of the left expression calculation can appear before the right expression is calculated. The result type and value coincide with those of the right expression. The list of parameters to be passed (see above) can be considered as an example.

Example:

for(i=0,j=99; i<100; i++,j--) Print(array[i][j]);

Precedence rules

Each group of operations in the table has the same priority. The higher is the priority, the higher is the position of the group in the table. The precedence rules determine the grouping of operations and operands.

()     Function call                     From left to right
[]     Referencing to an array element
!      Logical negation                  From right to left
-      Sign changing operation
++     Increment
--     Decrement
~      Bitwise negation (complement)
&      Bitwise operation AND             From left to right
|      Bitwise operation OR
^      Bitwise operation exclusive OR
<<     Left shift
>>     Right shift
*      Multiplication                    From left to right
/      Division
%      Module division
+      Addition                          From left to right
-      Subtraction
<      Less than                         From left to right
<=     Less than or equal
>      Greater than
>=     Greater than or equal
==     Equal
!=     Not equal
||     Logical OR                        From left to right
&&     Logical AND                       From left to right
=      Assignment                        From right to left
+=     Assignment addition
-=     Assignment subtraction
*=     Assignment multiplication
/=     Assignment division
%=     Assignment module
>>=    Assignment right shift
<<=    Assignment left shift
&=     Assignment bitwise AND
|=     Assignment bitwise OR
^=     Assignment exclusive OR
,      Comma                             From left to right

Parentheses that have higher priority are applied to change the execution order of the operations.

Attention: Priority of performing operations in MQL4 differs to some extent from that conventional in the C language.

This article url: http://www.myeatrade.com/418/

Please login to post comment, if you haven't account you can register free