/algo-trading-skills:broker-account-margin-call-handlingfrom algo-trading-skills
Implements tiered margin-call prevention for algorithmic trading bots: early-warning house ratios, broker cushion checks, and pre-trade order gating. Use when a bot trades Reg T, portfolio, or futures margin accounts.
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 an algorithmic trading bot operates on a margin account (Reg T, Portfolio Margin, or futures margin). When adverse market moves push maintenance margin requirements toward or past account equity, brokers liquidate positions at market prices, and they choose which ones. Acting first — cancelling resting orders, blocking new leverage, and unwinding on your own schedule with liquidity limits — is the difference between a controlled reduction and a forced one.
Use it to run three gates: a tiered house ratio as early warning, a broker-cushion check that catches deficiency the house ratio can miss, and a pre-trade veto on orders that would consume margin you do not have.
excess_liquidity and
available_funds from the broker; do not derive them from NLV.net_liquidation_value, initial_margin,
maintenance_margin, and critically excess_liquidity and available_funds.Order.whatIf = true submission, whose OrderState returns
initMarginChange and maintMarginChange.average_daily_volume. It is a required field, not an optional one: it
caps how much of a position the plan will sell, and a guessed value defeats the cap.Evaluate account health with evaluate_margin_health(snapshot). It escalates on
the worse of two signals:
excess_liquidity < 0, the broker's own cushion, which is authoritative.
Unusable input (NaN, infinity, negative margin) raises MarginDataError rather than
grading the account. A margin engine that reports NORMAL because its feed broke is
worse than one that stops.Act on the tier. Defaults are house policy, not regulation — set them for your book:
Handle non-positive equity as a different problem. If NLV $\le 0$ the ratio is
undefined — the engine returns math.inf and the action HALT_AND_ESCALATE rather than
DE_LEVERAGE_IMMEDIATELY. There is no target margin to de-leverage toward against
non-positive equity; stop trading and involve a human.
Gate every new order through guard_new_order(snapshot, margin_impact, initial_margin_impact=...). Pass initial_margin_impact whenever you have it: new
positions are opened against initial margin (Reg T requires 50% on a long margin
equity purchase) not maintenance margin (FINRA 4210's 25% minimum), so a
maintenance-only projection is the weaker constraint and can pass an order the broker
refuses. The guard returns True or raises — it never returns False, so treat any
exception as a hard veto.
Plan de-leveraging with plan_deleveraging(snapshot, positions), which orders by
short-option tail risk, then margin density, then liquidity, and caps each slice at
max_participation_rate of ADV. Then re-price the plan before acting on it if you
are on Portfolio Margin or SPAN — the planner's linear "units × margin per unit" model
does not hold when margin is computed on portfolio-level stressed loss, and unwinding a
hedge leg can increase the requirement. Send each slice through the broker's own
pre-trade check first.
Expect the plan to be incomplete under illiquidity. The ADV cap can leave the plan short of the required reduction. That is the intended trade-off — dumping the position would crush its price, lower NLV and trigger a second deficiency — but it means you must check whether the plan actually clears the deficit and escalate when it does not.
Full step-by-step procedure with broker-specific detail: see
references/workflows.md. Broker triggers, cushion definitions and regulatory reference points: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
maintenance_margin / NLV uses NLV; the broker's
cushion uses Equity with Loan Value, which excludes non-margin-eligible value and can be
much lower. An account can read 70% and healthy while excess_liquidity is already
negative and liquidation is underway.>= breach, >= critical and >= warning into the healthy branch.
A broken feed must fail closed.python -m unittest discover -s skills/broker-account-margin-call-handling/scriptsexcess_liquidity and confirm the
state is MARGIN_CALL_BREACH with broker_deficiency set — this is the failure mode
most likely to be missed in production.MarginDataError every time; a
NORMAL result from any of them is a fail-open bug.WARNING.initial_margin_impact exceeds available_funds and confirm the
veto, even when the maintenance projection alone would have passed.kill-switch-and-drawdown-circuit-breakersoptions-margin-span-calculation-globalcorrelation-aware-exposure-limitsmargin-utilization-circuit-breakerWhat 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-agnostic-adapter-interfaceUse when strategy code must run across several brokers and should not import kiteconnect, alpaca-py or ibapi directly; defines an adapter contract with Decimal prices, typed exceptions and normalised order status across venues.Other plugins' skills for the same job