TermlessDocs

Guides

Setting up a bot

How to wire a trading bot to Termless so it stays right when the broker link blips: one login for prices and orders, one key per program, an Idempotency-Key per order, what to do with an unconfirmed trade, timeouts, and where to watch the link.

This page is the short list of choices that keep a bot correct when something goes wrong: the broker's server goes quiet for a few seconds, the link drops and comes back, or Termless restarts under it. Read Trading safely and Health checks for bots for the details behind each one.

One login is enough#

A bot can read bars and prices and place orders through the same account. You do not need a second MT5 login for data.

Each kind of request has its own queue per account. The broker serves one candle read at a time, so candle reads wait for each other, but an order never waits behind a candle read: orders have their own queue. All of them share one connection to the broker, so a very large download can slow everything a little while it arrives; ask for the bars you need, not years of them on every tick.

A second login only pays when you want two things kept fully apart, for example test scripts that must never touch the account your bot trades on.

One key per program#

Give each program its own key, so you can see what each one does in the activity trail and cut one off without touching the others.

  • A program that only reads (a dashboard, a price recorder, a bar collector) gets a read key limited to the accounts it needs. A bug in it then cannot place an order.
  • The program that trades gets a trade key, limited to its account.
  • An admin can change a key's name, scope and accounts in place (PATCH /api/v1/keys/{id}): the key itself stays the same, so nothing that holds it has to change. A stream the key opened on an account it may no longer read ends at once.

One Idempotency-Key per order#

Send an Idempotency-Key header with every order, and make it name the decision, not the attempt: the same key every time you retry the same order, a new key only for a new order. Then a retry can never open the trade twice. A random key made fresh on each attempt gives you none of this.

Read the answer before you send again#

AnswerWhat happenedWhat to do
200The broker placed or filled it (ok: true).Nothing more.
422 rejectedThe broker refused it; retcode says why.Fix the request, then use a new key.
503 busy, 503 stopping, 503 disconnectedIt was not sent: it waited too long for its turn, Termless is restarting, or the link was down.Send it again with the same key once things are back.
504 unconfirmedThe broker took it but its final result did not come. It may have been filled.Do not send a new order. Retry with the same key after a few seconds: if the result came late (Termless keeps listening for 2 minutes), the retry returns it. Otherwise check positions and orders, then decide.
504 timeoutNo answer within the request's time.Check positions and orders before sending again.

Timeouts#

Give reads and orders at least 35 seconds before your own HTTP client gives up. Termless answers every request within 30 seconds, and inside that it does useful waiting for you: a read whose link dropped while it was in flight is asked again once the link is back (within 28 seconds), and a read that arrives while the link is coming back from a drop waits up to 10 seconds for it. A client that gives up at 15 seconds throws that work away and turns a blip into an error.

During a long outage (a broker's weekend maintenance) reads answer 503 in about 2 seconds, so a bot polling every second is not held up.

Poll less: read from the stream#

A bot that polls /summary and /positions a few times a second asks for what the server already pushes. One stream with events=account,positions,orders,trades (/stream or /ws) carries the account every 500 ms and the positions and pending orders whenever they changed, plus every trade push numbered, so the bot reads from its own memory and sends nothing. On the box this guide was written on, three bots polled about 2.5 million requests a day for what four streams carry. Keep polling for what a stream does not carry (candles, history), and read /ready before an order.

Candles: ask for the bars you need (count=3), not a window. Since 0.6.3 a count read starts narrow and from the newest bar it already knows for the symbol and timeframe, so a 3-bar read costs the broker a few bars, not four days of them.

A safety switch and limits#

  • Pause new trades on an account (its settings in the dashboard, or PATCH /api/v1/accounts/{id} with new_trades_paused) to stop every bot opening anything at once, without logging the account out. Closing and moving stops still work. Your bot sees 403 trading_paused (not sent) and /ready says new_trades_paused.
  • Give a trading key limits: max_volume (the most lots per order) and max_open_per_min (the most new orders a minute). A bug that sends 10 lots instead of 0.10, or a loop that keeps sending, is refused before anything reaches the broker (403 or 429 key_limit).
  • Before an order, ask GET /api/v1/accounts/{id}/ready. It answers from memory in well under a millisecond. Since 0.6.0 it also warns (ping_overdue, blocks nothing) when a ping to the broker has waited more than 2 seconds: the link may be cut within seconds, and holding the order that long is cheap.
  • To hear about drops as they happen, keep a stream open (/stream or /ws): its status events say when the link goes down and comes back.
  • For history, read GET /api/v1/accounts/{id}/events (every drop with its cause) or the outages in GET /api/v1/status. An outage cut for missed pings starts at the first unanswered ping, marked start_estimated, so its length is the real gap, not just the seconds after Termless gave up on the link.

Polling a "connected" flag once a minute misses most short drops. A drop that heals itself in a few seconds usually needs no alert at all: alert when the link stays down, or when an order comes back unconfirmed.