Tradingview 针稿。上次满足条件时返回

时间:2021-01-05 14:05:24

标签: pine-script

所以我在脚本中使用分形指标,但我需要在警报中返回最后一个分形值。为此,我需要每根蜡烛都返回最后一个分形信号的值(请注意,我现在没有放置最高()和最低()函数,但我会)

这就是我的代码的样子:

fTop = high[4] < high[2] and high[3] < high[2] and high[2] > high[1] and high[2] > high[0]
fBottom= low[4] > low[2] and low[3] > low[2] and low[2] < low[1] and low[2] < low[0]

plotshape(fTop ? close : na, title='Top Fractals', style=shape.triangleup, location=location.abovebar, color=color.red, offset=-2)
plotshape(fBottom ? close : na, title='Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=color.blue, offset=-2)

现在,如果我的条件不是满足蜡烛,脚本将返回“na”。我试图放置 fTop[1] / fBottom[1],但它返回最后一根蜡烛,而不是最后一个条件......有人有解决方案吗?

谢谢:)

1 个答案:

答案 0 :(得分:1)

我需要返回最后的分形值

下面的解决方案将绘制分形的收盘价。请注意,您的分形具有负偏移量 2,因此收盘系列具有 2 柱历史参考。

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

fTop = high[4] < high[2] and high[3] < high[2] and high[2] > high[1] and high[2] > high[0]
fBottom= low[4] > low[2] and low[3] > low[2] and low[2] < low[1] and low[2] < low[0]

plotshape(fTop ? close : na, title='Top Fractals', style=shape.triangleup, location=location.abovebar, color=color.red, offset=-2)
plotshape(fBottom ? close : na, title='Bottom Fractals', style=shape.triangledown, location=location.belowbar, color=color.blue, offset=-2)

var float fixTop = na
var float fixBot = na

if fTop
    fixTop := close[2]
else if fBottom
    fixBot := close[2]


plot(fixTop, change(fixTop) ? na : color.green, style = plot.style_linebr, offset = -2)
plot(fixBot, change(fixBot) ? na : color.red, style = plot.style_linebr, offset = -2)

enter image description here

要返回分形的高/低值,请更改这部分代码:

if fTop
    fixTop := high[2]
else if fBottom
    fixBot := low[2]

enter image description here