Indicator · Pine Script v5 · On the chart

Hammer and Hanging Man (Candlestick Pattern)

by augustoerni

This indicator marks Hammer and Hanging Man candlestick patterns on the price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code35
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago9 Aug 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

35 lines of Pine Script v5 from candlesticks/hammer_hanging_man.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

hammer_hanging_man.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AugustoErni

//@version=5
indicator("Hammer and Hanging Man (Candlestick Pattern)", overlay=true)

import AugustoErni/uptrend_downtrend_loopback_candle_identification_lib/3 as upAndDownTrendsCandleIdentification

hammerHangingManBodySizeThreshold = input.float(defval=0.50, title="Hammer/Hanging Man body size threshold in percentage (0.50 = 50%)", step=0.01)
hammerHangingManLowerShadowMultiplier = input.float(defval=1.8, title="Hammer/Hanging Man lower shadow multiplier", step=0.1)
hammerHangingManUpperShadowThreshold = input.float(defval=0.10, title="Hammer/Hanging Man upper shadow threshold in percentage (0.10 = 10 %)", step=0.01)
lookbackPeriod = input.int(defval=3, title="Lookback Period", minval=1, step=1, tooltip="Loopback period to identify uptrend/downtrend analysis from candles.")
showHammer = input.bool(defval=true, title="Show Hammer", tooltip="Show or hide the Hammer pattern.")
showHangingMan = input.bool(defval=true, title="Show Hanging Man", tooltip="Show or hide the Hanging Man pattern.")

isBullishCandle = close > open
isBearishCandle = open > close
realBody = math.abs(close - open)
upperShadow = high - math.max(close, open)
lowerShadow = math.min(close, open) - low

isSmallRealBody = realBody <= hammerHangingManBodySizeThreshold * (high - low)
isLongLowerShadow = lowerShadow >= hammerHangingManLowerShadowMultiplier * realBody
isShortUpperShadow = upperShadow <= hammerHangingManUpperShadowThreshold * (high - low)

isLookbackDowntrendCandle = upAndDownTrendsCandleIdentification.downtrendLoopbackCandleIdentification(lookbackPeriod)
isLookbackUptrendCandle = upAndDownTrendsCandleIdentification.uptrendLoopbackCandleIdentification(lookbackPeriod)

isHammer = isBullishCandle and isSmallRealBody and isLongLowerShadow and isShortUpperShadow and isLookbackDowntrendCandle
isHangingMan = isBearishCandle and isSmallRealBody and isLongLowerShadow and isShortUpperShadow and isLookbackUptrendCandle

barcolor(isHammer ? color.white : isHangingMan ? color.black : na)

plotshape(isHammer and showHammer, title="Hammer", style=shape.arrowup, location=location.belowbar, color=color.rgb(255, 255, 255), text="Hammer", textcolor=color.green)
plotshape(isHangingMan and showHangingMan, title="Hanging Man", style=shape.arrowdown, location=location.abovebar, color=color.rgb(0, 0, 0), text="Hanging Man", textcolor=color.red)

Overview

A Hammer requires a bullish candle with a small real body, a long lower shadow, and a short upper shadow after a lookback downtrend. A Hanging Man uses the same body and shadow rules on a bearish candle after a lookback uptrend. Inputs set body size, shadow ratios, lookback length, and visibility of each pattern. Matching bars are colored and labeled with arrows. It does not generate entries, exits, or risk management.

Details

Published by

augustoerni on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Candlestick patterns, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Hammer/Hanging Man body size threshold in percentage (0.50 = 50%)float0.50hammerHangingManBodySizeThreshold
Hammer/Hanging Man lower shadow multiplierfloat1.8hammerHangingManLowerShadowMultiplier
Hammer/Hanging Man upper shadow threshold in percentage (0.10 = 10 %)float0.10hammerHangingManUpperShadowThreshold
Lookback Periodint3lookbackPeriod
Show HammerbooltrueshowHammer
Show Hanging ManbooltrueshowHangingMan
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns
257 stars

This indicator detects a large set of classic candlestick patterns and plots colored labels on the price chart. Trend context uses a 50-period simple moving average, or the 50 and 200 averages stacked together, and can be switched off. Users filter bullish, be

Indicator v4 Candlestick patternsTrend
Updated 6 weeks ago · None stated GitHub repository of Snag - All Candlestick Patterns

Five-in-one Strategy

by capissimo
114 stars

Five-in-one Strategy combines five analytical tools into a single price chart overlay: the Dual Thrust Trading Algorithm (DTTA), Semafore, HalfTrend, Nick Rypock Trailing Reverse, and Tweezer or Kangaroo Tail candlestick patterns. It generates trade signals us

Indicator v4 TrendBreakout Repaints
Updated 1 year ago · None stated GitHub repository of Five-in-one Strategy

HA: Heikin-Ashi

by mihakralj
99 stars

This indicator calculates and plots Heikin-Ashi smoothed candlesticks directly on the price chart. It derives the smoothed close from standard price averages and recursively computes the open, high, and low values. The script does not issue trade signals or al

Indicator v6 Candlestick patternsTrend
Updated 4 weeks ago · Apache-2.0 GitHub repository of HA: Heikin-Ashi
55 stars

This indicator overlays on the price chart and provides trend and momentum analysis. It calculates and plots three exponential moving averages with fixed periods of 20, 50, and 200 using yellow, orange, and blue colors respectively. The script also computes th

Indicator v6 TrendOscillators & momentum
Updated 6 months ago · Apache-2.0 GitHub repository of Trend & Momentum Analysis

Confluence Suite

by TamaraKGreen
46 stars

The Confluence Suite indicator draws multiple trading signals and tools directly on the price chart. It generates entry and exit signals based on selected modes and filters, with an AI signal classifier rating quality from 1-4. Additional features include smar

Indicator v5 TrendSMC / ICT / price action
Updated 7 weeks ago · None stated GitHub repository of Confluence Suite

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers