Skip to main content
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.
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:
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

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 and Lines, labels, boxes and polylines.