> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirrorpip.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to avoid repainting in Tradingview pine script

> This article guides user step by step on how to avoid repainting in tradingview pine script

# How to Avoid Repainting Signals in TradingView Strategies

## The Complete Guide to Building Reliable TradingView Strategies for Automation and Mirrorpip

One of the most common reasons why a TradingView strategy performs exceptionally well during backtesting but fails during live trading is **repainting**.

Many traders discover that signals shown on historical charts do not match the signals generated in real-time. A strategy that appears highly profitable in the Strategy Tester may produce completely different results once connected to an exchange through automation platforms such as Mirrorpip.

Understanding and eliminating repainting is one of the most important steps when building reliable TradingView indicators, strategies, and automated trading systems.

In this guide, we'll explain what repainting is, why it happens, how to identify it, and the best practices to avoid repainting in TradingView Pine Script strategies.

***

# What is Repainting in TradingView?

Repainting occurs when a signal changes after a candle has closed or when historical signals appear differently than they would have in real-time.

In simple terms:

A strategy is repainting if it uses information that was not actually available at the time the signal was generated.

This can result in:

* Signals appearing earlier than they should
* Buy and sell markers moving after chart refresh
* Better backtest results than live performance
* Unrealistic win rates
* False confidence in strategy profitability

***

# Why Repainting is Dangerous for Automated Trading

For manual traders, repainting can be misleading.

For automated trading, repainting can be disastrous.

Consider the following scenario:

### Historical Chart

Buy signal appears at:

* Price: 100

Backtest shows:

* Entry: 100
* Exit: 110
* Profit: 10%

Everything looks perfect.

### Real-Time Trading

The same signal only becomes valid after the candle closes:

* Actual Entry: 104
* Actual Exit: 110
* Profit: 5.7%

The strategy performance is dramatically different.

This is why eliminating repainting is critical before connecting a TradingView strategy to Mirrorpip for live execution.

***

# Common Causes of Repainting

Most repainting issues come from one of the following sources:

### 1. Using Open Candles

Signals generated while a candle is still forming can change before the candle closes.

### 2. Multi-Timeframe Data

Improper use of higher timeframe data can cause signals to change retroactively.

### 3. Future Data Leakage

Using information that would not have been available at the time of the signal.

### 4. Tick-by-Tick Calculations

Strategies that calculate on every tick may behave differently in backtests and live trading.

### 5. Improper Indicator Design

Some custom indicators are intentionally or unintentionally designed using future information.

***

# Best Practice #1: Generate Signals Only After Candle Close

This is the single most effective way to avoid repainting.

### Incorrect Approach

Generating signals during candle formation:

```python theme={null}
buySignal = ta.crossover(close, ema)
```

The crossover may appear and disappear multiple times before the candle closes.

***

### Correct Approach

Wait for candle confirmation:

```python theme={null}
buySignal =
     ta.crossover(close, ema)
     and barstate.isconfirmed
```

Now the signal is only generated after the candle closes.

Once generated, it cannot change.

***

# Example: EMA Crossover Strategy

### Repainting Version

```python theme={null}
fastEMA = ta.ema(close, 9)
slowEMA = ta.ema(close, 21)

buySignal = ta.crossover(fastEMA, slowEMA)
```

The crossover may occur temporarily during candle formation.

***

### Non-Repainting Version

```python theme={null}
fastEMA = ta.ema(close, 9)
slowEMA = ta.ema(close, 21)

buySignal =
     ta.crossover(fastEMA, slowEMA)
     and barstate.isconfirmed
```

This signal only triggers after candle close.

***

# Best Practice #2: Use Previous Candle Values

Many professional strategy developers prefer using completed candle data.

### Example

Instead of:

```python theme={null}
buySignal = close > ema
```

Use:

```python theme={null}
buySignal = close[1] > ema[1]
```

This ensures:

* No intrabar changes
* Stable signals
* Consistent backtests
* Better live execution accuracy

***

# Best Practice #3: Avoid Repainting with Multi-Timeframe Strategies

Multi-Timeframe (MTF) strategies are one of the biggest sources of repainting.

Imagine:

