# Define variables for candle body and shadows
def body = AbsValue(close - open);
def upperShadow = high - Max(open, close);
def lowerShadow = Min(open, close) - low;
#--------------------------------------------------
# Bullish Engulfing Pattern
#--------------------------------------------------
def bullishEngulfing =
open[1] > close[1] and # Previous candle is bearish
close > open and # Current candle is bullish
close > open[1] and # Current candle's close is higher than previous candle's open
open < close[1]; # Current candle's open is lower than previous candle's close
#--------------------------------------------------
# Bearish Engulfing Pattern
#--------------------------------------------------
def bearishEngulfing =
open[1] < close[1] and # Previous candle is bullish
close < open and # Current candle is bearish
close < open[1] and # Current candle's close is lower than previous candle's open
open > close[1]; # Current candle's open is higher than previous candle's close
#--------------------------------------------------
# Hammer Pattern
#--------------------------------------------------
def hammer =
lowerShadow > 2 * body and # Long lower shadow
upperShadow < body / 2 and # Short upper shadow
close > open; # Close is higher than open (for bullish hammer)
#--------------------------------------------------
# Inverted Hammer Pattern
#--------------------------------------------------
def invertedHammer =
upperShadow > 2 * body and # Long upper shadow
lowerShadow < body / 2 and # Short lower shadow
close > open; # Close is higher than open (for bullish inverted hammer)
#--------------------------------------------------
# Shooting Star Pattern (Bearish)
#--------------------------------------------------
def shootingStar =
upperShadow > 2 * body and # Long upper shadow
lowerShadow < body / 2 and # Short lower shadow
close < open; # Close is lower than open (for bearish shooting star)
#--------------------------------------------------
# Hanging Man Pattern (Bearish)
#--------------------------------------------------
def hangingMan =
lowerShadow > 2 * body and # Long lower shadow
upperShadow < body / 2 and # Short upper shadow
close < open; # Close is lower than open (for bearish hanging man)
#--------------------------------------------------
# Piercing Pattern (Bullish)
#--------------------------------------------------
def piercingPattern =
open[1] > close[1] and # Previous candle is bearish
close > open and # Current candle is bullish
open < close[1] and # Current candle's open is lower than previous candle's close
close > (open[1] + close[1]) / 2; # Current close is above midpoint of previous candle
#--------------------------------------------------
# Dark Cloud Cover Pattern (Bearish)
#--------------------------------------------------
def darkCloudCover =
open[1] < close[1] and # Previous candle is bullish
close < open and # Current candle is bearish
open > close[1] and # Current candle's open is higher than previous candle's close
close < (open[1] + close[1]) / 2; # Current close is below midpoint of previous candle
#--------------------------------------------------
# Morning Star Pattern (Bullish)
#--------------------------------------------------
def morningStar =
open[2] > close[2] and # First candle is bearish
close[1] < open[1] and # Second candle is bearish (often a small body)
close > open and # Third candle is bullish
close > (open[2] + close[2]) / 2; # Third candle's close is above midpoint of first candle
#--------------------------------------------------
# Evening Star Pattern (Bearish)
#--------------------------------------------------
def eveningStar =
open[2] < close[2] and # First candle is bullish
close[1] > open[1] and # Second candle is bullish (often a small body)
close < open and # Third candle is bearish
close < (open[2] + close[2]) / 2; # Third candle's close is below midpoint of first candle
#--------------------------------------------------
# Three White Soldiers Pattern (Bullish)
#--------------------------------------------------
def threeWhiteSoldiers =
close[2] > open[2] and close[1] > open[1] and close > open and # All three candles are bullish
close > close[1] and close[1] > close[2] and # Consecutive higher closes
open > close[1] and open[1] > close[2]; # Consecutive higher opens
#--------------------------------------------------
# Three Black Crows Pattern (Bearish)
#--------------------------------------------------
def threeBlackCrows =
close[2] < open[2] and close[1] < open[1] and close < open and # All three candles are bearish
close < close[1] and close[1] < close[2] and # Consecutive lower closes
open < close[1] and open[1] < close[2]; # Consecutive lower opens
#--------------------------------------------------
# Bullish Harami Pattern
#--------------------------------------------------
def bullishHarami =
open[1] > close[1] and # Previous candle is bearish
close > open and # Current candle is bullish
close < open[1] and # Current candle's close is lower than previous candle's open
open > close[1]; # Current candle's open is higher than previous candle's close
#--------------------------------------------------
# Bearish Harami Pattern
#--------------------------------------------------
def bearishHarami =
open[1] < close[1] and # Previous candle is bullish
close < open and # Current candle is bearish
close > open[1] and # Current candle's close is higher than previous candle's open
open < close[1]; # Current candle's open is lower than previous candle's close
# Plot the patterns on the chart using AddChartBubble
AddChartBubble(bullishEngulfing, high, "BE", color.GREEN, yes);
AddChartBubble(bearishEngulfing, low, "SE", color.RED, yes);
AddChartBubble(hammer, high, "H", color.GREEN, yes);
AddChartBubble(invertedHammer, high, "IH", color.GREEN, yes);
AddChartBubble(shootingStar, low, "SS", color.RED, yes);
AddChartBubble(hangingMan, low, "HM", color.RED, yes);
AddChartBubble(piercingPattern, high, "PP", color.GREEN, yes);
AddChartBubble(darkCloudCover, low, "DCC", color.RED, yes);
AddChartBubble(morningStar, high, "MS", color.GREEN, yes);
AddChartBubble(eveningStar, low, "ES", color.RED, yes);
AddChartBubble(threeWhiteSoldiers, high, "3WS", color.GREEN, yes);
AddChartBubble(threeBlackCrows, low, "3BC", color.RED, yes);
AddChartBubble(bullishHarami, high, "BH", color.GREEN, yes);
AddChartBubble(bearishHarami, low, "SH", color.RED, yes);