Home » Candlestick Patterns » Detecting Hammer Candlestick Pattern in Pine Script

Detecting Hammer Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Hammer Candlestick Pattern

The Hammer candlestick pattern is a bullish reversal indicator typically seen at the end of a downtrend. It features a short body with a long lower shadow, at least twice the length of the body, and little to no upper shadow. This pattern suggests that although selling pressure initially drove prices down, buyers regained control, pushing the price back up near the opening level. The Hammer is considered a sign of a potential bullish reversal, but traders usually look for confirmation from subsequent price action or other technical indicators.

Hammer Candlestick Pattern

Importance in Technical Analysis

The Hammer candlestick pattern holds significant importance in technical analysis for several reasons:

  1. Reversal Signal: The Hammer is a key indicator of a potential reversal in trend, particularly at the end of a downtrend. Its unique formation signals that selling pressure was overcome by buying pressure, suggesting a shift in market sentiment.
  2. Market Psychology Insight: This pattern provides insight into market psychology, showing that despite strong selling pressure, buyers were able to push the price back up, indicating a change in momentum.
  3. Decision-Making Tool: For traders and analysts, the Hammer serves as a crucial decision-making tool. It can signal a good entry point for a long position, assuming the pattern is confirmed by subsequent bullish price action.
  4. Risk Management: The distinct structure of the Hammer helps in setting clear stop-loss levels. The low of the Hammer often acts as a level of support, below which traders might consider cutting losses.
  5. Volume Analysis: Observing volume during the formation of a Hammer can add to its reliability. Higher volume during the pattern suggests stronger buying interest and can validate the reversal signal

Example From Trading View Chart

Example From Trading View Chart

Defining the Hammer Candlestick Pattern Criteria 

Here’s how you can define these criteria:

  • Real Body Size: The real body of the candle (the difference between the opening and closing prices) should be small.
  • Long Lower Wick: The lower wick (the difference between the low and the lower end of the real body) should be significantly long, typically at least twice the length of the real body.
  • Upper Wick: The upper wick (the difference between the high and the higher end of the real body) should be small or nonexistent.

Code For Detecting Hammer

//@version=5
indicator("Hammer Candlestick Pattern", shorttitle="Hammer", overlay=true)

// Function to check for a downtrend
isDowntrend() =>
    close[1] < close[2] and close[2] < close[3]

// Define the hammer criteria
bodySize = math.abs(close - open)
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low

// Conditions for a valid hammer
isSmallBody = bodySize < (high - low) * 0.2
isLongLowerWick = lowerWick > bodySize * 2
isSmallUpperWick = upperWick < bodySize

// Check if current candle is a hammer
isHammer = isSmallBody and isLongLowerWick and isSmallUpperWick and isDowntrend()

// Plotting
if isHammer
    label.new(bar_index, high, "HAMMER",style = label.style_label_down, color=color.green, textcolor=color.white, yloc=yloc.abovebar)

Output

Output

Overview of the script 

1. Initialization:

//@version=5

indicator("Hammer Candlestick Pattern", shorttitle="Hammer", overlay=true)

This sets up the script with Pine Script version 5, gives it a name (“Hammer Candlestick Pattern”), a short title (“Hammer”), and specifies that it should be overlaid on the price chart.

2. Defining a Downtrend Function:

isDowntrend() =>

    close[1] < close[2] and close[2] < close[3]

This function isDowntrend checks if the asset is in a downtrend. It returns true if the close price of the previous candle (close[1]) is less than the close two candles back (close[2]), and that in turn is less than the close three candles back (close[3]).

3. Calculating Candlestick Components:

bodySize = math.abs(close - open)

upperWick = high - math.max(open, close)

lowerWick = math.min(open, close) - low

bodySize calculates the absolute size of the candle’s body.

upperWick calculates the size of the upper wick.

lowerWick calculates the size of the lower wick.

4. Setting Hammer Criteria:

isSmallBody = bodySize < (high - low) * 0.2

isLongLowerWick = lowerWick > bodySize * 2

isSmallUpperWick = upperWick < bodySize

These lines define the criteria for a candlestick to be considered a Hammer:

The body should be small (isSmallBody).

The lower wick should be long (isLongLowerWick).

The upper wick should be small or non-existent (isSmallUpperWick).

5. Identifying the Hammer Pattern:

isHammer = isSmallBody and isLongLowerWick and isSmallUpperWick and isDowntrend()

This line combines the criteria: a small body, a long lower wick, a small upper wick, and the presence of a downtrend. If all these conditions are met, isHammer is set to true.

Plotting the Label:

if isHammer

    label.new(bar_index, high, "HAMMER",style = label.style_label_down, color=color.green, textcolor=color.white, yloc=yloc.abovebar)

This block of code plots a label on the chart whenever a Hammer pattern is detected. The label is placed above the bar (yloc. abovebar) with the text “HAMMER”. The style and colors are set for visibility.

Frequently Asked Questions

How do I identify a Hammer pattern in a chart?

Look for a candle with a small real body at the upper end of the trading range, a long lower shadow (at least twice the length of the real body), and minimal or no upper shadow. This should occur during a downtrend.

Does the color of the Hammer candlestick matter?

The Hammer can be either red (bearish) or green (bullish), but its effectiveness as a reversal signal does not significantly depend on its color.

What is the difference between a Hammer and an Inverted Hammer pattern?

The Hammer pattern appears at the end of a downtrend and has a long lower shadow. The Inverted Hammer also appears at the end of a downtrend but has a long upper shadow instead.

Can I modify the Pine Script code for different variations of the Hammer pattern?

Yes, you can modify the code to suit different variations or sensitivity of the Hammer pattern. For instance, you might adjust the ratio that defines a ‘long’ lower shadow, or change the criteria for a downtrend. The script is customizable to fit different trading styles and analysis needs.

Is volume important when considering a Hammer pattern?

Yes, higher trading volume during the formation of the Hammer can strengthen the reversal signal.

Conclusion

In conclusion, detecting the Hammer candlestick pattern using Pine Script is a powerful method for traders looking to capitalize on potential bullish reversals in a downtrend. The script we’ve discussed provides a straightforward yet effective approach to identifying this pattern in trading charts. By setting specific criteria for the candle’s body size, lower wick, and position within a downtrend, the script can accurately highlight Hammer formations.

Leave a Comment