【问题标题】:Pine script how to add labels instead of plotPine 脚本如何添加标签而不是绘图
【发布时间】:2021-05-16 15:48:58
【问题描述】:

我是 Pine 脚本的初学者,正在尝试新事物。以下是一个 sn-p,它将每日枢轴作为阶梯线添加到图表中。但是,我想在每个值上添加标签而不是行。恳请您帮助实现这一目标。

sd = input(true, title="Show Daily Pivots?")

//Pivot Range Calculations - Mark Fisher
pivot = (high + low + close ) / 3.0 
bc = (high + low ) / 2.0 
tc = (pivot - bc) + pivot
r1 = (pivot * 2) - low
s1 = (pivot * 2) - high
r2 = pivot + (high - low)
s2 = pivot - (high - low)
r3 = r1 + (high - low)
s3 = s1 - (high - low)
r4 = r3 + (r2 - r1)
s4 = s3 - (s1 - s2)

//Daily Pivot Range 

dtime_r1 = security(syminfo.tickerid, 'D', r1[1])
dtime_r2 = security(syminfo.tickerid, 'D', r2[1])
dtime_r3 = security(syminfo.tickerid, 'D', r3[1])
dtime_r4 = security(syminfo.tickerid, 'D', r4[1])
dtime_s1 = security(syminfo.tickerid, 'D', s1[1])
dtime_s2 = security(syminfo.tickerid, 'D', s2[1])
dtime_s3 = security(syminfo.tickerid, 'D', s3[1])
dtime_s4 = security(syminfo.tickerid, 'D', s4[1])

offs_daily = 0 

plot(sd and dtime_r1 ? dtime_r1 : na, title="Daily r1",style=plot.style_stepline, color=color.green,linewidth=1)
plot(sd and dtime_r2 ? dtime_r2 : na, title="Daily r2",style=plot.style_stepline, color=color.green,linewidth=1)
plot(sd and dtime_r3 ? dtime_r3 : na, title="Daily r3",style=plot.style_stepline, color=color.green,linewidth=1)
plot(sd and dtime_r4 ? dtime_r4 : na, title="Daily r4",style=plot.style_stepline, color=color.green,linewidth=1)
plot(sd and dtime_s1 ? dtime_s1 : na, title="Daily s1",style=plot.style_stepline, color=color.red,linewidth=1)
plot(sd and dtime_s2 ? dtime_s2 : na, title="Daily s2",style=plot.style_stepline, color=color.red,linewidth=1)
plot(sd and dtime_s3 ? dtime_s3 : na, title="Daily s3",style=plot.style_stepline, color=color.red,linewidth=1)
plot(sd and dtime_s4 ? dtime_s4 : na, title="Daily s4",style=plot.style_stepline, color=color.red,linewidth=1)

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    像这样?

    //@version=4
    study("My Script", overlay=true)
    
    sd = input(true, title="Show Daily Pivots?")
    
    f_new_label(_title, _color) => label.new(na, na, _title, color=_color, style=label.style_label_left)
    
    var label_r1 = f_new_label("r1", color.green)
    var label_r2 = f_new_label("r2", color.green)
    var label_r3 = f_new_label("r3", color.green)
    var label_r4 = f_new_label("r4", color.green)
    
    var label_s1 = f_new_label("s1", color.red)
    var label_s2 = f_new_label("s2", color.red)
    var label_s3 = f_new_label("s3", color.red)
    var label_s4 = f_new_label("s4", color.red)
    
    f_label_move(_label, _value) =>
        if sd
            label.set_xy(_label, bar_index, _value)
            label.set_text(_label, array.join(array.copy(array.slice(str.split(label.get_text(_label),""),0,2)),"") + " : " + tostring(round(_value,2)))
        else
            label.set_xy(_label, na, na)
    
    //Pivot Range Calculations - Mark Fisher
    pivot = hlc3
    bc = hl2
    tc = (pivot - bc) + pivot
    r1 = (pivot * 2) - low
    s1 = (pivot * 2) - high
    r2 = pivot + (high - low)
    s2 = pivot - (high - low)
    r3 = r1 + (high - low)
    s3 = s1 - (high - low)
    r4 = r3 + (r2 - r1)
    s4 = s3 - (s1 - s2)
    
    //Daily Pivot Range 
    [dtime_r1,dtime_r2,dtime_r3,dtime_r4,dtime_s1,dtime_s2,dtime_s3,dtime_s4] = security(syminfo.tickerid, 'D', [r1[1],r2[1],r3[1],r4[1],s1[1],s2[1],s3[1],s4[1]])
    
    if barstate.islast
        f_label_move(label_r1, dtime_r1)
        f_label_move(label_r2, dtime_r2)
        f_label_move(label_r3, dtime_r3)
        f_label_move(label_r4, dtime_r4)
        
        f_label_move(label_s1, dtime_s1)
        f_label_move(label_s2, dtime_s2)
        f_label_move(label_s3, dtime_s3)
        f_label_move(label_s4, dtime_s4)
    

    【讨论】:

    • 我尝试将其添加到图表中,但是,没有做任何事情。想知道为什么
    • 您在哪个代码和时间框架上尝试过这个?
    • 这就像一个魅力。非常感谢。
    • 如果可能还有一个问题,如何打印标签的价格?例如:s1:50500
    • 更新了我的答案,包括标签上的价格。
    猜你喜欢
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多