在 for 循环生成的行上绘制标签

Draw labels on for loops generated lines

我使用 for 循环在整数(00 和 50)上生成水平线。为了用它们对应的价格标记这些线,我使用 varlabel.set_y 绘制标签。但是,只有一行被标记(最高值的一行)。如何标记每 5 行?

请在下面找到我的代码:

//@version=4
study(title="Round Numbers 2", overlay=true)

StepSize = 500
nbarsforward = input(title="Extend bar", defval = 70)
dt = time - time[1]

var number_of_lines = 5
var step = syminfo.mintick*StepSize
var float roundNumberLine = na
var label1 = label.new(x=na, y=na, xloc=xloc.bar_time, style=label.style_none, textcolor=color.black)

for counter = 0 to number_of_lines - 1
    roundNumberLine := ceil(close / step) * step + (counter * step)
    line.new(bar_index, roundNumberLine, bar_index - 1, roundNumberLine, xloc=xloc.bar_index, extend=extend.both, color=color.black, width=2)

label.set_y(label1, roundNumberLine)
label.set_x(label1, x=time + nbarsforward * dt)
label.set_text(label1, text=tostring(roundNumberLine))

试试这个:

//@version=4
study(title="Round Numbers 2", overlay=true)

StepSize = 500
nbarsforward = input(title="Extend bar", defval = 70)
dt = time - time[1]

var number_of_lines = 5
var step = syminfo.mintick*StepSize

var lines = array.new_line(number_of_lines)
var labels= array.new_label(number_of_lines)
for counter = 0 to number_of_lines - 1
    roundNumberLine = ceil(close / step) * step + (counter * step)
    if na(array.get(lines, counter))
        l = line.new(bar_index, roundNumberLine, bar_index - 1, roundNumberLine, xloc=xloc.bar_index, extend=extend.both, color=color.black, width=2)
        array.set(lines, counter, l)
    else
        l = array.get(lines, counter)
        line.set_x1(l, bar_index - 1)
        line.set_x2(l, bar_index)
        
    if na(array.get(labels, counter))        
        l = label.new(x=time + nbarsforward * dt, y=roundNumberLine, text=tostring(roundNumberLine), xloc=xloc.bar_time, style=label.style_none, textcolor=color.black)
        array.set(labels, counter, l)
    else
        l = array.get(labels, counter)
        label.set_x(l, time + nbarsforward * dt)