Identifying Bullish and Bearish Patterns with EMA and Hammer Candles in Pine Script

In the world of technical analysis, understanding candlestick patterns and moving averages can give traders insights into potential market direction. Today, we’ll go through a Pine Script code that identifies key candlestick patterns, specifically Bullish Engulfing, Bearish Engulfing, Hammer, and Inverted Hammer candles, along with Exponential Moving Averages (EMAs) for additional trend confirmation. By the end, you’ll have a clear understanding of how each pattern is detected and how EMAs can be used to support these signals.

Table of Contents

  1. Introduction to Candlestick Patterns and EMAs
  2. Setting Up Bullish and Bearish Engulfing Patterns
  3. Adding Exponential Moving Averages (EMAs)
  4. Identifying Hammer and Inverted Hammer Candles
  5. Plotting and Alert Conditions
  6. Complete Code for the Indicator
  7. Conclusion

1. Introduction to Candlestick Patterns and EMAs

Candlestick patterns and Exponential Moving Averages (EMAs) are essential components of technical analysis. Candlestick patterns help traders spot possible reversals or continuations, while EMAs highlight the trend direction. In this article, we’ll use Pine Script to combine these elements and identify:

  • Bullish and Bearish Engulfing Candles
  • Bullish and Bearish Hammer Candles (and their inverted forms)
  • EMAs (5, 9, 15, 50, 100, and 200-period)

This script will help traders visualize these patterns and moving averages on the chart, providing potential buy or sell signals.


2. Setting Up Bullish and Bearish Engulfing Patterns

An engulfing candle pattern occurs when a candle “engulfs” the previous one, indicating a potential reversal. Here’s how we define the logic for Bullish and Bearish Engulfing patterns in Pine Script.

Bullish Engulfing Condition

A Bullish Engulfing candle forms when:

  • The previous candle is bearish (close[1] < open[1]).
  • The current candle is bullish (close > open).
  • The current candle’s close is higher than the previous close.
  • The current candle’s high is higher than the previous high, and its low is lower than the previous low.
  • Volume is higher than the previous candle.
1
bullish_engulfing = (close[1] < open[1]) and (close > open) and (close > close[1]) and (close > high[1]) and (low < low[1]) and (open < open[1]) and (high > high[1]) and (volume > volume[1])

Bearish Engulfing Condition

A Bearish Engulfing candle forms when:

  • The previous candle is bullish (close[1] > open[1]).
  • The current candle is bearish (close < open).
  • The current candle’s close is lower than the previous close.
  • Volume is higher than the previous candle.
1
bearish_engulfing = (close[1] > open[1]) and (close < open) and (close < open[1]) and (close < close[1]) and (low < low[1]) and (open > open[1]) and (high > high[1]) and (volume > volume[1])

These conditions are then used to plot signals on the chart when a Bullish or Bearish Engulfing candle is detected.


3. Adding Exponential Moving Averages (EMAs)

EMAs are calculated by giving more weight to recent prices, making them more responsive to price changes than Simple Moving Averages. Here, we include 5, 9, 15, 50, 100, and 200-period EMAs to provide different levels of trend indication.

1
2
3
4
5
6
ema_5 = ema(close, 5)
ema_9 = ema(close, 9)
ema_15 = ema(close, 15)
ema_50 = ema(close, 50)
ema_100 = ema(close, 100)
ema_200 = ema(close, 200)

These EMAs are then plotted on the chart to help traders identify trends and potential support or resistance levels.


4. Identifying Hammer and Inverted Hammer Candles

Hammer and Inverted Hammer candles indicate potential reversals. A Hammer candle has a small body and a long lower shadow, signaling buying pressure after a downtrend. Conversely, an Inverted Hammer has a long upper shadow and small body, often indicating a potential reversal in an uptrend.

Hammer and Inverted Hammer Conditions

