TermlessDocs

Guides

The MetaTrader5 order_send mapping

How the official MetaTrader5 Python package's order_send request maps onto Termless, so an existing script ports over without rewriting its trading code.

If you already have code written against the official MetaTrader5 Python package, its trading code carries over: the server has POST /api/v1/accounts/{id}/order_send, which takes the same request dict order_send() takes and answers with the trade result, so a script that talks HTTP keeps building its requests the way it always did.

The request#

JSON
{"action": "TRADE_ACTION_DEAL", "symbol": "XAUUSD", "volume": 0.1, "type": "ORDER_TYPE_BUY", "price": 2391.6,
 "sl": 2380.0, "tp": 2420.0, "deviation": 20, "type_filling": "ORDER_FILLING_IOC", "type_time": "ORDER_TIME_GTC",
 "comment": "from mt5 script", "magic": 7}

Enum fields take the integer constants or their official names; numbers may also arrive as strings. Field names are the official ones, with one convenience: stoplimit may also be spelled stop_limit.

FieldValuesNotes
action1 TRADE_ACTION_DEAL, 5 TRADE_ACTION_PENDING, 6 TRADE_ACTION_SLTP, 7 TRADE_ACTION_MODIFY, 8 TRADE_ACTION_REMOVE, 10 TRADE_ACTION_CLOSE_BYRequired.
symbolRequired for deal, pending and sltp.
volumelotsRequired for deal and pending.
type0 ORDER_TYPE_BUY, 1 ORDER_TYPE_SELL, 2 ORDER_TYPE_BUY_LIMIT, 3 ORDER_TYPE_SELL_LIMIT, 4 ORDER_TYPE_BUY_STOP, 5 ORDER_TYPE_SELL_STOP, 6 ORDER_TYPE_BUY_STOP_LIMIT, 7 ORDER_TYPE_SELL_STOP_LIMIT, 8 ORDER_TYPE_CLOSE_BY
priceThe trigger price of a pending order; the price you saw for a deal.
stoplimit (or stop_limit)The limit price of a stop limit order.
sl, tpprices, 0 = none
deviationpoints
type_filling0 ORDER_FILLING_FOK, 1 ORDER_FILLING_IOC, 2 ORDER_FILLING_RETURN, 3 ORDER_FILLING_BOCA mode the symbol does not allow is replaced by one it does (IOC, then FOK, then return), as the web terminal does; the answer's warnings says which was sent. A pending order goes out as return unless the symbol is exchange-executed.
type_time0 ORDER_TIME_GTC, 1 ORDER_TIME_DAY, 2 ORDER_TIME_SPECIFIED, 3 ORDER_TIME_SPECIFIED_DAY
expirationunix seconds, trade server clockThe broker keeps whole minutes; one with seconds gets a warning naming the minute.
commentup to 255 charactersThe broker keeps the first 31; for a deal or pending order Termless keeps the whole text (see below).
magicThe web protocol has no field for it, so the broker stores 0; for a deal or pending order Termless keeps it (see below).
positionticketFor TRADE_ACTION_SLTP, and for a deal that closes a position.
position_byticketFor TRADE_ACTION_CLOSE_BY.
orderticketFor TRADE_ACTION_MODIFY and TRADE_ACTION_REMOVE.

The answer is the same trade result every other trade route returns (ok, retcode, retcode_text, deal, order, volume, price, bid, ask, comment, request_id, and warnings when something was changed), 200 when placed or done and 422 when the broker refused it. The official package's OrderSendResult has the same fields under the same names, warnings aside.

A FOK request on a symbol that allows IOC, with a magic number
{"ok": true, "retcode": 10009, "retcode_text": "Request completed", "deal": 81200131, "order": 51830031,
 "volume": 0.1, "price": 2391.6, "bid": 2391.42, "ask": 2391.6, "comment": "Request completed", "request_id": 31,
 "warnings": ["ORDER_FILLING_FOK is not allowed for XAUUSD; sent ORDER_FILLING_IOC",
              "magic is not sent to the broker (MetaTrader's web protocol has no field for it); termlessd keeps it for this order"]}

Which route does what#

Every order_send action has a plainer equivalent. New code should use those; they validate more and read better.

Official requestTermless route
TRADE_ACTION_DEAL with ORDER_TYPE_BUY or SELL and no positionPOST /orders with {symbol, side, volume, sl?, tp?, deviation?, comment?, magic?}
TRADE_ACTION_DEAL with position (closing)DELETE /positions/{ticket} (?volume= for a partial close)
TRADE_ACTION_PENDINGPOST /orders with type: "limit", "stop" or "stop_limit", price, stop_limit?, expiration?
TRADE_ACTION_SLTPPATCH /positions/{ticket} with {sl?, tp?}
TRADE_ACTION_MODIFYPATCH /orders/{ticket} with {price?, stop_limit?, sl?, tp?, expiration?}
TRADE_ACTION_REMOVEDELETE /orders/{ticket}
TRADE_ACTION_CLOSE_BYPOST /order_send only

Porting a script#

Official package
import MetaTrader5 as mt5

mt5.initialize()
mt5.login(12345678, password="...", server="Pepperstone-Demo")
tick = mt5.symbol_info_tick("XAUUSD")
result = mt5.order_send({
    "action": mt5.TRADE_ACTION_DEAL, "symbol": "XAUUSD", "volume": 0.1,
    "type": mt5.ORDER_TYPE_BUY, "price": tick.ask, "sl": 2380.0, "tp": 2420.0,
    "deviation": 20, "type_filling": mt5.ORDER_FILLING_IOC, "comment": "from mt5 script",
})
print(result.retcode, result.order)
Over HTTP, same request
import os, requests

base = os.environ["TERMLESS_URL"] + "/api/v1/accounts/pepperstone-12345678"
auth = {"Authorization": "Bearer " + os.environ["TERMLESS_KEY"]}

tick = requests.get(f"{base}/quotes", headers=auth, params={"symbols": "XAUUSD"}).json()[0]
r = requests.post(f"{base}/order_send", headers=auth, json={
    "action": "TRADE_ACTION_DEAL", "symbol": "XAUUSD", "volume": 0.1,
    "type": "ORDER_TYPE_BUY", "price": tick["ask"], "sl": 2380.0, "tp": 2420.0,
    "deviation": 20, "type_filling": "ORDER_FILLING_IOC", "comment": "from mt5 script",
})
result = r.json()
print(result["retcode"], result["order"])           # 422 when refused: ok is False

What differs from the terminal#

  • magic never reaches the broker: the MT5 web protocol has no field for it, so the broker stores 0. Through the server, Termless keeps the number (and a comment past 31 characters) for every deal or pending order it sends, for 400 days, and puts it back on the positions, orders and deals that order made, the closing deal included, and on the stream events. MetaTrader's own apps still show magic: 0.
  • type_filling may be swapped for a mode the symbol allows, instead of failing with 10030; the answer's warnings names the mode sent.
  • reason on positions, orders and deals is a word (web, sl, tp, stop_out, ...), not the official *_REASON_* number.
  • order_check() is POST /api/v1/accounts/{id}/order_check: the same request, checked against the symbol and the account (volume 10014, trade mode 10017, 10042, 10043, 10044, market hours 10018, a pending order's price against the market 10015, stops on the wrong side or closer than the stops level 10016, free margin 10019) and never sent. A read key may call it. The freeze level is not checked.
  • The other official calls (positions and orders by ticket, history by position, margin and profit, recent ticks, candles by position, order books) have routes too: the MetaTrader5 Python mapping lists every one.
  • Times in the result and in positions are the trade server's clock, as in the terminal.