If you have ever tried to mark buy and sell signals directly on the TradingView chart, you have probably run into the label.new and label.delete functions in Pine Script. They are simple on the surface, but they have details — the label limit, persistence across bars and memory management — that crash the scripts of anyone just starting out. This guide shows, in an honest and direct way, how to really use both functions in Pine Script v5/v6, with code you can copy and test today.
Want to turn these visual signals into automated execution at an options broker? See how to connect analysis and bot:
See the bot and Quotex API in Python →What each function does
In short: label.new() creates a label on the chart and returns an identifier of type label. label.delete() receives that identifier and removes the label. You almost always use the two together: create one when a signal appears and delete the old ones so you neither clutter the chart nor hit the limit.
max_labels_count, up to 500). If you create a label on every bar without deleting, the script silently drops the oldest drawings.
Example 1: marking every EMA crossover
The most common case. Here we create a buy label (“COMPRA”) when the fast EMA crosses above and a sell label (“VENDA”) when it crosses below:
The main arguments of label.new are the x position (usually bar_index), the y position (a price, such as high or low), the text, the style (shape and direction of the arrow), and the colors. size accepts size.tiny through size.huge.
Example 2: keeping only the last label (the real use of label.delete)
In many cases you do not want an entire history of labels, only the most recent one. The pattern is to store the identifier in a var variable (which persists across bars) and delete the previous one before creating the new one:
label.delete(na) does not throw an error. That is why you can call delete even before the first label has been created, without needing an if not na(ultima).
Example 3: limiting the history with an array
If you want to keep, say, the last 5 labels and automatically delete the oldest ones, store the identifiers in an array<label>:
Common mistakes that crash the script
The first is creating labels outside an if, making one per bar and hitting the limit. The second is forgetting the var and losing the identifier on every bar — then label.delete never finds the right label. The third is trying to modify a label that has already been deleted: after label.delete, any label.set_text on that id throws a runtime error. If you only need to update the text/position of a fixed label, prefer label.set_* instead of deleting and recreating.
var and use label.set_xy and label.set_text on the following bars.
FAQ
Does label.new work the same in v5 and v6? Yes. The signature and behavior of label.new and label.delete are the same. v6 brought type and performance improvements, but the code above runs in both versions.
Can I use dynamic text, such as the price? You can. Use str.tostring(close, format.mintick) inside text to display formatted values.
Why do my old labels disappear on their own? It is the 500-label limit. Increase it with max_labels_count=500 in indicator() or manage it with an array, as in Example 3.
Does label.new send orders to the broker? No. Labels are just visual drawings on the chart. To actually execute trades you need a strategy (strategy) in a backtest or an external bridge via API/bot.
Disclaimer: binary options and leveraged trading are high-risk products and can lead to the total loss of your capital. This content is educational; it does not constitute an investment recommendation or a promise of results. Indicators and scripts do not guarantee profit. Always test on a demo account before using real money.
