A local-first backtest engine built to reject my own trading rules
A personal research system. It reads my trade fills and public market data on my laptop, scores each trade against written rules, and backtests rule ideas before I am allowed to believe them. Its most useful output so far is the list of ideas it killed, starting with the first one that looked great.
Solo project, October to December 2026. Stack: Python (153 files, about 22,000 lines), pandas, launchd, Chrome DevTools Protocol. The main repository is private; two screeners that came out of it are open source.
Why local-first
The inputs are my own fills and statements. I did not want them on anyone's server, and I did not want broker credentials in code.
- Fills without credentials. The first importer read confirmation emails through a browser that was already logged in, over the DevTools Protocol on localhost. The script never types a password. A later version reads the local mail client instead.
- Read-only broker access. One broker is read through its official read-only API, with keys in the macOS Keychain.
- Data stays on disk. The repository ignores everything by default and allow-lists code and documents. Price panels are local files of about 42 MB and 52 MB.
- The laptop is the scheduler. Jobs ran under launchd, and the dashboards were served on localhost.
What does leave the machine: requests to public market-data endpoints, one text-parsing step that calls a hosted LLM API, and code pushed to a private repository. One small replay tool was deployed to a static host behind a signed-link gate.
Pipeline
Each input in the data layer has a primary source, a fallback and a local cache. Every refresh records how fresh each input is, and a stale input is flagged in the report instead of being used silently. After one data provider started refusing requests, I added per-domain route memory so a refresh tries the environment proxy, a direct connection and the system proxy in the order that last worked.
Keeping the future out
- Signals are computed at the close and executed the next day.
- Buys are limit orders at the prior close and fill only if the next day's low reaches them.
- The first 252 trading days are warm-up and are never evaluated.
- A stock can be picked only after the date it joined the index, which removes "it will be added later" as hidden knowledge.
- Costs are 0.15% per side. Idle cash earns the three-month bill rate.
The falsification log
Each row is a claim I wanted to be true, the control I ran against it, and what happened. Returns are annualised unless stated. The benchmark is an index ETF held over the same period, which returned 19.4% a year.
| Claim | Control | Result | Verdict |
|---|---|---|---|
| Version 1: buy the strongest three-month performers in one sector | Backtest with a 14-row ablation grid | 26.2%, against 19.4% | Looked like a win |
| …and the ranking is what earns it | Same rules, ranking replaced by random numbers, 20 seeds | Random mean 12.3% | Ranking matters |
| …and the rules generalise | Same rules on the whole index and on 5 sector groups | 10.3%, against 11.5% equal-weight | Rejected |
| …and the entry and exit rules add value | Random picks under the rules, against simply holding the sector equal-weight | 12.3%, against 24.1% | Rules subtract. v1 retired |
| Which entries and exits work at all | 10 entries × 8 exits, with a random-day baseline, in two halves (2017–21, 2022–26) | Random +1.57% per trade; best entry +2.61%; breakout +0.08% | Kept cells positive in both halves |
| A fixed 12% stop protects the downside | Same grid | About a point less on average, and a worse worst-decile | Stop removed |
| Market-environment factors carry an edge | 60,936 events bucketed by factor, both halves | 8 features held in both halves; 4 did not | 8 in, 4 out |
| Version 3: the resulting score can run a portfolio alone | Portfolio backtest, 8 variants | Best 10.7%, with a 38% drawdown | Rejected as a standalone |
| Nine published classic strategies work on this data | Same data, same costs | None beat the plain trend-filtered index on risk | None adopted |
| Buying sharp post-earnings drops | Fitted on four months (161 events), then run on two unseen years (1,243 events) | +4.6% → +0.2% over 5 days; win rate 69% → 49% | Failed out of sample |
| An order-flow reversal rule from a trading community | 76 days of tick data; blind controls, a mirror test, a 2,000-draw bootstrap by day | Original rule lost. The mirrored rule also lost, so the effect is not symmetric. | Original rejected |
The retirement note for version 1 says its good result most likely came from picking the right sector with hindsight, plus survivorship in today's index members, plus parameters chosen on the same data. Version 3 survives only as a small sleeve beside a plain index core, and even that rests on thresholds chosen in-sample.
A simplified version of the order-flow rule is kept as a paper-trading candidate, with a note attached that its numbers are in-sample.
Decisions inside the engine
- Weights are measured, not chosen. A feature's weight is its measured edge over the baseline, scaled and rounded to the nearest half point.
- Position size is a risk budget. Each slot gets 3% of net value divided by one-month volatility, capped per name and per sector.
- Exit on a signal or on time. Positions close on a momentum reversal or after 60 days. The fixed stop is gone because the grid above said so.
- Features with no history are logged, not scored. Inputs I only started collecting recently go to a daily log until there is enough data to test them.
Biases and gaps that remain
- Survivorship. The universe is today's index members. Delisted and removed names are missing, so absolute returns are too high. Comparisons between variants are more trustworthy than any single number.
- No walk-forward. Thresholds and weights are fitted on the full sample. The two-halves split is a stability check. It does not select parameters.
- One true out-of-sample test, and it failed.
- Prices are split-adjusted and not dividend-adjusted, for the benchmark as well.
- A flat cost. There is no model of slippage or market impact.
- Thin tests. One unit-test file and one table test with 26 cases exist. Neither covers the backtest engine.
- Three simulators coexist. The "unified" engine did not replace the two older ones.
- One set of reported numbers has no committed script that reproduces it. The nearest committed run comes out lower.
What became public
Two screeners that carry no positions and no recommendations are open source.
- us-lowpos-fig: a three-gate screen over US large caps with a 100-point score and no third-party dependencies. It never outputs "buy".
- hk-cigarbutt-daimi: a four-gate balance-sheet screen over Hong Kong listings.