⚡ Operators in MQL
Operators in MQL ⚡
Programming work on the Logical solution of the given problem. when you trying to solve some problem, you need to perform some operations on the provided data like:-
- Mathematical Operations - Addition, Subtraction, Division, Multiplication
- Logical Operations - AND, OR, NOR, etc.
- Conditional Operations - less than (<), greater than (>), equal to (=)
For Example,
You want to place a Buy Order of "XAUUSD" in Your Demo Forex Trading Account if the ASK price is greater than $1745.23.
This can be written logically like this,
let, current price p = ASK; If p > 1745.23, place "BUY"
What is an Operator in MQL?
You can say that Operators are special symbols, which have a specific pre-defined meaning and use in the programming language. In MQL4, there are many types of Operators for easy performing of Forex Trading. which is quite similar to C and C++ programming languages.
Types of Operators in MQL
Assignment Operator in MQL
Assignment Operator is the most used and basic operator is used to assign value to variables. which is denoted by the equal (=) sign.
Source Code Example
int x = 10 ;
here, Equal Sign is the Assignment Operator which is used to assign value "10" to the variable "x" which is a type of Integer.
In MQL4, There are some other different forms also of Assignment Operator as mentioned below.
No | Operator | Use in Operation of | Example | Mathematical Expression |
---|---|---|---|---|
1. | += | Increase of the variable y by x | y += x | y = y + x |
2. | -= | Reduce the value of the variable y by x | y -= x | y = y - x |
3. | *= | Multiply the value of variable y by x | y *= x | y = y * x |
4. | /= | Divide the value of variable y by x | y /= x | y = y / x |
5. | %= | The residue of the division of the variable y by x | y %= x | y = y % x |
Source Code Examples
void OnStart() { // we are learning Assignment Operators int x = 2; int y = 10; y += x; Alert("Value of y = ", y); y -= x; Alert("Value of y = ", y); y *= x; Alert("Value of y = ", y); y /= x; Alert("Value of y = ", y); }
Output
In the above example, you get the output as mentioned above. Now I try to explain to you the reason for the output generated by the MQL Compiler.
- Initial value of x = 2 and y = 10
- Next, y += x means y = y + x, that is y = 10 + 2, hence y = 12 (see the last line of output, because Alert function shows the latest Message on Top and Oldest Message on bottom)
- Next, y -= x means y = y - x, that is y = 12 - 2 (*Attention Now the value of y is changed by the previous Instruction that's why I mentioned y as 12 and not as 10 ), hence y = 10.
- Next, y *= x means y = y * x, that is y = 10 * 2, hence y = 20
- Next y /= x means y = y / x, that is y = 20/2, hence y = 10.
Arithmetic Operators in MQL
Arithmetical Operation can be performed using the Following Arithmetic Operators in MQL.
No | Operator | Use in Operation of | Example | Mathematical Expression |
---|---|---|---|---|
1. | + | Addtion of the variable y and x | z = x + y | z = 2 + 3 |
2. | - | Subtraction of the value of variable y from x | z = x - y | z = 5 - 3 |
3. | * | Multiply the value of variable y with x | z = y * x | z = 5 * 3 |
4. | / | Divide the value of variable y with x | z = y / x | z = 20 / 5 |
5. | % | The remainder of the division of the variable y by x | z = y % x | z = 21 % 5 (Answer z = 1) |
6. | ++ | Increment the value of Variable by +1 | x++ | x = x + 1; Example: x = 10; x++; (then x = 11 when we use) |
7. | -- | Decrement of the value of the variable by 1 | x-- | x = x - 1; Example: x = 21; x--; (then x = 20 when we use) |
void OnStart()
{
// we are learning Arithmetic Operators
int z = 20;
z++;
Alert("Value of z = ", z);
z--;
Alert("Value of z = ", z);
int x = 10;
int y = 3;
z = x % y;
Alert("Value of z =", z);
}
In MQL, relational operators are used for comparison between two variables. The list of Available Relational Operators is as follows.
No | Operator | Use in Operation of | Example | Mathematical Expression |
---|---|---|---|---|
1. | == | True, if Two variables are Equal, otherwise False |
z = x == y | if x = 3 and y = 3 then z = True |
2. | != | True, if Two variables are not Equal otherwise False |
z = x != y | if x = 3 and y = 7 then z = True |
3. | > | True, if x is greater than y (x > y) otherwise False |
z = x > y | if x = 3 and y = 2 then z = True |
4. | < | True, if x is less than y (x < y) otherwise False |
z = x < y | if x = 3 and y = 23 then z = True |
5. | >= | True, if x is greater or equal to y (x >= y) otherwise False |
z = x >= y | if x = 13 and y =13 then z = True |
6. | <= | True, if x is less or equal to y (x <= y) otherwise False |
z = x <= y | if x = 13 and y =13 then z = True |
Logical Operators in MQL
No | Operator | Use in Operation of | Example | Mathematical Expression |
---|---|---|---|---|
1. | ! | NOT (Logical Negation) | !x | if x = True then "!x" is value is False, which means this change the value of the operator from True to False and False to True |
2. | && | AND (Logical Disjunction) | x > y && y > z |
Gives True, if both side conditions are True, otherwise False |
3. | || | OR (Logical Conjunction) | x > y || y > z |
Gives True, if anyone side condition is True, if both conditions are False then it gives False |
void OnStart()
{
// we are learning Arithmetic Operators
int x, y, z;
bool enable= False;
enable = !enable;
Alert("Enable = ", enable);
x = 10;
y = 20;
enable = x > y ;
z = x > y ;
Alert("Value of z =", z);
Alert("Value of Enable =", enable);
}
Output
if you use DataType "bool", which is used for storing the Boolean Values {True or False} then you can store the output of Logical Instruction in True or False.
Nonetheless,
For showing the difference, I have used both of the styles.
[ enable = x > y ] is giving output as "enable = False" because [ x =10 and y = 20] and
[ z = x > y ] is giving output as "z = 0" for[ x = 10 and y = 20]. because "z" is an Integer variable and 0 is work as a False in 1 works as a True, if you use Integer variable for logical operations instead of Boolean variable. Keep in mind.
We will discuss more examples of Relational and Logical Operators in the Tutorials of If-Else decision-making statements in Forex Trading Programming MQL.
Post a Comment