In this article you will learn the fundamental concept of MQL5 Operators and what they allow you to do in your MT5 custom indicators and Expert Advisors.
What Are MQL5 Operators?
Operators are essential components in any programming language, and MQL5 is no different.
They enable the manipulation of data, control program flow, and make calculations possible.
Understanding MQL5 operators and how to use them efficiently is crucial for developing robust trading algorithms.
A MQL5 Operator is a symbol or keyword that performs an operation on one or more operands. Operands are the data items the operator acts upon, such as variables, constants, or expressions.
If you think about a program, whether it is a script, an indicator, or an expert advisor, this is a sequence of actions. These actions are in their most basic form a sequence of operators acting on data. So you may think of a program like a sequence of operators.
You can find other information about operators in the official documentation.
Categories of MQL5 Operators
MQL5 provides a wide range of operators, each with its own function and use case.
To make the learning easier, in MQL5, and in all languages, you can group operators in categories.
Categories also indicate what type of function or purpose the operators have.
We will cover here the most popular and important operators, the ones that you will likely use even in basic tools.
Note that there are more niche operators for specific functions that will be omitted as not frequently used.
Arithmetic Operators
These operators perform mathematical calculations on data.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
|
1 2 3 |
int a = 10, b = 3; int sum = a + b; Print("Sum: ", sum); |
Assignment Operators
Assignment operators assign values to variables or constants.
| Operator | Description | Example |
|---|---|---|
= | Assign | x = 5 |
+= | Add and assign | x += 2 |
-= | Subtract and assign | x -= 3 |
*= | Multiply and assign | x *= 4 |
/= | Divide and assign | x /= 2 |
Example:
|
1 2 3 4 5 6 7 |
int x = 5; //at this stage x has a value of 5 //with += we are taking x, adding 3 and assign the new value back to x x += 3; //at this stage x has a value of 5 + 3 = 8 Print("Updated x: ", x); //this will print "Updated x: 8" |
Comparison Operators
These operators compare two values and return a boolean result based on the result of the comparison.
Comparison operators are often used with conditional or loop operators.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater or equal | a >= b |
<= | Less or equal | a <= b |
Example:
|
1 2 3 4 |
if (a > b) { Print("a is greater than b"); } |
Logical Operators
Logical operators evaluate boolean expressions.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a > 5 && b < 10 |
| || | Logical OR | a > 5 || b < 10 |
! | Logical NOT | !isActive |
Example:
|
1 2 3 4 |
bool isActive = true; if (!isActive) { Print("Not active"); } |
Increment and Decrement Operators
These operators increase or decrease a variable’s value by one.
| Operator | Description | Example |
|---|---|---|
++ | Increment | x++ |
-- | Decrement | x-- |
Example:
|
1 2 3 4 5 6 7 |
int counter = 0; //counter has a value of 0 at this point //++ will take counter and increment its value by 1 counter++; //counter has a value of 1 at this point Print("Counter: ", counter); //it will print "Counter: 1" |
Conditional Operators
Conditional operators check the status of a comparison and logical operator to verify if a condition is met.
They are very important in the design of custom tools.
| Operator | Description | Example |
|---|---|---|
if-else | If | if (a > b) |
?: | Ternary | x = (a > b) ? a : b; |
Example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//if else operator //verify the condition if true is equal to false, if so enter the condition if(true==false){ Print("Never"); } //if the condition in the if isn't met, then do this else{ Print("Always"); } //ternary operator //if the condition "a>b" is met then assign a to max, otherwise assign b int max = (a > b) ? a : b; Print("Max: ", max); |
Loop Operators
We will have another article dedicated to loops, but in short, loop operators allow the repetition of a set of actions.
| Operator | Description | Example |
|---|---|---|
for | repeat actions a number of times | for(initialize; condition; increment) |
while | repeat actions until a condition is met | while(condition) |
Example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//for operator repeat actions a number of times //for i starting from zero until it's less than 10, execute code, increment i by i at each execution for(int i=0;i<10;i++){ //prints the value of i at each run Print(i); } //while operator //execute code as long as IsActive is true while(IsActive==true){ //prints "Active is true" //Print(IsActive); } |
Operator Precedence in MQL5
MQL5 operators follow a specific order of execution.
Arithmetic operators generally have higher precedence than comparison or logical operators.
Using parentheses can help control the evaluation order.
Example:
|
1 2 |
int result = (a + b) * c; Print("Result: ", result); |
When to Use MQL5 Operators
- Arithmetic operators for calculations.
- Assignment operators for setting and updating values.
- Comparison operators for decision making.
- Logical operators for combining conditions.
- Conditional operators to select the next step based on a condition.
- Loop operators to execute a set of actions repeatedly
- Increment and decrement operators for counters and loops.
Conclusion
MQL5 operators are the building blocks for expressions and program logic.
By mastering their use and understanding when to apply each type, you’ll write more efficient, readable, and powerful trading scripts.
Make sure to experiment with these operators and integrate them into your MQL5 development toolkit.
For questions and support please Contact Us.