Pine Script: How to Add and Display Text on a Horizontal Line / Hline?

Pine Script: How to Add and Display Text on a Horizontal Line / Hline?
★ Key Takeaways

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

Curious as to what pine Script: How to Add and Display Text on a Horizontal Line / Hline implies? Find the full explanation with up-to-date information.

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]) 
1
Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.