Resolution

All pages

0016 · IBKR: correct first, then robust, then effortless

Status: accepted, 2026-09-26. Built for issue #13. The code is src/broker/ibkr.ts and src/broker/ibkr/ (call layer, pacing, reply prompts, contracts, session supervisor, Flex reconciliation, routes), with the gateway container in compose.yaml and deploy/ibkr-gateway/. Tests run against a scripted fake gateway (test/helpers/fake-ibkr.ts; test/ibkr-*.test.ts, test/live-ibkr.test.ts). Reference: docs/ibkr.md.

Context

The first IBKR venue worked on the happy path. It did not survive the paths that matter with real money:

  • an answer lost after the order reached IBKR;
  • IBKR's first, empty answer to a forced order list;
  • reply prompts confirmed without reading them;
  • a previous close taken for a live price;
  • a session that expired between two minutes of use;
  • a position cache that trails a fill by seconds and raised a false halt.

Setting it up meant reading several IBKR pages.

Decision

  1. Save before send; a lost answer is unknown. An order is saved as sending before it is written. Anything lost after the write is unknown and is resolved by the client order id (cOID), never reported as "nothing was sent". The blank first answer of account/orders?force=true is followed by a plain call.
  2. Allowlist the reply prompts. Only o163 and o403 are confirmed. Named dangerous ids, unknown ids, an empty id list and KID/PRIIPs text are refused with IBKR's words.
  3. Refuse stale prices. Halted, close-only, and market-data states N, O, Z and Y refuse the order. D (delayed) is allowed with a notice.
  4. One session model. A supervisor tickles whenever IBKR is configured, revives a session at most every five minutes, re-primes the portfolio endpoints, reads the paper flag, and reduces everything to six states. The topbar, the Connections card and Operations all read that one model from the snapshot. Pacing follows IBKR's published limits, and a second consecutive 429 pauses all calls.
  5. No false recon alarms. A break must persist across two reads at least two minutes apart. Names with a pending corporate action are excluded.
  6. Contracts by ISIN, on the home exchange, currency checked. Ambiguity is a refusal, not a guess. Results and failures are cached, so opening a panel does not search again.
  7. The preview is signed. When live, IBKR's what-if is part of the digest the owner signs. A blocked preview cannot be signed, and the what-if runs again just before sending.
  8. EUR only until an FX leg exists. Cash is read per currency from the ledger. A live order in another currency is refused with that reason, not converted silently.
  9. A wizard of real tests. Seven steps on the broker card, each a real call, none of which places an order.
  10. Flex reconciliation reports; it never fixes. Executions are matched by execution id and then by client order id. Each difference is recorded once as a recon.discontinuity with both numbers. Corporate actions are flagged only.
  11. The gateway container is optional and private. It runs under a compose profile, is published on loopback only, shares a private network with the app, and its build is checked against a sha256 you supply.

Consequences

  • An unknown order that IBKR does not hold can be sent again by recovery. IBKR's duplicate-cOID check is the guard.
  • Going live on a paper login is not refused. The paper flag is recorded in the signed terms, so a paper rehearsal of the whole go-live stays possible.
  • The contract map lives in memory. After a restart it is resolved again, and the go-live pins it.
  • Not built: an FX leg, a paper round-trip step in the wizard, a login proxy in the fund, certificate pinning, and push notices for a session that needs login. See docs/ibkr.md, "Not done yet".