在 TradingView 中将线文本添加到 OHLC 绘图形状系列

Adding Line Text to OHLC plot shape series in TradingView

我正在为 TradingView 进行前一天的高点、低点和中点的 OHLC 研究。我正在尝试添加一个类似于水平线文本功能的 label/text。我在下面附上了两张图片作为我想要实现的示例。

如何将 label/text 添加到我的线图中?

代码如下:

h = security(syminfo.tickerid,"D",high,barmerge.gaps_off,barmerge.lookahead_on)
m = security(syminfo.tickerid,"D",hl2,barmerge.gaps_off,barmerge.lookahead_on)
p = security(syminfo.tickerid,"D",hlc3,barmerge.gaps_off,barmerge.lookahead_on) 
l = security(syminfo.tickerid,"D",low,barmerge.gaps_off,barmerge.lookahead_on)

plotshape(series = condition and PrevBars ? h[1] : na, title = "Previous high",color = na,location=location.absolute ,editable = true)
plotshape(series = condition and PrevBars ? m[1] : na, title = "Previous median",color = na,location=location.absolute ,editable = true)
plotshape(series = condition and PrevBars ? p[1] : na, title = "Previous pivot",color = na,location=location.absolute ,editable = true)
plotshape(series = condition and PrevBars ? l[1] : na, title = "Previous low",color = na ,location = location.absolute , editable = true)

// Draw lines from the previous highs and lows
newSession = change(time('D'))
count = barssince(newSession)

var line PrevHigh = na
var line PrevMedian = na
var line PrevPivot = na
var line PrevLow = na

if (newSession)
    PrevHigh := line.new(x1=bar_index, y1=h[1],
                 x2=bar_index, y2=h[1], color = color.green,width=1)
    PrevMedian := line.new(x1=bar_index, y1=m[1],
                 x2=bar_index, y2=m[1], color = color.yellow,width=1)
    PrevPivot := line.new(x1=bar_index, y1=p[1],
                 x2=bar_index, y2=p[1], color=color.orange,width=1)             
    PrevLow  := line.new(x1=bar_index,y1=l[1],
                 x2=bar_index,y2=l[1],color = color.red,width = 1)
    line.delete(id = PrevHigh[1])
    line.delete(id = PrevMedian[1])
    line.delete(id = PrevPivot[1])
    line.delete(id = PrevLow[1]) 
    
if (not barstate.islast)
    line.set_x2(id= PrevHigh, x=bar_index)
    line.set_x2(id= PrevMedian, x=bar_index)
    line.set_x2(id= PrevPivot, x=bar_index)
    line.set_x2(id= PrevLow, x=bar_index)
else
    line.set_xloc(id=PrevHigh, x1=time[count + 1],
         x2=time_close + (1 * 86400000), xloc=xloc.bar_time)
    line.set_xloc(id=PrevMedian, x1=time[count + 1],
         x2=time_close + (1 * 86400000), xloc=xloc.bar_time)
    line.set_xloc(id=PrevPivot, x1=time[count + 1],
         x2=time_close + (1 * 86400000), xloc=xloc.bar_time)
    line.set_xloc(id=PrevLow, x1=time[count + 1],
         x2=time_close + (1 * 86400000), xloc=xloc.bar_time)

实现此目的的一种方法是在行的顶部放置一个标签。您已经知道行中的 y 部分。使用相同的 y 并创建一个带有一点偏移的标签。

这是一个例子:

//@version=5
indicator("test", overlay=true)

f_draw_label(price, txt, offset, txtColor) =>
    x_offset = bar_index + offset
    var label_id = label.new(x=x_offset, y=price, text=txt, textcolor=txtColor, style=label.style_none)
    label.set_xy(label_id, x_offset, price)
    label.set_text(label_id, txt)
    label.set_textcolor(label_id, txtColor)
    label_id
    
f_delete_line_and_label(line_id, label_id) =>
    line.delete(line_id[1])
    label.delete(label_id[1])

h = request.security(syminfo.tickerid,"D",high,barmerge.gaps_off,barmerge.lookahead_on)
m = request.security(syminfo.tickerid,"D",hl2,barmerge.gaps_off,barmerge.lookahead_on)
p = request.security(syminfo.tickerid,"D",hlc3,barmerge.gaps_off,barmerge.lookahead_on) 
l = request.security(syminfo.tickerid,"D",low,barmerge.gaps_off,barmerge.lookahead_on)

var line line_h = na
var label label_h = na
var line line_m = na
var label label_m = na
var line line_p = na
var label label_p = na
var line line_l = na
var label label_l = na

if (barstate.islast)
    line_h := line.new(bar_index[1], h, bar_index, h, extend=extend.both, color=color.green)
    line_m := line.new(bar_index[1], m, bar_index, m, extend=extend.both, color=color.blue)
    line_p := line.new(bar_index[1], p, bar_index, p, extend=extend.both, color=color.orange)
    line_l := line.new(bar_index[1], l, bar_index, l, extend=extend.both, color=color.red)
    
    label_h := f_draw_label(h, "High: " + str.tostring(h), 20, color.green)
    label_m := f_draw_label(m, "HL2: " + str.tostring(m), 20, color.blue)
    label_p := f_draw_label(p, "HLC3: " + str.tostring(p), 20, color.orange)
    label_l := f_draw_label(l, "Low: " + str.tostring(l), 20, color.red)
    
    line.delete(line_h[1])
    line.delete(line_m[1])
    line.delete(line_p[1])
    line.delete(line_l[1])
    
    label.delete(label_h[1])
    label.delete(label_m[1])
    label.delete(label_p[1])
    label.delete(label_l[1])