New objects and new text are different operations
Calling label.new on every historical bar allocates a new object each time. Assigning its result to the same variable name does not delete the previous object. A setter changes the object referenced by an existing ID. var keeps the initial reference across bars unless you explicitly reassign it.
- Labels accept series strings, so price text can use str.tostring(close, format.mintick). plotshape text requires a const string.
- Labels can be created inside an if block. Put the condition around creation when an event is absent; do not allocate an invisible object on every bar.
| Requirement | Allocation rule | Cleanup rule |
|---|---|---|
| Caption on every qualifying bar | One new label per event | Delete old events or accept garbage collection |
| Current price beside the latest bar | One label for the script run | Move and rewrite the same object |
| Last three event prices | New label when an event closes | Delete the oldest before adding a fourth |
| Fixed CROSS caption across chart history | Use a conditional plotshape series | No individual label ID to delete |