Backtest Pine Script
Run supported Pine Script v4/v5/v6 strategies in QTrader, inspect historical fills and understand differences from TradingView.
Before you start
Use a supported Pine v4, v5 or v6 strategy. QTrader accepts a supported subset, translates it into its strategy dialect and runs it against historical candles. It does not reproduce every TradingView feature. Start with the example below to separate a setup problem from an unsupported script feature.
You can open the workbench as a guest. Available markets, historical coverage and usage limits depend on current data and access; consult current plans rather than assuming unlimited runs.
Follow the workflow
- Open the example in the workbench. Choose the market, timeframe and date window you want to inspect.
- Read compile diagnostics before changing settings. An unsupported construct needs a supported equivalent; changing its version marker does not add engine support.
- Check initial capital, order size, commission, slippage and any leverage. A different cost or market setup can change a result even when the code is unchanged.
- Press Backtest. Inspect the individual entry and exit bars in Trades alongside the chart, then check drawdown and fees in the report.
- Change one input or assumption at a time. Keep the market, window and settings recorded when comparing two runs.
Worked example
This long-only SMA crossover enters when the fast average crosses above the slow average and closes on the reverse cross. Its history and warm-up window affect when the first signal can occur.
SMA 50/200 cross (Pine): The same SMA 50/200 cross in Pine Script: long only, buys when the fast SMA crosses above the slow one and closes when it crosses back below; both lengths are inputs. The link opens this exact bundled example as unsaved work on its recommended timeframe.
//@version=6
strategy("SMA 50/200 cross", initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
fastLength = input.int(50, "Fast SMA", minval=2)
slowLength = input.int(200, "Slow SMA", minval=2)
fast = ta.sma(close, fastLength)
slow = ta.sma(close, slowLength)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
Limits and interpretation
Pine history uses close[1] for the previous bar. A normal close-of-bar signal fills at the next bar open unless supported order-processing settings change that timing. Price orders, costs, candle history and engine versions can produce differences from another platform.
Backtests simulate fills from candle data. They do not establish future performance or guarantee the price, liquidity or execution of a live exchange order. Use the supported/ignored/unsupported reference for exact syntax and backtest rules for fill assumptions. The futures reference describes isolated margin, liquidation and funding.