> ## 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 keep one label that moves with the last bar

> Keeping a single label in a static variable and moving it with label.set_xy, or deleting and re-creating it on the last bar.

`label.new` creates a new label every time it runs, so calling it on each bar leaves a trail of labels. To have one label, keep its
handle in a `static` variable. A `static` initialiser runs once, on the first bar, so the label is created once; after that, move it
with `label.set_xy` and change its text with `label.set_text`.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last close label", overlay=true)
static label tag = label.new(bar_index, close, "")
label.set_xy(tag, bar_index, close)
label.set_text(tag, str.tostring(close))
```

On every bar the label is moved to that bar's close, so when the script has run through the history it sits on the last bar.

## Delete and re-create

If the label should appear only on the last bar, or you would rather build it fresh than change it (a label's style can also be
changed in place with `label.set_style`), keep the handle in a `static` variable initialised to `na`, delete the old label and create a new one on `barstate.islast`:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Last close label, re-created", overlay=true)
static label tag = na
if barstate.islast {
    label.delete(tag)
    tag := label.new(bar_index, close, str.tostring(close), style=label.style_label_left)
}
```

On a live chart the last bar is the forming one, and the script runs on every tick of it. Drawings made on a forming bar are reset to
the last closed bar's drawings at each new tick, and a `static` value starts each run at its last committed value, so every tick
starts from the same state.

## The mistake to avoid

> [!WARNING]
>
> Calling `label.new` on every bar without keeping the handle. Each bar adds a label, and once the count passes the limit the oldest are
> deleted. The limit is 50 labels unless you raise `max_labels_count` in `indicator` (up to 500). See
> [How to keep drawings under the limit](https://gocharting.com/docs/scripting/how-to/keep-drawings-under-the-limit.md) and
> [Lines, labels, boxes and polylines](https://gocharting.com/docs/scripting/chart-output/drawings.md).


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