MyCandleLibrary
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rezaraz

//@version=5

// @description TODO: Candle Pattern Library
library("MyCandleLibrary")

// @function TODO: Identify Bullish Engulfing Candle
// @param n TODO: Candle Number
// @returns TODO: If Identify Bullish Engulfing candle return True
export IsEngulfingCandle(int n) =>
	//TODO : add function body and return value here
	
	var trendRule1 = "SMA50"
	var trendRule2 = "SMA50,SMA200"
	C_Len = 14 // ta.ema depth for bodyAvg
	C_ShadowPercent = 5.0 // size of shadows
	C_ShadowEqualsPercent = 100.0
	C_DojiBodyPercent = 5.0
	C_Factor = 2.0 // shows the number of times the shadow dominates the candlestick body

	C_BodyHi = math.max(close[n], open[n])
	C_BodyLo = math.min(close[n], open[n])
	C_Body = C_BodyHi - C_BodyLo
	C_BodyAvg = ta.ema(C_Body, C_Len)
	C_SmallBody = C_Body < C_BodyAvg
	C_LongBody = C_Body > C_BodyAvg
	C_UpShadow = high[n] - C_BodyHi
	C_DnShadow = C_BodyLo - low[n]
	C_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_Body
	C_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_Body
	C_WhiteBody = open[n] < close[n]
	C_BlackBody = open[n] > close[n]
	C_Range = high[n]-low[n]
	C_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLo
	C_BodyMiddle = C_Body / 2 + C_BodyLo
	C_ShadowEquals = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercent
	C_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100
	C_Doji = C_IsDojiBody and C_ShadowEquals

	//patternLabelPosLow = low - (ta.atr(30) * 0.6)
	//patternLabelPosHigh = high + (ta.atr(30) * 0.6)

	//label_color_bullish = input(color.blue, "Label Color Bullish")
	C_EngulfingBullishNumberOfCandles = 2
	C_EngulfingBullish = C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close[n] >= open[n+1] and open[n] <= close[n+1] and ( close[n] > open[n+1] or open[n] < close[n+1] )
	//C_EngulfingBullish = C_UpTrend and C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close[n] >= (close[n+1]+open[n+1])/2
	//alertcondition(C_EngulfingBullish, title = "New pattern detected", message = "New Engulfing – Bullish pattern detected")
	//if C_EngulfingBullish
    	//var ttBullishEngulfing = "Engulfing\nAt the end of a given downward trend, there will most likely be a reversal pattern. To distinguish the first day, this candlestick pattern uses a small body, followed by a day where the candle body fully overtakes the body from the day before, and closes in the trend’s opposite direction. Although similar to the outside reversal chart pattern, it is not essential for this pattern to completely overtake the range (high to low), rather only the open and the close."
    	//label.new(bar_index, patternLabelPosLow, text="BE", style=label.style_label_up, color = label_color_bullish, textcolor=color.white, tooltip = ttBullishEngulfing)
	//bgcolor(ta.highest(C_EngulfingBullish?1:0, C_EngulfingBullishNumberOfCandles)!=0 ? color.new(color.blue, 90) : na, offset=-(C_EngulfingBullishNumberOfCandles-1))
	C_EngulfingBullish

