如何在 TradingView pine 脚本中画一条垂直线?

How to draw a vertical line in TradingView pine script?

我正在尝试使用基于 Web 的 TradingView platform to make my own custom scripts to display various financial market properties. This is possible through its pine 脚本编写 engine/interpreter。

目前我正在尝试在主图表或指标图表上简单地显示一条垂直线。但是,他们的脚本引擎似乎不支持垂直线,除非使用绘图的 histogramcolumn 类型。无论哪种方式,我都无法获得任何令人满意的台词。


一些测试

(1) 我像这样使用 bgcolor() 取得了一些小成功:

//@version=3
study(title="vbar1", overlay = false)
trange(res, sess) => not na(time(res, sess))
vlinecol = #000000 // black
plot(n, color = na) // check last value from plot but don't display
vline =  (n < 5710) ? na : trange("1", "0700-0701") ? vlinecol : na
bgcolor(vline, transp=0)

这导致:

(2)plot()style=histogram 参数一起使用时的结果要好得多:

//@version=3
study(title="vbar2", overlay = true) // scale=scale.none only for overlay=true
vlinecol = #000000 // black
cond = barstate.islast
bh = 10*high   // Use 10 x the window max price height for top of vbar (or use 1e20)
bo = -10       // Set offset from last bar
plot(cond ? bh : na, color=vlinecol, linewidth=2, offset=bo, style = histogram, transp=0)

结果如下:

它是旧的 post,但这可以帮助其他人。 你可以用它画一条线:

testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
plot((time==testPeriodStart)?10e20:na,color=black, linewidth=1, style=line)

虽然我无法绘制虚线

Dany 的回答没有在图表上为我显示任何内容,但是将样式设置为直方图就可以了。

//@version=3
study("Vertical lines", overlay=true, scale=scale.none)

plot((time == timestamp(2019,01,01,0,0)) ? 10e20 : na, 
      color = red, linewidth = 10, title = "27", style = histogram)

plot((time == timestamp(2019,01,02,0,0)) ? 10e20 : na, 
      color = green, linewidth = 10, title = "28", style = histogram)

使用bgcolor()color(),示例:

vline =  (n < 5710) ? na : trange("1", "0700-0701") ? vlinecol : na
bgcolor(vline ? color(black, 0) : color(white, 100))

它在每一列上画一条线,但请注意,在 false 的情况下,颜色的透明度值为 100。没有渲染,除了 true 案例的条形图。

如果有人有兴趣使用新的 v4 line.new() 函数:

注意:根据需要调整LineLengthMult

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © marketscripters

//@version=4
study("My Script", overlay=true)
LineLengthMult = 10
LineLength = atr(100) * LineLengthMult

drawVerticalLine(offset) =>
    line.new(bar_index[offset], low-LineLength, bar_index[offset], high+LineLength, color=color.new(color.yellow, 50), width=3)

if bar_index % 21 == 0
    drawVerticalLine(0)

编辑:使用自动缩放垂直线的代码更新了答案。