需要确定最后一个信号是 toBey 还是 Sell

Need to establish if the last signal was toBuy or toSell

我正在尝试根据 slowMA 的斜率创建一个 toBuy 和 toSell 信号 indicator.the 我的代码的问题是我收到了卖出信号,即使之前没有买入信号。有没有办法在给出卖出信号之前检查先前的信号是否为买入信号,以便它们成对出现?我想过使用 valuewhen() 函数,但不知道如何应用它。 请帮助

//@version=4
study(title="Bridger MMI ", shorttitle="MMI", overlay=true)

r = rsi(close, 21)                                                 // RSI of Close
slowMA = sma(r, 2)                                          // Moving Average of RSI 7 bars back *** this is the green one I'm watching
                                                         

angleSlowMA= (atan(slowMA[0]-slowMA[1]))*180/3.14159   // atan gives angle in radians given opposite side/adjacent, adjacent =1, therefore atan(opposite) will give angle
                                                       // to convert radians to degrees, multiply by 180 and divide by pi

toBuy = angleSlowMA >= 15 and angleSlowMA <= 30
lastWasToBuy = valuewhen(toBuy, angleSlowMA, 1)      // this gives the value of the angle at the last toBuy

toSell = angleSlowMA <15 

plotshape(toBuy, title="Slope Positive", location=location.belowbar, color=color.lime, transp=0, style=shape.triangleup, text="BUY")            
plotshape(toSell, title="Slope Negative", location=location.abovebar, color=color.yellow, transp=0, style=shape.triangledown, text="SELL")

我们在这里使用 var 来保存跨栏的状态,并且只在转换上绘制 buy/sell 标记:

//@version=4
study(title="Bridger MMI ", shorttitle="MMI", overlay=true)

r = rsi(close, 21)                                      // RSI of Close
slowMA = sma(r, 2)                                      // Moving Average of RSI 7 bars back *** this is the green one I'm watching
                                                         
angleSlowMA= (atan(slowMA[0]-slowMA[1]))*180/3.14159    // atan gives angle in radians given opposite side/adjacent, adjacent =1, therefore atan(opposite) will give angle

// Use `var` to save state of vars across bars.
var bool toBuy = false
var bool toSell = false
if angleSlowMA >= 15 and angleSlowMA <= 30
    toBuy  := true
    toSell := false
else if toBuy and angleSlowMA <15
    toSell := true
    toBuy  := false

lastWasToBuy = valuewhen(toBuy, angleSlowMA, 1)      // this gives the value of the angle at the last toBuy

// Only show markers on transitions.
plotshape(toBuy and not toBuy[1], title="Slope Positive", location=location.belowbar, color=color.lime, transp=0, style=shape.triangleup, text="BUY")            
plotshape(toSell and not toSell[1], title="Slope Negative", location=location.abovebar, color=color.yellow, transp=0, style=shape.triangledown, text="SELL")

[编辑:2021.05.10 13:24 — LucF]

调试图:

plotchar(toBuy, "toBuy", "▲", location.top, size = size.tiny)
plotchar(toSell, "toSell", "▼", location.bottom, size = size.tiny)