Scheduled paper-trading routines for Alpaca with alerts, a dashboard and risk rules the code cannot skip.
A replay of the real output of python scripts/demo.py, captured in docs/demo-captured.txt. It runs against the local test server on made-up prices.
In a full made-up trading day against a local test server, the bot opened three positions with stops, then the price tape was dropped 40 percent overnight. The daily loss limit tripped the kill switch, the next open was refused, and the account held no positions and no orders (captured output, reproduce it with python scripts/demo.py).
Someone who wants an AI-assisted trading system starts with paper money, and the first thing that can go wrong is the bot, not the market. A model that suggests a 10,000 share order, a rerun that sends the same order twice, a crashed script with a stop that was never placed. ledgerbell is the small, boring part that makes those cases impossible to reach: one gateway every order passes through, five scheduled routines, and a log line for everything.
The owner sets the strategy. The example rule in strategy.py (buy above the 20 day average, sell below it) only shows where theirs plugs in and makes no claim about returns.
flowchart LR
C[cron: 5 routines] --> R[routines.py]
R --> M[Claude Code headless, no tools]
R --> S[strategy rule]
M -->|JSON proposals| G
S -->|proposals| G[Desk.execute: risk rules, stop, ledger]
G -->|OTO order with stop| A[Alpaca REST, paper only]
R --> L[(ledger.jsonl)]
R --> N[email and text alerts]
L --> D[dashboard.html]
K[kill switch: KILL file] --> G
| Routine | When (New York, weekdays) | What it does |
|---|---|---|
| premarket | 08:00 | Reads account, positions and daily bars, asks the model for a briefing and suggested orders, saves the day's plan, alerts |
| open | 09:35 | Runs the daily loss check, sends the plan and the rule's orders through the risk rules, audits stops, alerts |
| midday | 12:00 | Loss check, places any missing stop, warns on a position halfway to its stop |
| eod | 16:15 | Day recap with orders sent and refused, rewrites the dashboard, alerts |
| weekly | Friday 18:00 | Counts a week of ledger events, lists recent refusals, writes a report file, alerts |
The hard rules, all in risk.py as pure functions:
- Max position size: one symbol holds at most
max_position_pctof equity, counting what is already held. An order that is too big is cut down, one with no room is refused. - Max open positions, no short selling, and only symbols on the watchlist.
- Daily loss limit: equity down
daily_loss_limit_pctagainst yesterday's close refuses new buys and flattens the account. - Stop loss: every entry is an Alpaca OTO order, so the stop lives at the broker and survives this program crashing. The midday audit places a stop on any position that has none, and closes one that is already past it.
- Kill switch: a
KILLfile. Engaging it writes the file first, then cancels all orders and closes all positions, then alerts. Nothing trades until a person runsledgerbell unkill --yes. - Reruns are safe: order ids are built from the routine, the date, the symbol and the side, and
client_order_idis documented as a unique identifier, so a second run of the same routine cannot place the order twice (the test server rejects a duplicate the same way).
Every order from the example rule or the model goes through Desk.execute, the only place an order endpoint is called.
Options leg (off until [options] enabled = true). One shape of option order can leave the program: a single leg long call or put bought to open, with a premium cap (1 percent of equity, premium already held counted), an expiry window (7 to 45 days) and a stop on the premium (50 percent under the entry). A short or naked leg, a spread, a sell, a contract outside the window, a symbol off the watchlist and a second entry while a held contract has no stop are each refused with a named reason before the broker is called. ledgerbell options SPY prints contracts with prices and greeks. Alpaca's documentation lists no OTO class for options, so the stop is sent once the entry has filled and the open and midday audits cover a contract that missed it. Rules, quotes and limits: docs/OPTIONS.md.
More market data providers. [market_data] provider = "finnhub" takes each symbol's latest price from Finnhub's free /quote endpoint (key in FINNHUB_API_KEY) to size an order and place its stop. The free key answers 403 for daily candles, so the moving average still comes from Alpaca's bars and the ledger records the fallback.
provider = "polygon" does the same with Polygon (renamed Massive in October 2025): the key is sent as Authorization: Bearer, a 429 or a 5xx is retried twice, and daily bars come from the aggregates endpoint the free plan includes. Hosts, the free plan limits and the fallback: docs/INTEGRATIONS.md.
Text message alerts. Besides email and a chat webhook, sms_to and sms_from in [alerts] send each alert as a text through Twilio's Messages API (basic auth, a form body, cut to 1,600 characters, credentials from the environment only). A channel that fails is named in the alert problems list and never stops a routine. Details: docs/INTEGRATIONS.md.
Airtable run log. [airtable] enabled = true appends one row per routine run (routine, time, status, orders suggested, placed and refused) through Airtable's REST API; python scripts/airtable_setup.py makes the table. A failing log never fails a routine. Details: docs/INTEGRATIONS.md.
What ran for real (options only, all together), on the paper account during market hours: 3804 active SPY contracts in the window, one contract bought at 7.16 with its stop at 3.54 accepted, a second refused by the premium cap, then the four routines with Finnhub and Airtable on (a real AAPL quote of 349.45, five Airtable rows written and read back) and kill flattening the account. The real run also found a bug the stand-in had hidden: the midday scan measured an option against the share stop share. It now uses the option one, with a test.
An HTTP API (FastAPI, src/ledgerbell/api.py) answers "would this order pass the risk rules" and sends alerts, and runs on Modal's free plan. Alerts can also go out through Amazon SES, and a Netlify function reports whether the bot is still running. All three ran for real: the Modal endpoint answered in 4.6 s cold and about 1.1 s warm, one test alert was delivered through SES to a verified address, and the Netlify status function answers 200 (https://ledgerbell-status.netlify.app/status). Figures, the deliberate breakages and the limits: docs/SERVERLESS.md.
rust/ holds bookwatch, an async Rust (Tokio) client that keeps a local order book from a websocket depth stream: a snapshot, then deltas with update ids, a new snapshot when an id is skipped, reconnects with backoff that lose and double apply nothing, and a bounded channel so a slow consumer loses notifications and never state. delta_position turns a leader's position change, a slippage cap and a book into a follower order. A worker fleet sits on top: an Axum control server, Protobuf over WebSockets with monotonic sequence ids, replay and failover to a second server, SQLite in WAL mode for the worker's acknowledged position and nonces, EIP-712 signing checked against the specification's own test vector with a throwaway key, and a slippage ladder, momentum filter, fill stabilization window and circuit breaker as tested functions.
It was built and tested against local stand in servers written from Binance's and Polymarket's public documentation, never a live exchange or feed. 194 tests pass, some of them killing real processes; cargo-mutants on the whole crate caught 239 of 491 mutants (250 did not compile, 1 equivalent survived, 1 timed out). On an AMD Ryzen 7 9800X3D the median delta to best bid change was 24 to 33 microseconds over loopback, a timing of this code and not of a network or an exchange. Its own workflow, rust.yml, runs on Linux when rust/ changes. Details and captured runs: rust/README.md.
agent.py runs Claude Code headless (claude -p --tools "", every tool off) on a prompt with the account and ten closes per symbol, and demands strict JSON. The reply is validated (symbols on the watchlist, whole share quantities, at most five orders), re-asked once with the exact validation error, and dropped if it is still wrong. Prices come from our bars, never from the model. The model can suggest an order and cannot place one.
Captured run with the real model: the briefing and the two suggested orders are real Claude Code output on the test server's made-up prices. The risk rules then cut the 15 share SPY suggestion to 9 to fit the size limit. The model is not deterministic, so another run suggests something else.
You need Python 3.11 or newer. For the real thing, also a free Alpaca paper account and its trade only API keys. The test server needs no account.
python -m venv .venv
.venv\Scripts\activate # Windows; source .venv/bin/activate on Linux and macOS
pip install -e ".[dev]"
python scripts/demo.py
The last line runs a whole made-up day and prints it. For your own paper account (keys come from the Alpaca dashboard; python scripts/paper_check.py is a harmless first run that cancels what it places):
cp config.example.toml config.toml
export APCA_API_KEY_ID=... APCA_API_SECRET_KEY=...
ledgerbell run premarket
ledgerbell status
deploy/crontab schedules all five routines on the New York clock. Other commands: ledgerbell kill --reason "...", ledgerbell unkill --yes, ledgerbell dashboard, ledgerbell options SPY (contracts with greeks), ledgerbell quote AAPL (the configured provider's price) and ledgerbell standin to start the test server by itself. The options, Finnhub, Polygon, Twilio and Airtable settings are in config.example.toml, each off until switched on. The live endpoint is refused unless the config says mode = "live" and live_confirmed = true.
python -m pytest # 271 tests, about three minutes
python scripts/mutants.py # removes one guard at a time in a temp copy
The tests drive the real client over HTTP against the test server, which is written from Alpaca's public documentation: orders with stops, a duplicate order id, a 401, paging through bars, stops firing on a moving tape, the loss limit tripping the kill switch, a broker that is down while the kill switch engages, alerts to a real local webhook, and the command line end to end. The options leg adds the same stand-in endpoints for contracts, snapshots and option orders, and there are local stand-ins for Finnhub, Airtable, Polygon and Twilio. The mutation run breaks 86 guards one at a time (each risk rule and each option rule, the exit stop, the kill file written first, the paper host check, the key handling of each provider and more): 86 caught, 0 survived, 0 stale anchors (output, which keeps the first run where one survived and the test it led to).
On a Monday evening with the market closed, scripts/paper_check.py and the routines ran against a real Alpaca paper account (captured output): account, clock and daily bars from the free IEX feed, a one share entry with an attached stop accepted as a queued OTO order, the same client_order_id refused a second time with a 422, cancel all and close all, then premarket with the real model on real prices, open skipping because the market was closed, and eod. That run found a bug the test server had hidden: Alpaca's bars start defaults to the beginning of today, so daily bars came back empty until the client sent a start date. The test server now behaves the same way and a test pins it.
- Fills, the stops and the kill switch were seen on a real paper account for one session (2026-11-09). A stop firing at the broker and the daily loss limit on a real account are tested against the local test server only, and the 60 to 90 days of paper running the work is meant for have not been done.
- The test server fills market orders at its own price and fires stops at the next price it is given. It does not model slippage, partial fills, fees or the market calendar, and its option chain is made up: no implied volatility surface, assignment, exercise or early expiry.
- Shares are whole, long only, US stocks. Options are single long calls and puts bought to open, on Alpaca's free indicative feed whose quotes are modified (so the entry is a limit a few percent over the ask). No spreads, no short or covered legs, no exercise or assignment handling. Crypto is not covered.
- Finnhub's free plan has no candles, so it supplies prices and Alpaca still supplies the bars.
- The Polygon provider and the Twilio text alert have only been run against local stand-ins written from the vendors' documentation. Polygon's free plan allows five calls a minute and a routine makes about ten for a five symbol watchlist, so on that plan some calls get a 429, the routine falls back to Alpaca's bars and bar closes for them and the ledger says so.
- Market holidays are handled by asking Alpaca's clock, not by a calendar here.
- The email alert is tested over a real SMTP conversation with a small local mail server (no login or TLS); it has not been sent through a hosted mail provider. The webhook alert is tested over HTTP, and the text alert over HTTP against the Twilio stand-in.
- Nothing here is investment advice, and the example rule has not been backtested.
Backtesting and a Notion log are next. The run log goes to Airtable today.
Python 3.11, requests, FastAPI on Modal, boto3 with Amazon SES, a Netlify Function, pytest, ruff, GitHub Actions, Alpaca's trading and market data REST APIs (options included), Finnhub, Polygon, Twilio, Airtable, Claude Code in headless print mode, plain cron.
MIT, see LICENSE.
