如何在Pine脚本(Tradingview)中画线?

问题描述 投票:3回答:2

Pine编辑器仍然没有内置函数来绘制线条(例如支撑线,趋势线)。我找不到任何直接或间接的方法来绘制线条。我想构建如下所示的函数(仅举例)

draw_line(price1, time1,price2, time2)

任何想法或建议?

line finance tradingview-api pine-script
2个回答
8
投票

不幸的是,我不认为这是他们想要提供的东西。注意到4年前从未发过的几个有希望的帖子。唯一的另一种方式,似乎涉及一些计算,通过用一些线图近似你的线,你隐藏不相关的部分。

对于example

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

会产生这样的东西:

enter image description here

然后,您可以尝试用red替换na以仅获取绿色部分。

例2

我做了一些实验。显然Pine是如此残缺,你甚至无法将一个情节放在功能中,所以唯一的方法似乎是使用一条线的斜率公式,如下所示:

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

结果非常好,但使用起来太不方便了。 enter image description here


1
投票

现在可以在Pine Script v4中使用:

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

0
投票

更简洁的绘制线代码:

//@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)
© www.soinside.com 2019 - 2024. All rights reserved.