8

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:

enter image description here

(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:

enter image description here

2
  • 2
    Hey. Do You know how to draw vertical line on specific day? for ex. plot(timestamp(2018, 02, 13, 12, 00),linewidth=3,color=green, style = histogram ) --- But there is no line on chart :) Commented Feb 13, 2018 at 15:33
  • I'm sure you can, but it's not easy to do in pine-script. Look in the documentation and let us know! Commented Feb 14, 2018 at 11:39

7 Answers 7

11

If anyone is interested in using the new v4 line.new() function:

Note: Adjust LineLengthMult as desired.

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

tradingview chart showing vertical lines by marketscripters.com

EDIT: Answer updated with code that auto-scales the vertical lines.

Sign up to request clarification or add additional context in comments.

2 Comments

Finally some progress!
In case you are getting multiple lines, when needing just the first, add to your code: line.delete(myLine[1])
6

Just if it helps someone : Using André's answer above, static calling drawVerticalLine function multiple times using timestamp

( you can use Timestamp with GMT / UTC / "America/New_York" ( check documentation for each timezone implementation ) )

enter image description here

//@version=4 study("VerticalLine at specific time", overlay=true) // Function drawVerticalLine drawVerticalLine(targetTime) => line.new(x1=targetTime, y1=low, x2=targetTime, y2=high, xloc=xloc.bar_time, extend=extend.both, color=color.new(#ea5e50, 10), style=line.style_solid, width=2) // Call Function drawVerticalLine and passing TargetTime targetTime = timestamp("GMT+3", year, month, dayofmonth, 17, 00, 00) drawVerticalLine(targetTime) targetTime1 = timestamp("GMT+3", year, month, dayofmonth, 18, 00, 00) drawVerticalLine(targetTime1) targetTime2 = timestamp("GMT+3", year, month, dayofmonth, 20, 00, 00) drawVerticalLine(targetTime2) 

Comments

5

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

Comments

2

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.

Comments

1

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) 

Comments

1

This is a simple indicator do draw a vertical on the market open at 01:00.

//@version=5 indicator("Vertical_Line_New_Day" , "#VLND", overlay = true) isTargetTime = hour(time) == 1 and minute(time) == 0 if isTargetTime line.new(bar_index[1], low, bar_index[1], high, color=color.new(color.orange, 1), width=1, style=line.style_dashed, extend=extend.both) 

Comments

0

Valid in pinescript @version=6

To display a vertical line, define a vertical segment (with low and high) and use extend=extend.both :

line.new(Signal_xloc, low, Signal_xloc, high, color=color.red, width=1, extend=extend.both) 

Here Signal_xloc is the xloc location of your vertical line (bar_index)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.