使用 pine 脚本绘制前一天的高/低/收盘

我有一个 pine 脚本来绘制前一天的高点/开盘价/低点,如下所示:

//@version=4
strategy("Plot Lines", overlay=true)

PDH = security(syminfo.tickerid,"D",high)
PDO = security(syminfo.tickerid,"D",open)
PDL = security(syminfo.tickerid,"D",low)

plot(PDH, title="High",color=color.red,linewidth=2,trackprice=true)
plot(PDO, title="Open",color=color.yellow,linewidth=2,trackprice=true)
plot(PDL, title="Low",color=color.green,linewidth=2,trackprice=true)

脚本运行良好,但我只希望显示前一天并忽略前一天的前一天,这样图表就不会那么混乱。

这是我从上面的脚本中得到的: enter image description here

如您所见,它为前一天绘制 PDH/PDO/PDL,但我只想要前一天(一天)。任何帮助或建议将不胜感激!

新编辑 enter image description here

应用脚本后的结果 enter image description here

stack overflow Plotting previous day high/low/close using pine script
原文答案
author avatar

接受的答案

@vitruvius 的回答很好,但我想补充一点。
无需画线并删除旧线。您可以只定义一次,然后将它们移动到最后一个栏。此外,可以在一次 security() 调用中请求这些值。

//@version=5
indicator("Plot Lines", overlay=true)

f_newLine(_color) => line.new(na, na, na, na, xloc.bar_time, extend.right, _color)

f_moveLine(_line, _x, _y) =>
    line.set_xy1(_line, _x,   _y)
    line.set_xy2(_line, _x+1, _y)

var line    line_open = f_newLine(color.yellow)
var line    line_high = f_newLine(color.red)
var line    line_low  = f_newLine(color.green)

[pdo,pdh,pdl] = request.security(syminfo.tickerid,"D", [open,high,low])

if barstate.islast
    f_moveLine(line_open, time, pdo)
    f_moveLine(line_high, time, pdh)
    f_moveLine(line_low , time, pdl)

编辑 1

//@version=5
indicator("Plot Lines", overlay=true)

f_newLine(_color) => line.new(na, na, na, na, xloc.bar_time, extend.right, _color)

f_moveLine(_line, _x, _y) =>
    line.set_xy1(_line, _x,   _y)
    line.set_xy2(_line, _x+1, _y)

var line    line_open = f_newLine(color.yellow)
var line    line_high = f_newLine(color.red)
var line    line_low  = f_newLine(color.green)

[pdo,pdh,pdl,pdt] = request.security(syminfo.tickerid,"D", [open[1],high[1],low[1],time[1]])

if barstate.islast
    f_moveLine(line_open, pdt, pdo)
    f_moveLine(line_high, pdt, pdh)
    f_moveLine(line_low , pdt, pdl)

答案:

作者头像

您可以使用 line() 函数代替 plot()

如果是最后一根,则画线,并沿途删除以前的线。

//@version=4
study("Plot Lines", overlay=true)

PDH = security(syminfo.tickerid,"D",high)
PDO = security(syminfo.tickerid,"D",open)
PDL = security(syminfo.tickerid,"D",low)

var line l_pdh = na, var line l_pdo = na, var line l_pdl = na

if barstate.islast
    l_pdh := line.new(bar_index-1, PDH, bar_index, PDH, extend=extend.both, color=color.green)
    l_pdo := line.new(bar_index-1, PDO, bar_index, PDO, extend=extend.both, color=color.blue)
    l_pdl := line.new(bar_index-1, PDL, bar_index, PDL, extend=extend.both, color=color.red)

line.delete(l_pdh[1])
line.delete(l_pdo[1])
line.delete(l_pdl[1])

enter image description here

相关问题