Understanding Operations and Expressions in MQL5
Watch on YouTube: Mastering Operations and Expressions in MQL5
In MQL5, operations and expressions are foundational tools for developing effective trading algorithms. Here’s a detailed breakdown of the key operation types:
Arithmetic Operations
These are basic mathematical calculations used to manipulate numerical data. Arithmetic operators include:
- Addition (+): Combines two values.
- Subtraction (-): Subtracts one value from another.
- Multiplication (*): Multiplies two values.
- Division (/): Divides one value by another.
Example:
void OnStart() {
int x = 10, y = 3;
int sum = x + y; // Result: 13
int product = x * y; // Result: 30
Print("Arithmetic Operations: Sum = ", sum, ", Product = ", product);
}Assignment Operations
These operators assign or modify variable values.
- Simple Assignment (=): Sets a value.
- Compound Assignments (+=, -=, *=, /=, %=): Perform an operation and update the variable in a single step.
Example:
void OnStart() {
int balance = 1000;
balance += 500; // Balance becomes 1500
balance -= 200; // Balance becomes 1300
Print("Updated Balance = ", balance);
}Operations of Relation
In MQL5, Boolean values are interpreted numerically: FALSE corresponds to zero, while any non-zero value represents TRUE. Expressions involving relational or logical operations return either FALSE (0) if the condition is not met, or TRUE (1) if the condition is satisfied. These operators compare values, often for decision-making in conditional statements.
- Equal (==): Checks if two values are the same.
- Not Equal (!=): Checks if two values are different.
- Greater Than (>), Less Than (<): Compares magnitudes.
- Greater or Equal (>=), Less or Equal (<=): Includes equality in comparisons.
Example:
void OnStart()
{
// Retrieve the current Ask price of the symbol using SymbolInfoDouble
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
// Check if the Ask price is greater than 1.2000
if (price > 1.2000)
// Print a message if the price is above 1.2000
Print("Price is above 1.2000");
else
// Print a message if the price is 1.2000 or below
Print("Price is 1.2000 or below");
}
Logical Operations (Boolean Operations)
These combine multiple conditions into a single expression.
- AND (&&): True if all conditions are true.
- OR (||): True if at least one condition is true.
- NOT (!): Reverses the truth value of a condition.
Example:
void OnStart()
{
// Declare two boolean variables and initialize them
bool condition1 = true, condition2 = false;
// Check if condition1 is true AND condition2 is false (!condition2 negates condition2)
if (condition1 && !condition2)
// If the conditions are met, print "Condition met"
Print("Condition met");
else
// If the conditions are not met, print "Condition not met"
Print("Condition not met");
}
Increment and Decrement Operations
Used to increase or decrease a variable’s value by 1.
- Increment (++): Adds 1.
- Decrement (–): Subtracts 1.
Example:
void OnStart() {
int count = 0;
count++; // Result: 1
count--; // Result: 0
Print("Final Count = ", count);
}Ternary Operation
A shorthand for conditional expressions, written as:
condition ? value_if_true : value_if_false
Example:
void OnStart() {
double spread = 2; // spread 2 pips
string message = (spread > 5) ? "Spread is high" : "Spread is low";
Print("Spread Check: ", message);
}Expressions
Expressions combine variables, constants, operators, and functions to compute values. Use parentheses to control precedence and ensure clarity.
Example:
void OnStart() {
int result = (5 + 3) * 2; // Result: 16
Print("Expression Result = ", result);
}Final Thoughts
Understanding and mastering these operations and expressions allow you to create robust and efficient trading scripts and Expert Advisors. They serve as the building blocks for decision-making, calculations, and logic in MQL5 programs. Keep practicing to enhance your coding skills!
