MetaTrader 5 APITrading

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

MetaTrader 5 Trading API

Place, modify, and close MetaTrader 5 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 MT5 terminal, never a shared gateway.

How trading works on MT5

  • This is not the MT5 Gateway API. MetaQuotes' Gateway API, like its Manager and Server API, is a toolkit for brokers that connects their own MT5 server to liquidity providers and other systems. This API trades a single account through its own terminal. What the broker-side APIs do
  • Eight order types. Besides market, limit and stop orders, MT5 supports BuyStopLimit and SellStopLimit: a stop that, once triggered, places a limit order at stopLimitPrice.
  • Netting changes what an opposite order does. On a netting account an opposite order reduces, closes or reverses the position instead of opening a second one. Check marginMode in /AccountInfo.
  • Orders, deals and positions. A market order produces a deal (the reply's deal) and a position. Closing part of a position keeps its ticket; only the volume shrinks.
  • Native retcodes. retcode and retcodeDescription are MT5's own trade result codes; outcome sums them up as applied, no_change, partial or rejected.
  • Reads don't queue behind orders. Orders run in a dedicated trade Expert Advisor, while reads such as /OpenedOrders are answered by a separate service in the terminal, so they come back while an order is still waiting for the dealer.
  • Bursts are sent in parallel. Several orders can be at the broker at once, so the replies to a burst can come back in a different order than you sent them. Match each reply by its order, deal or clientOrderId, never by arrival.
  • 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
  • Calculators are MT5's own. OrderCalcMargin and OrderCalcProfit call MT5's built-in functions at the price you pass.

Order routes are executed inside your own terminal. A 200 response only means MT5 returned a result; always inspect success and retcode to confirm the broker accepted it. Every trade reply also carries position (the position the request opened or acted on), time and timeUtc of the fill (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's trade engine, exec how long it took there and at the broker, both in milliseconds.

POST /mt5/{account_id}/OrderSend

Submit a market or pending order. Market (Buy/Sell) needs symbol + volume; price is ignored. Pending (*Limit/*Stop) additionally needs price; StopLimit variants also need stopLimitPrice. 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. 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, BuyStopLimit, SellStopLimit (case- and separator-insensitive).
  • volume (required): Volume in lots.
  • price: Required for pending orders; ignored for market orders.
  • stopLimitPrice: Required for *StopLimit operations.
  • 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. Brokers require a minimum lead time, a few minutes on some.
  • 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/mt5/{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": "Request completed",
  "deal": 211438760,
  "order": 211438761,
  "volume": 0.1,
  "price": 1.15333,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "2026-11-03T10:15:42.318Z",
  "timeUtc": "2026-11-03T08:15:42.318Z",
  "remainderTicket": 0,
  "clientOrderId": "copy-7f3a-0001"
}
POST /mt5/{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, stopLimitPrice, and expiration. Omit a field to keep its current value.

  • ticket (required): Position or pending-order ticket.
  • stopLoss / takeProfit: New SL/TP; omit to keep current.
  • price: New price (pending orders only).
  • stopLimitPrice: New StopLimit price (*StopLimit pendings 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/mt5/{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": "Request completed",
  "deal": 0,
  "order": 211438761,
  "volume": 0.0,
  "price": 0.0,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "",
  "remainderTicket": 0
}
POST /mt5/{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. For a pending order, volume is ignored and the order is deleted. A position keeps its ticket through a partial close, so remainderTicket is always 0 on MT5.

  • 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/mt5/{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": "Request completed",
  "deal": 211438770,
  "order": 211438771,
  "volume": 0.05,
  "price": 1.15325,
  "bid": 1.15325,
  "ask": 1.15333,
  "comment": "",
  "position": 211438761,
  "time": "2026-11-03T11:02:07.904Z",
  "timeUtc": "2026-11-03T09:02:07.904Z",
  "remainderTicket": 0
}
GET /mt5/{account_id}/OrderCalcMargin

Required margin for a hypothetical order. Asks MT5 to compute the broker-side margin needed to open the given volume at the given price, returned in the account currency. 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, BuyStopLimit, SellStopLimit.
  • volume (required): Volume in lots.
  • price (required): Hypothetical entry price.
Example request
curl -H "X-API-Key: fxs_live_..." \
  "https://api.fxsocket.com/mt5/{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 /mt5/{account_id}/OrderCalcProfit

Projected P&L for a hypothetical open → close move, returned in the account currency. 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/mt5/{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"
}