When to Use
Invoke this whenever a bot consumes real-time order and fill updates from
TradeStation so that a disconnect cannot leave its position ledger wrong. It
covers frame classification (order / heartbeat / EndSnapshot / GoAway /
error), stalled-connection detection, REST catch-up across the offline window,
and deduplication of the cumulative order snapshots the stream emits.
Two facts drive everything below, and both contradict the shape most people
assume:
- The transport is not a WebSocket. TradeStation serves order updates over
RFC2616 HTTP/1.1 chunked streaming — an ordinary
GET whose body never ends.
This skill keeps the historical websocket slug for discoverability; the wire
protocol is HTTP streaming. There is no /v2/stream/orders endpoint: order
streaming exists only in v3.
- Every order frame is a full cumulative snapshot, not a delta.
Legs[].ExecQuantity is the total executed so far. A ledger that does += on
each frame double-counts on the first reconnect, because the stream replays a
snapshot of current orders on connect.
When NOT to Use
Do not use this skill for order placement idempotency (see
order-placement-idempotency), for backoff/jitter policy (see
websocket-reconnection-with-state-recovery), or for any other broker — the
frame shapes and status codes here are TradeStation-specific.
Prerequisites
- TradeStation API v3 OAuth access token with the
ReadAccount scope.
- Correct base URL for the environment:
https://api.tradestation.com for
live, https://sim-api.tradestation.com for the simulator. The environment is
selected by the host, not by the account id — a paper account id sent to the
live host trades real money.
- Account ID(s), comma-separated, 1–25 per stream.
- An order ledger that applies order state by assignment, not accumulation,
and can be committed durably before the update is marked processed.
- Awareness of the documented limits: 40 concurrent order streams, and 320
requests per rolling 5 minutes on the order-details resource used for catch-up.
Workflow
Open the stream. GET {base}/v3/brokerage/stream/accounts/{accounts}/orders
with Authorization: Bearer {token}. Expect Transfer-Encoding: chunked and
Content-Type: application/vnd.tradestation.streams.v3+json.
Reassemble frames before parsing. HTTP chunk boundaries are not message
boundaries — proxies re-chunk freely, so one chunk may hold several JSON
objects and one object may span chunks. Buffer until a complete JSON value is
available; only then call classify_frame(). A fragment is a framing bug, not
an order.
Classify every frame, and branch on the control frames.
{"Heartbeat": N, "Timestamp": ...} — liveness only; reset the stall timer.
{"StreamStatus": "EndSnapshot"} — the initial replay of current orders is
done; frames after this are live changes.
{"StreamStatus": "GoAway"} — the server is shutting down. Terminate the
request, back off, reconnect, run catch-up. Treating this as a no-op is how
a bot goes silently dark while believing it is connected.
{"Error": ..., "Message": ...} — terminate the request and reconnect; do
not keep reading.
- Anything with an
OrderID — an order snapshot.
Detect stalls from heartbeat silence. TradeStation sends a heartbeat after
5 seconds of an idle stream, so silence beyond ~15 seconds (three missed
heartbeats) means the socket is hung, not quiet — a state TCP will not report
on its own. Measure on a monotonic clock so an NTP step cannot fabricate or
mask a stall. On stall: tear the connection down rather than waiting longer.
Apply, then commit — in that order. is_duplicate() is a pure query;
nothing is remembered until mark_processed(). Persist the update first, then
mark it. Marking first turns the stream into at-most-once delivery: a crash in
between loses the fill permanently, because catch-up will suppress it as a
duplicate.
Reconcile the gap on reconnect, with the right query shape. There is no
"everything since epoch-seconds N" call. Query both:
GET /v3/brokerage/accounts/{accounts}/orders — today's and open orders.
It takes no since parameter.
GET /v3/brokerage/accounts/{accounts}/historicalorders?since={date} —
closed orders, which the first endpoint omits. since is a date
(2026-09-02), limited to 90 days, so catch-up necessarily re-reads whole
days and leans on deduplication to absorb the overlap.
Derive since from the broker's own event timestamps (ClosedDateTime /
OpenedDateTime / heartbeat Timestamp), never the local clock: the query is
evaluated against the broker's clock, so local skew silently narrows the
recovery window. catch_up_since_date() does this and clamps to the 90-day
limit.
Paginate the catch-up fully. Both endpoints cap a page at 600 orders and
return a nextToken valid for one hour. A fetch that ignores it truncates
recovery at 600 orders and drops the rest of the gap without any error.
Deduplicate on the full order state. The key must cover per-leg executed
quantity, not a summed total: a two-leg order that fills 1/9 then 9/1 has an
unchanged total and is a genuinely different state. Normalise decimal strings
so the stream's "100" and REST's "100.00" produce one key.
Full step-by-step procedure with broker-specific detail: see references/workflows.md.
Sourced endpoint, field and limit tables: see references/standards.md.
Printable pre-flight checklist: see assets/checklist.md.
Common Pitfalls
- Reading
FilledQuantity / AveragePrice. Neither field exists on a
TradeStation v3 order. Executed quantity lives on Legs[].ExecQuantity; the
average fill price is the top-level FilledPrice. Code that reads the absent
names silently records zero for every fill — and if that zero feeds the
deduplication key, every partial fill after the first on an order collapses to
one signature and is dropped.
- Treating
GoAway or an error frame as "nothing to do". Both oblige the
client to end the HTTP request. A parser that returns "no order here" for them,
the same as for a heartbeat, leaves the bot reading a dead socket forever.
- Marking an event processed before the ledger has committed it. The window
between "received" and "persisted" is exactly where a crash loses a fill that
catch-up would otherwise have recovered.
- Accumulating
ExecQuantity instead of assigning it. Cumulative snapshots
plus += equals a double-counted position on the first reconnect, because the
stream replays current orders on connect.
- Sending a Unix timestamp as
since. historicalorders takes a date and
nothing finer, and /orders takes no since at all. Sub-day precision is not
available; overlap and dedupe instead.
- Reconciling only against
/orders. It returns today's and open orders, so
an order that opened and closed inside the outage window is absent from it.
historicalorders is where it will be.
- Using HTTP chunks as message delimiters. TradeStation documents that
proxies re-chunk streams, so this "works" until it silently truncates a JSON
object in production.
- Letting one bad field kill the loop.
float("n/a") on an unexpected
payload raises mid-stream and takes the connection down with it. Coerce
defensively and log.
- Confusing
FPR with FLP. FPR is "Partial Fill (Alive)" — more can
still execute. FLP is "Partial Fill (UROut)" — the remainder was cancelled
and the order is done. Treating FPR as terminal abandons a working order.
- Unbounded dedupe state. A signature set that only grows is a slow memory
leak in a process meant to run for months.
Verification
- Feed a v3 order frame with
Legs[].ExecQuantity and confirm the extracted
quantity and FilledPrice are non-zero — the regression that zero fills come
from reading v2-era field names.
- Feed three successive
FPR frames on one order (2 → 5 → 9 of 10) and confirm
three distinct events survive deduplication.
- Feed
{"StreamStatus": "GoAway"} and an {"Error": ...} frame and confirm each
forces a reconnect rather than being silently swallowed.
- Advance a fake monotonic clock past the stall threshold and confirm
is_stream_stalled() trips; confirm a heartbeat resets it.
- Confirm
catch_up_since_date() returns a YYYY-MM-DD string derived from
broker event time and clamped to 90 days.
- Confirm
reconcile_missed_orders() returns uncommitted states only, dedupes
the overlap between the two endpoints, and commits nothing itself.
- Run the unit suite:
python -m unittest discover -s skills/tradestation-websocket-order-updates/scripts.
Related Skills
websocket-reconnection-with-state-recovery
websocket-subscription-reconciliation-after-reconnect
order-placement-idempotency
sandbox-vs-production-endpoint-drift
webhook-based-order-fill-notifications
1---2name: tradestation-websocket-order-updates3description: >-4 Use when consuming TradeStation's v3 order update stream, which is HTTP chunked5 streaming rather than a WebSocket. Classifies stream and control frames, detects6 stalls by heartbeat, and dedupes cumulative fill snapshots across reconnects.7license: Apache-2.08metadata:9 source: https://skillmd.com/skills/himanshuj16/tradestation-websocket-order-updates10 domain: algorithmic-trading11 subdomain: broker-integration12 tags: broker-integration, tradestation-api, websocket-stream, order-updates, gap-reconciliation13 brokers_frameworks: TradeStation WebAPI v314 version: "2.0.0"15 author: algo-trading-skills-contributors16---1718## When to Use1920Invoke this whenever a bot consumes real-time order and fill updates from21TradeStation so that a disconnect cannot leave its position ledger wrong. It22covers frame classification (order / heartbeat / `EndSnapshot` / `GoAway` /23error), stalled-connection detection, REST catch-up across the offline window,24and deduplication of the cumulative order snapshots the stream emits.2526Two facts drive everything below, and both contradict the shape most people27assume:2829- **The transport is not a WebSocket.** TradeStation serves order updates over30 RFC2616 HTTP/1.1 chunked streaming — an ordinary `GET` whose body never ends.31 This skill keeps the historical `websocket` slug for discoverability; the wire32 protocol is HTTP streaming. There is no `/v2/stream/orders` endpoint: order33 streaming exists only in **v3**.34- **Every order frame is a full cumulative snapshot, not a delta.**35 `Legs[].ExecQuantity` is the total executed so far. A ledger that does `+=` on36 each frame double-counts on the first reconnect, because the stream replays a37 snapshot of current orders on connect.3839## When NOT to Use4041Do **not** use this skill for order *placement* idempotency (see42`order-placement-idempotency`), for backoff/jitter policy (see43`websocket-reconnection-with-state-recovery`), or for any other broker — the44frame shapes and status codes here are TradeStation-specific.4546## Prerequisites4748- TradeStation API v3 OAuth access token with the `ReadAccount` scope.49- Correct **base URL for the environment**: `https://api.tradestation.com` for50 live, `https://sim-api.tradestation.com` for the simulator. The environment is51 selected by the host, *not* by the account id — a paper account id sent to the52 live host trades real money.53- Account ID(s), comma-separated, 1–25 per stream.54- An order ledger that applies order state by **assignment**, not accumulation,55 and can be committed durably before the update is marked processed.56- Awareness of the documented limits: 40 concurrent order streams, and 32057 requests per rolling 5 minutes on the order-details resource used for catch-up.5859## Workflow60611. **Open the stream.** `GET {base}/v3/brokerage/stream/accounts/{accounts}/orders`62 with `Authorization: Bearer {token}`. Expect `Transfer-Encoding: chunked` and63 `Content-Type: application/vnd.tradestation.streams.v3+json`.64652. **Reassemble frames before parsing.** HTTP chunk boundaries are *not* message66 boundaries — proxies re-chunk freely, so one chunk may hold several JSON67 objects and one object may span chunks. Buffer until a complete JSON value is68 available; only then call `classify_frame()`. A fragment is a framing bug, not69 an order.70713. **Classify every frame, and branch on the control frames.**72 - `{"Heartbeat": N, "Timestamp": ...}` — liveness only; reset the stall timer.73 - `{"StreamStatus": "EndSnapshot"}` — the initial replay of current orders is74 done; frames after this are live changes.75 - `{"StreamStatus": "GoAway"}` — the server is shutting down. Terminate the76 request, back off, reconnect, run catch-up. Treating this as a no-op is how77 a bot goes silently dark while believing it is connected.78 - `{"Error": ..., "Message": ...}` — terminate the request and reconnect; do79 not keep reading.80 - Anything with an `OrderID` — an order snapshot.81824. **Detect stalls from heartbeat silence.** TradeStation sends a heartbeat after83 5 seconds of an idle stream, so silence beyond ~15 seconds (three missed84 heartbeats) means the socket is hung, not quiet — a state TCP will not report85 on its own. Measure on a *monotonic* clock so an NTP step cannot fabricate or86 mask a stall. On stall: tear the connection down rather than waiting longer.87885. **Apply, then commit — in that order.** `is_duplicate()` is a pure query;89 nothing is remembered until `mark_processed()`. Persist the update first, then90 mark it. Marking first turns the stream into at-most-once delivery: a crash in91 between loses the fill permanently, because catch-up will suppress it as a92 duplicate.93946. **Reconcile the gap on reconnect, with the right query shape.** There is no95 "everything since epoch-seconds N" call. Query **both**:96 - `GET /v3/brokerage/accounts/{accounts}/orders` — today's and open orders.97 It takes **no** `since` parameter.98 - `GET /v3/brokerage/accounts/{accounts}/historicalorders?since={date}` —99 closed orders, which the first endpoint omits. `since` is a **date**100 (`2026-09-02`), limited to 90 days, so catch-up necessarily re-reads whole101 days and leans on deduplication to absorb the overlap.102103 Derive `since` from the broker's own event timestamps (`ClosedDateTime` /104 `OpenedDateTime` / heartbeat `Timestamp`), never the local clock: the query is105 evaluated against the broker's clock, so local skew silently narrows the106 recovery window. `catch_up_since_date()` does this and clamps to the 90-day107 limit.1081097. **Paginate the catch-up fully.** Both endpoints cap a page at 600 orders and110 return a `nextToken` valid for one hour. A fetch that ignores it truncates111 recovery at 600 orders and drops the rest of the gap without any error.1121138. **Deduplicate on the full order state.** The key must cover per-leg executed114 quantity, not a summed total: a two-leg order that fills 1/9 then 9/1 has an115 unchanged total and is a genuinely different state. Normalise decimal strings116 so the stream's `"100"` and REST's `"100.00"` produce one key.117118> Full step-by-step procedure with broker-specific detail: see `references/workflows.md`.119> Sourced endpoint, field and limit tables: see `references/standards.md`.120> Printable pre-flight checklist: see `assets/checklist.md`.121122## Common Pitfalls123124- **Reading `FilledQuantity` / `AveragePrice`.** Neither field exists on a125 TradeStation v3 order. Executed quantity lives on `Legs[].ExecQuantity`; the126 average fill price is the top-level `FilledPrice`. Code that reads the absent127 names silently records zero for every fill — and if that zero feeds the128 deduplication key, every partial fill after the first on an order collapses to129 one signature and is dropped.130- **Treating `GoAway` or an error frame as "nothing to do".** Both oblige the131 client to end the HTTP request. A parser that returns "no order here" for them,132 the same as for a heartbeat, leaves the bot reading a dead socket forever.133- **Marking an event processed before the ledger has committed it.** The window134 between "received" and "persisted" is exactly where a crash loses a fill that135 catch-up would otherwise have recovered.136- **Accumulating `ExecQuantity` instead of assigning it.** Cumulative snapshots137 plus `+=` equals a double-counted position on the first reconnect, because the138 stream replays current orders on connect.139- **Sending a Unix timestamp as `since`.** `historicalorders` takes a date and140 nothing finer, and `/orders` takes no `since` at all. Sub-day precision is not141 available; overlap and dedupe instead.142- **Reconciling only against `/orders`.** It returns today's and open orders, so143 an order that opened *and* closed inside the outage window is absent from it.144 `historicalorders` is where it will be.145- **Using HTTP chunks as message delimiters.** TradeStation documents that146 proxies re-chunk streams, so this "works" until it silently truncates a JSON147 object in production.148- **Letting one bad field kill the loop.** `float("n/a")` on an unexpected149 payload raises mid-stream and takes the connection down with it. Coerce150 defensively and log.151- **Confusing `FPR` with `FLP`.** `FPR` is "Partial Fill (Alive)" — more can152 still execute. `FLP` is "Partial Fill (UROut)" — the remainder was cancelled153 and the order is done. Treating `FPR` as terminal abandons a working order.154- **Unbounded dedupe state.** A signature set that only grows is a slow memory155 leak in a process meant to run for months.156157## Verification158159- Feed a v3 order frame with `Legs[].ExecQuantity` and confirm the extracted160 quantity and `FilledPrice` are non-zero — the regression that zero fills come161 from reading v2-era field names.162- Feed three successive `FPR` frames on one order (2 → 5 → 9 of 10) and confirm163 three distinct events survive deduplication.164- Feed `{"StreamStatus": "GoAway"}` and an `{"Error": ...}` frame and confirm each165 forces a reconnect rather than being silently swallowed.166- Advance a fake monotonic clock past the stall threshold and confirm167 `is_stream_stalled()` trips; confirm a heartbeat resets it.168- Confirm `catch_up_since_date()` returns a `YYYY-MM-DD` string derived from169 broker event time and clamped to 90 days.170- Confirm `reconcile_missed_orders()` returns uncommitted states only, dedupes171 the overlap between the two endpoints, and commits nothing itself.172- Run the unit suite: `python -m unittest discover -s skills/tradestation-websocket-order-updates/scripts`.173174## Related Skills175176- `websocket-reconnection-with-state-recovery`177- `websocket-subscription-reconciliation-after-reconnect`178- `order-placement-idempotency`179- `sandbox-vs-production-endpoint-drift`180- `webhook-based-order-fill-notifications`