pine script : how to add and display text on a horizontal line / hline?

I want to include a text input feature to add text onto horizontal lines / Hlines that I am plotting onto a rate-of-change indicar. These lines are essentially like 70/30 RSI overbought and oversold lines, but I am using them instead for probability zones. I want to be able to write in text that will show up on each line in the plot. Tradingview provides a custom horizontal line drawing tool with a text input and option to align the text to right, center or left. I would like these features included inside a rate of change indicator with the set of horizontal lines which mark of the probability zones. Thank you for any help

//@version=5
indicator(title="Rate Of Change", shorttitle="ROC-w-prb-lne", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
length = input.int(1, minval=1)
source = input(close, "Source")
roc = 100 * (source - source[length])/source[length]
plot(roc, color=#2962FF, title="ROC-w-probty ln")
hline(0.5, color=#edde35, title="50% (+25%) prob Line")
hline(-0.5, color=#edde35, title="50% (-25%) prob line")
hline(1.0, color=#2950de, title="75% (+38%) Line")
hline(-1.0, color=#2950de, title="75% (-36%) Line")
hline(2.5, color=#787B86, title="95% (+48%) line")
hline(-2.5, color=#787B86, title="95% (-48%) line")
hline(3.5, color=#c73030, title="98% (+49%) line")
hline(-3.5, color=#c73030, title="98% (-49%) line")

1 Answer

You should use a 'label' if you want to display some text. Set its y argument to your hline's value, and manipulate the x argument as you wish.

You can use bar_index, chart.left_visible_bar_time or chart.right_visible_bar_time to manipulate the x coordinate.

Below are some examples:

//@version=5
indicator("My script")
hline_price_1 = 100
hline_price_2 = 75
hline_price_3 = 50
hline_price_4 = 25
hline(hline_price_1)
hline(hline_price_2)
hline(hline_price_3)
hline(hline_price_4)
label hline_label_1 = label.new(bar_index, hline_price_1, str.tostring(hline_price_1), style=label.style_none, textcolor=color.white)
label hline_label_2 = label.new(bar_index - 50, hline_price_2, str.tostring(hline_price_2), style=label.style_none, textcolor=color.white)
label hline_label_3 = label.new(chart.left_visible_bar_time, hline_price_3, str.tostring(hline_price_3), xloc=xloc.bar_time, style=label.style_none, textcolor=color.white)
label hline_label_4 = label.new(chart.right_visible_bar_time, hline_price_4, str.tostring(hline_price_4), xloc=xloc.bar_time, style=label.style_none, textcolor=color.white)
label.delete(hline_label_1[1])
label.delete(hline_label_2[1])
label.delete(hline_label_3[1])
label.delete(hline_label_4[1])

enter image description here

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like