/algo-trading-skills:tradestation-websocket-order-updatesfrom algo-trading-skills
Consumes TradeStation's v3 order update stream (HTTP chunked streaming, not WebSocket). Classifies frames, detects stalls via heartbeat, dedupes cumulative fill snapshots across reconnects, and performs REST catch-up.
Ships in algo-trading-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
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:
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.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.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.
ReadAccount scope.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.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.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: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
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.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.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.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./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.float("n/a") on an unexpected
payload raises mid-stream and takes the connection down with it. Coerce
defensively and log.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.Legs[].ExecQuantity and confirm the extracted
quantity and FilledPrice are non-zero — the regression that zero fills come
from reading v2-era field names.FPR frames on one order (2 → 5 → 9 of 10) and confirm
three distinct events survive deduplication.{"StreamStatus": "GoAway"} and an {"Error": ...} frame and confirm each
forces a reconnect rather than being silently swallowed.is_stream_stalled() trips; confirm a heartbeat resets it.catch_up_since_date() returns a YYYY-MM-DD string derived from
broker event time and clamped to 90 days.reconcile_missed_orders() returns uncommitted states only, dedupes
the overlap between the two endpoints, and commits nothing itself.python -m unittest discover -s skills/tradestation-websocket-order-updates/scripts.websocket-reconnection-with-state-recoverywebsocket-subscription-reconciliation-after-reconnectorder-placement-idempotencysandbox-vs-production-endpoint-driftwebhook-based-order-fill-notificationsWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
36 skills in this plugin
/alpaca-paper-live-key-separationUse when a bot connects to the Alpaca Trading API and must never cross paper keys with the live endpoint; pins the base URL per environment, checks the key prefix, probes /v2/account and requires an explicit ALLOW_LIVE_TRADING flag./api-key-least-privilege-audit-toolUse before deploying a live bot, to audit existing broker API keys for scopes they do not need. Flags withdrawal, transfer and admin rights that turn a leaked execution key into theft./broker-account-margin-call-handlingUse when a bot trades a Reg T, portfolio or futures margin account and must act before the broker liquidates: tiered maintenance-margin warnings cross-checked against broker excess liquidity, initial-margin order gating and liquidity-aware de-leveraging.npx claudepluginhub himanshuj16/algo-trading-skills --plugin algo-trading-broker-integrationOther plugins' skills for the same job