Pine Script · 8 min read

Why your Pine Script backtest won't match live trading

You coded a strategy, the tester shows a beautiful curve, and live it bleeds. That gap is almost never bad luck — it's four fixable modelling errors. Here's each one and how to close it.

TradingView's Strategy Tester is a simulation. If the simulation is optimistic, live trading corrects it — painfully. Four things cause the divergence.

1. Repainting

If your logic reads the current, unclosed bar, the backtest sees a candle's final shape while live you only have a forming candle that can still change. The tester “knew” the close; you don't. Fix: act only on confirmed bars. Full detail in Pine repainting explained.

longCond = ta.crossover(close, ta.sma(close, 20))
if longCond and barstate.isconfirmed
    strategy.entry("L", strategy.long)

2. Lookahead via request.security()

Pulling higher-timeframe data with lookahead enabled leaks future values into past bars. The backtest uses information that did not exist yet. Always request HTF series with lookahead_off and offset the series by one bar.

3. Unrealistic fills

By default the tester can fill at the bar's close or an ideal price. Live, you get the next available price with a spread. Add process_orders_on_close=true where appropriate and stop assuming limit orders always fill at your exact level.

4. Ignored costs

Commission and slippage quietly erase thin edges, especially on high-frequency systems. Model them:

strategy("My System",
     commission_type=strategy.commission.cash_per_contract,
     commission_value=0.62,
     slippage=2)

The honest workflow

Confirmed-bar logic, lookahead-off HTF, realistic fills and costs — then validate out-of-sample and run a Monte Carlo so you see a distribution, not one lucky path. That last step is the difference between a tester screenshot and a number you can trust. See how to know if a backtest is overfit and our methodology.

Every Puravida Edge strategy is validated this way — realistic fills, costs, and 1,500-path resampling — not a clean Strategy Tester curve.

FAQ

Why is my TradingView backtest so much better than live results?

Usually one of four causes: repainting (acting on unclosed bars), lookahead in request.security(), unrealistic order fills, or ignored commission and slippage. Fix all four and the gap shrinks dramatically.

Does adding commission and slippage really matter?

Yes — on higher-frequency strategies, realistic costs can turn a profitable-looking backtest into a losing one. Always model them in the strategy() call.

How do I make my Pine backtest realistic?

Act only on confirmed bars, request higher-timeframe data with lookahead off, add commission and slippage, use process_orders_on_close where appropriate, then test out-of-sample and with Monte Carlo.

Not financial advice. Performance figures referenced are hypothetical, modeled outputs (1,500-path Monte Carlo on a 12-month sample). Past performance does not guarantee future results. Tool names are referenced for education; verify current features and prop-firm rules directly.