/algo-trading-skills:broker-margin-interest-accrual-trackingfrom algo-trading-skills
Computes net P&L for leveraged or short books by modeling tiered margin-loan rates, short borrow fees, and calendar-day accrual with 360/365 day counts.
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 computing net P&L for a strategy that borrows — one that runs leverage, carries positions overnight, or sells short. Backtests that ignore financing overstate returns and Sharpe by the entire cost of the leverage that produced them, and the overstatement scales with the benchmark rate: a book carrying a permanent debit at a 5% margin rate gives up 5% a year before it makes a single trading decision.
Use it to answer three questions with numbers rather than estimates: what does the debit balance cost under the broker's tiered schedule, what does the borrow cost on the short book, and what does gross P&L become once both are subtracted.
tiers_from_benchmark(benchmark_apr, spreads).Configure the schedule, and let it reject bad input. Build tiers with
tiers_from_benchmark or explicit MarginRateTier values. The constructor validates
that brackets start at zero, are contiguous, and that the top tier is open-ended.
That last check is not pedantry: a schedule capped at $100k silently prices everything
above the cap at 0%, so a $200k balance reports 2.5% instead of 5% — half the true
cost, with no error.
Feed dates, not day counts. Call accrue_daily_balances(balances, through_date)
with one EodBalance per observation. It derives the day count from the dates, so it
cannot be handed a trading-day count by mistake. Use the scalar
calculate_interest_accrual(start_date, holding_days, ...) only for a constant
balance, and read holding_days as calendar days.
Know why weekends cost three days. Not because settlement is slow — because the balance still exists on Saturday and Sunday and interest is computed on the daily balance. The consequence is the part people get wrong in both directions: the total depends only on the number of calendar days in the window. Batching Sat/Sun into Friday's ledger row changes granularity, never the total. Applying a weekend multiplier on top of a calendar-day count double-charges; feeding a trading-day count under-charges by roughly 2/7.
Register holidays for ledger alignment, not for extra cost. add_holidays() makes
the Friday before a holiday Monday carry one four-day block rather than a three-day
block plus a row dated on a day the market was shut. The total is identical either
way; what changes is whether the ledger reconciles line-by-line against the broker's.
Charge the borrow fee on collateral, not market value. IBKR computes it as
value x rate / 360 where value is 102% of the prior day's settlement price,
rounded up to the next whole dollar, times shares. Set short_collateral_markup=1.02
to approximate it, or pass an exactly computed EodBalance.short_collateral_usd to
match a statement. The default of 1.0 charges on raw market value and understates.
Subtract both from gross P&L. adjusted_net_pnl_usd is gross less margin interest
less borrow fees. Report it alongside gross, never instead of it — the gap between the
two is the number that tells you whether the leverage was worth carrying.
Full procedure: see
references/workflows.md. Sourced day-count, collateral and settlement conventions: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
RateScheduleError rather than returning a plausible-looking number.FinancingDataError.python -m unittest discover -s skills/broker-margin-interest-accrual-tracking/scripts100000 * 0.05 / 360 * 7), with the Friday row carrying days_accrued == 3.RateScheduleError; pass NaN
as a balance and confirm FinancingDataError. A number coming back from either is a
fail-open bug.short-selling-borrow-cost-and-availability-modelingbroker-account-margin-call-handlingmulti-currency-pnl-and-fx-conversionbacktesting-ml-models-against-transaction-costsexecution-realistic-simulationWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
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./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./broker-api-changelog-diffing-toolUse before upgrading a broker SDK or OpenAPI spec, to diff two schema snapshots for removed endpoints, newly required parameters, enum mutations and type changes so CI fails before the change reaches an order path./broker-api-deprecation-notice-monitoringUse when a long-running bot calls REST endpoints a broker may retire; parses RFC 8594 Sunset and RFC 9745 Deprecation headers plus changelog feeds into one dated migration deadline with an escalation tier.Other plugins' skills for the same job