MT4 API: control MetaTrader 4 accounts over REST & WebSocket
MQL4 cannot open network sockets, so most MT4 integrations end up as fragile file or DLL bridges. MetaTrader API runs the MT4 connection for you and gives your code the same JSON endpoints it uses for MT5.
# open orders of a connected MT4 account
curl -s -H "x-api-key: $MTAPI_KEY" \
"https://api.metatraderapi.net/OpenedOrders?id=$MTAPI_ID"
# buy 0.01 lots with a stop loss, safe to retry
curl -s -H "x-api-key: $MTAPI_KEY" \
"https://api.metatraderapi.net/OrderSend?id=$MTAPI_ID&symbol=EURUSD&operation=Buy&volume=0.01&stoploss=1.0500&clientOrderId=mt4-0001"
# on MT4 a refused order reports the MT4 error number in "state"import os, requests
API = "https://api.metatraderapi.net"
s = requests.Session()
s.headers["x-api-key"] = os.environ["MTAPI_KEY"]
ACCOUNT = os.environ["MTAPI_ID"]
for o in s.get(f"{API}/OpenedOrders", params={"id": ACCOUNT}, timeout=75).json():
print(o["ticket"], o["symbol"], o["lots"], o["profit"])const API = "https://api.metatraderapi.net";
const headers = { "x-api-key": process.env.MTAPI_KEY };
const id = process.env.MTAPI_ID;
const history = await (await fetch(`${API}/OrderHistory?id=${id}`, { headers })).json();
console.log(history.length, "closed orders");What is an MT4 API?
An MT4 API lets your own software read and control a MetaTrader 4 trading account over the network: account data, quotes, orders, positions and history. MetaTrader 4 itself has no public API for trading accounts, and MQL4 has no socket functions, which is why classic solutions need an Expert Advisor plus a DLL or file bridge on a Windows VPS. MetaTrader API is a hosted MT4 API: connect the account with its broker server, login and password, and call it over REST, WebSocket or gRPC, with the same endpoints as MT5.
From account to first API call in minutes
- STEP 1Connect your MT4 accountBroker server name exactly as MetaTrader 4 shows it, login and the main password (not the investor password). Demo accounts work too.
- STEP 2Copy the account id and API keyThe same key format as MT5 (mt5api_…, sent as x-api-key); MT4 and MT5 accounts can share one key.
- STEP 3Call the same endpoints as MT5Your code does not change between platforms, apart from the few MT4 differences listed below.
What the MT4 API gives you
A selection of the 100+ endpoints. All of them, with parameters and responses: documentation and OpenAPI.
| Area | Endpoint | What it does |
|---|---|---|
| Account | /AccountSummary | balance, equity, margin, free margin, leverage, currency |
| Market data | /SymbolList | the broker's exact symbol names (with suffixes) |
| Market data | /GetQuote | bid / ask for one symbol; /GetQuoteMany for several |
| Market data | /PriceHistory | candles (OHLC) for a symbol and timeframe |
| Orders | /OrderSend | market and pending orders with SL / TP and clientOrderId |
| Orders | /OrderModify | move stop loss, take profit or a pending price |
| Orders | /OrderClose | close an order, fully or partially |
| Orders | /OrderDelete | delete a pending order |
| Positions | /OpenedOrders | open orders and pending orders |
| History | /OrderHistory | closed orders; /OrderHistoryPagination for large accounts |
| Connection | /ConnectEx | connect an account and wait until it is ACTIVE |
| Experts | /EaUpload | upload and run your own compiled Expert Advisor (.ex4) |
Ways to reach MetaTrader 4, compared
THE USUAL WAYS
- Expert Advisor + DLL / ZeroMQ bridge: MQL4 has no sockets, so a DLL or file bridge inside a running MT4 app on a Windows VPS; breaks on MT4 updates.
- Copy of the MT4 app per account on a VPS: one MetaTrader 4 desktop app per account to install, log in, update and monitor.
- MetaQuotes MT4 Manager API: licensed to brokers for their own server; not available for an account at another broker.
METATRADER API
- MetaTrader API: no MQL4, no DLL, no VPS; MT4 and MT5 through the same REST and WebSocket endpoints.
- Works with your own account at any broker: no broker permission, Manager licence or server access needed.
- Each account runs on its own isolated connection; broker passwords are stored AES-256-GCM encrypted.
- Uptime measured live on the public status page.
MT4 differences you should know
MT4 accounts use the same endpoints as MT5 with a few platform limits: MT4 has no stop-limit orders and no /BalanceHistory; a refused MT4 order reports the MT4 error number in state (MT5 reports a retcode such as 10009); a REST call waits up to 15 s for an MT4 account (30 s for MT5). Everything else, including the WebSocket stream, clientOrderId and Expert Advisor upload (.ex4), works the same.
Limits, in numbers
| Rate limit | bursts of 40 requests, refilled at 20 per second, per API key |
| WebSocket | 25 concurrent streams per account, quotes at most every 200 ms per symbol, heartbeat every 10 s |
| Timeouts | a REST call waits up to 30 s for the account (MT4: 15 s); set your client timeout to 75 s |
| Accounts | up to 50 seats per customer, more on request |
| Retries | every order can carry a clientOrderId: a resend with the same id never opens a second order |
One price per connected account
REST, WebSocket, gRPC, MT4 and MT5 included. Billed monthly; add or release seats anytime.