how to draw lines in Pine script (Tradingview)?

Ibrahim picture Ibrahim · Sep 22, 2017 · Viewed 20.9k times · Source

Pine editor still does not have built-in functions to plot lines (such as support lines, trend lines). I could not find any direct or indirect method to draw lines. I want to build function that look like below (for example only)

draw_line(price1, time1,price2, time2)

any Ideas or suggestions ?

Answer

not2qubit picture not2qubit · Dec 3, 2017

Unfortunately I don't think this is something they want to provide. Noticing several promising posts from 4 years ago that never came through. The only other way, seem to involve some calculations, by approximating your line with some line plots, where you hide the non-relevant parts.

For example:

...
c = close >= open ? lime : red
plot(close, color = c)

would produce something like this:

enter image description here

Then, you could try to replace red with na to get only the green parts.

Example 2

I've done some more experiments. Apparently Pine is so crippled you can't even put a plot in function, so the only way seem to be to use the point slope formula for a line, like this:

//@version=3
study(title="Simple Line", shorttitle='AB', overlay=true)

P1x = input(5744)
P1y = input(1.2727)
P2x = input(5774)
P2y = input(1.2628)
plot(n, color=na, style=line)   // hidden plot to show the bar number in indicator

// point slope
m = - (P2y - P1y) / (P2x - P1x)

// plot range
AB = n < P1x or n > P2x ? na : P1y - m*(n - P1x)
LA = (n == P1x) ? P1y : na
LB = (n == P2x) ? P2y : na

plot(AB, title="AB", color=#ff00ff, linewidth=1, style=line, transp=0)
plotshape(LA, title='A', location=location.absolute, color=silver, transp=0, text='A', textcolor=black, style=shape.labeldown)
plotshape(LB, title='B', location=location.absolute, color=silver, transp=0, text='B', textcolor=black, style=shape.labelup )

The result is quite nice, but too inconvenient to use. enter image description here


UPDATE: 2019-10-01

Apparently they have added some new line functionality to Pinescript 4.0+. Here is an example of using the new vline() function:

//@version=4
study("vline() Function for Pine Script v4.0+", overlay=true)

vline(BarIndex, Color, LineStyle, LineWidth) => // Verticle Line, 54 lines maximum allowable per indicator
    return = line.new(BarIndex, -1000, BarIndex, 1000, xloc.bar_index, extend.both, Color, LineStyle, LineWidth)

if(bar_index%10==0.0)
    vline(bar_index, #FF8000ff, line.style_solid, 1) // Variable assignment not required

As for the other "new" line function, I have not tested it yet.