Japanese Candle Patterns Detector in Potential Price Zone
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MoriFX

//@version=5
indicator('Japanese Candle Patterns Detector in Potential Price Zone', shorttitle='Japanese Patterns', overlay=true)

// User inputs
HamStar = input(title='Hammer and ShootingStar', defval=true)
DojiInput = input(title='Doji', defval=true)
EngulfInput = input(title='Engulfing Candle', defval=true)
MarubozuInput = input(title='Marubozu', defval=true)
TweezersInput = input(title='Tweezers Top / Buttom', defval=true)
ThreeWhiteSoldiersInput = input(title='Three White Soldiers / Black Crows', defval=true)
HaramiInput = input(title='Harami', defval=true)


// Candle calculator
bodySize = math.abs(open - close)
HlowerShadow = math.abs(low - open)
HupperShadow = math.abs(high - close)
SlowerShadow = math.abs(low - close)
SupperShadow = math.abs(high - open)

rsiValue = ta.rsi(close, 14)
atrValue = ta.atr(12)
dojiBodySize = atrValue * 5 / 100
EngulfBodySize = atrValue * 20 / 100
HamStarBodySize = atrValue * 15 / 100

//  Patterns detector
Hammer = HlowerShadow > bodySize * 2.5 and low <= ta.lowest(low, 12) and HlowerShadow > HupperShadow * 2.5 and close <= open[1] and bodySize > HamStarBodySize
Star = SupperShadow > bodySize * 2.5 and high >= ta.highest(high, 12) and SupperShadow > SlowerShadow * 2.5 and open <= close[1] and bodySize > HamStarBodySize
Doji = bodySize <= dojiBodySize and (low <= ta.lowest(low, 12) or high >= ta.highest(high, 12))
BullEng = close > high[1] and close[1] < open[1] and bodySize[1] > EngulfBodySize and rsiValue < 50
BearEng = close < open[1] and close[1] > open[1] and bodySize[1] > EngulfBodySize and rsiValue > 50
Marubozu = open == high and close == low or open == low and close == high
TweezerTop = high == high[1] and rsiValue > 50
TweezerBut = low == low[1] and rsiValue < 50
ThreeWhiteSoldiers = close[3] < open[3] and close[2] > open[2] and close[1] > open[1] and close > open and rsiValue[2] < 50 and bodySize[2] > dojiBodySize * 2 and bodySize[1] > dojiBodySize * 3 and bodySize > dojiBodySize * 4
ThreeBlackCrows = close[3] > open[3] and close[2] < open[2] and close[1] < open[1] and close < open and rsiValue[2] > 50 and bodySize[2] > dojiBodySize * 2 and bodySize[1] > dojiBodySize * 3 and bodySize > dojiBodySize * 4
BullHarami = close[2] < open[2] and close[1] < open[1] and bodySize[1] >= atrValue[1] and open > close[1] and close < open[1]
BearHarami = close[2] > open[2] and close[1] > open[1] and bodySize[1] <= atrValue[1] and open < close[1] and close > open[1]


// Plot signal on chart
plotshape(HamStar ? Hammer : na, style=shape.triangleup, location=location.belowbar, textcolor=color.new(color.green, 0), text='Ham')
plotshape(HamStar ? Star : na, style=shape.triangledown, location=location.abovebar, textcolor=color.new(color.red, 0), text='Star')
plotshape(DojiInput ? Doji : na, style=shape.cross, location=location.abovebar, textcolor=color.new(color.gray, 0), text='Doji')
plotshape(EngulfInput ? BullEng : na, style=shape.triangleup, location=location.belowbar, textcolor=color.new(color.green, 0), text='BulEng')
plotshape(EngulfInput ? BearEng : na, style=shape.triangledown, location=location.abovebar, textcolor=color.new(color.red, 0), text='BerEng')
plotshape(MarubozuInput ? Marubozu : na, style=shape.diamond, location=location.abovebar, textcolor=color.new(color.fuchsia, 0), text='Marubozu')
plotshape(TweezersInput ? TweezerTop : na, style=shape.triangledown, location=location.abovebar, textcolor=color.new(color.red, 0), text='TwzrTop')
plotshape(TweezersInput ? TweezerBut : na, style=shape.triangleup, location=location.belowbar, textcolor=color.new(color.green, 0), text='TwzrBut')
plotshape(ThreeWhiteSoldiersInput ? ThreeWhiteSoldiers : na, style=shape.triangleup, location=location.belowbar, textcolor=color.new(color.green, 0), offset=-2, text='3Sol')
plotshape(ThreeWhiteSoldiersInput ? ThreeBlackCrows : na, style=shape.triangledown, location=location.abovebar, textcolor=color.new(color.red, 0), offset=-2, text='3Crow')
plotshape(HaramiInput ? BullHarami : na, style=shape.triangleup, location=location.belowbar, textcolor=color.new(color.green, 0), text='BullHarami')
plotshape(HaramiInput ? BearHarami : na, style=shape.triangledown, location=location.abovebar, textcolor=color.new(color.red, 0), text='BearHarami')

// Alert
alertcondition(Doji or Hammer or Star or BullEng or BearEng or Marubozu or TweezerTop or TweezerBut or ThreeWhiteSoldiers or ThreeBlackCrows or BullHarami or BearHarami, title='All type of Japanese candles', message='Japanese Pattern found for {{ticker}}')
alertcondition(DojiInput ? Doji : na, title='Doji candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(HamStar ? Hammer or Star : na, title='Hammer/Star candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(EngulfInput ? BullEng or BearEng : na, title='Engulfing candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(MarubozuInput ? Marubozu : na, title='Marubozu candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(TweezersInput ? TweezerTop or TweezerBut : na, title='TweezerTop/But candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(ThreeWhiteSoldiersInput ? ThreeWhiteSoldiers or ThreeBlackCrows : na, title='ThreeWhiteSoldiers/BlackCrows candle ', message='Japanese Pattern found for {{ticker}}')
alertcondition(HaramiInput ? BullHarami or BearHarami : na, title='Harami candle ', message='Japanese Pattern found for {{ticker}}')




