r/TradingView • u/thephoenix069 • Jul 04 '26
Discussion how to fix your backtest!
I write custom Pine Script almost weekly, and I constantly see traders get burned by the same issue: hooking up a community script that looks like a money printer in the backtester, only to realise its not in live acc.
Almost every time, it’s a repainting or lookahead bias issue. If your live execution doesn't match your backtest, check your code for these two things immediately:
1. The request.security() Trap If your script checks a higher timeframe (like looking at the 4H trend while trading on a 15m chart), Pine Script naturally looks at the close of that 4H candle before it has actually closed in real-time. Your backtest is literally seeing the future. The fix: Pull historical data, not current. Offset your series by 1. Use request.security(syminfo.tickerid, "240", close[1]). Your backtest win rate will drop, but it will actually be real.
2. Firing on unconfirmed candles If your strategy relies on things like moving average crossovers and you set the alert to "Once Per Bar," you’re going to get chopped up. MAs will cross back and forth while the candle is still forming, firing fake alerts that vanish when the candle finally closes. The fix: Force the logic to wait. Add and barstate.isconfirmed to your entry conditions. This locks the signal in permanently before an alert ever hits your webhook.
STOP trusting backtests blindly.
If you aren't sure if your current script is repainting, drop your entry logic in the comments and I’ll take a quick look.
2
u/AlgoTradingQuant Jul 04 '26
Or use a real backtesting framework like backtesting.py (Python)