# The MetaTrader5 Python mapping Every function of the official MetaTrader5 Python package and the termlessd route that does the same, with what differs. Code written against the official `MetaTrader5` Python package moves to Termless by calling termlessd over HTTP: the server keeps the account logged in and has a route for every call. The tables put each official function next to its route. A route written short, such as `GET /summary`, is under `/api/v1/accounts/{id}` (so `GET /api/v1/accounts/{id}/summary`); the others are written in full. Each needs a `read` key unless said otherwise. A route never takes the official positional arguments: it takes query parameters or a JSON body, and answers JSON in the shapes of the [reference](/docs/reference). A query parameter a route does not take is a `400` that names the ones it does. ## Connection and account | Official call | termlessd route | What differs | |---|---|---| | `initialize()` | `POST /api/v1/accounts` (admin), once | termlessd logs the account in and keeps it connected; a script never logs in itself. | | `login()` | `POST /api/v1/accounts` for another login; `PATCH /api/v1/accounts/{id}` with a new `password` or `server` (admin) | | | `shutdown()` | `PATCH /api/v1/accounts/{id}` with `{"enabled": false}`, or `DELETE` (admin) | | | `version()` | `GET /api/v1/accounts/{id}`: `status.server_build`; `GET /api/v1/health`: termlessd's own version (to callers with a key) | No terminal, so no terminal version or release date. | | `last_error()` | The HTTP status and `{"error": {"code", "message"}}`; a refused trade's `retcode` | | | `account_info()` | `GET /summary`, or `events=account` on a stream | The summary has the fields a bot uses (balance, equity, margin, free margin, levels, leverage, currency); `demo` and `hedging` stand in for `trade_mode` and `margin_mode`. Profit, equity and margin are worked out from live prices, as the terminal does. | | `terminal_info()` | `GET /api/v1/accounts/{id}` (`status`: connected, ping, build, last error) and `GET /api/v1/health` | It describes the session, since there is no terminal. | ## Symbols and prices | Official call | termlessd route | What differs | |---|---|---| | `symbols_total()` | `GET /symbols?limit=10000`, and count the list | | | `symbols_get(group=)` | `GET /symbols?q=&limit=` | `q` is plain text matched in the name or description, not a mask. | | `symbol_info(symbol)` | `GET /symbols/{symbol}` for the spec (`GET /specs?names=` for many at once, 0.7.0), `GET /quotes?symbols=` for the prices | Two routes instead of one tuple. The spec also says whether the symbol can be traded now (`tradeable`) and lists every order type's margin rate (`margin_rates`). | | `symbol_info_tick(symbol)` | `GET /quotes?symbols=`, or a stream | Subscribes on first use and waits up to 3 s for a price; the symbol stays subscribed `hold` seconds after the last read (default 600, 0.7.0). | | `symbol_select(symbol, enable)` | `PATCH /api/v1/accounts/{id}` with `symbols` (the watchlist, admin); a stream's `symbols=`; `/quotes` and `/ticks` subscribe on first use | | | `market_book_add(symbol)` | The first `GET /book?symbol=` adds it | termlessd keeps a book for every caller until it is released, at most 32 per account, and adds them again after a reconnect. | | `market_book_get(symbol)` | `GET /book?symbol=&max_age_ms=` | Levels as in `BookInfo` (`type` 1 sell, 2 buy, 3 sell market, 4 buy market, `price`, `volume`, `volume_dbl` in the broker's units) plus `side` and `lots` (0.7.0), sells from the highest price down, then buys; the answer carries the book's `time` (0 when the broker sent none, an empty list when its market is closed). A book older than `max_age_ms` (100 to 60000, default 1000) is asked for again, since brokers may send changes rarely. | | `market_book_release(symbol)` | `DELETE /book?symbol=` | The answer says whether a book was released. | | `copy_rates_from(symbol, tf, date_from, count)` | `GET /candles?symbol=&timeframe=&count=&to=` | | | `copy_rates_from_pos(symbol, tf, start_pos, count)` | `GET /candles?symbol=&timeframe=&count=&offset=` | `offset` is `start_pos`; count plus offset at most 50,000. | | `copy_rates_range(symbol, tf, date_from, date_to)` | `GET /candles?symbol=&timeframe=&from=&to=` | A range over 100,000 bars is `400`; over 50,000 keeps the newest 50,000. | | `copy_ticks_from(symbol, date_from, count, flags)` | `GET /ticks?symbol=&from=&count=` | Only ticks seen since the symbol's first `/ticks` read (0.7.0; the web protocol serves no tick history): 1,000 per symbol by default and 100,000 per account, `count` at most 20,000, a reconnect starts it again, and there is no `flags` filter: each tick carries its `flags`. | | `copy_ticks_range(symbol, date_from, date_to, flags)` | `GET /ticks?symbol=&from=&to=` | As above; `count` (up to 20,000) caps the answer. | Timeframes are the official 21 (`M1` to `MN1`); the ones the broker does not serve (`M2`, `H3`, ...) are built from finer bars, as the terminal does. The timeframe is a name (`timeframe=M5`), not a `TIMEFRAME_*` number. Market times are the trade server's clock. ## Orders and positions | Official call | termlessd route | What differs | |---|---|---| | `orders_total()` | `GET /summary` (`orders`), or count `GET /orders` | | | `orders_get(symbol=, group=, ticket=)` | `GET /orders?symbol=`, `GET /orders/{ticket}` | No group masks. `GET /orders` lists pending orders only. | | `positions_total()` | `GET /summary` (`positions`), or count `GET /positions` | | | `positions_get(symbol=, group=, ticket=)` | `GET /positions?symbol=`, `GET /positions/{ticket}` | No group masks. | | `order_calc_margin(action, symbol, volume, price)` | `GET /calc?symbol=&side=&type=&volume=&price=` | `side` and `type` (`market`, `limit`, `stop`, `stop_limit`) say what `action` (an `ORDER_TYPE_*`) says; `price` may be left out for the current one. For a pending type, `margin` is what the order needs once it fills and `margin_pending` what the broker holds while it waits (its pending rate, usually 0). | | `order_calc_profit(action, symbol, volume, price_open, price_close)` | `GET /calc?...&price=&close=` | One call answers margin and profit. | | `order_check(request)` | `POST /order_check` with the `order_send` request | Checks the volume (`10014`), the trade mode (`10017`, `10042` long only, `10043` short only, `10044` close only), today's sessions (`10018`), a pending order's price against the market (`10015`), the stop-loss and take-profit on the right side of the price and at least the stops level away (`10016`), and the free margin (`10019`), as the terminal does. Stop changes and order changes are checked against the market too. The answer has `ok` and `request_margin`, not the request. It never sends the order. | | `order_send(request)` | `POST /order_send` (`trade` key), or the plainer routes in [order_send mapping](/docs/order-send) | `magic` is not in the web protocol, so the broker stores 0: termlessd keeps it per order and puts it back on positions, orders, deals and events. A comment past 31 characters is kept the same way. | The official helpers `Buy()`, `Sell()` and `Close()` are `POST /orders` and `DELETE /positions/{ticket}`. A trailing stop runs in the desktop terminal, not at the broker; a bot trails a stop by moving it with `PATCH /positions/{ticket}` (see [Trading](/docs/trading#trailing-a-stop)). ## History | Official call | termlessd route | What differs | |---|---|---| | `history_orders_total(date_from, date_to)` | Count `GET /orders/history?from=&to=` | A page holds at most 5,000; `before` gets the next. | | `history_orders_get(date_from, date_to, group=)` | `GET /orders/history?from=&to=&symbol=` | `symbol` is one exact name (any case), not a mask. The broker picks orders by when they were placed, so an old order cancelled today is in the window of the day it was placed. | | `history_orders_get(position=)`, `(ticket=)` | `GET /orders/history?position=` or `?ticket=` | Without `from`, from a day before the position or order opened when it is still open, else 10 years back (at most 10 years per request). | | `history_deals_total(date_from, date_to)` | Count `GET /deals?from=&to=` | As for orders. | | `history_deals_get(date_from, date_to, group=)` | `GET /deals?from=&to=&symbol=` | As for orders. | | `history_deals_get(position=)`, `(ticket=)` | `GET /deals?position=` or `?ticket=` | `ticket=` is an order's ticket (`DEAL_ORDER`), as in the official call. Without `from`, as for orders. | ## Other differences - Times are the trade server's clock. Give the account a clock rule (`clock`, 0.7.0) and every answer carries UTC beside them (`time_utc`, `time_utc_msc`), which the official package never does. - `reason` on positions, orders and deals is a word (`web`, `sl`, `tp`, `stop_out`, ...), not a `*_REASON_*` number. - Enums are words (`side: "buy"`, `type: "buy_limit"`, `entry: "out"`), except in the `order_send` and `order_check` request, which takes the official numbers or names. - termlessd keeps many accounts connected for many clients, numbers trade events so a client can resume (across a restart too), and records every write in the [audit trail](/docs/trails).