We’ve all been there. You code or find a new scalping strategy on TradingView. The backtest looks like the Holy Grail—a smooth 45-degree equity curve, 85% win rate, and massive profit factors. You load it onto a live chart, fund the account, and... it immediately starts hemorrhaging money.
Welcome to the Repainting Trap.
In 99% of these cases, your strategy isn’t actually a stroke of genius; it’s just illegally looking into the future. In Pine Script, this is known as leaking future data. Here is exactly how it happens, why the Pine engine gets confused, and how to fix the two most common traps.
1. The Higher Timeframe (HTF) Trap
This is the most common way coders accidentally leak future data. It happens when you use request.security() to pull data from a higher timeframe down to your scalping timeframe (e.g., pulling 1-hour data onto a 5-minute chart).
The Problem:
Historically, if you ask Pine Script for the 1-hour close while you are on the 10:05 AM 5-minute candle, Pine engine already knows how that 1-hour candle ended. It will feed the final 11:00 AM closing price to your 10:05 AM candle. Your strategy executes a perfect trade at 10:05 based on information it couldn't possibly have known yet! But in live trading, the 1-hour close doesn't exist yet, so the indicator behaves completely differently.
Code: Select all
// This looks at the 1-hour close on historical bars before the hour is actually over!
htf_close = request.security(syminfo.tickerid, "60", close)You must tell Pine Script to only pull the data from the previous, fully closed higher timeframe candle.
Code: Select all
// This forces the script to use the last completed 1-hour candle, preventing leaks.
htf_close_safe = request.security(syminfo.tickerid, "60", close[1], lookahead = barmerge.lookahead_on)