Guide
Backtest a Strategy with an AI Assistant
In TradingLead Desktop, an AI assistant can backtest a Python or Pine strategy on the chart you have open. It confirms the chart context, states the assumptions, runs backtesting.run and polls backtesting.get until the run finishes. It then reads the metrics and trades and can compare or archive runs. For parameter tuning it uses the Strategy Optimizer's holdout or walk-forward modes, capped at 4 parameters and 12 combinations. Everything is a historical simulation, with no broker execution.
What the assistant can do
With TradingLead Desktop and its managed AI integration, a terminal assistant such as GitHub Copilot CLI or Claude Code can drive the same backtesting engine you use by hand. It works through the public tl-backtesting and tl-strategy-optimization skills. Every step is a named command from the command layer, and the result appears in the visible Backtesting panel.
The assistant does not invent performance. It reports what the engine returns: metrics, trades, equity, the actual tested period and the candle count.
Before you start
- Install Desktop and enable Help > AI integrations. See control TradingLead with AI.
- Open the chart you want to test. The backtest uses the chart's current account, feed, symbol and interval.
backtesting.runhas no overrides for these, so ask the assistant tochart.openthe right symbol first. - Have a strategy ready, or ask the assistant to write one:
- A Python strategy, which can run in vectorized or sequential mode.
- A Pine
strategy(), which runs vectorized only. Pine support is a documented subset; see Pine Script.
Step 1: Confirm context and capabilities
Ask: "Target my active chart tab, read chart.getState and backtesting.capabilities, and tell me the symbol, interval and loaded history."
The assistant reads the state. It does not change anything yet.
Step 2: State assumptions explicitly
A backtest is only as honest as its costs. Give the assistant the numbers, for example: "₹1,00,000 initial capital, fixed 10 shares, 5 bps commission, 1 tick slippage, no pyramiding."
- Pine runs accept these through explicit
pineSettings. Omitted values keep the script's own declaration defaults. - Python runs take configuration and parameters.
- Session hours are optional explicit assumptions, such as a half-day or an evening Muhurat session. They are not an inferred exchange calendar.
Step 3: Run and poll
The assistant calls backtesting.run. It passes source code or a saved source ID, sets language:"pine" for Pine, and includes a retry key. The returned run can still be preparing, so it polls backtesting.get until the run completes or reports an explicit error. A run that was only accepted is never reported as complete.
History limits:
| Mode | Maximum candles |
|---|---|
| Pine (vectorized) | 20,000 (default) |
| Python vectorized | 20,000 (default) |
| Python sequential | 250 (default and maximum) |
The newest candle is always excluded. If no range is given, the run uses the chart's selection. If a history fetch fails, the run fails rather than quietly producing a shorter backtest. Ask the assistant to report the actual period and candle count it tested.
Step 4: Read results the right way
Ask for metrics, then trades in pages. Useful details to know:
- Python signals are decided at candle close and fill on a later eligible candle.
- Wins and losses use net P&L after fees. Breakeven trades stay in the win-rate denominator.
- Pine runs keep the emulator's actual fills and fees. Drawdown in the main report is sampled at candle close.
Then ask: "Compare this run with my previous run." The assistant uses backtesting.compare, which refuses incompatible runs explicitly. It can also archive a run with backtesting.librarySave or export it with backtesting.export. See the backtest a strategy guide for how to read the panel.
Step 5: Optimize without fooling yourself
Parameter search belongs to the Strategy Optimizer, which runs on a completed backtest. The assistant:
- Reads the run's declared parameter metadata. It doesn't invent keys.
- Builds a grid of at most 4 parameters and 12 combinations.
- Chooses a mode:
sweep: in-sample only, and it can overfit.holdout: picks the winner on training data, then tests it once.walkForward: 1–5 rolling folds, each starting flat with the original capital.
- Calls
backtesting.optimizePreview, which returns the candidates, fold dates and workload without executing anything. - Runs
backtesting.optimizeand reads the per-fold results. - Optionally reruns the selected parameters with
backtesting.optimizeRerunand builds an offline report withbacktesting.optimizeReport.
The objective is net P&L or lowest drawdown, measured on training data only. Ask the assistant to show held-out results next to training results. A big gap between them is a warning sign. For more, read optimize a strategy without overfitting and the glossary entries for walk-forward analysis and overfitting.
Step 6: Practise forward
When you're happy, ask the assistant to start a paper session from the completed backtest with backtesting.forward, or a replay with backtesting.replay. Both are simulations through the tl-paper-trading skill; see paper trading. No real order is ever sent.
Example prompts
- "Backtest a 20/50 EMA crossover in Python on the current daily chart, 5 bps commission, and report net P&L, max drawdown, win rate and trade count."
- "Run my saved Pine strategy with ₹2,00,000 capital, then list the 10 largest losing trades."
- "Preview a 3-fold walk-forward of fast 10/15/20 and slow 40/50/60, then run it and show training vs test per fold."
More examples are in the AI prompt cookbook.
Steps
- Open the symbol, feed and interval you want to test in TradingLead Desktop
- Ask the assistant to read chart.getState and backtesting.capabilities for that tab
- Give the strategy source and explicit capital, quantity, commission and slippage assumptions
- Let it run backtesting.run with a retry key and poll backtesting.get until a terminal state
- Review metrics, trades and the actual tested period and candle count
- Optionally preview and run a holdout or walk-forward optimization on the completed run
Limitations
- Requires TradingLead Desktop with managed AI integration (Apple Silicon Mac 0.1.8+, current 0.1.18); the public Windows 0.1.7 build lacks it.
- Pine strategy support is a documented subset run vectorized only, not TradingView parity.
- Python sequential mode is limited to 250 candles; other modes to 20,000.
- Session hours, costs and slippage are your explicit assumptions, not an exchange-verified calendar.
- Optimization grids are limited to 4 keys and 12 combinations; walk-forward to 1-5 folds.
- Backtests may consume history allowance and are not investment advice.
Frequently asked questions
- Can an AI assistant run a backtest in TradingLead for me?
- Yes, through TradingLead Desktop. The tl-backtesting skill tells the assistant to confirm the chart context, run backtesting.run with your Python or Pine source and poll backtesting.get until the run finishes, then report the metrics and trades.
- Which symbol and timeframe does the AI backtest use?
- The chart's current account, feed, symbol and interval. backtesting.run has no symbol or interval overrides, so the assistant changes the chart first with chart.open if needed.
- How much history can a backtest use?
- Pine and vectorized Python runs support up to 20,000 candles. Python sequential mode is capped at 250 candles. The newest candle is always excluded.
- Can the AI find the best parameters without overfitting?
- It can run holdout or walk-forward tests where parameters are chosen on training data and scored on held-out candles, up to 4 parameters and 12 combinations. An in-sample sweep can overfit, and no mode proves future performance.
- Does a backtest place trades with my broker?
- No. Backtests are simulations on historical candles. TradingLead has no live broker order commands.
Related
Last updated: