How to Backtest a Trading Strategy With Claude
How to backtest a trading strategy with Claude: prompts that produce testable code, the lookahead and overfitting checks to demand, and how to run Claude-written strategies on real data.
By Polarline Markets team
Claude is very good at writing trading strategy code. It is not a backtester. A backtest happens when code runs against historical market data and produces trades you can inspect. This guide shows how to use Claude for the part it is good at, and how to make sure the numbers that come back are real.
The one rule
This is not a criticism of Claude in particular. In our ScoutBench AI trading benchmark, every agent we tested, with the same model held constant, scored at most 28.3 out of 100 on hallucination resistance. Language models invent plausible numbers. Your workflow has to stop that from reaching a decision.
Two ways to backtest with Claude
Option 1: Claude writes the code, you run it
Ask Claude, in the chat app or in Claude Code, to write a self-contained Python backtest, then run it on your machine with data you chose. You stay in control, but you also own every check: data loading, fills, fees, and validation.
Option 2: Claude inside a backtesting harness
Use Claude as the model inside a tool that already enforces a strategy contract and runs the backtest for you. Scout supports this with bring your own key: add an Anthropic key, and Claude writes the strategy while Scout's engine handles data, next-bar fills, a strategy contract that guards against lookahead, and robustness tests.
A prompt that produces testable code
Whichever option you pick, be explicit. Vague prompts let the model choose parameters you never decided on.
Write a Python backtest for this strategy and nothing else: - Market: SPY, daily bars, 2015-01-01 to 2025-12-31, loaded from a CSV I provide - Entry: 20-day average of prior closes crosses above the 50-day average - Exit: 20-day average crosses back below the 50-day average - Size: 95% of cash, long only, no leverage - Fills: at the NEXT bar's open, never the signal bar's close - Costs: 0.05% per trade Report CAGR, max drawdown, Sharpe, trade count, and a full trade list. Do not estimate or describe results. I will run the code myself.
The last line matters. It tells Claude its job is the code, not the verdict.
Five checks before you believe the result
- Lookahead: indicators must use only bars before the decision, and orders must fill on a later bar. Ask Claude to point to the exact lines that guarantee both.
- Out-of-sample: fit on one period and test on another. A walk-forward split shows whether the edge holds on data the parameters never saw.
- Nearby parameters: rerun with 15/45 and 25/55 instead of 20/50. A real effect does not vanish with a small change.
- Trade order: shuffle or resample trades to see the range of outcomes luck alone could produce.
- Data quality: look for gaps, splits, and bad bars before trusting any metric.
Running Claude-written strategies in Scout
In Scout, the same idea becomes a strategy class that follows the Shape-C contract: an on_bar method, an injected broker, next-bar fills, and only the bar's open treated as known at decision time. The steps:
- Install the Scout CLI on macOS or Linux.
- Run
/initand choose Anthropic, then paste your own key. Keys are stored in your local Scout config. - Describe the strategy in plain English, as specifically as the prompt above.
- Read the code with
/code, check it with/review, and run it with/backtest.
Each run reports more than 60 metrics, with optional walk-forward, Monte Carlo, regime, and data-quality blocks, plus a full trade log. The AI backtesting overview explains each check.
Does the harness matter if the model is the same?
ScoutBench was built to answer that. With one model held constant across four harnesses, Scout averaged 53.3 and Claude Code 46.3 across five trading tasks. The biggest gaps were on repairing lookahead bias (70 against 51.7) and building a BTC strategy end to end (81.7 against 55). Claude Code did better on numeric discipline (45 against 30). The takeaway is not that one tool is always better; it is that the checks around the model change the outcome. See the full method.
Using ChatGPT instead? Read our ChatGPT trading strategy backtest guide. To keep everything local with your own key, start with the Scout CLI, the AI trading terminal.
Backtests are research, not investment advice. Past performance does not guarantee future results.
