支撑位和阻力位的价格标签未正确显示

时间:2019-09-22 05:58:21

标签: plot pine-script

编辑以包含我想要的图片。

the long arrows show where the labels should be

我的label.new代码不符合我的要求,即在阻力/支撑位上方/下方打印标签。

我尝试过

label.new(bar_index, top1, tostring(top1), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labeldown, color=color.red, size=size.small)

但是xloc是不正确的,它在编译时会显示当前蜡烛上的所有标签。

study(title="srlabel", overlay=true)

 //code from [RS] support and resistance indicator

window1 = input(title='lookback window 1', type=input.integer, defval=8)
window2 = input(title='lookback window 2', type=input.integer, defval=21)

top1 = valuewhen(high >= highest(high, window1), high, 0)
bot1 = valuewhen(low <= lowest(low, window1), low, 0)
top2 = valuewhen(high >= highest(high, window2), high, 0)
bot2 = valuewhen(low <= lowest(low, window2), low, 0)

//this is what I've added to show the price of the s/r levels

label.new(bar_index, top1, tostring(top1), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labeldown, color=color.red, size=size.small)

label.new(bar_index, bot1, tostring(bot1), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labelup, color=color.blue, size=size.small)

//plots

t1 = plot(top1, color=top1 != top1[1] ? na : color.red, linewidth=1, title="")
b1 = plot(bot1, color=bot1 != bot1[1] ? na : color.blue, linewidth=1, title="")
t2 = plot(top2, color=top2 != top2[1] ? na : color.red, linewidth=1, title="")
b2 = plot(bot2, color=bot2 != bot2[1] ? na : color.blue, linewidth=1, title="")

fill(t1, t2, transp=80, color=color.red, title="")
fill(b1, b2, transp=80, color=color.blue, title="")

我想在标签各自的价格蜡烛上方和下方显示标签。我认为解决方案全部取决于该xloc规范。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

仅当级别更改时,此命令才会打印标签:

if change(top1)
    label.new(bar_index, top1, tostring(top1), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labeldown, color=color.red, size=size.small)

if change(bot1)
    label.new(bar_index, bot1, tostring(bot1), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labelup, color=color.blue, size=size.small)

enter image description here

[EDIT 2019.09.23 20:39 — LucF] 也许这更接近于您正在寻找的东西:

if change(top2)
    label.new(bar_index, top2, tostring(top2), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labeldown, color=color.red, size=size.small)

if change(bot2)
    label.new(bar_index, bot2, tostring(bot2), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_labelup, color=color.blue, size=size.small)

enter image description here