You find a divergence indicator. You scroll back through the chart and it looks perfect — every signal printed right at the turn, lines drawn exactly where price reversed.

Then you run it live, and a signal you saw yesterday is simply... gone. Or it moved. Or the beautiful historical line now connects two swings that weren't both visible when the alert fired.

That's repainting, and in divergence indicators it isn't a rare bug. It's the default outcome of how most of them are written. I just finished building a multi-timeframe MACD + Stochastic divergence suite for TradingView, and the entire design — and the testing — was organised around one goal: making repainting structurally impossible, and then proving it instead of claiming it.

Why divergence indicators repaint

A divergence compares two swing points: two price highs against the oscillator values at those same moments. The trap is in the word "swing."

A pivot high is only a pivot high once the bars to its right have printed and stayed lower. ta.pivothigh(high, 5, 5) confirms a pivot five bars after the fact. If your script then draws the signal back at the pivot bar — which is where it looks best — it is showing you information from the future, beautifully arranged.

Multi-timeframe versions add a second trap. If you pull a 4H MACD value onto a 15m chart with the wrong request.security settings, you read the 4H bar while it is still forming. Its value at 10:15 is not its value at the 12:00 close. The divergence that "existed" at 10:15 can legally cease to exist by the close — and your chart history quietly rewrites itself.

Two failure modes, one result: the backtest lies, and the live chart disagrees with your screenshots.

The fix: only closed bars may speak

The engine I built follows three rules:

1. Detection runs entirely inside the higher timeframe. The whole divergence check — pivots, oscillator sampling, the lot — executes inside the request.security context, exactly as if the script were running natively on the HTF chart. A pivot is used only after its right-side bars have closed on that timeframe.

2. The confirmed event is shifted one full HTF bar before the chart sees it. Inside the engine function, the returned tuple is the previous HTF bar's event, component by component:

// Return the PREVIOUS HTF bar's event (see header: why this cannot repaint) [evCode[1], evTime[1], evP1[1], evP2[1], evO1[1], evO2[1], evT1[1], evT2[1], evEntry[1], evSL[1], evTP1[1], evTP2[1]] 

3. It is fetched with lookahead_on — deliberately. This is the part that surprises people. lookahead = barmerge.lookahead_on combined with the [1] shift means: chart bars in HTF bucket K+1, and only those, can see the event that confirmed at the close of HTF bar K. No chart bar ever reads an HTF bar that hasn't fully closed:

[c, cTime, p1, p2, o1, o2, t1, t2, entry, sl, tp1, tp2] = request.security( syminfo.tickerid, tfStr, f_events(f_macdLine()), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on) 

The label, the lines and the alert appear at the first lower-timeframe bar of the next HTF bucket. From that moment they can never move, because every input they were computed from is already final. The cost is honesty: the signal arrives one HTF bar after the pivot confirms, not painted retroactively at the perfect spot. That delay is the price of a signal that stays put.

Drawings are also created exactly once per confirmation timestamp and never edited afterwards — no "helpful" repositioning on later bars.

How I proved it: truncate-and-compare

Claiming "non-repainting" is easy. I wanted a mechanical proof, so I re-implemented the Pine logic line-for-line as a reference engine (identical pivot rules, identical MACD/Stochastic math, identical HTF resampling, identical confirmation timing) and tested it against real exchange data: BTC, ETH, SOL, a gold proxy (PAXG) and XRP, across 1m→15m up to 1H→4H combinations, 1,500–2,000 bars per series.

The core test is bar replay by truncation:

  1. Run the engine on the full series. Record every signal: bar, type, prices.
  2. Cut the data at 45%. Re-run. Every signal confirmed before the cut must reappear identically.
  3. Repeat at 65% and 85%.

If any earlier signal changes, moves or vanishes when future data is removed, the logic repaints — full stop.

Result: 20 bar-replay tests, 466 individual signal checks, 0 changed.

The replay tests were part of a 50-test suite, all of which passed:

  • 10 planted-pattern tests — synthetic series with known regular and hidden divergences (bullish and bearish, both oscillators). The engine found exactly the planted events: no misses, no extras.
  • 6 no-divergence controls — series where price and oscillator agree, plus pure trend ramps. Zero signals, as required.
  • 8 robustness tests — flat series, NaN-injected data, a 100× spike bar, 300 removed bars (time gaps), ±5% whipsaw. No crashes, no invalid output.
  • 6 SL/TP sanity suites — 110 real-data signals checked: the stop always sits beyond the swing on the correct side, and targets land exactly at 1.5R and 2.5R. Zero violations.

One honest limitation: a reference engine mirrors the logic, and Pine's internal EMA/ATR seeding in the first warmup bars can differ marginally. That can nudge an oscillator reading in the warmup zone; it cannot change pivot logic, confirmation timing, or the non-repainting property — which are what these tests verify. The full report, with every test listed, is in the repo as VERIFICATION.md.

What the suite does

  • 8 higher-timeframe slots at once, each with its own timeframe dropdown and enable toggle — one 15m chart can watch 1H, 4H, 12H and Daily divergences simultaneously.
  • Regular + hidden divergence, bullish and bearish, for both MACD (measured on the MACD line, sampled exactly at the pivot bar) and Stochastic (measured on smoothed %K).
  • Swing-to-swing lines on price, labels on confirmation ("Bull Div", "Hidden Bear", …), and one alert per divergence type.
  • SL/TP levels on every signal: stop beyond the swing plus a 0.25×ATR buffer, TP1 at 1.5R, TP2 at 2.5R.
  • Companion pane scripts: a single overlay can't draw inside a separate pane, so each indicator ships with a twin that runs the identical engine and draws the divergence lines on the oscillator itself.
  • Every rule is an input — pivot left/right, min/max bars between pivots, oscillator lengths — so you can encode your own divergence definition without restructuring code.

Try it

The code is open source (MIT), Pine Script v6, and free to use:

GitHub: https://github.com/kamalmosadegh/mtf-divergence-indicators

Paste any of the four scripts into TradingView's Pine Editor, add to chart, and set your timeframes. If you run the replay test yourself — scroll to any historical signal, then replay forward from before it — I'd genuinely like to hear what you find. And if your own indicator repaints, check which of the two traps it's falling into: the pivot drawn at its own bar, or the unclosed HTF bar. It's almost always one of those two.

Kamal Mosadegh — Pine Script developer. More projects: portfolio · LinkedIn · GitHub