Pine Script: Hanging Man Candlestick Pattern

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

Posted: Expertise: Intermediate/Advanced

Why the Hanging Man Pattern Matters in Pine Script

The Hanging Man candlestick pattern is a single-candle bearish reversal pattern that typically forms after an uptrend. It signals a potential top and a shift in momentum from buyers to sellers. The Hanging Man's unique shape indicates that despite initial buying pressure during the trading period, sellers stepped in strongly to push prices back down, often near the opening price. This shows that the market struggles to maintain higher prices, making Hanging Man patterns invaluable for:

Understanding the Hanging Man Candlestick Pattern

A Hanging Man 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 an uptrend. It looks identical to a Hammer, but its significance is entirely dependent on its position within an uptrend.

Hanging Man (Bullish Body)

Open below close. Even with a green body, the long lower wick shows selling pressure.

Hanging Man (Bearish Body)

Open above close. A red body reinforces the bearish sentiment.

Key Characteristics:

Basic Hanging Man Pattern Detection in Pine Script

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

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

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

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

// 2. Long lower shadow (at least 2x the body size)
isLongLowerShadow = lowerShadow >= realBody * 2

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

// Combine criteria for Hanging Man
isHangingMan = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Plot signal on the chart (above the bar for bearish reversal)
plotshape(isHangingMan, title="Hanging Man", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small)

// Alert condition (optional)
alertcondition(isHangingMan, title="Hanging Man Detected", message="Hanging Man Candlestick Pattern Detected!")

Advanced Hanging Man Strategies

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

1. Hanging Man with Uptrend Confirmation (Using Moving Average)

//@version=5
strategy("Hanging Man 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 uptrend detection

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

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

isHangingMan = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Trend Confirmation: Price should be above the MA (uptrend)
// AND the MA itself should be sloping upwards (optional, but stronger)
isUptrend = close > ma and ma > ma[1]

// Strategy Logic with Trend Confirmation
shortSignal = isHangingMan and isUptrend

if (shortSignal)
    strategy.entry("Short", strategy.short)
    
// Plot MA
plot(ma, "Trend MA", color.blue)

// Plot confirmed signals
plotshape(shortSignal, title="Confirmed Hanging Man", location=location.abovebar, color=color.new(color.maroon, 0), style=shape.arrowdown, size=size.normal)

2. Hanging Man with Volume Confirmation

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

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

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

isHangingMan = isSmallBody and isLongLowerShadow and isSmallUpperShadow

// Volume Confirmation:
// Hanging Man 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
confirmedHangingMan = isHangingMan and isVolumeConfirmed

// Plot confirmed signals
barcolor(confirmedHangingMan ? color.new(color.purple, 60) : na)

plotshape(confirmedHangingMan, title="Hanging Man (Vol Confirmed)", location=location.abovebar, color=color.new(color.red, 0), style=shape.labeldown, size=size.small, text="Bear 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. Hanging Man with RSI Confirmation

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

// RSI Inputs
rsiLength = input(14, "RSI Length")
overbought = input(70, "RSI Overbought Level")

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

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

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

isHangingMan = isSmallBody and isLongLowerShadow and isSmallUpperShadow

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

// Combined signal
signalHangingMan = isHangingMan and rsiConfirmation

// Plot signals
plotshape(signalHangingMan, title="Hanging Man (RSI Confirmed)", location=location.abovebar, color=color.new(color.teal, 0), style=shape.arrowdown, size=size.normal)

// Plot RSI in a separate pane
plot(rsiValue, "RSI", color.blue, linewidth=2, display=display.pane)
hline(overbought, "Overbought", color.red)

Optimizing Hanging Man Pattern Performance

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

Common Hanging Man Pattern Pitfalls to Avoid

Warning: Not all Hanging Man-shaped candles are valid Hanging Man patterns. Context and confirmation are crucial. A Hanging Man in a downtrend is known as a "Hammer" and indicates a bullish reversal. Always understand the context.

Conclusion

The Hanging Man candlestick pattern is a strong and visually intuitive signal for bearish reversals. By understanding its precise characteristics and, more importantly, its contextual significance within an uptrend, you can leverage it effectively in your trading. Implementing Hanging Man 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.