I am trying to create a toBuy and toSell signal based on the slope of a slowMA indicator.the problem with my code is that I get Sell signals, even without prior Buy signals. Is there a way to check if the previous signal was a buy, before the Sell signal is given, so they come in matching pairs? I thought of using valuewhen() function, but can't figure out how to apply it. Please help ????
//@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")