# https://www.tradingview.com/v/VFjyDUJx/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://moz
# @ChartingCycles
#indicator(title='Exhaustion Signal', shorttitle='Exhaustion', overlay=true, timeframe='')
# Converted by Sam4Cok@Samer800 - 12/2023
input chartTimeframe = yes;
input HigherTimeframe = AggregationPeriod.FIFTEEN_MIN;
input source = FundamentalType.CLOSE;
input lookback = 4;
input signalDisplay = {Default "Fast & Slow Signals", "Fast Signals", "Slow Signals"};
input fastCandleCount = 9;#,"Candle Count")
input slowCandleCount = 12;#,"Candle Count")
#// // CALCS
def na = Double.NaN;
def fast = signalDisplay==signalDisplay."Fast Signals";
def slow = signalDisplay==signalDisplay."Slow Signals";
def CTF = Fundamental(FundamentalType = source);
def HTF = Fundamental(FundamentalType = source, Period = HigherTimeframe);
def src = if chartTimeframe then CTF else HTF;
def buySignals = if !slow and src < src[lookback] then
if buySignals[1] == fastCandleCount then 1 else buySignals[1] + 1 else 0;
def sellSignals = if !slow and src > src[lookback] then
if sellSignals[1] == fastCandleCount then 1 else sellSignals[1] + 1 else 0;
def BuyOrSell = Max(buySignals, sellSignals);
def buy = buySignals and BuyOrSell == fastCandleCount;
def sell = sellSignals and BuyOrSell == fastCandleCount;
#// // CALCS
def buySignals2 = if !fast and src < src[lookback] then
if buySignals2[1] == slowCandleCount then 1 else buySignals2[1] + 1 else 0;
def sellSignals2 = if !fast and src > src[lookback] then
if sellSignals2[1] == slowCandleCount then 1 else sellSignals2[1] + 1 else 0;
def BuyOrSell2 = Max(buySignals2, sellSignals2);
def buy2 = buySignals2 and BuyOrSell2 == slowCandleCount;
def sell2 = sellSignals2 and BuyOrSell2 == slowCandleCount;
#// LABELS
plot buySig = if buy then low else na;
plot sellSig = if sell then high else na;
buySig.SetPaintingStrategy(PaintingStrategy.SQUARES);
sellSig.SetPaintingStrategy(PaintingStrategy.SQUARES);
buySig.SetLineWeight(2);
sellSig.SetLineWeight(2);
buySig.SetDefaultColor(Color.CYAN);
sellSig.SetDefaultColor(Color.MAGENTA);
plot buySig2 = if buy2 then low else na;
plot sellSig2 = if sell2 then high else na;
buySig2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sellSig2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
buySig2.SetLineWeight(2);
sellSig2.SetLineWeight(2);
buySig2.SetDefaultColor(Color.CYAN);
sellSig2.SetDefaultColor(Color.MAGENTA);
#-- End OF Code