Summary
Operators and expressions are fundamental elements in MQL4 used for performing calculations, assignments, and logical evaluations. Arithmetic operators such as addition, subtraction, multiplication, and division enable mathematical operations with variables. Comparison operators like "==" or ">" are used to evaluate conditions, whose result is either "true" or "false". Logical operators like "&&" (and) and "||" (or) connect these conditions for more complex evaluations. Assignment operators, on the other hand, are used to assign values to variables or alter their value through mathematical operations.
Top 3 Key Takeaways:
- Arithmetic Operators: These operators, such as addition (+), subtraction (-), multiplication (*), division (/), and modulo (%), are used to perform basic mathematical operations in MQL4.
- Comparison Operators: Comparison operators like ==, !=, >, <, >=, and <= yield logical values (true or false) and are essential for control structures and conditional statements.
- Logical Operators: These operators (&&, ||, !) enable the combination of multiple comparison operators to formulate complex conditions used in decision-making within trading systems.
Table of contents
Click for open/close
Arithmetic Operators
Arithmetic operators are used to perform mathematical functions, such as addition, subtraction, multiplication, and division, on numbers. Each mathematical function is defined by specific symbols. The following symbols represent their respective mathematical functions:
- The addition symbol "+"
- The subtraction symbol "-"
- The multiplication symbol "*"
- The division symbol "/"
- The modulo division symbol "%"
Example of Addition
MQL Code
int Value1 = 6;
int Value2 = 8;
int Result = Value1 + Value2;
The variable "Value1" is assigned the number 6, and the variable "Value2" is assigned the number 8. The addition result of both variables is stored in the variable "Result". The variable "Result" holds the value 14 (8 plus 6).
Example of Subtraction
MQL Code
int Value1 = 12;
int Value2 = 4;
int Result = Value1 - Value2;
The variable "Value1" is assigned the number 12, and the variable "Value2" is assigned the number 4. The subtraction result of both variables is stored in the variable "Result". The variable "Result" holds the value 8 (12 minus 4).
Example of Multiplication
MQL Code
int Value1 = 3;
int Value2 = 5;
int Result = Value1 * Value2;
The variable "Value1" is assigned the number 3, and the variable "Value2" is assigned the number 5. The multiplication result of both variables is stored in the variable "Result". The variable "Result" holds the value 15 (3 times 5).
Example of Division
MQL Code
int Value1 = 24;
int Value2 = 4;
int Result = Value1 / Value2;
The variable "Value1" is assigned the number 24, and the variable "Value2" is assigned the number 4. The division result of both variables is stored in the variable "Result". The variable "Result" holds the value 6 (24 divided by 4).
Example of Modulo Division
MQL Code
int Value1 = 13;
int Value2 = 2;
int Result = Value1 % Value2;
The variable "Value1" is assigned the number 13, and the variable "Value2" is assigned the number 2. The modulo result from the division is stored in the variable "Result". The variable "Result" holds the value 1 (13 divided by 2 equals 6 with a remainder of 1). The modulo operator determines the remainder of a division when a whole number result is obtained.
Assignment Operator
The assignment operator, the equals sign, is used to assign a new value to a variable.
Example
MQL Code
int X = 6;
int Y = 8;
int X = Y;
The variable "X" is assigned the number 6, and the variable "Y" is assigned the number 8. In the third line, the content of variable Y is assigned to variable X. X now contains the value 6.
Assignment Operator Combined with Arithmetic Operators
MQL Code
X += Y; // The variable X is increased by the value of Y.
X -= Y; // The variable X is decreased by the value of Y.
X *= Y; // The variable X is multiplied by the value of Y.
X /= Y; // The variable X is divided by the value of Y.
Comparison Operators
The result of comparison operators is always either "true" or "false". The following comparison operators are available in MQL4:
- X == Y is true if X is equal to Y.
- X <= Y is true if X is less than or equal to Y.
- X != Y is true if X is not equal to Y.
- X >= Y is true if X is greater than or equal to Y.
- X < Y is true if X is less than Y.
- X > Y is true if X is greater than Y.
Example
MQL Code
int Var1 = 6;
int Var2 = 8;
if(Var1 > Var2){
// do something
}
In this example, the code within the curly brackets is executed if the value of the variable "Var1" is greater than the value of the variable "Var2".
Logical Operators
Logical operators are used in MQL4 to combine comparison operators. For instance, if the question is whether the variable "Var1" is greater than the variable "Var2" AND the variable "Var3" is greater than the variable "Var2", comparison operators need to be combined. Logical operators are used for this. The following logical operators are available in MQL4:
- The logical negation NOT "!"
- The logical OR "||"
- The logical AND "&&"
Example
MQL Code
!X; // is true if X is false.
X==1 || Y==2; // is true if X equals 1 OR Y equals 2.
X==1 && Y==2; // is true if X equals 1 AND Y equals 2.
int Var1 = 15;
int Var2 = 3;
int Var3 = 7;
if(Var1 > Var2 && Var3 > Var2){
// do something
}
It checks if variable "Var1" is greater than the variable "Var2" AND if variable "Var3" is greater than the variable "Var2".
Questions and Answers
In MQL4, the following arithmetic operators are available: addition (+), subtraction (-), multiplication (*), division (/), and modulo (%), to perform mathematical calculations.
Comparison operators (==, !=, >, <) compare two values and return either "true" or "false". Logical operators (&&, ||, !) combine multiple comparison operators to formulate more complex conditions.
The modulo operator (%) returns the remainder of an integer division. For example, 13 % 2 returns 1, as 13 divided by 2 leaves a remainder of 1.