A Deep Dive into Pine Script Stability by Jayadev Rana
Imagine this scenario:
You find a “Holy Grail” strategy on TradingView. The backtest results are incredible—90% win rate, massive profits, and zero drawdowns. You excitedly switch it on for live trading, dreaming of passive income.
Welcome to the nightmare of Repainting. As a Pine Script Expert Freelancer, the most common request I get is: “Jayadev, can you fix this indicator? It cheats.”
Today, I’m going to pull back the curtain on why this happens and how a professional developer fixes it.
The “Crystal Ball” Glitch
Repainting occurs when a script “looks into the future” to calculate past data. It’s like betting on a horse race after you already know who won.
In Pine Script, this usually happens with the powerful but dangerous function: request.security().
The Dangerous Code (Do Not Use This)
//@version=5
// WARNING: This script repaints!
indicator("Repainting MTF Example", overlay=true)
// Getting Daily Close on a 1-Hour Chart
dailyClose = request.security(syminfo.tickerid, "D", close)
plot(dailyClose)
If you run this code on a 1-hour chart at 10:00 AM, the script already grabs the Closing Price of the day (which hasn’t happened yet in real-time). This creates fake “perfect” signals that disappear in live markets.
The 3 Types of Repainting
Most traders think repainting is just one thing. It’s actually three different bugs:
- Future Leak Repainting: Using `request.security` without proper offsets. The script reads data from higher timeframes that hasn’t closed yet.
- Real-Time Repainting: Indicators that calculate on every tick (price update). A crossover might happen at 10:14 AM, but by 10:15 AM (candle close), the price dropped and the crossover was invalidated.
- Offset Repainting: Using negative offsets like `plot(close, offset=-5)`. This literally draws lines in the past.
How I Code “Non-Repainting” Scripts
When clients hire me for Custom TradingView Indicator Development, reliability is my #1 deliverable. I don’t build scripts that lie.
The Reliable Solution
//@version=5
// SUCCESS: This script does NOT repaint.
indicator("Reliable MTF Example", overlay=true)
// We use 'barmerge.lookahead_on' carefully,
// AND we reference the previous bar [1]
dailyClose = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
plot(dailyClose)
By adding [1], we tell the script: “Only show me the data that has actually closed.” The signals might look slightly less perfect on history, but they will work 100% correctly in live trading.
Frequently Asked Questions
1. Can any repainting indicator be fixed?
2. Does repainting affect automated trading?
3. What is the ‘barmerge.lookahead’ warning?
4. Why do my alerts fire but the chart shows nothing?
5. Is Heikin Ashi considered repainting?
6. How much does it cost to fix a script?
7. Can you verify if a script repaints before I buy it?
Stop Trading with Phantom Signals
Your capital is too valuable to risk on buggy code. A strategy that repaints is not a strategy—it’s a trap.
Do you have a Multi-Timeframe Indicator that seems too good to be true? Or perhaps you need a custom strategy built from scratch?
>> Hire Me to Fix Your Script