DEV Community

Rezali
Rezali

Posted on

How to Catch a Pine Script Repaint Bug Before It Costs You Real Money

I've watched too many TradingView strategies look great in the Strategy Tester and then fall apart the moment real money went live. Almost every time, the code compiled fine. The bug wasn't syntax. It was repainting, the script quietly using information it shouldn't have had yet.

Repainting doesn't throw an error. It just quietly makes your backtest better than your live trading will ever be. Here are the four places it actually comes from, and how to catch each one before you trust a strategy.

1. request.security() with the wrong lookahead

If you pull a higher-timeframe value with request.security() and don't handle the offset correctly, the current, still-forming HTF bar can leak into your calculation. The fix is barmerge.lookahead_off combined with offsetting the source by one bar, e.g. close[1]. lookahead_on is only safe when you've already offset the source yourself. Using it directly on a live value is the single most common repaint source in Pine scripts posted online.

2. Signals computed before the bar closes

If your entry logic runs on close or ta.crossover() without a barstate.isconfirmed guard, the signal can appear, then disappear, then reappear as the candle's still-forming close price changes. What you saw fire in real time is not always what the finished bar actually did. Guard any entry/exit logic that matters with barstate.isconfirmed if you're evaluating it intrabar.

3. Same-bar stop/target ambiguity

When your stop and your target could both have been hit inside the same bar's high-low range, the Strategy Tester has to guess which one happened first. It doesn't always tell you which assumption it made, and that one hidden assumption can flatter your win rate without you ever seeing it happen.

4. Bar Replay is the real manual test

TradingView's Bar Replay tool is the closest thing to a repaint detector you already have. Step through history bar by bar and watch whether a signal that appeared in the past matches what you originally saw. If a signal moves, vanishes, or appears retroactively as you replay forward, that's repainting, live in front of you.

A worked example

I wrote a simple opening range breakout strategy (SPY, 9:30-9:35 ET range, stop = 1x range, target = 2x range, one trade a day). Before trusting the Strategy Tester's number on its own, I pulled 60 days of real 5-minute SPY bars independently (free, no login, Yahoo Finance's chart endpoint) and replicated the exact entry/exit logic in plain Python, with a conservative rule: if a bar could have hit both the stop and the target, count the stop.

Result: 60 trades, 30 wins, 30 losses, net +2.63%, profit factor 1.35, max drawdown 1.42%. The two independent methods agreed on the shape of the result. That agreement is the bar I'd want cleared before trusting any single Strategy Tester number by itself. (Full script and numbers: happy to share in the comments if anyone wants to replicate it.)

Doing that cross-check by hand every time is slow, so I ended up building a tool that automates it: codemyedge (codemyedge.com) takes a strategy described in plain English, generates the Pine Script, then actually runs it on real data and shows you the chart, so you can see whether it repaints before you risk anything on it. Script generation is free and unlimited, chart previews (the part that catches the repaint) are free up to 10 a day. It's one option among a few Pine-generation tools out there now (Pineify and LuxAlgo Quant are two others). Whichever one you use, verify the output the way I did above before you trust it live.

What's your go-to way of catching a repaint bug before it bites you?

Top comments (0)