Server-Sent Events need no websocket library, custom protocol or handshake. An
EventSource in a
browser, a for await loop on a server, or curl -N on the command line is a complete client.
Rebuild from the snapshot
Account streams send a full snapshot on every connect. Market streams first send held history withcached: true, then live bars and a repaired snapshot when history finishes. Cached history
does not establish a live price. Apply each market snapshot to its recent window and retain older
bars you paged in. For an account stream:
- When you connect, replace your local state with the snapshot.
- After that, apply each event as it arrives.
- If the connection drops, reconnect and replace your state again.
Follow execution reports
An execution report is one event in an order’s life: accepted, filled, cancelled, replaced, rejected, expired, triggered or restated, with the order’s status after it. The account stream carries them asreport events, in the account’s order, beside the account frames. Use them when
you keep your own order blotter, or when your system already speaks FIX: execType and ordStatus
are FIX’s ExecType and OrdStatus, each value by its FIX name, and orderId, clOrdId,
origClOrdId, cumQty, leavesQty, avgPx, lastQty and lastPx mean what they mean in FIX.
Each report event is one of two kinds:
Tell reports apart by
execId: it is each report’s identity on the account, and the point a stream
resumes from. sequence orders reports and nothing more. It is the account’s sequence of the event
that holds the report, so several reports can share it and it skips numbers between reports.
An order keeps its orderId for its whole life. Its clOrdId is the id you placed it or last
replaced it under, and the account frame’s order rows name the order that rests now as
providerOrderId. When you replace an order, clOrdId moves to the new id and the replaced report
names the old one in origClOrdId. A stop loss that trdrs moves for its breakeven or trailing rule
reports restated and keeps its clOrdId, with no origClOrdId: the id you know stays the order’s,
while providerOrderId names the order that rests after the move. Quantities and prices are exact
decimal strings, and avgPx and commission are exact fractions, so divide them only to display them.
text is a sentence saying why an order ended or was refused, on a cancelled or rejected
report. An order refused when you send it reports rejected with refusal.code carrying the
refusal’s code, the same code the order routes answer with; act on the code, not the sentence. An
order that fills now, sent with a limit the market is already past, is accepted and cancelled: it
reports new and then cancelled, with the same refusal.code. A
resting order the book rejects later reports rejected with text alone. A cancel or a replace that is refused gets its answer from the route you called and
sends no report. When you send an order again with an id the account already answered, you get an
order_status report of the order as it stands, with execId "0", and nothing is executed twice.
The stream resumes reports for you. Every report except an order_status one carries its execId
as the event’s id: line, so an EventSource that reconnects on its own sends it back as
Last-Event-ID:
When you open a new connection yourself rather than letting an
EventSource reconnect, pass the
last execId you received as after. An open stream can also be sent a status when its reports
couldn’t be carried whole; replace what you hold with it the same way.
The journal can’t state the reports after an execId it holds no report of, and can’t state a report of
an order that was refused when it was sent, which no journal event holds; when any such report falls
among those you missed, you are sent a status instead.
A status summarizes rather than replays. An order you hold that is missing from it ended while you
were away, and you don’t get its fills; an order that was placed and ended while you were away isn’t
in it at all. GET /api/account/orders with filter=all lists how each order ended.
An account sends report events once its execution reports are on, and none until then. They can
also pause while your stream stays open, when trdrs stops carrying the account’s reports for a
while. A paused stream is sent nothing, and its report events resume with a status, so replace
what you hold when it arrives. Everything else on the stream stays as it is, so a client that skips
report events needs no change.
Reconnect when a stream asks
trdrs releases without stopping: the engine serving your stream restarts for a release while another engine keeps serving. Before it closes your stream, it sends onereconnect event:
afterMs milliseconds, which is never more than 1,000. The new
connection reaches an engine that is staying and opens as every connection does, with a fresh
snapshot where the stream sends one, so the rule above still holds. A reconnect isn’t an error:
keep what you show until the new stream’s first events arrive, and don’t count it toward a backoff
of your own.
The event also sets the stream’s retry: to afterMs, so an EventSource that reconnects on its
own follows it with no code from you. Streams closed together are asked to wait for different
times across the second, so they don’t all reconnect in the same moment.
Start the news and calendar from a read
The news and calendar streams send nothing when you connect. Read/api/news or /api/calendar
first, then add each new headline as it arrives, and read your calendar window again when a notice
arrives.