/algo-trading-skills:broker-failover-secondary-account-routingfrom algo-trading-skills
Routes order flow to a backup broker account when the primary degrades, classifying safe failures, preventing duplicate sends, and pinning reducing orders to the account holding the position.
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 trading system holds a backup broker relationship and needs order flow to continue after the primary degrades — connection loss, sustained 5xx, throttling, or an outage mid-session. It provides a circuit breaker over two broker legs, with the failover decision gated on why the primary failed.
A generic circuit breaker guards idempotent reads, where retrying elsewhere is free. This one guards live order submission across two accounts, which breaks three of the pattern's usual assumptions:
A failed call is not a failed order. Not every error is an outage. A probe is a real order.
And one assumption that endpoint failover never has to think about at all: positions do not net across accounts. Failing a closing order over to the backup account does not flatten anything — it leaves you long in one account and short in the other, with double the gross exposure.
order-placement-idempotency.webhook-based-order-fill-notifications.sec-rule-15c3-5-risk-controls-us.kill-switch-and-drawdown-circuit-breakers.multi-region-failover-for-broker-connectivity and
smart-order-router-failover-on-venue-outage.client_order_ids per order intent. This is the key you reconcile on when
a call is ambiguous. Note what it does not buy you here: a client order id
de-duplicates at the broker that issued it, and the secondary broker has never seen
it. Cross-broker duplicates cannot be prevented by the broker — only by not
sending the second order.strict_symbol_mapping=True in
production.Classify the failure before deciding anything. This is the whole skill. Four classes, three different answers:
| Class | Meaning | Failover? |
|---|---|---|
UNAVAILABLE | Connection refused, DNS failure — the request never left | Yes, safe |
RATE_LIMITED | 429/418 — not accepted, but back off this leg | Yes, and honour Retry-After |
AMBIGUOUS | Timeout, connection reset, 5xx — it may be working | No — resolve first |
REJECTED | Bad symbol, insufficient buying power, size limit | No — the order is dead |
Anything unrecognised is AMBIGUOUS. The errors are not symmetric: a wrong
"ambiguous" costs one status query, a wrong "failed" costs a duplicate order in an
account that will never net it off.
Never fail over an ambiguous outcome. Binance's REST documentation states the general case for 5xx: "It is important to NOT treat this as a failure operation; the execution status is UNKNOWN and could have been a success." Either resolve it through a status query, or stop and make the caller reconcile. A resolver that itself errors means still ambiguous — never read a failed status check as "not there".
Never fail over a terminal rejection. "Insufficient buying power" is the primary's pre-trade risk control working. Routing it to a second account is shopping for a broker that will say yes. Re-raise it, and do not count it against broker health — otherwise the strategy's own bad orders trip the breaker.
Pin reducing orders to the account that holds the position. Mark every closing or
reducing order PositionEffect.REDUCE. In an account with no position the identical
instruction opens new exposure. In US equities it is also a compliance event: Reg SHO
Rule 200(c) deems a person to own securities "only to the extent that he has a net
long position", Rule 200(g) permits a "long" mark only where the seller owns the
security, and Rule 203(b)(1) requires a locate before a short sale order is accepted.
Netting the two accounts requires the independent-trading-unit conditions of Rule
200(f), including a written plan. If the holding account is down, the correct answer
is to refuse and escalate — not to open a position somewhere else.
Bound the half-open probes. Each probe is capital at risk against a broker you
believe is down. Generic implementations permit several concurrent trial calls
(resilience4j's permittedNumberOfCallsInHalfOpenState defaults to 10); here the
default is one, claimed under an explicit permit so concurrent callers cannot all
probe at once.
Ignore stale successes. A slow call that started before the breaker tripped can return after it. Honouring it closes a circuit that other threads opened for good reason. Tag each attempt with the circuit generation and discard successes from an older one.
Measure the recovery timeout on a monotonic clock. time.time() moves when NTP
steps it; a backward jump holds the circuit open indefinitely.
Handle "both legs down" as its own outcome. It is the case that matters most and the one most likely to be untested. The caller needs a typed error naming the order, not whatever the secondary happened to raise.
Full failure-classification tables and phase procedure: see
references/workflows.md. Cited standards, Reg SHO analysis, and thresholds: seereferences/standards.md. Printable go-live checklist: seeassets/checklist.md.
except Exception: route_to_secondary
is the single most damaging line this pattern attracts.python -m unittest discover -s skills/broker-failover-secondary-account-routing/scripts
and confirm all tests pass.TimeoutError. Confirm the router
raises AmbiguousOrderStateError carrying the client_order_id, and that the
secondary received nothing.order_status_resolver that finds the order; confirm the router returns the
real primary fill and still does not touch the secondary. Then make the resolver raise
and confirm the outcome stays ambiguous rather than becoming "not found".REJECTED BrokerError; confirm it is re-raised, the
secondary is untouched, and the circuit stays CLOSED with primary_failures == 0.UNAVAILABLE; confirm the order reaches the secondary with the
secondary's symbol mapping applied.AllBrokersUnavailableError names the order.PositionEffect.REDUCE sell. Confirm PositionAffinityError and that no short was
opened at the secondary.OPEN.time.time to a fixed value and confirm recovery still happens on schedule.manual_open, wait past the recovery timeout, and confirm the primary is still
not probed until manual_reset.strict_symbol_mapping=True, submit an unmapped symbol and confirm
SymbolMappingError — not an ambiguous outcome, and no failover.order-placement-idempotencybroker-api-idempotent-cancel-requestsbroker-agnostic-adapter-interfacemulti-region-failover-for-broker-connectivitysmart-order-router-failover-on-venue-outagemulti-broker-consolidated-position-viewmulti-broker-rate-limit-handlingkill-switch-and-drawdown-circuit-breakerswebhook-based-order-fill-notificationsus-reg-sho-short-sale-locate-requirementssec-rule-15c3-5-risk-controls-uscounterparty-and-broker-concentration-riskWhat 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