* Trading on a 5-minute chart
* Using a 1-hour Supertrend filter

The current 1-hour candle is still forming.

Its value may change several times before closing.

***

## Incorrect MTF Example

```python theme={null}
htfTrend =
request.security(
    syminfo.tickerid,
    "60",
    supertrendDirection
)
```

This can repaint because the higher timeframe candle is incomplete.

***

## Correct MTF Example

```python theme={null}
htfTrend =
request.security(
    syminfo.tickerid,
    "60",
    supertrendDirection[1],
    lookahead = barmerge.lookahead_on
)
```

This uses the last completed 1-hour candle.

The value can no longer change.

***

# Example: Multi-Timeframe Supertrend Strategy

Suppose your strategy requires:

### Long Conditions

* 5 Minute Supertrend = Bullish
* 15 Minute Supertrend = Bullish
* 1 Hour Supertrend = Bullish

A repainting implementation may continuously change as the higher timeframe candle forms.

A properly coded strategy should only reference completed higher timeframe candles.

This creates stable signals that match both backtesting and live trading.

***

# Best Practice #4: Avoid Future Data References

Future data creates the worst form of repainting.

Examples include:

```python theme={null}
close[-1]
```

or any custom logic that indirectly accesses future bars.

A strategy should never use information that would not have been available at the time of the trade.

If future data is used, historical performance becomes meaningless.

***

# Best Practice #5: Be Careful with calc\_on\_every\_tick

Many developers enable:

```python theme={null}
strategy(
     "My Strategy",
     calc_on_every_tick = true
)
```

This allows calculations to update on every price movement.

The problem is:

Historical candles only store:

* Open
* High
* Low
* Close

TradingView cannot recreate every tick from historical data.

As a result:

* Backtests behave differently
* Live signals behave differently
* Strategy results become inconsistent

For most automated strategies:

```python theme={null}
strategy(
     "My Strategy",
     calc_on_every_tick = false
)
```

is the safer option.

***

# Best Practice #6: Create Alerts Only After Candle Close

Many traders accidentally create repainting alerts.

### Risky Alert

```python theme={null}
alertcondition(
     buySignal,
     "Buy"
)
```

Signal may disappear before candle close.

***

### Recommended Alert

```python theme={null}
alertcondition(
     buySignal and barstate.isconfirmed,
     "Buy"
)
```

This ensures alerts are generated only after confirmation.

This is especially important when using Mirrorpip for automated execution.

***

# Best Practice #7: Handle Heikin Ashi Correctly

Many traders use Heikin Ashi charts because they provide smoother trends.

However, running a strategy directly on a Heikin Ashi chart can create unrealistic backtest results.

A better approach is:

* Use a normal candlestick chart
* Access Heikin Ashi values internally
* Generate signals from Heikin Ashi data
* Execute trades using real market prices

This significantly improves backtest reliability.

***

# How to Test if a Strategy Repaints

Before deploying any strategy:

### Step 1

Open TradingView Bar Replay.

### Step 2

Run the strategy candle by candle.

### Step 3

Compare generated signals with historical chart signals.

### Step 4

Refresh the chart.

### Step 5

Check whether signals move or disappear.

If signals change after refresh, the strategy likely contains repainting issues.

***

# Mirrorpip Best Practices for Non-Repainting Strategies

When building TradingView strategies for Mirrorpip automation:

✅ Generate signals only after candle close

✅ Use confirmed candles whenever possible

✅ Use completed higher timeframe candles

✅ Avoid future data references

✅ Keep calc\_on\_every\_tick disabled

✅ Create alerts only on confirmed signals

✅ Test using TradingView Bar Replay

✅ Validate live behavior before deploying significant capital

***

# Conclusion

Repainting is one of the most misunderstood issues in TradingView strategy development.

A strategy that looks profitable on historical charts may perform very differently in live markets if repainting is present.

By following proper Pine Script development practices, traders can build strategies that generate consistent signals, realistic backtest results, and reliable automation through Mirrorpip.

The goal should never be to create the best-looking backtest.

The goal should be to create a strategy whose historical results closely match its real-world performance.

That is the foundation of successful automated trading.
