Summary: Advanced guide to MQL4 data types and operators for EA developers. Covers integers, floats, strings, datetime, type conversion pitfalls, and arithmetic operators with production-tested examples.




Understanding MQL4 data types and operators is foundational for writing robust Expert Advisors. Incorrect type handling is a leading cause of unexpected EA behavior and calculation errors in live trading .

1. Core Data Types In MQL4

MQL4 provides several built-in data types for different kinds of data :

```cpp
// Integer types
int orderMagic = 12345; // Whole numbers, range: -2,147,483,648 to 2,147,483,647
datetime expirationTime = D'2025.12.31 23:59:59'; // Unix timestamp in seconds

// Floating-point types
double lotSize = 0.15; // Decimal numbers for volumes and prices
double stopLossDistance = 25.5; // Supports fractional values

// Boolean and string
bool isTradeAllowed = true; // true or false only
string orderComment = "EA Trade"; // Text data enclosed in double quotes

// Color type
color arrowColor = clrGreen; // Predefined color constants
```

2. Common Type Conversion Pitfalls

Implicit type conversion can cause subtle bugs. Always use explicit conversion functions :

```cpp
// PROBLEMATIC - integer division truncates
int a = 3, b = 2;
double result = a / b; // Returns 1.0, not 1.5! (integer division first)

// CORRECT - force floating-point division
double correctResult = (double)a / b; // Returns 1.5

// Safe conversion functions
int toInt = (int)1.9; // Returns 1 (truncation)
double toDouble = (double)5; // Returns 5.0
string numStr = IntegerToString(12345); // "12345"
string priceStr = DoubleToString(1.23456, 4); // "1.2346" (rounded to 4 decimals)
datetime fromString = StringToTime("2025.12.31 12:00"); // Converts date string
```

3. Arithmetic Operators In Practice

```cpp
// Complete arithmetic operations example
void DemonstrateArithmetic() {
double entryPrice = 1.10500;
double stopPoints = 25.0;
double takePoints = 50.0;
double pointValue = Point; // 0.00001 for 5-digit brokers

// Basic operations
double stopLoss = entryPrice - (stopPoints * pointValue);
double takeProfit = entryPrice + (takePoints * pointValue);

// Exponentiation using MathPow()
double squared = MathPow(2, 2); // 4.0
double squareRoot = MathSqrt(16); // 4.0

// Modulo operator (integer only)
int remainder = 17 % 5; // Returns 2
bool isEven = (remainder == 0);

Print("SL: ", DoubleToString(stopLoss, 5));
Print("TP: ", DoubleToString(takeProfit, 5));
}
```

4. Logical Operators For Trade Conditions

```cpp
// Trading condition with multiple logical operators
bool ShouldEnterLong() {
double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 1);
double maFast = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
double maSlow = iMA(NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1);

// Compound condition using AND (&&) and OR (||)
bool oversoldCondition = (rsi < 30);
bool trendCondition = (maFast > maSlow);
bool noOpenPosition = (OrdersTotal() == 0);

// Parentheses matter for operator precedence
return (oversoldCondition && trendCondition) ||
(rsi < 25 && noOpenPosition);
}

// Comparison operators in risk management
bool ValidateStopLoss(double slPrice, double currentPrice, bool isBuy) {
double minDistance = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;

if(isBuy) {
// Check if stop is too close using >= operator
return (currentPrice - slPrice) >= minDistance;
} else {
return (slPrice - currentPrice) >= minDistance;
}
}
```

5. String Operations For Order Management

```cpp
// String concatenation and manipulation
string GenerateOrderComment(string strategy, int signalId) {
string baseComment = "EA_" + strategy; // Concatenation with +
string fullComment = baseComment + "_sig" + IntegerToString(signalId);
return fullComment;
}

// Complete OrderSend with proper type handling
int SafeOrderSendWithTypes(double lot, double slPoints, double tpPoints) {
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double point = SymbolInfoDouble(Symbol(), SYMBOL_POINT);

// Convert points to price levels
double stopLoss = ask - (slPoints * point);
double takeProfit = ask + (tpPoints * point);

// Normalize prices to broker digits
int digits = (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);
stopLoss = NormalizeDouble(stopLoss, digits);
takeProfit = NormalizeDouble(takeProfit, digits);

int ticket = OrderSend(
Symbol(), // string
OP_BUY, // int (operation type)
lot, // double
ask, // double
3, // int (slippage)
stopLoss, // double
takeProfit, // double
"TypeSafeEA", // string
magicNumber, // int
0, // datetime (expiration)
clrNONE // color
);

if(ticket < 0) {
Print("OrderSend error: ", GetLastError());
}
return ticket;
}
```

6. Type Safety Best Practices

```cpp
// Input validation for external parameters
input double RiskPercent = 1.5; // Input as double
input int MaxSpread = 25; // Input as int

// Convert and validate in OnInit()
double normalizedRisk = MathMin(MathMax(RiskPercent, 0.1), 10.0);
int maxSpreadPoints = MaxSpread; // Use directly

// Always normalize prices before OrderSend
double NormalizePrice(double price, string symbol) {
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
return NormalizeDouble(price, digits);
}
```

Reference: MQL4 Documentation, “Language Basics” (docs.mql4.com/basis); MQL4 Tutorial, “Data Types and Operators” .