MetaTrader 4 APITrading

For AI agents: this page is also available as Markdown at /docs/mt4/trading.md. The index of every docs page is /docs/llms.txt.

MetaTrader 4 Trading API

Place, modify, and close MetaTrader 4 orders over a REST API: market and pending orders, partial closes, plus margin and profit calculators for position sizing. Every order executes inside your own dedicated MT4 terminal, never a shared gateway.

How trading works on MT4

  • Six order types. Buy, Sell, BuyLimit, SellLimit, BuyStop and SellStop. MT4 has no stop-limit orders; BuyStopLimit and SellStopLimit are rejected with a 400 MRPC_UNSUPPORTED before anything reaches the broker.
  • One ticket per trade. On MT4 the order you send is the position. The reply's order is that ticket, and deal is always 0 because MT4 has no separate deals.
  • Hedging only. An opposite order opens its own ticket instead of reducing the open one. To reduce a position, close its ticket fully or partially.
  • Partial closes renumber. OrderClose with a volume closes part of the ticket. The reply reports the original ticket, which now holds only the closed part, and the new ticket the remainder continues under as remainderTicket. Trade events link the two the same way.
  • Same retcodes as MT5. MT4 only returns an error number. The bridge maps it to the matching MT5 retcode (134 not enough money becomes 10019, 138 requote becomes 10004) and keeps the original in retcodeDescription, so one error-handling path covers both platforms. The full table is on Errors & Rate Limits. MT4 error to retcode table
  • Retries without double fills. A trade that times out answers 504 with a code that says what happened: MRPC_ABANDONED means it never reached the terminal, MRPC_TRADE_OUTCOME_UNKNOWN means the terminal took it and it may have executed. Send orders with a clientOrderId and either one can be resent with the same id, since the terminal never trades an id twice. All error codes
  • One request at a time. A single Expert Advisor in the MT4 terminal handles every request in turn, reads included, and each order waits for the broker's dealer to answer. A read sent while an order is in flight waits for it. To act on many accounts at once, send one request per account (or use the batch endpoints) rather than many in parallel to one account.
  • Calculators are computed. MT4 has no margin or profit calculator. OrderCalcMargin multiplies the broker's per-lot margin at the live price by the volume, so the price you pass doesn't change it. OrderCalcProfit uses tick size and today's tick value, which makes it an estimate for symbols quoted in another currency.

Order routes are executed inside your own terminal. A 200 response only means MT4 returned a result. Always inspect success, outcome and retcode to confirm the broker accepted it. MT4 itself only reports an error number; the bridge translates it into the same retcode values MT5 uses and keeps the original number in retcodeDescription (see Errors & Rate Limits). Every trade reply also carries position (on MT4 the order ticket), time and timeUtc of the fill (the open time for /OrderSend, the close time for /OrderClose; empty when nothing filled, such as a modify or a newly placed pending order), and a Server-Timing header: queue is how long the request waited for the terminal, exec how long it took there and at the broker, both in milliseconds.

POST /mt4/{account_id}/OrderSend

Submit a market or pending order. Market (Buy/Sell) needs symbol + volume; price is ignored and the order goes out at the current quote, and the reply reports the broker's actual fill price. Pending (*Limit/*Stop) additionally needs price. MT4 has no stop-limit orders: BuyStopLimit and SellStopLimit are rejected with a 400 MRPC_UNSUPPORTED before anything reaches the broker. Optional: slippage (default 10 points), stopLoss, takeProfit, an expiry for pending orders (expiration in broker server time or expirationUtc in real UTC, not both; absent → good-till-cancelled), comment, expertId (a.k.a. magic), and clientOrderId. On success the reply's order is the new ticket and deal is always 0. With a clientOrderId a retry is safe: the first request with an id trades, and any repeat with the same id, whether it overlaps the first or comes up to 10 minutes later, trades nothing and gets the first request's reply with the header Idempotent-Replayed: true. Reusing an id for a different order is refused with 409 MRPC_CLIENT_ORDER_ID_REUSED.

  • symbol (required): Symbol to trade, e.g. EURUSD.
  • operation (required): Buy, Sell, BuyLimit, SellLimit, BuyStop, SellStop (case- and separator-insensitive).
  • volume (required): Volume in lots.
  • price: Required for pending orders; ignored for market orders.
  • stopLoss / takeProfit: Optional SL/TP prices.
  • slippage: Max slippage in points (default 10).
  • expiration: Pending-order expiry in broker server time (date string); absent → good-till-cancelled.
  • expirationUtc: The same in real UTC, ISO-8601 such as 2026-11-03T17:00:00Z; use instead of expiration. MT4 brokers often require it at least 10 minutes ahead.
  • comment / expertId: Order comment and magic number.
  • clientOrderId: Your own id for this order, 1–64 characters, remembered for 10 minutes. Echoed in the reply.
Request body
{
  "symbol": "EURUSD",
  "operation": "Buy",
  "volume": 0.1,
  "stopLoss": 1.14800,
  "takeProfit": 1.16200,
  "comment": "my-strategy",
  "expertId": 123456,
  "clientOrderId": "copy-7f3a-0001"
}
Example request
curl -X POST https://api.fxsocket.com/mt4/{account_id}/OrderSend \
  -H "X-API-Key: fxs_live_..." \
  -H "Content-Type: application/json" \
  -d '{"symbol":"EURUSD","operation":"Buy","volume":0.1,"stopLoss":1.148,"takeProfit":1.162,"comment":"my-strategy","expertId":123456,"clientOrderId":"copy-7f3a-0001"}'
