/algo-trading-skills:alpaca-paper-live-key-separationfrom algo-trading-skills
Prevents catastrophic cross-environment mistakes when connecting bots to the Alpaca Trading API. Pins base URLs per environment (paper vs live), validates key prefixes, probes the account endpoint, and requires an explicit ALLOW_LIVE_TRADING flag before allowing orders.
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 a bot connects to the Alpaca Trading API (or any broker with distinct paper/live environments). Connecting a paper strategy to Alpaca's live endpoint (https://api.alpaca.markets) using live API keys — or passing live order signals into a paper endpoint — is a catastrophic operational error. Pinning the base URL per environment, inspecting the key prefix, probing /v2/account for tradability, and requiring an explicit ALLOW_LIVE_TRADING environment variable are all mandatory before any order is submitted.
The base URL is the control that actually separates the environments. Alpaca serves paper accounts from https://paper-api.alpaca.markets and live accounts from https://api.alpaca.markets; a live account is not reachable through the paper host. Every other check in this skill is defence-in-depth layered on that pin — treat them as corroboration, never as a substitute.
headless-broker-auth-patterns).PK.../AK... prefix convention is Alpaca-specific. Brokers using different key formats (e.g., IBKR account IDs DU.../U...) require their own validation logic. For a broker-agnostic host allow-list, see sandbox-credential-leakage-prevention.ALPACA_PAPER_KEY_ID vs ALPACA_LIVE_KEY_ID).https://paper-api.alpaca.markets for paper vs https://api.alpaca.markets for live).ALLOW_LIVE_TRADING=true environment flag for live execution mode.Load & Normalise the Environment Mode:
PAPER/LIVE value before any comparison. A value matching neither must raise, never fall through — an unrecognised mode that skips both branches is an authorisation, not a no-op.Inspect Credential Prefixes:
PK... indicates Alpaca paper credentials; AK... indicates live credentials.references/standards.md).Base URL & Mode Verification:
PAPER mode → https://paper-api.alpaca.marketsLIVE mode → https://api.alpaca.marketshttps://api.alpaca.markets.attacker.example — is rejected. Never use a substring or startswith test here.Live Execution Safety Gate (ALLOW_LIVE_TRADING):
LIVE mode unless ALLOW_LIVE_TRADING=true is explicitly set. Strip the value before comparing — a trailing newline from a .env loader should not silently block a legitimate live deployment.Account API Probe:
/v2/account on startup and reject a response that is not a mapping.status must be ACTIVE (or PAPER_ONLY, which is valid only in paper mode). A missing status is a veto, not an assumed ACTIVE.trading_blocked, account_blocked, or trade_suspended_by_user. Alpaca documents the first and last as "the account is not allowed to place orders" — a guard that ignores them authorises orders the broker will reject./v2/account does not return an is_paper field (verified against Alpaca's account schema and the official alpaca-py TradeAccount model). Use, in order: an is_paper bool if your SDK wrapper injects one; status == "PAPER_ONLY"; an account_number beginning PA (observed, unofficial — treat as a positive paper signal only, never as proof an account is live).Order Submission Veto Guard:
AlpacaEnvironmentManager.guard_order(), vetoing any outbound order if environment checks fail or key/URL mismatches occur.qty, an empty symbol, and any side outside buy/sell.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.
https://api.alpaca.markets in code and relying only on switching API keys in .env.ALPACA_KEY_ID for both paper and live testing, leading to accidental live deployment.is_paper field: Alpaca's /v2/account response has no is_paper key. Code written against it reads None on every call — so a guard that treats a missing value as live bricks paper trading, and one that treats it as paper waves live accounts through. Resolve the environment from fields the API really returns, and treat "unknown" as unknown.if mode == PAPER … elif mode == LIVE … with no else returns success for any third value, so a typo'd or foreign enum member authorises a live order with no ALLOW_LIVE_TRADING check. Normalise the mode up front and raise on anything unknown.status to ACTIVE: A truncated or error-shaped account payload then reads as a healthy account. An unreadable account is a veto.trading_blocked / account_blocked / trade_suspended_by_user mean the broker will refuse the order. Catching that locally turns a confusing broker rejection into a clear pre-trade veto.is True on a loosely-typed payload: An adapter that hands back "true"/"false" as strings defeats if account_data.get("trading_blocked") is True, so an explicitly blocked account reads as unblocked. Accept the string forms and fail closed on a value you cannot interpret.base_url.startswith("https://api.alpaca.markets") also accepts https://api.alpaca.markets.attacker.example. Compare against an exact allow-list.ALLOW_LIVE_TRADING=true).AlpacaEnvironmentManager raises EnvironmentMismatchError.ALLOW_LIVE_TRADING=true and confirm execution is blocked.is_paper=False when configured in paper mode and confirm the startup veto.is_paper field (status=ACTIVE, account_number=PA…) and confirm it is accepted, not vetoed.trading_blocked=true and confirm the order guard vetoes.status field and confirm the veto.qty=0, qty=NaN, and side="long" through guard_order() and confirm each is rejected.python -m unittest discover -s skills/alpaca-paper-live-key-separation/scripts and confirm a 100% pass rate.paper-to-live-promotion-checklistheadless-broker-auth-patternskill-switch-and-drawdown-circuit-breakerssandbox-credential-leakage-preventionsandbox-vs-production-endpoint-driftWhat 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
/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-interfaceOther plugins' skills for the same job