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

Detecting Inverted Hammer Candlestick Pattern in Pine Script

Photo of author
Published on

Overview of Inverted Hammer Candlestick Pattern

The Inverted Hammer candlestick pattern is a notable formation in technical analysis, often interpreted as a potential signal for a bullish reversal, particularly in a downtrend context. Here is an overview of its key aspects:

Inverted Hammer
  • Formation and Appearance: The Inverted Hammer appears as a single candlestick at the end of a downtrend. Its distinct features include a small body located at the lower end of the range, a long upper shadow that is typically at least twice the length of the body, and a minimal or absent lower shadow.
  • Market Implications: This pattern is indicative of a trading period where bullish traders start to gain some ground. The long upper shadow signifies that buyers drove the prices up during the session, but couldn’t sustain this momentum, leading to a close near the opening price. Despite this, the pattern suggests a diminishing bearish strength.
  • Bullish Reversal Signal: While the Inverted Hammer itself doesn’t confirm a bullish reversal, it signals the possibility of one. It is particularly compelling when it follows a strong downtrend, indicating that the market could be testing for a bottom.

Importance in Technical Analysis

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

  1. Early Indication of Trend Reversal: The Inverted Hammer is primarily valued for its potential to indicate an early reversal of a bearish trend. Its appearance can signal to traders and analysts that the downtrend may be losing momentum, and a bullish turnaround could be imminent.
  2. Sentiment Insight: This pattern provides insight into market sentiment. The long upper shadow indicates that buyers attempted to push the market higher, revealing underlying bullish sentiment even in a prevailing downtrend.
  3. Enhancing Trading Strategy: Incorporating the Inverted Hammer into a trading strategy allows traders to make more informed decisions. It can be a trigger for entry into a long position or an exit signal for those holding short positions.
  4. Risk Management Tool: Recognizing an Inverted Hammer helps in managing risk, particularly in short selling or when looking to enter a market at the bottom of a trend. It alerts traders to a potential change in market direction, enabling them to adjust their strategies accordingly.
  5. Complement to Other Analytical Tools: When used in conjunction with other forms of technical analysis, like trend lines, oscillators, and volume indicators, the Inverted Hammer can provide a more robust and comprehensive market analysis.

Example From Trading View

Example From Trading View

Defining the Inverted Hammer Candlestick Pattern Criteria

The criteria for identifying an Inverted Hammer candlestick pattern in technical analysis are as follows:

  • Small Body: The body of the candle, which is the difference between the opening and closing prices, should be small. This indicates a relatively tight range of trading activity within the timeframe of the candle.
  • Long Upper Shadow: The upper shadow, represented by the line above the body, should be significantly long, typically at least twice the length of the body. This long upper shadow signifies that the prices were driven up during the trading period but fell back near the opening price.
  • Minimal or No Lower Shadow: The Inverted Hammer should have little to no lower shadow, which is the line below the body. This suggests that the lows of the trading period were very close to the opening price.
  • Color of Body: The body of the Inverted Hammer can be either red or green (or black or white in traditional candlestick charts). A green body might indicate a stronger bullish potential than a red body, but both colors are considered as part of the Inverted Hammer pattern.

Code For Detecting Inverted Hammer

//@version=5
indicator("Inverted Hammer Detector", overlay=true)

// Define the characteristics of an Inverted Hammer
bodySize = math.abs(close - open)
upperShadow = high - math.max(open, close)
lowerShadow = math.min(open, close) - low
bodyThreshold = ta.atr(14) * 0.1 // Define how small the body should be; here it's 10% of the ATR
upperShadowThreshold = bodySize * 2 // Upper shadow should be at least twice the body size

// Check if the current candle matches the Inverted Hammer characteristics
isInvertedHammer = bodySize > 0 and 
                   upperShadow >= upperShadowThreshold and 
                   lowerShadow <= bodyThreshold and 
                   bodySize <= bodyThreshold

// Plotting
if isInvertedHammer
    label.new(bar_index, high, "Inverted Hammer", style=label.style_label_down, color=color.rgb(255, 238, 0), textcolor=color.rgb(0, 0, 0))

