Data Types in MQL4: What Nobody Tells You About Type Conversions
Camovia Tray Team · 2026-09-10
The Mistake That Cost a Trader 50 Pips
A trader I know spent an entire afternoon debugging his Expert Advisor. The logic was flawless. The entry conditions were perfect. Yet his EA kept opening trades at the wrong price—off by exactly one pip. He checked his indicators, his risk management, his broker's spreads. Nothing.
The culprit? An implicit type conversion he didn't even know existed.
double entryPrice = Ask - (StopLoss * Point);
int stopLossPoints = (int)((entryPrice - StopLoss) / Point); The code looked fine. But the compiler was quietly converting a double to an int in a way that truncated the fractional part—and with it, the precision he needed for accurate order placement.
This is the problem with MQL4 data types. They seem straightforward on the surface, but the hidden rules of type conversion and strictness can quietly undermine your code if you don't know what to watch for.
The Real Problem: MQL4 Is Forgiving, And That's Dangerous
By default, MQL4 doesn't enforce strict variable declaration or type checking . This means you can write code that compiles successfully but behaves unpredictably—like the trader who lost pips because he didn't notice a conversion that was silently truncating his price calculations.
The compiler might warn you about "possible loss of data due to type conversion," but it won't stop you . It assumes you know what you're doing. And if you're like most traders, you're focused on strategy logic, not type system nuances.
Here's what's actually happening under the hood:
- When you convert a
doubleto anint, the fractional part is deleted—not rounded - When you store a value that exceeds a type's range, it gets truncated silently
- When you mix types in expressions, MQL4 promotes them to the "highest" type in a priority hierarchy
None of this raises an error. But all of it can introduce subtle bugs that are extremely hard to trace.
The Overlooked Tool: #property strict
The first line of defense is a compiler directive most MQL4 developers ignore:
#property strict With strict mode enabled, undeclared variables become compile-time errors, and many implicit type conversions that could lead to data loss are flagged . This is the difference between catching a bug before it costs you money versus discovering it after a losing trade.
Without #property strict, MQL4 might implicitly declare variables and silently convert between types without warning . In practice, this means a simple typo in a variable name could compile successfully and introduce unintended behavior.
The Type Conversion Trap Most Developers Walk Into
The most common hidden conversion trap involves the MarketInfo() function. It returns a double, but NormalizeDouble() expects an int for its second parameter .
// This will trigger a compiler warning about potential data loss
NormalizeDouble(price, MarketInfo(Symbol(), MODE_DIGITS)); The fix is simple but easy to overlook:
// Explicitly cast to int
NormalizeDouble(price, (int)MarketInfo(Symbol(), MODE_DIGITS)); The warning is the compiler telling you "I'm going to convert this for you, but it might not do what you expect." Casting explicitly doesn't change the conversion—it simply acknowledges that you're aware of it .
Why This Matters For Your Trading System
If you're managing an MT4 terminal, the data integrity of your code has a direct impact on your trading outcomes. Price calculations, position sizing, stop-loss placement—all of it depends on MQL4 handling data types correctly.
This is part of why tools that complement MT4, like the Camovia Tray app, focus on clean data handling. Camovia Tray reads live quotes and position data directly from your MT4 or MT5 terminal, displaying them in the system tray without any data leaving your machine [product_knowledge]. It doesn't interfere with your EA's logic, but it can help you verify what's actually happening—checking real-time prices and open positions visually, independent of your code's calculations.
Safer Type Handling: Rules Worth Remembering
Here are the practical rules that would have saved that trader 50 pips:
Rule 1: Always use #property strict at the top of your code. It doesn't solve all problems, but it catches the most common ones.
Rule 2: When converting between numeric types, use explicit casting. This doesn't change the behavior, but it makes your intention clear and silences the warnings you might otherwise ignore.
Rule 3: Never assume a conversion does what you intuitively expect. (int)1.999 yields 1, not 2. Use MathRound() if you need rounding .
Rule 4: For string concatenation with numbers, use IntegerToString() or DoubleToString(). Implicit conversions from number to string can hide logic errors and generate compiler warnings .
Rule 5: Pay attention to compiler warnings. They're not errors, but they're pointing out places where the compiler is making assumptions that might not match your intentions.
The Bottom Line
MQL4's type system is designed to be flexible, but that flexibility comes at a cost. The language assumes you're paying attention. Most of the time, you're focused on strategy logic, not type conversions.
The best practice is to make your type handling explicit, enable strict mode, and treat warnings as signals worth investigating. Your trading code handles real money—it deserves the same precision you'd want from any professional financial application.
MT4/MT5 Tray Assistant
Silent tracking, one-click close - check quotes and manage positions right from the tray.
Download Camovia TrayFrequently Asked Questions
Can the tray icon be disguised?
Yes. The tray icon can disguise itself as a cloud drive or a common system tool, so market watching stays low-key.
What is the tray lock feature?
Lock the tray function instantly at key moments to protect your privacy and prevent private information from leaking.
What information can I monitor?
Quotes and open positions: symbol, direction, open time, current P&L, and more - all visible in the popup positions tab. Click an order to close it.
Which languages are supported?
Chinese and English, switchable on both the website and the client.
Online Tools
Popular Symbols Trading Hours
Open/close times, weekend hours & live market status:
Related Articles
MT5 Not Connecting? Here’s the Fix
Fix MT5 not connecting—build mismatches, server outages, VPN and firewall blocks—with the checks that resolve most cases fast.
Why Forex Leverage is a Double-Edged Sword – And How to Manage It Better
Leverage in forex can double your profits or wipe out your account. Learn how to manage it effectively, and discover how Camovia Tray lets you monitor open positions, view live quotes, and close trades right from your system tray—without opening MT4 or MT5.
How to Get Candlestick and Bar Prices in MQL4
Learn how to get candlestick and bar prices in MQL4 using arrays and i-functions. Camovia Tray helps you watch live quotes and manage positions from your system tray.
MT5 XAUUSD Open Time: Set by Your Broker, Not MT5
Trading hours on MT5 are not set by MT5 - they follow your broker and CME gold futures. Right-click XAUUSD in Market Watch for your real session. Camovia Tray.
MT5 Windows Version: Why Desktop Still Dominates Trading
Discover how Camovia Tray turns your MT5 Windows version into a tray tool for quick quotes, position management, and one-click close—keeping data local.
More Articles
How Much Can You Make a Day Trading Crypto? A Realistic Look at the Numbers
How much can you make a day trading crypto? See realistic daily earnings by account size, the costs that shrink profits, and how Camovia Tray helps you manage MT5/MT4 positions from the system tray.
How to Take Screenshots in MetaTrader 4 (Quick Ways)
Capture MT4 charts with the built-in screenshot tool or Windows shortcuts—what each method is good for, and how to keep your desktop clean.
3 MT4 Settings That Are Secretly Hurting Your Trading (And How to Fix Them)
Transform your MT4 with smarter settings. Check quotes & manage positions from the system tray using Camovia Tray—keep your workflow focused and private.
Market Data Tools: Why "More Data" Doesn’t Mean Better Trading
Discover how Camovia Tray fits into the market data tools landscape by turning MT4/MT5 into a tray tool for live quotes and position management—without the clutter.
The Best 0DTE Options Strategy Isn't a Secret—It's a System You Can Actually Run
Discover the best 0DTE options strategy with real execution discipline. Camovia Tray turns MT5/MT4 into a tray tool for quick position checks and one-click exits.