RobotFX curates the best open-source MetaTrader code to inspire your trading automation.
Introduction
MQL4TradeCompat is a compatibility framework that recreates the classic MQL4 order-centric trading model on top of the native MQL5 trading architecture.
The library allows legacy MQL4-style code to continue using familiar functions such as:
OrderSelect() OrderSend() OrderModify() OrderClose() OrderDelete() OrdersTotal() OrdersHistoryTotal()
while internally translating all operations into native MQL5 Position, Order, Deal, and History APIs.
Watch in-depth trading tutorials and EA reviews on the RobotFX YouTube channel. Subscribe for free tips.
Design Goal
MQL4 is based on a single order model:
Open Orders Pending Orders History Orders
MQL5 introduced a more complex structure:
Positions Orders Deals History Orders
The primary goal of this framework is to hide this complexity and expose a unified MQL4-compatible interface.
Core Components
1. CMQL4TradeCompat
Role
Public compatibility facade.
Responsibilities
OrderSelect() OrderSend() OrderModify() OrderClose() OrderDelete() OrdersTotal() OrdersHistoryTotal()
Purpose
Provides the same programming experience as MQL4 while internally dispatching requests to MQL5 APIs.
2. CLiveTradeInfo
Role
Live Trade Repository.
Responsibilities
Unifies:
Open Positions Pending Orders
into a single logical collection.
Key Features
- Position aggregation
- Pending order aggregation
- Unified ticket selection
- MQL4-style indexing
- Snapshot-based cache
- Chronological sorting
3. CHistoryTradeInfo
Role
History Reconstruction Engine.
Responsibilities
Builds logical historical trade records from:
Deals Closed Positions Historical Orders
Key Features
- Position reconstruction from deal chains
- Closed-trade aggregation
- Pending-order history support
- Profit/commission/swap calculation
- Weighted close-price calculation
- Historical ticket tracking
Architecture Flow
Legacy MQL4 EA | v +----------------------+ | CMQL4TradeCompat | +----------+-----------+ | +-------+-------+ | | v v +--------+ +---------+ | Live | | History | | Repository | Engine | +----+---+ +----+----+ | | +------+------+ | v +----------------------+ | Native MQL5 API | +----------------------+ | CTrade | | CPositionInfo | | COrderInfo | | CDealInfo | | CHistoryOrderInfo | +----------------------+
MQL4 Compatibility Mapping
| MQL4 Function | Internal MQL5 Layer |
|---|---|
| OrderSelect | Live/History Repository |
| OrderSend | CTrade |
| OrderClose | PositionClose / PartialClose |
| OrderModify | PositionModify / OrderModify |
| OrderDelete | OrderDelete |
| OrdersTotal | Live Repository |
| OrdersHistoryTotal | History Repository |
Repository Strategy
Instead of directly coupling every API call to MQL5 objects, the framework introduces a repository layer.
Live Repository
Positions
+
Pending Orders
↓
Unified Trade Pool History Repository
Deals
+
Historical Orders
↓
Unified Historical Pool This design makes MQL4 emulation significantly cleaner and easier to maintain.
Testing Strategy
The framework includes a dedicated integration test suite.
Validated Operations
✅ Market Buy
✅ Pending Order Placement
✅ Order Selection
✅ Order Modification
✅ Position Modification
✅ Position Close
✅ Pending Order Deletion
✅ Live Repository Refresh
✅ History Repository Validation
Test Environment
Demo Account Real Trading Server Native MT5 Engine
The framework was validated through actual trade execution rather than mock testing.
Risk: The demo test is for testing purposes, is not for live trading.
- Output:
========== MQL4TradeCompat test started ========== Symbol: EURUSD | volume: 0.01 | digits: 5 PASS: Symbol market data is valid PASS: OrderSend OP_BUY PASS: OrderSelect opened position by ticket PASS: Initial SL/TP are zero PASS: OrderModify open position PASS: Modified Stop Loss verified PASS: Modified Take Profit verified PASS: OrderClose full position PASS: Closed position is no longer in MODE_TRADES PASS: OrderSend OP_BUYLIMIT PASS: OrderSelect pending order by ticket PASS: OrderModify pending order PASS: OrderDelete pending order PASS: Deleted pending order is no longer in MODE_TRADES ========== MQL4TradeCompat test summary ========== PASS: 14 | FAIL: 0 | SKIP: 0 RESULT: PASS ========== MQL4TradeCompat test finished ==========
Technical Summary
MQL4TradeCompat is not merely a wrapper library. It is a lightweight MQL4 runtime emulation layer that reconstructs the classical MQL4 trading environment on top of the MQL5 execution model. Through dedicated live and historical repositories, the framework provides a unified order-centric view that simplifies migration of legacy Expert Advisors while preserving native MQL5 execution capabilities.

Level up your trading with professional RobotFX expert advisors and indicators. Visit robotfx.org for proven MT4/MT5 tools.
78313