First, the disclaimer that matters: Rulebook Trader trades on an Alpaca paper account. No real money, ever. The app checks which trading endpoint it's pointed at on startup and refuses to place a single order if it isn't the paper API. This is a side project, and it exists for three reasons: it's fun, it's a cheap way to test trading ideas against live markets instead of a spreadsheet, and it's a sandbox for trying tools I want to know well before I put them in front of a client.
The interesting part isn't the trading. It's the division of labor. Most "AI trading bot" pitches have a model picking stocks. This one deliberately doesn't. The rules make every decision; the AI gets exactly one narrow job, and no authority to override anything.
The rules do the trading
Every strategy is plain, deterministic code you could write on an index card. Trend + pullback buys a stock above its 50-day average when 14-day RSI dips below 35, with a 6% target, a 3% stop, and a 10-day maximum hold. RSI-2 mean reversion buys short, sharp dips in stocks above their 200-day average. Momentum breakout buys new 20-day highs on heavy volume with a trailing stop. Each has a bearish mirror image that only trades puts. They run side by side, every parameter is editable in the dashboard, and adding a new strategy is one file.
Around the strategies sits a layer of global risk rules nothing can override: position size as a percent of equity, maximum open positions overall and per strategy and per sector, a cap on new entries per day, a daily-loss halt, a re-entry cooldown, and a kill switch. Entries go in as bracket orders, so the stop and target live at the broker even if the app misses a tick.
One narrow job for AI: catch what the rule can't see
A rule that says "buy when RSI is oversold" has no idea why the stock is oversold. Sometimes it's an ordinary pullback. Sometimes it's a fraud announcement, an earnings miss, or a stock split that broke the price history. That gap is where the AI comes in.
Before any new entry, Claude Haiku gets the strategy's rules, the signal's indicators, the last 15 daily bars, and recent headlines for the symbol. Its instructions say, in so many words, that its job is not to second-guess the strategy's technical logic. It looks for exactly three things: news that changes the thesis, a binary event like earnings within a couple of trading days, or data that looks broken. It approves by default, vetoes only with a concrete reason drawn from the inputs, and is told never to invent news that isn't in the headlines.
The answer comes back as structured output, validated against a schema: a decision, a confidence score, a one-sentence reason, and up to three risk notes. It can run in two modes: veto, where it can block a trade, or advisory, where it only records an opinion. And it fails closed. If the review call errors out in veto mode, the trade is skipped, not waved through. Each review costs about $0.0005. After the close, the same model writes a short daily journal from the day's signals, fills, and scoreboard.
The stack, and why each piece
- Next.js on Vercel. The dashboard, the API routes, and the trading engine live in one TypeScript app, deployed on a free Hobby plan. One codebase, one deploy.
- Neon Postgres with Drizzle. Serverless Postgres suits a workload that wakes up once a minute and sleeps otherwise, and the free tier covers it. Drizzle keeps every query typed against the schema.
- A tiny Netlify function as the clock. Vercel's free plan only allows daily cron jobs, and the engine needs to tick every minute during market hours. Rather than pay for an upgrade, a separate Netlify scheduled function calls the engine's tick endpoint every minute on weekdays, authenticated with a shared secret. Vercel Cron still handles the two daily jobs: re-ranking the stock universe and writing the journal.
- Alpaca's paper API and free IEX data. Real order types, real fills against live quotes, zero risk, zero cost. Once a week, before the open, the scan list is re-ranked to the 500 most actively traded stocks plus the top 50 ETFs.
- Claude Haiku with Zod. The cheapest capable model is plenty for a bounded review task, and the same Zod library that validates the app's data also defines the reviewer's output schema.
All in, the infrastructure costs nothing, and the AI costs one to three cents a day.
Every decision gets logged, not just the trades
Every completed trade becomes one row in Postgres with the full story: which rule fired, the parameters it was running with, the app version, the indicators at entry and exit, slippage, the best and worst points while the position was open, the return versus simply holding SPY, and Claude's verdict, confidence, and reason. Every signal that didn't become a trade, because a risk rule blocked it or Claude vetoed it, gets logged too.
That second table is what makes the reviewer testable instead of decorative. The performance page compares how vetoed signals moved over the next three sessions against the approved ones, and checking whether Claude's confidence actually predicts anything is a one-line query against the trade log. A separate backtester replays each strategy's own entry and exit code against up to three years of daily bars, so a parameter change can be checked against history before it touches the live paper account. The backtester's known blind spots are written down, too: it leaves the reviewer out, and it uses today's symbol list for the whole period, which flatters the results.
Why this pattern is bigger than trading
Strip out the stock tickers and here's what's left: a repeated decision made by transparent rules, a cheap AI layer with a narrow mandate and veto power only, hard limits nothing can override, a fail-closed default, and a log of every decision, including the ones that didn't happen. That shape fits a lot of businesses that will never touch a brokerage account.
- Lead and dealer scoring. Rules rank prospects on fit and territory; AI only flags the ones whose reviews or news suggest the score is missing something.
- Purchase and expense approvals. Policy rules auto-approve the routine; AI reads the memo and flags anything that doesn't match the stated purpose, for a person to decide.
- Reorder and pricing rules. Thresholds trigger the reorder or the price change; AI checks for a known reason not to, like a supplier notice or a recall.
- Intake and compliance queues. Rules clear the easy cases; AI reads the edge cases and routes them to a person with a one-line reason attached.
In each case the AI is cheap, bounded, and checkable. You can see exactly what it blocked and whether blocking it was right. That is a far easier thing to trust, and to hand to a team, than a model making the decision itself. It's the same approach behind where AI does and doesn't pay off: start with a transparent rule, and let the logged outcomes tell you whether anything smarter is worth building.
The live dashboard is public and read-only at alpaca-agent-trader.vercel.app. None of it is investment advice. It's a workbench.
Let the rules decide. Give AI one narrow job, the power to say no, and a log that shows whether it was right.