pine script v5 - TradingviewPinescript Indicator Line Labels Keep Stacking Up With New Price Bars - Stack Overflow

admin2025-04-15  0

I created an indicator in Pinescript 5 with an array that I'm looping through to draw lines at the array values as well as a label for the line, however, when the chart moves to a new bar the label writes over the previous label and makes it illegible. I added a line to delete the previous label, but it's not working.

 //Allows user to hide values
 showTPOrth = input(true, "Show TPO RTH")
 
 tpoRTHlevels = array.new_float(5)
 array.set(tpoRTHlevels,0,5278)
 array.set(tpoRTHlevels,1,5337)
 array.set(tpoRTHlevels,2,5378)
 array.set(tpoRTHlevels,3,5441)
 array.set(tpoRTHlevels,4,5479)
 
 if barstate.islast
     if showTPOrth
         for k = 0 to array.size(tpoRTHlevels) - 1level_2 = array.get(tpoRTHlevels, k)
 
         Draw horizontal line
         line.new(x1=bar_index, y1=level_2, x2=bar_index + 1, y2=level_2, extend=extend.right, color=close_price > level_2 ? #1bb622 : #cf2020, width=1)
 
         Add a label to display the value
         l=label.new(bar_index + 4, level_2, text=str.tostring(level_2), textcolor=#666666, style=label.style_label_lower_left)
         label.delete(l[1]

Any help is appreciated!

I created an indicator in Pinescript 5 with an array that I'm looping through to draw lines at the array values as well as a label for the line, however, when the chart moves to a new bar the label writes over the previous label and makes it illegible. I added a line to delete the previous label, but it's not working.

 //Allows user to hide values
 showTPOrth = input(true, "Show TPO RTH")
 
 tpoRTHlevels = array.new_float(5)
 array.set(tpoRTHlevels,0,5278)
 array.set(tpoRTHlevels,1,5337)
 array.set(tpoRTHlevels,2,5378)
 array.set(tpoRTHlevels,3,5441)
 array.set(tpoRTHlevels,4,5479)
 
 if barstate.islast
     if showTPOrth
         for k = 0 to array.size(tpoRTHlevels) - 1level_2 = array.get(tpoRTHlevels, k)
 
         Draw horizontal line
         line.new(x1=bar_index, y1=level_2, x2=bar_index + 1, y2=level_2, extend=extend.right, color=close_price > level_2 ? #1bb622 : #cf2020, width=1)
 
         Add a label to display the value
         l=label.new(bar_index + 4, level_2, text=str.tostring(level_2), textcolor=#666666, style=label.style_label_lower_left)
         label.delete(l[1]

Any help is appreciated!

Share Improve this question asked Feb 4 at 14:21 bandelierbandelier 91 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Try this method:

//@variable Efficient label
var efficientLabel = label.new(na, na, text = _txt, color = color(na), textcolor = chart.fg_color)
if barstate.islast
    label.set_xy(efficientLabel, _x, _y)
转载请注明原文地址:http://www.anycun.com/QandA/1744713684a86592.html