/algo-trading-skills:webhook-based-order-fill-notificationsfrom algo-trading-skills
Guides building a webhook receiver for broker order-fill notifications, covering deduplication, idempotency, HMAC verification, and reconciliation against authenticated broker APIs.
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.
Use this when a broker or venue delivers order and execution updates by POSTing to an HTTP endpoint you host, and those updates feed a position ledger.
Confirm the webhook exists before you build the receiver. Outbound fill webhooks are the exception, not the norm. Of the venues checked while writing this skill:
| Venue | How fills actually arrive |
|---|---|
| Interactive Brokers | EWrapper.execDetails over the TWS API socket. The IBKR callback-notification service is account-management only (client registration, account changes, funding) and is enabled by request. |
| Alpaca | Server-Sent Events, GET /v2/events/trades, replayable by ULID. |
| TradeStation | Chunked HTTP streaming, Content-Type: application/vnd.tradestation.streams.v3+json. |
| Coinbase Advanced Trade | The authenticated WebSocket user channel. |
| Zerodha Kite Connect | A real postback: POST of a JSON body to the registered postback_url. |
| DhanHQ | A real postback, POSTed per order status change and per partial fill. |
If your venue is in the top four rows, you want
websocket-reconnection-with-state-recovery, not this skill.
And settle the trust question before writing a single ledger update. A verified signature proves only that the bytes came from someone holding the secret. It does not prove the payload matches broker state, and two of the two brokers here that do send postbacks fall short of authenticating the payload:
checksum = sha256(order_id + order_timestamp + api_secret). The digest covers three fields. filled_quantity, status and
average_price are outside it, so a captured postback can be edited in those
fields and still pass verification.So the webhook is a hint that something changed. The broker's authenticated order or trades endpoint is the authority on what changed.
websocket-reconnection-with-state-recovery.graceful-degradation-to-polling-fallback.multi-broker-consolidated-position-view.order-placement-idempotency, and its key is client-supplied, not
broker-supplied.localhost or 127.0.0.1.secrets-rotation-without-bot-downtime).(order_id, exec_id) whose check-and-set is
atomic — a unique index or SET NX, not an in-process set, if more than one
worker serves the endpoint.Verify the signature over the raw bytes, before parsing.
Compute HMAC-SHA256(raw_body, secret) and compare with
hmac.compare_digest(). Strip the algorithm label (sha256=, v1,) from
the front of the token only. Accept a space-delimited list of tokens and a
list of secrets, so a dual-secret rotation window works without dropping
deliveries. Reject with 401 and parse nothing.
If your broker's signature covers only some fields (Kite) or nothing at all
(Dhan), record that here and treat every field outside the digest as
untrusted for the rest of the flow.
Reject a payload with no timestamp — do not assume "now". Verify the event timestamp is within ±300 s of server time. A missing timestamp is a rejection, not a fresh one: defaulting it to the current time disables replay defence for precisely the payloads that omit it. Do this before the idempotency claim, so a stale replay cannot burn the key its legitimate twin will need.
Extract fields defensively, and never 500.
A JSON null order id stringifies to the truthy "None". An unguarded
float() on a non-numeric quantity raises out of the handler. Python's
json.loads accepts the non-standard NaN and Infinity literals, and a
NaN quantity poisons a ledger silently because every later comparison
returns False. Reject each of these with 400. A 500 is worse than a 400:
most publishers read a 5xx as "retry", so a malformed payload that crashes
the handler retries forever.
Claim order_id:exec_id atomically, once.
Check-and-set in one operation. A separate if not seen: mark() lets two
request threads both conclude "not a duplicate". On a redelivery, return
200 so the publisher stops retrying, and return a result the caller
cannot mistake for an applicable fill — a distinct status and a zeroed
quantity. The historical bug here is a duplicate returning SUCCESS with the
quantity intact and a caller doing if status == "SUCCESS": position += qty.
Retention must exceed the publisher's retry horizon, or a late retry is
ingested as new. Bound the store so a runaway publisher cannot exhaust
memory, and log loudly when the bound evicts anything.
Treat a same-key redelivery with different bytes as a correction, not a duplicate.
Interactive Brokers republishes a corrected execution as a fresh
execDetails whose execId differs from the original only in the digits
after the final period — so an IBKR correction is not a duplicate under an
exec_id key, and it is not an additional fill either. Compare the body
digest against the one stored at claim time; if it differs, flag for
reconciliation rather than dropping it silently.
Detect out-of-order and missing deliveries; do not silently reorder.
Track the highest sequence_num per order_id and the set of sequences
seen. A sequence below the high-water mark means a FILLED was applied
before its PARTIALLY_FILLED; a gap below the high-water mark means a
delivery is outstanding. Surface both to the caller. Buffering a fill to wait
for its predecessor needs a durable queue and a timeout policy, which is a
risk decision the caller owns — so report the fact rather than hiding it.
Reconcile, then book, then acknowledge.
Fetch the order or trades record from the broker's authenticated endpoint and
book that, using the webhook only as the trigger. Return 200 once the event
is durably recorded; do the reconciliation and ledger work off the request
path via a queue, so a slow broker call does not stall the acknowledgement
and trigger a redelivery. Log every rejection with the reason and a payload
digest — never the secret — for structured-logging-for-post-incident-forensics.
Full step-by-step procedure with broker-specific detail: see
references/workflows.md. Broker/framework coverage table for this skill: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
time.time(). This is a silent removal
of replay protection that no test notices, because the payload that triggers
it is the one that omits the field being checked.json.dumps(parsed)
is not the received body. Capture the raw bytes in middleware before the
framework's body parser runs.sha256= with replace(). It removes the label from anywhere
in the token, not just the front, silently altering the digest being compared.set; a redelivery routed to the other worker is a
fresh event to it. The claim must live where every worker sees it.exec_id identifies a fill for all time. An IBKR correction
reuses everything but the digits after the final period of the execId.status == "SUCCESS",
apply_to_ledger is True, and http_status == 200.filled_quantity over results whose status == "SUCCESS" gives
the single fill quantity, and that each redelivery returns http_status == 200.INVALID_SIGNATURE and http_status == 401,
and that the body was never parsed.timestamp field; confirm MISSING_TIMESTAMP.TIMESTAMP_DRIFT_EXCEEDED for both, and that neither consumed the
idempotency claim.filled_qty values of "abc", null, -5 and the raw JSON literal
NaN; confirm each returns a 400-class rejection and that no exception
escapes the handler.order_id: null; confirm it is rejected and not accepted as "None".out_of_order and requires_reconciliation, and that missing_sequences()
reports sequence 2 after delivering 1 and 4.DUPLICATE_CONTENT_MISMATCH and requires_reconciliation.apply_to_ledger is True.python -m unittest discover -s skills/webhook-based-order-fill-notifications/scripts and
confirm all tests pass.zerodha-kite-postback-webhook-verificationorder-placement-idempotencywebsocket-reconnection-with-state-recoverywebsocket-subscription-reconciliation-after-reconnectgraceful-degradation-to-polling-fallbackmulti-broker-consolidated-position-viewsecrets-rotation-without-bot-downtimestructured-logging-for-post-incident-forensicsWhat SKILL.md sets; muted values are Claude Code's defaults
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