# Repainting

> Why a signal can look perfect on history and never happen live, the four usual causes, and the patterns that prevent each.

A script **repaints** when what it shows on past bars differs from what it showed while those bars were live. A repainting strategy backtests beautifully and then loses money, because the signals it was tested on never existed in real time.

## The four usual causes

| Cause | What happens | Fix |
| --- | --- | --- |
| Acting on the live bar | A condition is true mid-bar, then false at the close, and the marker disappears | Require `barstate.isconfirmed` |
| Higher timeframe with lookahead | `request.security(..., lookahead_on)` on the current value reads the *future* close of the higher-timeframe bar on history | Use the `[1]` pattern below |
| Pivots and "future" bars | `ta.pivothigh(5, 5)` confirms five bars late; plotting it back with `offset = -5` hides that delay | Act on it when it is confirmed, not where it is drawn |
| `varip` and per-tick logic | Values that change within a live bar cannot be reproduced on history | Use `var` unless you truly need per-tick state |

## The non-repainting higher-timeframe pattern

Request the **previous** completed bar of the higher timeframe, with lookahead on, so that history and live behave the same:

```pine
//@version=6
indicator("HTF close without repainting", overlay = true)
htfClose = request.security(syminfo.tickerid, "60", close[1], lookahead = barmerge.lookahead_on)
plot(htfClose, "1h close (confirmed)", color = color.orange, style = plot.style_stepline)
```

## Confirmed signals

```pine
//@version=6
indicator("Signals that stay", overlay = true)
f = ta.ema(close, 9)
s = ta.ema(close, 21)
longSignal = ta.crossover(f, s) and barstate.isconfirmed
plotshape(longSignal, "Long", style = shape.triangleup, location = location.belowbar, color = color.teal)
```

## Checking a script

Watch it live for a while next to its history: if markers appear and vanish on the forming bar, it acts on unconfirmed data. The [checker](/docs/check) also warns about `lookahead_on` and `varip`.
