Stock trading bot: momentum rotation with a crash guard
An ML momentum-rotation strategy on the Nasdaq-100: 126-day lookback, monthly rebalance. CatBoost scores the universe. A crash guard and a sector cap keep the book honest when the regime turns.
An ML momentum-rotation strategy trading the Nasdaq-100, live on Alpaca paper trading since April 2026.
Strategy flow
- Alpaca WebSocket: Nasdaq-100 quotes and daily bars, auto-reconnect with backoff.
- Redis cache + Postgres: hot tick fan-out, 126 days of price history.
- CatBoost momentum scorer: ranks the universe on 126-day lookback features.
- Monthly rebalance planner: target weights, turnover-aware rotation.
- Risk gates → Alpaca paper: crash guard, sector cap, idempotent orders, full audit trail.
Risk rules
| Rule | What it does |
|---|---|
| Crash guard | Regime filter. When the index breaks trend, the book rotates to cash instead of chasing momentum down. |
| Sector cap | Concentration ceiling so a hot sector can’t own the whole portfolio. |
| Paper first | Live on Alpaca paper since April 2026. The real execution path, zero live capital until the track record earns it. |
| Monthly cadence | Rebalance is scheduled, not reactive. No intraday overtrading. |
Tech stack
CatBoost, FastAPI, Redis, PostgreSQL, Alpaca WebSocket.
From the original write-up (April 2026)
Status at the time: Phase 10, 14-day paper trading validation. Core mission: Execute high-probability trades with “Defense in Depth” safety protocols.
The bot operated as a stateful graph where each node is a specialized agent, so market data, sentiment, and risk are analyzed in a strict, logical sequence.
The LangGraph pipeline
- Scanner: ranks top tickers (AAPL, NVDA, etc.) via Claude based on momentum and volatility.
- Market data: fetches OHLCV data and computes technical indicators (RSI, MACD, Bollinger Bands).
- Research: aggregates news sentiment and fundamental analysis.
- Strategy: Claude synthesizes data into a concrete signal (entry, SL, TP).
- Risk gate: a 6-layer validation check (circuit breaker, position limits, R:R ratio).
- Human gate: terminal-based approval interrupt for final execution authority.
- Executor: automated order submission to the Alpaca API.
- Journal: post-trade analysis saved to PostgreSQL for continuous learning.
“Defense in depth” safety system
Capital preservation is more important than profit.
| Layer | Component | Function |
|---|---|---|
| Layer 1 | Redis circuit breaker | Hard-stop on all trading if daily loss hits $150. |
| Layer 2 | Paper guard | Hard-coded block that prevents live execution unless two separate flags are met. |
| Layer 3 | Risk gate | Validates risk/reward is ≥ 2.0 and position size is capped at $500. |
| Layer 4 | Human-in-the-loop | Requires manual approval in the terminal before any order hits the broker. |
Backtesting
A dedicated backtesting engine (scripts/backtest.py) replicates live signal logic without LLM costs: long-only momentum, 3% stop loss / 6% take profit, with Sharpe ratio, profit factor and max drawdown tracked by the journal agent.
Graduation criteria (paper → live)
- Zero unhandled exceptions over 14 days.
- At least 20 full cycles completed.
- Safety block rate remains below 30%.
- 100% adherence to the $500 position limit.
Need something like this?
Tell me what you do by hand today. I'll reply within two working days with whether I can help and what I'd build first.