MQL5 Variables, Types & Operators

Data types, variable declarations, arithmetic and comparison operators in MQL5.

Data Types in MQL5

MQL5 is a strongly typed language — every variable must have a declared type. The most important types for trading:

Integer types:

int    count = 10;        // 32-bit signed integer (-2B to +2B)
long   ticket = 12345678; // 64-bit signed integer (for order tickets)
bool   isActive = true;   // true or false
char   grade = 'A';       // single character

Floating-point types:

double price = 1.23456;   // 64-bit double precision (most common for prices)
float  ratio = 0.5f;     // 32-bit single precision (rarely used)

String type:

string symbol = "EURUSD";
string message = "Trade opened at " + DoubleToString(price, 5);

Datetime type:

datetime now = TimeCurrent();           // current server time
datetime specific = D'2026.03.07 12:00'; // date literal

Color type:

color lineColor = clrRed;
color custom = C'255,128,0';  // RGB notation
💡
Always use double for prices

In MQL5, all price data (Open, High, Low, Close, Ask, Bid) uses the double type. Never use float for prices — the reduced precision will cause calculation errors.

Variable Declaration and Scope

Variables can be declared at different levels:

// Global variable — accessible everywhere in the file
int globalCounter = 0;

void OnStart()
{
    // Local variable — only accessible inside this function
    double localPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    // Block scope — only accessible inside this if block
    if(localPrice > 1.0)
    {
        string msg = "Price is above 1.0";
        Print(msg);
    }
    // msg is NOT accessible here
}

Constants and Input Parameters

Constants are values that never change:

#define MAX_ORDERS 100      // preprocessor constant
const double PI = 3.14159;  // typed constant

Input parameters are special — they create user-configurable settings in the indicator/EA properties dialog:

input int    MAPeriod = 14;        // Moving Average Period
input double LotSize = 0.1;       // Trade Lot Size
input color  SignalColor = clrRed; // Signal Arrow Color

When a user attaches your indicator or EA to a chart, they see these parameters in a settings window and can adjust them without modifying code.

Arithmetic Operators

double a = 10.0, b = 3.0;
double sum        = a + b;    // 13.0
double difference = a - b;    // 7.0
double product    = a * b;    // 30.0
double quotient   = a / b;    // 3.333...
int    remainder  = 10 % 3;   // 1 (modulo — integers only)

int counter = 5;
counter++;    // increment: counter is now 6
counter--;    // decrement: counter is now 5
counter += 3; // compound assignment: counter is now 8

Comparison and Logical Operators

// Comparison — return true or false
bool isEqual    = (a == b);    // equal to
bool isNotEqual = (a != b);    // not equal to
bool isGreater  = (a > b);     // greater than
bool isLessEq   = (a <= b);    // less than or equal

// Logical — combine conditions
bool both  = (a > 5) && (b < 10);  // AND: both must be true
bool either = (a > 5) || (b > 10); // OR: at least one true
bool negate = !(a > 5);            // NOT: reverses the result
⚠️
Never compare doubles with ==

Floating-point arithmetic can produce tiny rounding errors. Instead of if(price == 1.23456), use: if(MathAbs(price - 1.23456) < _Point). The _Point variable holds the smallest price increment for the current symbol.

Control Flow

// if-else
if(rsiValue > 70)
{
    Print("Overbought");
}
else if(rsiValue < 30)
{
    Print("Oversold");
}
else
{
    Print("Neutral");
}

// for loop — iterate through price data
for(int i = 0; i < 100; i++)
{
    Print("Bar ", i, " Close: ", iClose(_Symbol, PERIOD_CURRENT, i));
}

// while loop
int attempts = 0;
while(attempts < 3)
{
    // try something
    attempts++;
}

// switch
switch(OrderType)
{
    case ORDER_TYPE_BUY:  Print("Buy order");  break;
    case ORDER_TYPE_SELL: Print("Sell order"); break;
    default: Print("Other order type"); break;
}

Arrays

Arrays are fundamental in MQL5 — indicator buffers, price data, and many built-in functions use arrays:

// Static array — fixed size
double prices[100];

// Dynamic array — resizable
double buffer[];
ArrayResize(buffer, 200);

// Array functions
ArraySetAsSeries(buffer, true);  // index 0 = most recent
int size = ArraySize(buffer);
ArrayFree(buffer);               // deallocate memory

Predefined Variables

MQL5 provides several predefined variables that are always available:

_Symbol    // current chart symbol (e.g., "EURUSD")
_Period    // current chart timeframe (e.g., PERIOD_H1)
_Point     // smallest price increment (e.g., 0.00001)
_Digits    // number of decimal places (e.g., 5)
_LastError // last error code (0 = no error)

Practical Example: Simple Price Analysis

void OnStart()
{
    // Get current prices
    double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    double spread = (ask - bid) / _Point;

    // Get yesterday's close
    double yesterdayClose = iClose(_Symbol, PERIOD_D1, 1);

    // Calculate change
    double change = bid - yesterdayClose;
    double changePct = (change / yesterdayClose) * 100;

    // Output
    Print("Symbol: ", _Symbol);
    Print("Bid: ", DoubleToString(bid, _Digits));
    Print("Ask: ", DoubleToString(ask, _Digits));
    Print("Spread: ", DoubleToString(spread, 1), " points");
    Print("Change from yesterday: ", DoubleToString(changePct, 2), "%");
}