We define specific conditions to identify Green (Bullish) and Red (Bearish) Hammers, as well as Green and Red Inverted Hammers. These conditions consider:

  • The body-to-shadow ratio.
  • The size of the body and shadows.
  • Volume.

Here’s how we define the conditions:

1
2
pure_hammer_condition = (close >= open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (lower_shadow_size >= min_shadow_size * body_size) and (upper_shadow_size < open_high_error * body_size) and (lower_shadow_size > upper_shadow_size) and (volume > volume[1])
pure_inverted_hammer_condition = (close <= open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (upper_shadow_size >= min_shadow_size * body_size) and (lower_shadow_size < open_high_error * body_size) and (upper_shadow_size > lower_shadow_size) and (volume > volume[1])

Reversal Hammer Conditions

These conditions capture Red Hammers and Green Inverted Hammers, which can indicate reversal patterns:

1
2
reversal_hammer_condition = (close < open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (lower_shadow_size >= min_shadow_size * body_size) and (upper_shadow_size < open_high_error * body_size) and (lower_shadow_size > upper_shadow_size) and (volume > volume[1])
reversal_inverted_hammer_condition = (close > open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (upper_shadow_size >= min_shadow_size * body_size) and (lower_shadow_size < open_high_error * body_size) and (upper_shadow_size > lower_shadow_size) and (volume > volume[1])

5. Plotting and Alert Conditions

Now that we’ve set up the conditions for each pattern, we can plot them on the chart and add alerts. We use plotshape() to visualize the signals on the chart and alertcondition() to trigger alerts when specific patterns appear.

Plotting the Signals

Here’s how we plot each pattern with different colors and shapes:

1
2
3
4
5
6
7
plotshape(signal, style=shape.labelup, location=location.belowbar, color=color.green, text="HVB", size=size.small)
plotshape(signal2, style=shape.labeldown, location=location.abovebar, color=color.red, text="HVB", size=size.small)

plotshape(pure_hammer_condition, style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.small, title="Green Hammer")
plotshape(pure_inverted_hammer_condition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Red Hammer")
plotshape(reversal_hammer_condition, style=shape.triangleup, location=location.belowbar, color=color.yellow, size=size.small, title="Reversal Hammer")
plotshape(reversal_inverted_hammer_condition, style=shape.triangledown, location=location.abovebar, color=color.maroon, size=size.small, title="Reversal inverted Hammer")

Adding Alerts

Alerts notify you when the selected patterns are identified. In this example, we add an alert for high volume Bullish Engulfing patterns.

1
alertcondition(signal, title="High Volume Bullish Engulfing", message="A high volume bullish engulfing candle has been identified")

6. Complete Code for the Indicator

Here is the complete code that combines all the components discussed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//@version=5
indicator("EMA and Candlestick Patterns", overlay=true)

// Input parameters for engulfing patterns
length = input.int(10, title="Length")
mult = input.float(1.5, title="Multiplier")
min_vol = input.int(1000, title="Minimum Volume")

// Calculate the bullish engulfing condition
bullish_engulfing = (close[1] < open[1]) and (close > open) and (close > close[1]) and (close > high[1]) and (low < low[1]) and (open < open[1]) and (high > high[1]) and (volume > volume[1])

// Calculate the bearish engulfing condition
bearish_engulfing = (close[1] > open[1]) and (close < open) and (close < open[1]) and (close < close[1]) and (low < low[1]) and (open > open[1]) and (high > high[1]) and (volume > volume[1])

// Calculate the high volume condition
high_volume = volume > mult * ta.sma(volume, length)

// Calculate the 9 EMA, 50 EMA, and other EMAs
ema_5 = ta.ema(close, 5)
ema_9 = ta.ema(close, 9)
ema_15 = ta.ema(close, 15)
ema_50 = ta.ema(close, 50)
ema_100 = ta.ema(close, 100)
ema_200 = ta.ema(close, 200)

// Combine the conditions to create the signal for engulfing patterns
signal_bullish_engulfing = bullish_engulfing
signal_bearish_engulfing = bearish_engulfing

// Plot the engulfing signals
plotshape(signal_bullish_engulfing, style=shape.labelup, location=location.belowbar, color=color.green, text="BE", size=size.small)
plotshape(signal_bearish_engulfing, style=shape.labeldown, location=location.abovebar, color=color.red, text="BE", size=size.small)

// Input parameters for hammer and inverted hammer
hammer_body_to_total_ratio = input.float(0.55, title="Hammer Body to Total Candle Ratio")
min_body_size = input.float(0.001, title="Minimum Body Size")
min_shadow_size = input.float(0.5, title="Minimum Shadow Size")
open_high_error = input.float(0.15, title="Open-High Margin of Error")

// Calculate body size and shadow size for hammer conditions
body_size = math.abs(close - open)
upper_shadow_size = high - math.max(open, close)
lower_shadow_size = math.min(open, close) - low

// Define conditions for green hammer and red inverted hammer
green_hammer_condition = (close >= open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (lower_shadow_size >= min_shadow_size * body_size) and (upper_shadow_size < open_high_error * body_size) and (lower_shadow_size > upper_shadow_size) and (volume > volume[1])
red_inverted_hammer_condition = (close <= open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (upper_shadow_size >= min_shadow_size * body_size) and (lower_shadow_size < open_high_error * body_size) and (upper_shadow_size > lower_shadow_size) and (volume > volume[1])

// Define conditions for red hammer and green inverted hammer
red_hammer_condition = (close < open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (lower_shadow_size >= min_shadow_size * body_size) and (upper_shadow_size < open_high_error * body_size) and (lower_shadow_size > upper_shadow_size) and (volume > volume[1])
green_inverted_hammer_condition = (close > open and body_size <= hammer_body_to_total_ratio * (high - low)) and (body_size >= min_body_size) and (upper_shadow_size >= min_shadow_size * body_size) and (lower_shadow_size < open_high_error * body_size) and (upper_shadow_size > lower_shadow_size) and (volume > volume[1])

// Plot hammer and inverted hammer signals
plotshape(green_hammer_condition, style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.small, title="Green Hammer")
plotshape(red_inverted_hammer_condition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Red Inverted Hammer")
plotshape(red_hammer_condition, style=shape.triangleup, location=location.belowbar, color=color.yellow, size=size.small, title="Red Hammer")
plotshape(green_inverted_hammer_condition, style=shape.triangledown, location=location.abovebar, color=color.maroon, size=size.small, title="Green Inverted Hammer")

// Add alert conditions for bullish engulfing pattern
alertcondition(signal_bullish_engulfing, title="High Volume Bullish Engulfing", message="A high volume bullish engulfing candle has been identified")

// Plot the EMAs on the chart
plot(ema_5, color=color.orange, linewidth=1, title="EMA-5", display = display.none)
plot(ema_9, color=color.purple, linewidth=1, title="EMA-9", display = display.none)
plot(ema_15, color=color.orange, linewidth=2, title="EMA-15")
plot(ema_50, color=color.purple, linewidth=2, title="EMA-50")
plot(ema_100, color=color.teal, linewidth=2, title="EMA-100", display = display.none)
plot(ema_200, color=color.rgb(230, 241, 75), linewidth=2, title="EMA-200", display = display.none)

Conclusion

With this Pine Script code, you can now identify key candlestick patterns like Bullish Engulfing, Bearish Engulfing, Hammer, and Inverted Hammer candles on your TradingView charts. By adding EMAs, you can confirm trends and make more informed trading decisions. This script helps visualize potential reversal and continuation signals, making it a valuable tool in any trader’s toolkit.

Happy trading!

Author

Himanshu Upreti

Posted on

2024-11-23

Updated on

2024-11-24

Licensed under

Comments