Feature
Strategy Backtesting with Python and Pine
TradingLead's Backtesting panel tests a Python or TLScript-Pine strategy() script on up to 20,000 closed candles from the chart you have open, with explicit commission, slippage and instrument assumptions. Results include a trade ledger, entry/exit markers on the chart, drawdown, win rate, profit factor and a buy-and-hold benchmark. Every result is a simulation on past data, not a prediction or investment advice.
What it does
TradingLead Backtesting lets you test a trading strategy on historical candles directly from the chart you are already analysing. You write (or pick) a strategy in Python or as a TLScript-Pine strategy() script, choose your capital, position size and cost assumptions, and the panel replays your rules over past data. The result is a full trade ledger, entry and exit markers on the chart, and a set of performance and risk metrics.
It is built for Indian traders who want to check an idea on Nifty, Bank Nifty or individual NSE/BSE stocks before risking money, and for anyone who wants to see why a strategy wins or loses rather than just a headline return. Which symbols and intervals are available depends on the data feed connected to your chart.
How it works
Backtesting lives in the /charts workspace as a Backtesting tab in the bottom panel, next to Paper Trading. When you run a strategy, TradingLead:
- Captures the chart's feed, symbol and interval and fetches up to 20,000 closed candles (or your chosen date range) from that source. If the source can't supply the full window, the run fails rather than silently testing fewer candles.
- Freezes the source code, inputs, settings and candles so the result can be reproduced and compared later.
- Executes the strategy:
- Python strategies run in a local isolated runtime (in the browser via WebAssembly, or natively in the Desktop app). Vectorized mode processes the whole history; sequential mode (up to 250 candles) calls your code after every completed candle with only past data and the real simulated account state.
- Pine
strategy()scripts run in a Pine broker emulator. Its actual fills, lots, fees and candle-close equity are carried into the shared report, not re-simulated from signals.
- Builds a report: metrics, Analytics, trades, events and the equity curve.
The shared engine follows a documented execution contract: signals decided at a candle close fill on the next candle, market orders fill at the next open with adverse slippage, and if a stop and a target are both touched inside one candle the stop wins. Positions still open at the end stay open; the engine does not invent a final exit.
Key capabilities
- Two strategy languages: Python and TLScript-Pine
strategy(), with bundled examples for each. - Cost modelling: commission in basis points, fixed per fill and per unit/lot, extra fees, and slippage in basis points embedded in fill prices. Defaults are 5 bps commission and 2 bps slippage.
- Instrument assumptions: full-collateral (no leverage) by default, or user-assumed fixed margin; units or lots, contract multiplier, quantity step and tick size.
- Risk controls: stop %, target % and close-trailing stop for the shared engine; Pine scripts keep their own
strategy.exitlogic. - Session calendars: optional user-assumed trading hours, holidays and special sessions (for example a Muhurat session) to validate intraday candles.
- Metrics and Analytics: net P&L, win rate, profit factor, expectancy, max drawdown with recovery episodes, exposure, holding time, monthly/yearly returns, cost attribution and a same-period buy-and-hold benchmark. Sharpe ratio is opt-in with explicit sampling assumptions.
- Trades on the chart: entry/exit arrows, labels with side, quantity, price, net P&L and exit reason, optional connecting lines, stop/target segments and open-position levels. Click a trade in the grid to focus it on the chart.
- Trade grid: search, filter and sort every exit leg; reorder and resize columns.
- Saved runs and comparison: keep up to three active runs, archive runs to a local library (up to 100 runs per account) and compare saved runs tested on the same candles.
- Apply to chart: attach a strategy to the chart; with automatic rerun enabled it recomputes when you change symbol or interval.
- Next steps: tune inputs with the Strategy Optimizer or replay the period manually with paper trading.
How to use it
- Open TradingLead Charts and select the symbol and interval.
- Open the Backtesting tab in the bottom panel.
- Choose Python or TLScript-Pine strategy(), and a saved script or the bundled example.
- Set initial capital, quantity, costs and the history range (or keep the defaults).
- Click Run backtest, review the preflight, then Start backtest and wait for the run to complete.
- Review the summary metrics and Analytics, then click trades to see them on the chart.
Step-by-step walkthroughs: Backtest a strategy and Backtest a Pine strategy.
Limitations
- A backtest is a simulation on past data. It does not place orders, and past results do not guarantee future returns.
- Fills are candle-level estimates. There is no tick replay, so real slippage, partial fills and liquidity can differ.
- Pine strategies run on a documented Pine v5/v6 subset in an emulator. Results are not guaranteed to match TradingView or a broker.
- Costs, margin and lot sizes are your assumptions, not verified NSE/BSE or broker schedules. Taxes and funding are not modelled.
- Data quality depends on the provider: gaps, corporate-action adjustments and freshness are not verified unless the feed reports them.
- There is no drag-and-drop strategy builder; strategies are code.
- Vectorized strategies can still peek at future data if the code does so. TradingLead cannot certify that a strategy has no lookahead bias.
Access
Backtesting is part of the main TradingLead workspace, which starts with a 5-day free trial and then needs a subscription. See pricing for current plans. To learn the concepts first, try the Backtesting Lab.
Steps
- Open a chart at /charts and pick the feed, symbol and interval you want to test.
- Open the Backtesting tab in the bottom panel and choose Python or TLScript-Pine strategy().
- Select a saved script or the bundled example, then set capital, quantity, costs and the history range.
- Click Run backtest, review the preflight, then Start backtest and wait for it to complete.
- Read the metrics and Analytics, then inspect individual trades on the chart and in the trade grid.
Limitations
- Simulation on historical candles only; no broker orders are placed and past performance does not guarantee future results.
- Fills are modelled at candle level (next-bar fills, stop-before-target when both touch); there is no tick-level replay.
- Pine strategies use a documented Pine v5/v6 subset in an emulator; results are not TradingView or broker parity.
- Costs, margin and lot rules are user-supplied assumptions, not verified exchange or broker schedules; taxes and funding are not modelled.
- History depends on the selected data provider; gaps, adjustments and freshness are unverified unless the feed reports them.
- Up to three active runs are kept locally per account; older runs must be saved or deleted to free a slot.
- No no-code / visual strategy builder; strategies are Python or Pine code.
Frequently asked questions
- Can I backtest without writing code on TradingLead?
- Not with a visual rule builder. Strategies are Python or Pine strategy() scripts. You can start from the bundled example strategies, and the Backtesting Lab's practice lesson shows how to turn plain-English rules into a prompt for an AI assistant and then review and backtest the resulting code.
- Can I run my TradingView Pine Script strategy on TradingLead?
- Many strategy() scripts that stay inside TradingLead's documented Pine v5/v6 subset can run. They execute in a Pine broker emulator whose actual fills feed the report, but results are not guaranteed to match TradingView, and some options such as calc_on_every_tick and bar magnifier are rejected.
- How much history can a backtest use?
- Up to 20,000 closed candles from the chart's data source, or an explicit date range within that cap. Python's sequential mode is limited to 250 candles. If the source cannot supply the requested history, the run fails instead of silently using fewer candles.
- Does the backtest include brokerage and slippage?
- Yes, as assumptions you set: commission in basis points, fixed per fill or per unit/lot, extra fees, and slippage in basis points applied to fills. The defaults are 5 bps commission and 2 bps slippage. They are not exchange-verified Indian charges, so enter values that match your own broker.
- Is backtesting free on TradingLead?
- Backtesting is part of the main TradingLead workspace, which starts with a 5-day free trial and then needs a subscription. See the pricing page for current plans.
- Does a good backtest mean the strategy will make money?
- No. A backtest replays rules on past candles under assumptions about fills and costs. Past performance does not guarantee future results, and an over-tuned strategy can look excellent in a backtest and fail live.
Related
Last updated: