/algo-trading-skills:broker-side-order-throttle-detectionfrom algo-trading-skills
Detects undeclared broker-side throttling when venues queue orders instead of rejecting them, using EWMA ACK RTT baseline and AIMD backoff.
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 skill when a bot dispatches order flow to a venue that queues or paces excess messages instead of rejecting them, so congestion never appears as an error code and shows up only as acknowledgment latency.
The Interactive Brokers TWS API is the reference case: it "is designed to accept up to
fifty messages per second coming from the client side," and beyond that rate messages
are queued and delayed rather than refused. The +PACEAPI connect option makes TWS
pace the client at 50/s instead of disconnecting it. In neither case does the client
receive a signal — the only observable is ACK RTT rising.
Use it to establish a latency baseline that a sustained throttle cannot quietly move, to detect acknowledgments that never arrive at all, and to slow dispatch until the condition clears.
Retry-After header; the IBKR Web API returns 429 and may put the IP in a 10-minute
penalty box. An explicit response is authoritative and a latency inference is not —
obey Retry-After and do not let this detector shorten it. See
multi-broker-rate-limit-handling.matching-engine-throttle-and-message-gapping-detection.WARMUP
and only the absolute ceiling and the ACK timeout can fire.time.monotonic()), in one process. Wall-clock timestamps can step backwards under
NTP correction and produce negative round trips.max_absolute_rtt_ms. The 500 ms default is a placeholder, not a standard.Register every submission, not just every acknowledgment.
Call register_order_submission(order_id, t_sub) at dispatch. A detector fed only by
completed ACKs sees only the orders that were not throttled into silence — the
sample stream is survivorship-biased, and the worst throttle produces no sample at all.
Record acknowledgments and compute RTT.
On ACK, call record_order_ack(order_id, t_sub, t_ack); RTT is (t_ack - t_sub) x 1000 ms.
A non-finite or time-reversed timestamp raises ThrottleDataError — do not clamp it
to 0 ms. A fabricated 0 ms sample pulls the baseline down and makes the next healthy
acknowledgments look anomalous.
Maintain the baseline, excluding throttled samples. Update the exponentially weighted mean and variance (Finch 2009, eq. 143) only from samples not classified as throttled. Admitting the anomaly is what lets a sustained throttle train the baseline onto itself and go quiet while still in force.
Classify against the pre-update baseline.
RTT >= max_absolute_rtt_ms → SILENT_THROTTLE, regardless of warmup.z >= z_score_threshold → SILENT_THROTTLE.WARMUP (report it; do not report it as healthy).z >= elevated_z_threshold → ELEVATED_LATENCY.NORMAL.where z = (RTT - EWMA) / sqrt(max(EWMVar, min_variance_clamp)). Evaluate every
threshold against the baseline as it stood before this sample, so the reported mean,
deviation and z-score reconcile and the decision is auditable.
Sweep for acknowledgments that never came.
Call sweep_pending_acks(now) at least as often as ack_timeout_ms. Any order older
than the timeout is reported ACK_TIMEOUT — the most severe state — once, then
dropped from the pending table so repeated sweeps do not re-escalate the same stall.
Apply AIMD backoff.
On a congestion signal, decrease dispatch rate multiplicatively (multiply the delay by
backoff_multiplier, clamped to max_backoff_ms). On a healthy acknowledgment,
increase dispatch rate additively (subtract backoff_additive_decrease_ms, to a floor
of zero). Chiu & Jain (1989) is the control law being applied.
Decide what recovery means, explicitly.
By default the baseline stays frozen for as long as throttling persists, so a
sustained throttle keeps alarming and keeps the backoff at its ceiling until a human
intervenes. If the latency shift is genuinely permanent (a re-route, a venue
migration), set rebaseline_after_consecutive to re-anchor after N consecutive
throttled samples — accepting that the detector will then go quiet at the new level.
Full procedure: see
references/workflows.md. Parameter reference and sourcing: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
NORMAL within
about four samples and the backoff decays to zero while the broker is still queuing
every order. Sustained throttling is the normal case during a volatility event, not
the exotic one — exclude throttled samples from the baseline.max(0.0, t_ack - t_sub) turns a
backwards clock step or an out-of-order callback into a "perfect" 0 ms acknowledgment
that drags the baseline down. Under the same clamp a NaN timestamp also becomes 0 ms,
because max(0.0, nan) returns 0.0 — the detector is then quietly mis-calibrated
with nothing logged. Reject both.WARMUP until enough
samples have accumulated, while keeping the absolute ceiling live.Retry-After. A latency inference is weaker evidence than
the venue's own answer. When both are present the venue wins.SILENT_THROTTLE, the backoff
climbs to max_backoff_ms, and the baseline mean is unchanged at 15 ms.SILENT_THROTTLE still fires.ack_timeout_ms, and confirm one
ACK_TIMEOUT report and no second report on the next sweep.t_ack < t_sub pair each raise ThrottleDataError and
leave the baseline untouched.z_score in the report equals (latest_rtt_ms - ewma_rtt_ms) / ewmsd_rtt_ms.python -m unittest discover -s skills/broker-side-order-throttle-detection/scripts and confirm a 100% pass rate.multi-broker-rate-limit-handlingmatching-engine-throttle-and-message-gapping-detectiontick-buffering-burst-handlingstructured-logging-for-post-incident-forensicsWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub himanshuj16/algo-trading-skills --plugin algo-trading-broker-integration36 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.Other plugins' skills for the same job