Output

Output

Overview of the script 

Step 1: Define the Indicator

//@version=5

indicator("Inverted Hammer Detector", overlay=true)

@version=5: Specifies that the script uses version 5 of Pine Script.

indicator(…): Declares a new indicator named “Inverted Hammer Detector”.

overlay=true: Indicates that the indicator will be plotted directly on the price chart.

Step 2: Calculate Candlestick Components

bodySize = math.abs(close - open)
upperShadow = high - math.max(open, close)
lowerShadow = math.min(open, close) - low

bodySize: Calculates the absolute size of the candle’s body (difference between closing and opening prices).

upperShadow: Determines the length of the upper shadow (distance between the high and the higher of the open or close).

lowerShadow: Measures the length of the lower shadow (distance between the lower of the open or close and the low).

Step 3: Define Pattern Criteria

bodyThreshold = ta.atr(14) * 0.1

upperShadowThreshold = bodySize * 2

bodyThreshold: Sets a threshold for the body size, here defined as 10% of the 14-period Average True Range (ATR).

upperShadowThreshold: Establishes that the upper shadow should be at least twice the size of the body.

Step 4: Identify the Inverted Hammer Pattern

isInvertedHammer = bodySize > 0 and 
                   upperShadow >= upperShadowThreshold and 
                   lowerShadow <= bodyThreshold and 
                   bodySize <= bodyThreshold

isInvertedHammer: A boolean condition that checks if the candle meets the Inverted Hammer criteria: a small body, a long upper shadow, and a minimal lower shadow.

Step 5: Plot the Indicator

if isInvertedHammer

    label.new(bar_index, high, "Inverted Hammer", style=label.style_label_down, color=color.rgb(255, 238, 0), textcolor=color.rgb(0, 0, 0))

if isInvertedHammer: Executes the following block if the current candle is an Inverted Hammer.

  • label.new(…): Creates a new label on the chart.
  • bar_index, high: Positions the label at the high of the current bar.
  • “Inverted Hammer”: The text displayed in the label.
  • style=label.style_label_down: Sets the label style to point downwards.
  • color and textcolor: Sets the background color of the label to yellow (color.rgb(255, 238, 0)) and the text color to black.

Frequently Asked Questions

What does the bodySize variable represent in the Pine Script for the Inverted Hammer pattern?

The bodySize variable calculates the absolute size of the candle’s body, which is the difference between the opening and closing prices. It’s used to determine whether the candle’s body is small enough to fit the criteria for an Inverted Hammer.

How is the upperShadow in the Inverted Hammer pattern calculated in the script?

The upperShadow is calculated by subtracting the higher of the open or close price from the high of the candle. This measures the length of the upper shadow, which is a crucial element of the Inverted Hammer pattern.

Why do we use the Average True Range (ATR) in defining the bodyThreshold?

The Average True Range (ATR) is used in defining the bodyThreshold to dynamically adjust the size criteria of the candle’s body relative to the market’s volatility. This makes the detection of the Inverted Hammer pattern more adaptable to different market conditions.

Can the criteria for detecting an Inverted Hammer in the script be adjusted?

Yes, the criteria such as the bodyThreshold and upperShadowThreshold can be adjusted to suit different trading strategies or to better fit the characteristics of the specific market being analyzed.

How does the script differentiate an Inverted Hammer from other candlestick patterns?

The script differentiates an Inverted Hammer from other patterns by specifically looking for a candle with a small body (defined by bodyThreshold), a long upper shadow (at least twice the body size, as per upperShadowThreshold), and a minimal lower shadow. These combined criteria help isolate the Inverted Hammer pattern from other similar patterns.

Conclusion

In conclusion, the Inverted Hammer candlestick pattern is a valuable tool in technical analysis, offering potential signals for bullish reversals, especially at the bottom of downtrends. While it is not a definitive indicator of a market turnaround, its presence warrants attention as it suggests a weakening bearish trend and an emerging bullish sentiment.

Leave a Comment