MetaTrader 4 version
MetaPortAPI for MT4 (MetaPortAPI.ex4) speaks the same protocol as the MT5 version: the same REST paths, websocket streams, legacy TCP commands, request parameters and reply tags. Code written for one works with the other, except for the few differences below, which come from what MetaTrader 4 itself offers.
Install
Section titled “Install”- In MetaTrader 4 open File → Open Data Folder.
- Copy
MetaPortAPI.ex4intoMQL4\Expertsand restart MetaTrader 4 (or right-click Expert Advisors → Refresh in the Navigator). - Drag MetaPortAPI onto a chart. On the Common tab tick Allow live trading and Allow DLL imports.
- The Experts tab shows
listening on TCP 81. Tryhttp://127.0.0.1:81/v1/quote?symbol=EURUSD.
For live accounts put your license file in MQL4\Files\MetaPortAPI.lic.
Differences from MT5
Section titled “Differences from MT5”| Area | MetaTrader 4 |
|---|---|
TICK_HISTORY, CALENDAR_LIST, TRACK_MBOOK |
Not available — MT4 has no tick history, economic calendar or market depth. They return ERROR_ID 4014. |
QUOTE |
MT4 ticks have no milliseconds: TIME ends in .0. FLAGS is always 6. |
| Timeframes | The nine MT4 timeframes: PERIOD_M1, M5, M15, M30, H1, H4, D1, W1, MN1. |
ORDER_SEND |
DEAL and ORDER both hold the new order ticket. TYPE_FILLING and ASYNC are ignored. On brokers that refuse SL/TP on market orders, the order is opened first and SL/TP are set straight after. |
ORDER_CLOSE (partial) |
MT4 moves the remaining volume to a new ticket. The reply’s ORDER holds that new ticket — use it for further changes. |
TRADE_HISTORY |
Built from the terminal’s Account History tab: right-click it and choose All History to see older trades. A closed order gives an IN and an OUT deal, both numbered with the order ticket. |
TRACK_TRADE_EVENTS |
Events are sent as {"MSG":"TRACK_TRADE_EVENTS","EVENT":"ORDER_OPENED","TICKET":123,"ORDER":{...}}. |
CUSTOM_INDICATOR |
PARAM1–PARAM4 are passed as numbers, or as text when they are not numeric. The indicator must be in MQL4\Indicators. |
Trade events
Section titled “Trade events”EVENT |
When |
|---|---|
ORDER_OPENED |
A market order was opened. |
PENDING_PLACED |
A pending order was placed. |
PENDING_FILLED |
A pending order was triggered and is now open. |
ORDER_MODIFIED |
SL, TP, price or expiration changed. |
ORDER_PARTIALLY_CLOSED |
Part of an order was closed (old ticket). |
ORDER_REMAINDER_OPENED |
The remaining volume after a partial close (new ticket). |
ORDER_CLOSED |
An order was fully closed. |
PENDING_DELETED |
A pending order was deleted or expired. |
ORDER holds the order in the same format as ORDER_LIST (open and pending orders) or TRADE_HISTORY (closed orders).