0
votes

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")
1

1 Answers

0
votes

We use var in here to save states across bars, and only plot buy/sell markers on transitions:

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

enter image description here

[EDIT: 2021.05.10 13:24 — LucF]

Debugging plots:

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