Explainer
How to backtest a trading strategy with AI
Use AI to sharpen the rules and draft code, then run them on real price data with stated fills and costs, and read every trade it produces.
Updated
What AI is good for here
An AI assistant is useful for the language side of a backtest: tightening rules, spotting gaps and drafting code. It is not a source of price data and it does not run a simulation by itself. Treat it as a helper at the desk, and let real data and the recorded trades decide.
- Prompt
- The instruction you give the assistant. A precise prompt names the market, the time zone, the bar size and every rule.
- Assumption
- Anything the rules leave open that someone had to decide, such as where an order fills.
- Look-ahead
- Using information that was not available yet at the moment a rule acts.
Step 1: Write the rules in plain words
Write the rules as short sentences. Name the market, the time zone, the entry trigger, the stop, the target and the exit if neither is hit. Do this before you ask the assistant for anything, so the rules are yours.
Step 2: Have the assistant restate and question them
Paste the rules in and ask it to restate them as a checklist, then to list every place they can be read two ways. Typical gaps: whether a time range includes its last minute, whether a target fills on touch or on a close, and what happens when a bar opens beyond the stop.
Answer each gap in writing. In our method, for instance, a range written as 18:00–19:00 ET includes both ends, which is 61 one-minute bars, and a target fills on touch at the exact price.
Let the assistant draft. Let real data and the trades decide.
Step 3: Draft the code, or choose the settings
If you code the test yourself, an assistant can draft it in Python or in a platform’s scripting language. Ask it to state its fill assumptions in comments, and to avoid using any value that is not known at the bar where the rule fires.
If you do not code, you can skip this step. Our service takes the rules in plain language instead.
Step 4: Get data the rules can use
Intraday rules need intraday bars. Check the bar size, the history, the time zone and how contract months are joined. We use 1-minute CME futures data licensed from Databento, with raw prices and the front month chosen by volume.
Step 5: Run it and check it by hand
Run the test, then open a few trades and compare them with the chart. Look at trades near contract rolls, near the session open and near time stops. If a trade surprises you, find out why before you trust the totals.
Step 6: Add costs and read every trade
Add commission and slippage before you read results. Commissions and fees vary by broker; a backtest should use your own.
Then read the trades, not just the summary. A summary can look tidy over trades you would not recognise.
Step 7: Change one thing and run again
Change one rule at a time and run it again. An assistant can suggest which rule to look at, but a change is only informative if it is the only one. In our service each run is a version, and the versions of a strategy are kept together.
Questions and answers
Can I backtest a trading strategy with AI?
AI can help you write and tighten the rules and draft code. The result still has to come from a run on real price data with stated fills and costs.
What should I ask an AI assistant when backtesting?
Ask it to restate your rules, list where they can be read two ways, state its fill assumptions and flag any input that would not be known at the bar where a rule fires.
Does AI remove the need for data?
No. A backtest needs price bars fine enough for the rules, and an assistant does not supply verified ones on its own.
Can I skip the code?
With our service, yes. You describe the rules in plain language and we run them. If something is ambiguous, we normally ask first.