how to draw lines in Pine script (Tradingview)?
Asked Answered
A

4

9

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 ?

Aegir answered 22/9, 2017 at 5:34 Comment(0)
D
13

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.

Dissonant answered 3/12, 2017 at 10:55 Comment(0)
F
6

This is now possible in Pine Script v4:

Preview

//@version=4
study("Line", overlay=true)
l = line.new(bar_index, high, bar_index[10], low[10], width = 4)
line.delete(l[1])

Here is a vertical line function by midtownsk8rguy on TradingView:

vline(BarIndex, Color, LineStyle, LineWidth) => // Verticle Line Function, ≈50-54 lines maximum allowable per indicator
    // return = line.new(BarIndex,   0.0, BarIndex,     100.0, xloc.bar_index, extend.both, Color, LineStyle, LineWidth) // Suitable for study(overlay=false) and RSI, Stochastic, etc...
    // return = line.new(BarIndex,  -1.0, BarIndex,       1.0, xloc.bar_index, extend.both, Color, LineStyle, LineWidth) // Suitable for study(overlay=false) and +/-1.0 oscillators
    return = line.new(BarIndex, low - tr, BarIndex, high + tr, xloc.bar_index, extend.both, Color, LineStyle, LineWidth) // Suitable for study(overlay=true)

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

You can also use if barstate.islast if you only draw your lines once instead of on each candle, this way you don't need to delete the previous lines.

Fortuitous answered 6/7, 2019 at 13:54 Comment(2)
Small thing: the x axis (time) only accepts integers. (first and third argument) (bar_index is an integer), in some way it makes sense but I wasn't expecting it when trying to use a moving average (this one returns floats for x)Conveyor
You can also use if barstate.islast if you only draw your lines once instead of on each candle, this way you don't need to delete the previous lines.Fortuitous
E
1

More compact code for draw lines:

//@version=3
study("Draw line", overlay=true)

plot(n, color=na, style=line)
AB(x1,x2,y1,y2) => n < x1 or n > x2 ? na : y1 + (y2 - y1) / (x2 - x1) * (n - x1)

plot(AB(10065,10136,3819,3893), color=#ff00ff, linewidth=1, style=line, 
transp=0)
plot(AB(10091,10136,3966.5,3931), color=#ff00ff, linewidth=1, style=line, 
transp=0)
Edomite answered 27/2, 2019 at 16:33 Comment(0)
L
0

Here is an example that might answer the original question:

//@version=4
study(title="trendline example aapl", overlay=true)

//@AAPL
line12= line.new(x1=int(1656322200000), 
y1=float(143.49), 
x2=int(1659519000000), 
y2=float(166.59), 
extend=extend.right, 
xloc=xloc.bar_time)

(to calculate the time it needs to be calculated as the *bar open time in unix milliseconds see: https://currentmillis.com/ ; can be calculated in excel with this formula = = (([date eg mm/dd/yyyy]+[bar open time eg 9.30am])- 0/24 - DATE(1970,1,1)) * 86400000 = ((6/27/2022+9:30:00 AM)- 0/24 - DATE(1970,1,1)) * 86400000 = ((44739+0.395833333333333)- 0/24 - DATE(1970,1,1)) * 86400000 = 1656322200000 ) adjust the zero/24 to offset the time zone if needed eg 1/24

Lindsylindy answered 12/8, 2022 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.