/////////////////////////////////////////////////// White Cloade //////////////////////////////////////////////////////
// @function TODO: Identify White Cloade Candle
// @param n TODO: Candle Number
// @returns TODO: If Identify White Cloade candle return True otherwise False
export IsWhiteCloadeCandle(int n) =>
	//TODO : add function body and return value here
	
	C_Len = 14 // ta.ema depth for bodyAvg
	C_ShadowPercent = 5.0 // size of shadows
	C_ShadowEqualsPercent = 100.0
	C_DojiBodyPercent = 5.0
	C_Factor = 2.0 // shows the number of times the shadow dominates the candlestick body

	C_BodyHi = math.max(close[n], open[n])
	C_BodyLo = math.min(close[n], open[n])
	C_Body = C_BodyHi - C_BodyLo
	C_BodyAvg = ta.ema(C_Body, C_Len)
	C_SmallBody = C_Body < C_BodyAvg
	C_LongBody = C_Body > C_BodyAvg
	C_UpShadow = high[n] - C_BodyHi
	C_DnShadow = C_BodyLo - low[n]
	C_HasUpShadow = C_UpShadow > C_ShadowPercent / 100 * C_Body
	C_HasDnShadow = C_DnShadow > C_ShadowPercent / 100 * C_Body
	C_WhiteBody = open[n] < close[n]
	C_BlackBody = open[n] > close[n]
	C_Range = high[n]-low[n]
	C_IsInsideBar = C_BodyHi[1] > C_BodyHi and C_BodyLo[1] < C_BodyLo
	C_BodyMiddle = C_Body / 2 + C_BodyLo
	C_ShadowEquals = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < C_ShadowEqualsPercent and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < C_ShadowEqualsPercent
	C_IsDojiBody = C_Range > 0 and C_Body <= C_Range * C_DojiBodyPercent / 100
	C_Doji = C_IsDojiBody and C_ShadowEquals

	//patternLabelPosLow = low - (ta.atr(30) * 0.6)
	//patternLabelPosHigh = high + (ta.atr(30) * 0.6)

	//label_color_bullish = input(color.blue, "Label Color Bullish")
	C_WhiteCloadeNumberOfCandles = 2
	C_WhiteCloade = C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close[n] > ((close[n+1] + open[n+1]) / 2)
	//C_EngulfingBullish = C_UpTrend and C_WhiteBody and C_LongBody and C_BlackBody[1] and C_SmallBody[1] and close[n] >= (close[n+1]+open[n+1])/2
	//alertcondition(C_EngulfingBullish, title = "New pattern detected", message = "New Engulfing – Bullish pattern detected")
	//if C_EngulfingBullish
    	//var ttBullishEngulfing = "Engulfing\nAt the end of a given downward trend, there will most likely be a reversal pattern. To distinguish the first day, this candlestick pattern uses a small body, followed by a day where the candle body fully overtakes the body from the day before, and closes in the trend’s opposite direction. Although similar to the outside reversal chart pattern, it is not essential for this pattern to completely overtake the range (high to low), rather only the open and the close."
    	//label.new(bar_index, patternLabelPosLow, text="BE", style=label.style_label_up, color = label_color_bullish, textcolor=color.white, tooltip = ttBullishEngulfing)
	//bgcolor(ta.highest(C_EngulfingBullish?1:0, C_EngulfingBullishNumberOfCandles)!=0 ? color.new(color.blue, 90) : na, offset=-(C_EngulfingBullishNumberOfCandles-1))
	C_WhiteCloade

/////////////////////////////////////////////////// White Hammer //////////////////////////////////////////////////////
// @function TODO: Identify White Hammer Candle
// @param n TODO: Candle Number
// @returns TODO: If Identify White Hammer candle return True otherwise False
export IsWhiteHammerCandle(int n) =>
	//TODO : add function body and return value here
	
	C_Len = 14 // ta.ema depth for bodyAvg
	C_ShadowPercent = 5.0 // size of shadows
	C_BodyHi = math.max(close[n], open[n])
	C_BodyLo = math.min(close[n], open[n])
	C_Body = C_BodyHi - C_BodyLo
	C_BodyAvg = ta.ema(C_Body, C_Len)
	C_SmallBody = C_Body < C_BodyAvg
	C_LongBody = C_Body > C_BodyAvg
	C_UpShadow = high[n] - C_BodyHi
	C_DnShadow = C_BodyLo - low[n]
	C_hight = high[n]-low[n]
	Avg = ta.ema(hlc3[n],14)
	C_WhiteHammer =  C_Body < C_BodyAvg and C_Body < C_hight and  C_UpShadow<= C_Body and C_DnShadow > (2*C_Body) and (low[n+1] > low[n]) and hl2[n]<Avg and ((C_Body[1]>C_BodyAvg and close[n+1]<open[n+1]) or (C_Body[2]>C_BodyAvg and close[n+2]<open[n+2]) or (C_Body[3]>C_BodyAvg and close[n+3]<open[n+3])) ? true : false
	C_WhiteHammer