Understanding Operations and Expressions in MQL4

Watch on YouTube: MQL4 Operations & Expressions: Essential Tools for Trading Algorithms

In MQL4, operations and expressions are fundamental building blocks for creating trading algorithms. They allow you to manipulate data, perform calculations, and control the logic flow of your code. Here’s a breakdown of key operation types in MQL4 with examples and explanations:

Arithmetic Operations

Arithmetic operators perform basic mathematical functions. These are commonly used for calculations in trading strategies, such as position sizing, price computations, and indicator adjustments.

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
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

Assignment operators assign or update values in variables. These operators are essential in managing variables that track trading statistics or indicator values over time.

  • Simple Assignment (=): Assigns a value to a variable.
  • Compound Assignments (+=, -=, *=, /=): Performs an operation and assigns the result to the variable.
void OnStart() {
    int balance = 1000;
    balance += 500;  // Balance becomes 1500
    balance -= 200;  // Balance becomes 1300
    Print("Updated Balance = ", balance);
}

Comparison Operations

Comparison operators are used for logical tests, often within control structures like if statements or loops. They return a boolean (true or false) and help decide the flow of trading logic.

  • Equal (==)
  • Not Equal (!=)
  • Greater Than (>)
  • Less Than (<)
  • Greater Than or Equal To (>=)
  • Less Than or Equal To (<=)
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

Logical operators evaluate multiple conditions in a single expression, useful for checking complex trade criteria or conditions for order execution.

  • Logical AND (&&) – True if both conditions are true.
  • Logical OR (||) – True if at least one condition is true.
  • Logical NOT (!) – Reverses the truth value.
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

Increment (++) and decrement (- -) operators increase or decrease a variable’s value by one. They’re typically used in loops or counters, particularly for repetitive calculations in indicator or EA development.

void OnStart() {
    int count = 0;
    count++;  // Result: 1
    count--;  // Result: 0
    Print("Final Count = ", count);
}

Ternary Operation

The ternary operator (? :) provides a shorthand for simple if-else expressions. It can be used to assign values based on a condition and is ideal for concise logic in variable assignments.

void OnStart() {
    double spread = 2; // spread 2 pips
    string message = (spread > 5) ? "Spread is high" : "Spread is low";
    Print("Spread Check: ", message);
}

Expressions in MQL4

An expression is a combination of variables, constants, operators, and function calls that produce a single value. Expressions are evaluated in a specific order, and you can use parentheses () to control the order of operations (precedence).

For example:

void OnStart() {
    int result = (5 + 3) * 2;  // Result: 16
    Print("Expression Result = ", result);
}

Putting It All Together

To summarize, MQL4’s operators and expressions offer diverse functionality for performing arithmetic, making comparisons, and controlling logic in code. By mastering these, you can develop more precise and efficient trading scripts and EAs.

Next: Operators

Previous: Variables