// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AugustoErni
//@version=5
indicator("Harami Bullish/Bearish Pattern (Candlestick Pattern)", overlay=true)
import AugustoErni/uptrend_downtrend_loopback_candle_identification_lib/3 as upAndDownTrendsCandleIdentification
showBullishHarami = input.bool(title="Show Bullish Harami", defval=true)
showBearishHarami = input.bool(title="Show Bearish Harami", defval=true)
lookbackPeriod = input.int(defval=5, title="Lookback Period", minval=1, step=1, tooltip="Loopback period to identify uptrend/downtrend analysis from candles.")
bodyThreshold = input.float(0.4, title="Body Threshold (percentage of candle range (0.4 = 40%))", step=0.01, tooltip="To identify long candle body.")
dojiThreshold = input.float(0.1, title="Doji Threshold (0.1 = 10%)", step=0.01, tooltip="To identify doji candle.")
weakCandleThreshold = input.float(0.2, title="Weak Candle Threshold (0.2 = 20%)", step=0.01, tooltip="To identify possible weak and/or small candle.")
isBullishCandle = close > open
isBearishCandle = open > close
isLongCandle = math.abs(open - close) >= (high - low) * bodyThreshold
isDoji = math.abs(close - open) <= (high - low) * dojiThreshold
isWeakCandle = math.abs(close - open) <= (high - low) * weakCandleThreshold
isLookbackUptrendCandle = upAndDownTrendsCandleIdentification.uptrendLoopbackCandleIdentification(lookbackPeriod)
isLookbackDowntrendCandle = upAndDownTrendsCandleIdentification.downtrendLoopbackCandleIdentification(lookbackPeriod)
isFirstCandleCondition = isLongCandle
isCurrentCandleCondition = isDoji or isWeakCandle
isBullishHarami = isFirstCandleCondition[1] and isCurrentCandleCondition and isLookbackDowntrendCandle
isBearishHarami = isFirstCandleCondition[1] and isCurrentCandleCondition and isLookbackUptrendCandle
barcolor(isBullishHarami and isBullishCandle ? color.white : isBullishHarami and isBearishCandle ? color.black : na)
barcolor(isBullishHarami and isBullishCandle[1] ? color.rgb(213, 213, 213) : isBullishHarami and isBearishCandle[1] ? color.rgb(90, 90, 90) : na, offset=-1)
barcolor(isBearishHarami and isBullishCandle ? color.white : isBearishHarami and isBearishCandle ? color.black : na)
barcolor(isBearishHarami and isBullishCandle[1] ? color.rgb(213, 213, 213) : isBearishHarami and isBearishCandle[1] ? color.rgb(90, 90, 90) : na, offset=-1)
plotshape(isBullishHarami and showBullishHarami, title="Bullish Harami", style=shape.arrowup, location=location.belowbar, color=color.rgb(255, 255, 255), text="Bullish Harami", textcolor=color.green)
plotshape(isBearishHarami and showBearishHarami, title="Bearish Harami", style=shape.arrowdown, location=location.abovebar, color=color.rgb(0, 0, 0), text="Bearish Harami", textcolor=color.red)