AI trading workflows

Backtest a trading strategy with Claude Code

Use Claude Code to define a historical experiment, poll its backtest and review warnings before testing an unseen period.

Evidence mode
Historical simulation
Submission
Explicit operation and polling

Keep an experiment record

Before the first run, write down the following fields. These are a planning checklist, not completed backtest results. Use the same values for the candidate and its baseline.

Strategy version and portfolio IDConnect the result to the exact entry, exit and sizing rules
Dates and evaluation intervalPrevent a different test window from changing the comparison
Starting capital and benchmarkMake the baseline comparable
Cost and fill assumptionsExplain what the simulation models and leaves unresolved
Unseen period and candidate budgetRecord how selection will be assessed before seeing the results

Write the experiment before the strategy

State what the rule is intended to test. Specify the universe, entries, exits, sizing, interval, initial capital, date range and benchmark. For a broad equity strategy SPY can be appropriate; a single-name options strategy needs its underlying benchmark instead of an automatic SPY comparison.

The first run should answer a specific question. A search over many candidates needs a recorded budget and a held-out period so the winner is not merely the best retrospective fit.

The tool sequence

Inspect the discovered inputs before submitting a call. For a new candidate, validate and save it first; for a saved candidate, start with get_portfolio. Record the portfolio and backtest identifiers returned by the account tools.

Validate a new candidatebuild_portfolioCanonical configuration and component issues; nothing is saved
Save the validated draftcreate_portfolioUse the reviewed configuration and retain the returned chat portfolio ID
Read the saved bookget_portfolioExact strategies, action sizing, and portfolio identity
Submit the historical testbacktest_portfolioExplicit dates, interval, capital and benchmark
Poll the same operationquery_backtest_statusTerminal state and warnings; do not resubmit while running
Inspect the curvequery_backtest_historyHistorical values for the returned backtest_id
Compare candidatescompare_backtestsIdentical period and assumptions, with activity and risk

Connect and inspect the candidate

Connect Claude Code through the current remote MCP OAuth guide. Ask for the complete strategy definition and confirm the exact portfolio ID before calling a backtest tool. Authoring and saving a candidate does not start a deployment.

text
Draft a daily SPY strategy using explicit entry, sizing and exit rules. Show the complete configuration and proposed backtest assumptions. Wait for my approval before submitting compute. Do not deploy or edit an existing live account.

Read the completed historical result

Keep the backtest_id returned by the accepted operation and poll its status. Inspect warnings, drawdown, activity, exposure and the benchmark comparison. An empty history while a job is running is not a failed strategy and is not a reason to submit a duplicate run.

A historical curve models the configured strategy over past data. It is not the curve of a running paper or brokerage portfolio.

Test the frozen rule on an unseen period

Freeze the selected rule before opening the unseen period. Record rejected variants and why the selected configuration advanced. Compare assumptions as well as returns, particularly for options fills and unavailable data.

Claude Code can write a typed Python or TypeScript script when repeated research calls need stable configuration and idempotency keys. The SDK guides show the exact authoring and polling lifecycle.

Continue exploring