Exhaustion Signal
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator(title='Exhaustion Signal', shorttitle='Exhaustion', overlay=true, timeframe='')

Candles = input.int(9,"Candle Count")
Candles2 = input.int(12,"Candle Count")

// //  CALCS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

buySignals = 0
buySignals := close < close[4] ? buySignals[1] == Candles ? 1 : buySignals[1] + 1 : 0

sellSignals = 0
sellSignals := close > close[4] ? sellSignals[1] == Candles ? 1 : sellSignals[1] + 1 : 0

BuyOrSell = math.max(buySignals, sellSignals)


buy =  buySignals and BuyOrSell == Candles
sell =  sellSignals and BuyOrSell == Candles

// //  CALCS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

buySignals2 = 0
buySignals2 := close < close[4] ? buySignals2[1] == Candles2 ? 1 : buySignals2[1] + 1 : 0

sellSignals2 = 0
sellSignals2 := close > close[4] ? sellSignals2[1] == Candles2 ? 1 : sellSignals2[1] + 1 : 0

BuyOrSell2 = math.max(buySignals2, sellSignals2)


buy2 =  buySignals2 and BuyOrSell2 == Candles2
sell2 =  sellSignals2 and BuyOrSell2 == Candles2

//  LABELS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

plotshape(buy, style=shape.diamond, color=color.new(#30ff85, 0), textcolor=color.new(color.white, 0), size=size.tiny, location=location.belowbar)
plotshape(sell, style=shape.diamond, color=color.new(#ff1200, 0), textcolor=color.new(color.white, 0), size=size.tiny, location=location.abovebar)
plotshape(buy2, style=shape.xcross, color=color.new(#30ff85, 0), textcolor=color.new(color.white, 0), size=size.tiny, location=location.belowbar)
plotshape(sell2, style=shape.xcross, color=color.new(#ff1200, 0), textcolor=color.new(color.white, 0), size=size.tiny, location=location.abovebar)


// ALERTS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

alertcondition(buy, 'Buy', 'Buy')  // Once per bar close
alertcondition(sell, 'Sell', 'Sell')  // Once per bar close

alertcondition(buy2, 'Buy', 'Buy')  // Once per bar close
alertcondition(sell2, 'Sell', 'Sell')  // Once per bar close


