Ask Your Question

Revision history [back]

There are a few ways to avoid labels overlapping in Pinescript:

  1. Use the input parameters overlay=true and yloc=yloc.abovebar in the label.new() function to automatically position labels above the bars without overlapping.

  2. Calculate the distance between labels and adjust the yloc parameter accordingly to create more space between labels. For example:

dist = abs(close - close[1])
label_yloc = close > close[1] ? yloc.belowbar : yloc.abovebar
label.new(bar_index, close, tostring(dist), yloc=label_yloc)
  1. Use the security() function to plot labels on a separate indicator pane, rather than directly on the price chart, to avoid overlapping with other price-related indicators. For example:
study("My Indicator")
upperBand = sma(close, 20) + stdev(close, 20)
lowerBand = sma(close, 20) - stdev(close, 20)
plot(upperBand, "Upper Band")
plot(lowerBand, "Lower Band")

myLabel = label.new(bar_index, upperBand, "My Label", color.white)
security(symbol, "D", myLabel)

This code plots the label "My Label" on a separate "D" indicator pane, avoiding overlap with the upper and lower bands plotted on the price chart.