Sometimes vertical lines can be a pretty good way to display signals or other information for your indicators. In this article, I will show you how you can plot a vertical line for a custom TradingView indicator in Pine Script.
You can do in just 1 line of code:
if (barstate.islast) line.new(bar_index, close, bar_index, close * 1.01, extend = extend.both)
You will see the following vertical line on your chart:
Here for simplicity, I’m using barstate.islast condition for simplicity. It will plot me only 1 line for the last bar. Of course, you can plot more lines depending on your needs.
In the function, you can see that I’m using bar_index (current bar) as both X coordinates. For Y-coordinate I’m using close and close * 1.01, but you basically use here any 2 values. When you add extend = extend.both Pine Script will plot the line for the entire height of your chart.
You can also easily style your vertical lines the same way you style other lines in Pine Script:
if (barstate.islast) line.new(bar_index, close, bar_index, close * 1.01, extend = extend.both, color = color.green, style = line.style_dotted, width = 3)
This will output you a dotted green line:
There is also a possibility to plot vertical lines into the future with the time xloc parameter. I will show an example in one of my future articles.