Response
{
  "success": true,
  "outcome": "applied",
  "retcode": 10009,
  "retcodeDescription": "Done",
  "deal": 0,
  "order": 211438761,
  "volume": 0.1,
  "price": 1.15333,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "2026-11-03T10:15:42.000Z",
  "timeUtc": "2026-11-03T08:15:42.000Z",
  "remainderTicket": 0,
  "clientOrderId": "copy-7f3a-0001"
}
POST /mt4/{account_id}/OrderModify

Modify an existing position or pending order. The ticket type decides what's modifiable: a position ticket accepts only stopLoss and takeProfit; a pending-order ticket also accepts price and expiration. Omit a field to keep its current value. Sending values that are already set returns retcode 10025 ("No changes") with outcome no_change, which is safe to treat as applied.

  • ticket (required): Position or pending-order ticket.
  • stopLoss / takeProfit: New SL/TP, omit to keep current.
  • price: New price, pending orders only.
  • expiration: New expiration (date string, broker server time), pending orders only.
  • expirationUtc: New expiration in real UTC (ISO-8601), instead of expiration.
Request body
{
  "ticket": 211438761,
  "stopLoss": 1.15000,
  "takeProfit": 1.16500
}
Example request
curl -X POST https://api.fxsocket.com/mt4/{account_id}/OrderModify \
  -H "X-API-Key: fxs_live_..." \
  -H "Content-Type: application/json" \
  -d '{"ticket":211438761,"stopLoss":1.15,"takeProfit":1.165}'
Response
{
  "success": true,
  "outcome": "applied",
  "retcode": 10009,
  "retcodeDescription": "Done",
  "deal": 0,
  "order": 211438761,
  "volume": 0.1,
  "price": 1.15333,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "",
  "remainderTicket": 0
}
POST /mt4/{account_id}/OrderClose

Close a position or delete a pending order: the action is inferred from the ticket type. For a position, supply volume for a partial close (must respect the symbol's volumeStep) or omit it for a full close; the close goes out at the current bid or ask. For a pending order, volume is ignored and the order is deleted. On MT4 a partial close renumbers the trade: the part you closed keeps the original ticket, which is what the reply's order and position report, and the remaining volume carries on under a new ticket, reported as remainderTicket (0 after a full close). price is the broker's actual fill price.

  • ticket (required): Position ticket (closes it) or pending-order ticket (deletes it).
  • volume: Partial close volume; omit or 0 = full close.
  • slippage: Max slippage in points.
Request body
{
  "ticket": 211438761,
  "volume": 0.05
}
Example request
curl -X POST https://api.fxsocket.com/mt4/{account_id}/OrderClose \
  -H "X-API-Key: fxs_live_..." \
  -H "Content-Type: application/json" \
  -d '{"ticket":211438761,"volume":0.05}'
Response
{
  "success": true,
  "outcome": "applied",
  "retcode": 10009,
  "retcodeDescription": "Done",
  "deal": 0,
  "order": 211438761,
  "volume": 0.05,
  "price": 1.15325,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "2026-11-03T11:02:07.000Z",
  "timeUtc": "2026-11-03T09:02:07.000Z",
  "remainderTicket": 211438790
}
GET /mt4/{account_id}/OrderCalcMargin

Required margin for a hypothetical order, in the account currency. MT4 has no margin calculator of its own, so the bridge multiplies the broker's per-lot margin requirement (already leverage-adjusted) by the volume. That figure is taken at the live price: the price you pass is accepted but does not change the result, and neither does the direction. Does not place an order, purely a sizing/risk query.

  • symbol (required): Symbol name as listed by /symbols.
  • operation (required): Buy, Sell, BuyLimit, SellLimit, BuyStop, SellStop.
  • volume (required): Volume in lots.
  • price (required): Hypothetical entry price (ignored on MT4, see above).
Example request
curl -H "X-API-Key: fxs_live_..." \
  "https://api.fxsocket.com/mt4/{account_id}/OrderCalcMargin?symbol=EURUSD&operation=Buy&volume=0.1&price=1.15333"
Response
{
  "symbol": "EURUSD",
  "operation": "Buy",
  "volume": 0.1,
  "price": 1.15333,
  "margin": 28.83,
  "currency": "USD"
}
GET /mt4/{account_id}/OrderCalcProfit

Projected P&L for a hypothetical open → close move, returned in the account currency. MT4 has no profit calculator of its own, so the bridge computes it from the symbol's tick size and tick value: (close − open) / tickSize × tickValue × volume, signed by direction. The tick value is today's, so for symbols quoted in another currency the result is an estimate at the current conversion rate. Does not place an order, useful for position sizing and UI.

  • symbol (required): Symbol name as listed by /symbols.
  • operation (required): Buy or Sell (pending variants accepted: only the direction is used).
  • volume (required): Volume in lots.
  • priceOpen (required): Hypothetical open price.
  • priceClose (required): Hypothetical close price.
Example request
curl -H "X-API-Key: fxs_live_..." \
  "https://api.fxsocket.com/mt4/{account_id}/OrderCalcProfit?symbol=EURUSD&operation=Buy&volume=0.1&priceOpen=1.15333&priceClose=1.16333"
Response
{
  "symbol": "EURUSD",
  "operation": "Buy",
  "volume": 0.1,
  "priceOpen": 1.15333,
  "priceClose": 1.16333,
  "profit": 100.0,
  "currency": "USD"
}