How to Backtest a Strategy in TradingView: Steps, a Script and What the Fills Assume
Write or load a Pine Script strategy, add it to a chart, set commission and slippage in its Properties, and read the strategy report. By default TradingView fills market orders at the next bar's open with no commission and no slippage, so a result without those costs is flattering; Bar Magnifier needs the Premium plan, C$82.95 a month billed annually.
How do you backtest a strategy in TradingView?
A backtest in TradingView runs a Pine Script strategy over the chart's historical bars and records the trades it would have placed. The steps are short.
First, open a chart of the market and timeframe you want to test, for example the Micro E-mini S&P 500 continuous contract on a 15-minute chart. Second, open the Pine Editor, paste a strategy script (one is below), and add it to the chart. Third, open the strategy's Properties and set the costs: commission, slippage and position size. Fourth, read the strategy report, which shows a Metrics tab, a list of trades and a Properties tab, and check the list of trades against the chart before you trust any total.
The third step is the one people skip. Per the Pine Script documentation, if you do not specify a commission the strategy applies none to filled orders, and the slippage default is 0 ticks. A strategy that looks profitable with free, instant fills can lose money once it pays for them. Everything in this guide is about closing that gap between the tester and real trading.
A script you can copy: moving-average cross with MES costs
The script below is a plain 20 and 50 bar simple moving-average cross, long and short, one contract, with commission set at US$0.94 per contract per side and one tick of slippage. The US$0.94 figure is NinjaTrader's Free-plan all-in cost per side for one Micro E-mini S&P 500 contract (US$0.39 commission + US$0.36 exchange and NFA + US$0.19 clearing); change it to your own broker's number. It is an example of method, not a recommended strategy, and we are not claiming it makes money. We wrote it to the Pine Script v6 documentation but did not run it inside TradingView for this page, so paste it in and fix any error the editor shows.
//@version=6
strategy("SMA cross, MES cost model", overlay = true,
initial_capital = 50000,
default_qty_type = strategy.fixed, default_qty_value = 1,
commission_type = strategy.commission.cash_per_contract, commission_value = 0.94,
slippage = 1,
pyramiding = 0,
process_orders_on_close = false,
use_bar_magnifier = false)
fastLen = input.int(20, "Fast SMA", minval = 1)
slowLen = input.int(50, "Slow SMA", minval = 2)
fast = ta.sma(close, fastLen)
slow = ta.sma(close, slowLen)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.entry("Short", strategy.short)
plot(fast, "Fast SMA", color.blue)
plot(slow, "Slow SMA", color.orange)The line commission_type = strategy.commission.cash_per_contract charges a fixed amount per contract on every order, which is the right model for futures. For stocks priced in percent, use strategy.commission.percent instead. Slippage is in ticks. The default position size here is one contract (default_qty_type = strategy.fixed); the documentation says this default applies only to orders that do not set their own quantity.
What does the strategy tester assume about fills?
This is the part that decides whether your backtest means anything. The Pine Script documentation describes the broker emulator in these terms. By default it fills orders using only chart data, after a bar closes. A market order fills on the next available tick, which with default settings is the open of the following bar. A limit order fills at the limit price or better once the market reaches it. A stop order activates a market or limit order once price touches the stop level or worse. If price jumps over an order level in the gap between two bars, the order fills at the next bar's open, not at your price.
Inside a single bar, the emulator does not know which came first, the high or the low. It assumes a path: if the open is closer to the high than to the low, the bar went open, high, low, close; otherwise open, low, high, close. Two further defaults to know: process_orders_on_close is false, so an order created on a bar's closing tick fills on the next tick, and calc_on_every_tick is false, so a strategy runs once per historical bar.
| Setting | Default | What it does to your result |
|---|---|---|
| Commission | None unless you set it | Understates cost on every trade |
| Slippage | 0 ticks | Fills at the exact price the emulator picks |
| Market order fill | Next bar's open | Signal on the close, fill one bar later |
| Intrabar path | Open to nearer extreme first | Decides whether a stop or a target is hit first |
| Margin (long and short) | 100, leverage of 1:1 | Futures margin is not modelled unless you change it |
| Limit fill assumption | 0, fills as soon as price reaches the level | Optimistic about queue position |
Worked example: one trade, with and without costs
This is a hypothetical trade to show the arithmetic. A fast average crosses above a slow one on the close of a bar at 5,000.00. The next bar opens at 5,001.25, so the emulator fills the long there. With one tick of slippage on MES (0.25 index points) the entry is 5,001.50.
Later a cross down is signalled at 5,010.00 and the next bar opens at 5,010.50. The exit fills there less one tick of slippage on a sell, at 5,010.25. The trade gains 5,010.25 minus 5,001.50 = 8.75 points. An MES point is worth US$5 (CME contract specification; our reader could not load that page, so confirm it), so the gross result is 8.75 x US$5 = US$43.75. Commission of US$0.94 per side adds US$1.88, leaving US$41.87, about C$59.84 at 1.4291 CAD per USD (Bank of Canada, 17 Nov 2026).
Compare with a naive backtest: entry at the signal close, 5,000.00, exit at 5,010.00, no costs, gives 10 points x US$5 = US$50. The tested result is US$8.13 lower, 16% less, on a single trade. Over hundreds of trades with a thin edge, that difference is the strategy.
Worked example: which comes first, the stop or the target?
Take one bar that opens at 5,000.00, reaches a high of 5,012.00, a low of 4,996.00 and closes at 5,008.00. You hold a long with a stop at 4,997.00 and a limit target at 5,010.00. Both levels sit inside the bar's range, so either could have been hit first.
The open is 12.00 points from the high and 4.00 from the low, so it is closer to the low. The emulator therefore assumes open, low, high, close, which means the 4,997.00 stop is hit first and the trade ends as a loss of 3.00 points, or US$15.00 on one MES contract (3.00 x US$5), before costs. In real trading the price may have gone to 5,010.00 first and you would have won.
This is why Bar Magnifier exists. The documentation says it makes the emulator take OHLC data from a lower timeframe, so it can see which level was reached first within the bar. You switch it on with use_bar_magnifier = true in the strategy declaration, or by choosing High for Bar detalization in the Properties tab. It requires a Premium or Ultimate plan. The emulator can request at most 200,000 bars from the lower timeframe, bars without intrabar data fall back to the default assumptions, and the setting has no effect on 1-second or tick charts.
What does it cost to backtest properly?
We did not find a plan restriction on running the strategy tester itself, but the plans differ in how much history you can test. Our TradingView plans page (/platforms/tradingview/plans/), read on 23 November 2026, lists historical bars of 5K on Basic, 10K on Essential and Plus, 20K on Premium and 40K on Ultimate, with monthly prices billed annually of C$0, C$19.95, C$41.95, C$82.95 and C$279.92. Premium is C$995.40 a year, and it is the lowest plan that can turn on Bar Magnifier.
The bar limits matter. At 5,000 bars, a 15-minute chart of a US index future covers only a few weeks of its regular session, which is far too little to test a strategy. Count the bars you need before you pick a plan: a test over one year of 15-minute bars of a market trading around 23 hours a day needs roughly 23 x 4 x 252 = 23,184 bars. That exceeds the 20K on Premium. TradingView's documentation also mentions Deep Backtesting, which keeps all trades instead of the latest 9000; we did not confirm its plan limits or how far back it tests. Data for CME futures costs extra on TradingView; our plans page lists C$9.80 a month for CME Group.
Common ways a TradingView backtest misleads you
Most bad results come from a short list of causes. Not setting commission and slippage. Trading a thin edge that vanishes at realistic costs. Testing on non-standard chart types: the documentation warns that results on such charts do not reflect actual market conditions by default because they use synthetic prices, which is why Heikin Ashi charts need fill_orders_on_standard_ohlc = true to fill at real prices. Overfitting, which means tuning inputs until the past looks good. And ignoring that a strategy keeps the latest 9,000 trades under the default testing range, which affects the trade list and exports but not the Metrics tab.
The documentation also warns that strategies behaving differently on historical and real-time bars may have lookahead bias, and that changing the Script execution settings can cause repainting. The check is forward testing: run the strategy on real-time data in a paper account and compare it with what the backtest would have done.
A backtest is also not a prop firm result. A prop firm account uses simulated capital under the firm's own rules (loss limits, consistency rules). Test the rules separately, using our prop firm guides.
Who should not rely on the strategy tester
Skip it as proof of an edge if you have fewer than a few hundred trades, if you tuned the settings on the same data, or if your strategy depends on queue position or intrabar order, such as scalping with limit orders. Do not buy a paid plan to chase backtest accuracy before you have a strategy worth testing. For tick-level or order-book testing, use a platform that stores that data. Nothing here is investment advice; a backtest describes the past under assumptions.
Frequently asked questions
Is TradingView backtesting free?
We found no plan restriction on the strategy tester itself, but the free Basic plan allows only 5K historical bars and Bar Magnifier needs Premium or Ultimate. Premium is C$82.95 a month billed annually, as read on 23 November 2026.
Does TradingView include commission and slippage in backtests?
Only if you set them. The documentation says no commission is applied if you do not specify it, and the default slippage is 0 ticks.
What does the bar magnifier do?
It lets the strategy emulator read lower-timeframe data inside each bar to see which price level was hit first. It needs a Premium or Ultimate plan and can request at most 200,000 bars.
At what price does a TradingView strategy fill my market order?
By default at the open of the bar after the signal, plus or minus any slippage you set.
Can I trust a TradingView backtest?
Treat it as an estimate. Fills are modelled from bar data, and results change with commission, slippage, chart type and history length.
Sources
Provider documents behind this page, checked November 23, 2026.