How to Avoid Repainting in Pine Script
So you've been working with Pine Script and keep hearing about this thing called "repainting"? Yeah, it's one of those annoying problems that can really mess up your trading results. Let me break it down for you in simple terms and show you how to avoid it.

What's Repainting Anyway?
Think of repainting like this: imagine you're looking at old photos and suddenly they start changing - that person who was wearing a red shirt is now wearing blue. That's basically what happens with repainting indicators.
When you backtest your strategy, it might show amazing results because the indicator "knows" what happened next. But when you run it live, those same signals look completely different. It's like having a crystal ball during backtesting that disappears in real trading.
Here's what's actually happening:
- Your indicator gives you one signal when you're looking at historical data
- But when that same bar was forming in real-time, it was giving you different signals
- This makes your backtests look way better than reality
The Two Main Types You'll Run Into

Indicator Repainting: Your moving averages, RSI, or whatever indicator you're using keeps changing its mind about past values. One minute it says "buy," the next it says "sell" for the same historical point.
Strategy Repainting: Your whole trading strategy looks like it's making money hand over fist in backtests, but when you trade it live, it's a different story entirely.
Why This Happens (The Technical Stuff)
Most of the time, repainting happens because your script is looking at data that isn't "final" yet. Think about it - when a bar is still forming, the high, low, and close are constantly changing. If your indicator uses these values, it's going to keep changing its signals.
Another common culprit is when you're pulling data from higher timeframes. If you're on a 5-minute chart but grabbing daily data, and that daily bar isn't closed yet, you're going to get repainting.
How to Fix This Mess
Alright, here's the good stuff - how to actually prevent repainting:
1. Use request.security()
the Right Way
When you're grabbing data from other timeframes, you need to be careful. Here's a trick that works:
//@version=5
indicator("No More Repainting", overlay=true)
// This function prevents repainting
getSafeData(symbol, timeframe, source) =>
request.security(symbol, timeframe, source[barstate.isconfirmed ? 0 : 1])
// Use it like this
dailyHigh = getSafeData(syminfo.tickerid, "D", high)
plot(dailyHigh, color=color.red)
What this does is simple: if the current bar isn't finished yet, it looks at the previous bar instead. No more changing signals!
2. Check if the Bar is Actually Done
Pine Script gives you some handy variables to work with:
barstate.isconfirmed
- tells you if the bar is finishedbarstate.isrealtime
- tells you if you're looking at live data
Use them like this:
if barstate.isconfirmed
// Only do your calculations when the bar is actually finished
// No more flip-flopping signals
3. Stop Using Current Bar Data
This one's simple but easy to forget. Instead of using the current bar's data (which is still changing), use the previous bar:
// Don't do this:
currentClose = close
// Do this instead:
previousClose = close[1]
The [1]
means "one bar ago" - data that's already set in stone.
4. Set Up Alerts Properly
If you're using alerts, make sure they only fire once per bar:
if barstate.isconfirmed and crossover(shortMA, longMA)
alert("Time to buy!", alert.freq_once_per_bar_close)
This way you won't get bombarded with alerts every time the price ticks.
Some Tools That Can Help
Look, coding all this stuff from scratch can be a pain. There are tools out there like Pineify that handle a lot of this repainting prevention automatically.

You can check it out at Pineify if you want something that takes care of the technical stuff for you.
Here's what it can do if you're curious.

The nice thing about using a tool like this is that you don't have to worry about all the technical gotchas - it handles the repainting prevention for you.
Some Quick Tips
- Keep your code clean: Comment everything so you remember what you were thinking six months from now
- Test everything: Don't just backtest - paper trade your strategies first
- Stay updated: Pine Script keeps getting better, so newer versions might have easier ways to handle this stuff
Wrapping Up
Repainting is annoying, but it's totally fixable. The key is to make sure you're only using data that's actually final, not data that's still changing. Use those barstate
variables, be careful with request.security()
, and always reference previous bars when you can.
Once you get the hang of it, you'll never have to deal with those frustrating "why doesn't this work in real trading?" moments again. Your backtests will actually mean something, and your live trading results won't be a nasty surprise.
Trust me, taking the time to fix repainting issues now will save you a lot of headaches (and money) down the road.