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

# How to avoid a result that changes after the bar closes

> Keeping a value fixed once drawn by latching it on barstate.isconfirmed, reading the previous bar, or running the script only when a bar closes.

On a forming bar the script runs again on every tick, so anything computed from `close`, `high` or `low` of that bar keeps moving until
the bar closes (a script that does this is said to repaint). To keep a result from moving, base it on a bar that has closed. There are three ways; the first is the most flexible.

## Latch the value on `barstate.isconfirmed`

`barstate.isconfirmed` is true on historical bars (all but the last one while the market is open) and, on the forming bar, false until the bar closes. Assign a `static`
variable only when it is true, and plot the variable:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Settled close", overlay=true)
static float settled = na
if barstate.isconfirmed {
    settled := close
}
plot(settled, title="Last closed close")
```

A `static` value is committed when a bar closes. On the forming bar each run starts from the last committed value, so `settled` holds
the previous bar's close and does not follow the ticks. On history every bar is confirmed (bar the last, while the market is open), so `settled` equals `close`.

## Other ways

* **Read the previous bar.** `high[1] - low[1]` rests on a bar that has closed. The cost is one bar of delay.
* **Run only at bar close.** With `calc_on_every_tick=false` in `indicator`, the script runs on a forming bar only when the bar closes.
  History is unaffected.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Settled range", overlay=false, calc_on_every_tick=false)
plot(high - low, title="Bar range")
```

Two related tools. A variable declared `intra` keeps its value across the repeated runs of one forming bar and starts over on the next bar (see [Values that persist across bars](https://gocharting.com/docs/scripting/evaluation/persistence.md)). A strategy has three flags that decide when it runs and fills: `calc_on_every_tick`, `calc_on_order_fills` and `process_orders_on_close` (see [Strategies and backtesting](https://gocharting.com/docs/scripting/strategies/overview.md)).

## Alerts

`alert` fires at most once per bar, on realtime bars only. Guard it with `barstate.isconfirmed` so it fires on the closed bar's
values:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Closed-bar alert", overlay=true)
fast = talib.sma(close, 10)
slow = talib.sma(close, 30)
if barstate.isconfirmed and talib.crossover(fast, slow) {
    alert("Fast crossed above slow")
}
plot(fast, title="Fast")
plot(slow, title="Slow")
```

## The mistake to avoid

Reading `close` on the forming bar and treating it as final. The compiler cannot warn about it, because the script is valid; the
value simply changes with each tick. See [Recalculation and confirmation](https://gocharting.com/docs/scripting/evaluation/recalculation.md) and
[How to tell a forming bar from one that has closed](https://gocharting.com/docs/scripting/how-to/tell-a-forming-bar-from-one-that-has-closed.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.