How to draw a vertical line in TradingView pine script?

旧巷老猫 提交于 2019-12-23 19:19:02

问题


I'm trying to use the web based TradingView platform to make my own custom scripts to display various financial market properties. This is possible through its pine scripting engine/interpreter.

At the moment I'm trying to simply display a vertical line on either the main chart or on an indicator chart. However, it doesn't seem that their scripting engine is supporting vertical lines, except by using the plot's histogram or column types. Either way, I am not able to get any satisfactory lines.


SOME TESTS

(1) I've had some minor success with using bgcolor() like this:

//@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)

This results in:

(2) A much better result when using plot() with the style=histogram argument:

//@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)

with the following result:


回答1:


It is an old post, but this could help others. You can use this to draw a line:

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

I was not able to plot a dashed line though




回答2:


Dany's answer did not display anything on the chart for me, however setting the style to a histogram did the trick.

//@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)



回答3:


Use bgcolor() and color(), example:

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

It is painting a line on every column, but notice that on the false case the color has a transparency value of 100. Nothing renders, except the bars for the true case.



来源:https://stackoverflow.com/questions/47610638/how-to-draw-a-vertical-line-in-tradingview-pine-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!