Pine Script: Hammer Candlestick Pattern

Unlock powerful bullish reversal signals with this comprehensive guide to detecting and strategizing with Hammer patterns in TradingView's Pine Script.

Subscribe now @ $99/month

Why the Hammer Pattern Matters in Pine Script

The Hammer candlestick pattern is a single-candle bullish reversal pattern that typically forms after a downtrend. It signals a potential bottom and a shift in momentum from sellers to buyers. The Hammer's unique shape indicates that despite selling pressure during the trading period, buyers stepped in strongly to push prices back up, often near the opening price. This makes Hammer patterns invaluable for:

Understanding the Hammer Candlestick Pattern

A Hammer candle has a small real body (the difference between open and close) and a long lower shadow (wick) that is typically at least twice the length of the real body. It has little or no upper shadow. The color of the body (bullish or bearish) is less important than its shape and location after a downtrend.

Hammer (Bullish Body)

Open below close, indicating buying strength from the low.

Hammer (Bearish Body)

Open above close, but still shows strong rejection of lower prices.

Key Characteristics:

Basic Hammer Pattern Detection in Pine Script

Here's how to create a basic indicator to detect Hammer patterns in Pine Script v5:

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

// Calculate candle body and wick lengths
realBody = math.abs(close - open)
upperShadow = math.max(open, close) - high
lowerShadow = low - math.min(open, close) // Corrected calculation for lower shadow length

// Define Hammer criteria
// 1. Small real body
isSmallBody = realBody <= (high - low) * 0.3 // Body is at most 30% of total candle range

// 2. Long lower shadow (at least 2x or 3x the body size, or total range)
// Using 2x body size as a common heuristic
isLongLowerShadow = lowerShadow >= realBody * 2

// 3. Little to no upper shadow (upper shadow is less than 10-20% of real body or total range)
isSmallUpperShadow = upperShadow <= realBody * 0.5 // Upper shadow is at most 50% of body, or less than 10% of total range
// Alternative for upper shadow: upperShadow < (high - low) * 0.1

// Combine criteria for Hammer
isHammer = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Plot signal on the chart
plotshape(isHammer, title="Hammer", location=location.belowbar, color=color.new(color.blue, 0), style=shape.triangleup, size=size.small)

// Alert condition (optional)
alertcondition(isHammer, title="Hammer Detected", message="Hammer Candlestick Pattern Detected!")

Advanced Hammer Strategies

While basic detection is useful, combining Hammer patterns with other indicators and market context provides stronger, more reliable signals for potential reversals.

1. Hammer with Downtrend Confirmation (Using Moving Average)

//@version=5
strategy("Hammer with Trend Confirmation", overlay=true)

// Input for Moving Average length
maLength = input(50, "MA Length (for trend)")
ma = ta.ema(close, maLength) // Using EMA for downtrend detection

// Hammer Pattern Detection (from basic example)
realBody = math.abs(close - open)
upperShadow = math.max(open, close) - high
lowerShadow = low - math.min(open, close)

isSmallBody = realBody <= (high - low) * 0.3
isLongLowerShadow = lowerShadow >= realBody * 2
isSmallUpperShadow = upperShadow <= realBody * 0.5

isHammer = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Trend Confirmation: Price should be below the MA (downtrend)
// AND the MA itself should be sloping downwards (optional, but stronger)
isDowntrend = close < ma and ma < ma[1]

// Strategy Logic with Trend Confirmation
longSignal = isHammer and isDowntrend

if (longSignal)
    strategy.entry("Long", strategy.long)
    
// Plot MA
plot(ma, "Trend MA", color.blue)

// Plot confirmed signals
plotshape(longSignal, title="Confirmed Hammer", location=location.belowbar, color=color.new(color.navy, 0), style=shape.arrowup, size=size.normal)

2. Hammer with Volume Confirmation

//@version=5
indicator("Hammer with Volume Confirmation", overlay=true)

// Hammer Pattern Detection (from basic example)
realBody = math.abs(close - open)
upperShadow = math.max(open, close) - high
lowerShadow = low - math.min(open, close)

isSmallBody = realBody <= (high - low) * 0.3
isLongLowerShadow = lowerShadow >= realBody * 2
isSmallUpperShadow = upperShadow <= realBody * 0.5

isHammer = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Volume Confirmation:
// Hammer candle's volume should be significantly higher than average volume
avgVolume = ta.sma(volume, 20) // 20-period Simple Moving Average of Volume
isVolumeConfirmed = volume > avgVolume * 1.5 // Volume is 1.5x average

// Combined signal
confirmedHammer = isHammer and isVolumeConfirmed

// Plot confirmed signals
barcolor(confirmedHammer ? color.new(color.aqua, 60) : na)

plotshape(confirmedHammer, title="Hammer (Vol Confirmed)", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, size=size.small, text="Hammer Vol")

// Optional: Plot volume for visual check
plot(volume, title="Volume", color=color.gray, style=plot.style_columns)
plot(avgVolume, title="Avg Volume", color=color.orange, style=plot.style_line)

3. Hammer with RSI Confirmation

//@version=5
indicator("Hammer with RSI Confirmation", overlay=true)

// RSI Inputs
rsiLength = input(14, "RSI Length")
oversold = input(30, "RSI Oversold Level")

// Calculate RSI
rsiValue = ta.rsi(close, rsiLength)

// Hammer Pattern Detection
realBody = math.abs(close - open)
upperShadow = math.max(open, close) - high
lowerShadow = low - math.min(open, close)

isSmallBody = realBody <= (high - low) * 0.3
isLongLowerShadow = lowerShadow >= realBody * 2
isSmallUpperShadow = upperShadow <= realBody * 0.5

isHammer = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// RSI Confirmation: RSI should be in oversold territory or rising from it
rsiConfirmation = rsiValue <= oversold or (rsiValue[1] <= oversold and rsiValue > rsiValue[1])

// Combined signal
signalHammer = isHammer and rsiConfirmation

// Plot signals
plotshape(signalHammer, title="Hammer (RSI Confirmed)", location=location.belowbar, color=color.new(color.fuchsia, 0), style=shape.arrowup, size=size.normal)

// Plot RSI in a separate pane
plot(rsiValue, "RSI", color.blue, linewidth=2, display=display.pane)
hline(oversold, "Oversold", color.green)

Optimizing Hammer Pattern Performance

To maximize the effectiveness of Hammer patterns in your Pine Script strategies:

Common Hammer Pattern Pitfalls to Avoid

Warning: Not all Hammer-shaped candles are valid Hammer patterns. Context and confirmation are crucial. A Hammer in an uptrend is known as a "Hanging Man" and indicates a bearish reversal.

Conclusion

The Hammer candlestick pattern is a strong and visually intuitive signal for bullish reversals. By understanding its precise characteristics and, more importantly, its contextual significance within a downtrend, you can leverage it effectively in your trading. Implementing Hammer detection in Pine Script allows you to automate signal generation, combine it with other technical indicators for higher probability trades, and refine your price action analysis to make more informed trading decisions on TradingView.

Enhance Your Trading

Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Subscribe now @ $99/month