Ask Your Question
3

How can I avoid labels overlapping in Pinescript?

asked 2022-04-10 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-12-25 03:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-10 11:00:00 +0000

Seen: 19 times

Last updated: Dec 25 '22