Backtest Pine Script online: paste, compile and test your strategy
Backtest Pine Script online: paste a version 6 strategy, fix errors by line number, set commission and slippage, read the report and run the optimiser.
To backtest a Pine Script strategy online in SuperCharting, paste it into the Script Editor, let it compile, click Add to chart, and the Strategy Tester opens under the chart with the full report. Nothing is installed, the account is currently free, and the same panel lets you change costs and run the optimiser. This page is for traders who already have a Pine strategy and want to test it; if you are starting from a blank page, read write your first strategy instead.
Pine Script is TradingView's language. SuperCharting runs Pine-compatible scripts with its own engine, so most version 6 strategies run as they are, and the ones that do not are reported by line. Past results do not guarantee future results.
How to backtest a Pine Script strategy in SuperCharting
- Open the chart you want to test on, for example
NSE:RELIANCEon the 1D interval. - Press Ctrl + E, or click Script Editor in the right-hand widget bar.
- Paste your strategy over the text in the editor. If it is saved as a
.pineor.txtfile, use Open file… from the editor's menu instead. - Wait a moment: the console under the code shows a tick and the script's name, such as "✓ Donchian breakout with ATR stop (strategy)", or a cross and the first error.
- Click Add to chart. Entry and exit marks appear on the bars and the Strategy Tester opens with the report.
- Click Save so the strategy is kept in your account.
If you close the tester, right-click the strategy's name in the chart legend and choose Strategy Tester to bring it back.
Compile errors and runtime errors
Compile errors
The editor checks the code as you type. A syntax problem shows as a cross with the line number, for example "line 5: unexpected 'EOF'" when a bracket is never closed. An unclosed bracket is reported where the text ran out, so look upward from there.
Runtime errors
Some problems only show when the strategy runs on bars. A call to a function the engine does not have, for instance, compiles but stops on the chart with a message such as "Runtime: line 3, bar 0: unknown function ...". The free Pine Script editor page covers the editor itself.
An example strategy to paste
This Donchian breakout buys when the close moves above the highest high of the previous 20 bars, and exits on a stop that is the higher of the 10-bar low and two ATRs below the average entry price. Shorts are off unless you tick Allow shorts in its settings.
//@version=6
strategy("Donchian breakout with ATR stop", overlay = true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 20, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 2)
entryLen = input.int(20, "Breakout length", minval = 2)
exitLen = input.int(10, "Exit length", minval = 2)
atrLen = input.int(14, "ATR length", minval = 1)
stopMul = input.float(2.0, "Stop (x ATR)", minval = 0.5, step = 0.25)
useShort = input.bool(false, "Allow shorts")
upper = ta.highest(high, entryLen)[1]
lower = ta.lowest(low, entryLen)[1]
exitHi = ta.highest(high, exitLen)[1]
exitLo = ta.lowest(low, exitLen)[1]
atr = ta.atr(atrLen)
plot(upper, "Upper", color = color.green)
plot(lower, "Lower", color = color.red)
if strategy.position_size == 0 and close > upper
strategy.entry("Long", strategy.long)
if useShort and strategy.position_size == 0 and close < lower
strategy.entry("Short", strategy.short)
strategy.exit("Long stop", from_entry = "Long", stop = math.max(exitLo, strategy.position_avg_price - stopMul * atr))
strategy.exit("Short stop", from_entry = "Short", stop = math.min(exitHi, strategy.position_avg_price + stopMul * atr))
The [1] on each channel matters: without it the current bar's high is part of the channel and the close can never break above it.
Reading the report and changing the costs
The tester shows Key stats first (Total PnL, Max drawdown, Profitable trades, Profit factor), then performance, trades analysis and the List of trades. Click a trade in the list to see it on the chart, and check a few by eye. The Strategy Tester guide explains every figure.
The numbers in the strategy() line are only the starting point. Open the strategy's Settings from its legend and go to the Properties tab, where anything you change overrides the code:
- Commission: percent, cash per contract or cash per order. Try the example at 0.05% and again at 0.1% and see how much of the profit survives.
- Slippage: ticks added to market and stop orders.
- Initial capital, Default order size and Pyramiding: trade size and stacked entries.
- Order execution delay: One tick (fill from the next bar) or None (fill on the signal bar's close, which is less realistic).
On the tester's toolbar, Testing period limits the test to the last 7, 30, 90 or 365 days, the entire history or a Custom date range, and Bar detalization switches between about 4 and about 24 price steps per bar
Running the optimiser on your inputs
Click Optimise in the tester to open Optimise strategy. Tick inputs and give each a From, To and Step, for example Breakout length 10 to 60 step 5 and Stop (x ATR) 1 to 4 step 0.5. Choose what to maximise or minimise, such as net profit, profit factor or max drawdown, a method (Random, Random improvement, Sequential improvement, Annealing or Brute force) and the number of cycles. Sweep across timeframes repeats the whole search on a list such as 1h, 4h, 1D.
Every cycle is a separate backtest, and the chart does not change until you click Apply best to chart or a row in the results. Add a Result filter or Save criteria, say at least 30 trades, so a setting with a handful of lucky trades cannot win, and test the chosen setting on a date range the optimiser did not see. The strategy optimiser and backtesting guide pages explain how to avoid fitting to noise.
What is and is not supported
Most of the strategy language works: strategy.entry, strategy.order, strategy.exit with stop, limit, profit, loss and trailing exits, strategy.close, strategy.close_all, strategy.cancel, the strategy.risk. rules, and the strategy.opentrades. and strategy.closedtrades.* functions. Be aware of these limits before you trust a result:
request.securityworks on higher timeframes of the chart's own symbol. Asking it for a different symbol does not load that market, so a strategy that compares two markets will not test correctly.request.security_lower_tfsees real 1, 5, 15 or 60 minute bars only for the newest part of the history; older bars get empty arrays.importworks only for the libraries bundled with the site. Any other library gives a "library not found" error.- The test covers the bars loaded on the chart, up to 40,000, so a 5-minute test spans far less time than a daily one.
- A very heavy script can stop because it ran out of time.
- If the List of trades is empty although the signals look right, check the Testing period in the tester's toolbar and the Default order size on the Properties tab: an order too small for one share or contract, or a testing period that ends before the signals, gives no trades.
For a comparison with other free tools, see free backtesting software.
TradingView is a trademark of TradingView, Inc.; SuperCharting is not affiliated with it.
Frequently asked questions
Can I paste a strategy I copied from TradingView?
Yes, any open-source script you have the right to use. Many run unchanged; anything unsupported is reported by line.
Is this a Pine Script online compiler?
The editor compiles Pine-compatible version 6 code as you type and shows the first error with its line. It runs scripts on SuperCharting's charts, not on TradingView.
Does backtesting cost anything?
SuperCharting is currently free to use, and the Strategy Tester and optimiser are part of every account.
Why do my results differ from another platform?
Data, bars loaded, costs and fill rules all change a backtest. Match the Properties tab and testing period first.
Does a good backtest mean the strategy will work?
No. It shows how the rules behaved on past data with the costs you set. Past results do not guarantee future results.