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.
- 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.
/mt5/{account_id}/OrderSendSubmit 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, expiration (absent → good-till-cancelled), comment, expertId (a.k.a. magic).
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: Date string; absent → good-till-cancelled.comment / expertId: Order comment and magic number.
{
"symbol": "EURUSD",
"operation": "Buy",
"volume": 0.1,
"stopLoss": 1.14800,
"takeProfit": 1.16200,
"comment": "my-strategy",
"expertId": 123456
}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}'{
"success": true,
"retcode": 10009,
"retcodeDescription": "Request completed",
"deal": 211438760,
"order": 211438761,
"volume": 0.1,
"price": 1.15333,
"bid": 1.15325,
"ask": 1.15333,
"comment": ""
}/mt5/{account_id}/OrderModifyModify 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), pending orders only.
{
"ticket": 211438761,
"stopLoss": 1.15000,
"takeProfit": 1.16500
}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}'{
"success": true,
"retcode": 10009,
"retcodeDescription": "Request completed",
"deal": 0,
"order": 211438761,
"volume": 0.0,
"price": 0.0,
"bid": 1.15325,
"ask": 1.15333,
"comment": ""
}/mt5/{account_id}/OrderCloseClose 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.
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.
{
"ticket": 211438761,
"volume": 0.05
}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}'{
"success": true,
"retcode": 10009,
"retcodeDescription": "Request completed",
"deal": 211438770,
"order": 211438771,
"volume": 0.05,
"price": 1.15325,
"bid": 1.15325,
"ask": 1.15333,
"comment": ""
}/mt5/{account_id}/OrderCalcMarginRequired 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.
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"{
"symbol": "EURUSD",
"operation": "Buy",
"volume": 0.1,
"price": 1.15333,
"margin": 28.83,
"currency": "USD"
}/mt5/{account_id}/OrderCalcProfitProjected 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.
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"{
"symbol": "EURUSD",
"operation": "Buy",
"volume": 0.1,
"priceOpen": 1.15333,
"priceClose": 1.16333,
"profit": 100.0,
"currency": "USD"
}