Advanced Candlestick Detection
Coding doji candles, hammers and shooting stars, calculating gaps, piercing line, dark cloud cover, and multi-bar reversal patterns.
Advanced Candlestick Patterns
Beyond basic engulfing patterns, professional traders use a repertoire of nuanced candle formations that encode specific buyer/seller dynamics. Doji candles reveal indecision. Hammers and shooting stars show rejection at key levels. Gaps expose overnight sentiment shifts. Piercing lines and dark cloud covers are two-bar reversal patterns with strict rules. In crypto's volatile markets, these patterns fire frequently: the skill is knowing which parameters to tune and which context filters to apply.
Doji Candle Detection
A doji occurs when the open and close are nearly equal, producing a tiny body relative to the total range. The key parameter is the body-to-range ratio threshold. A ratio below 10% means the body is less than 10% of the high-low range. Doji candles on BTC at round-number levels ($60,000, $70,000) often precede significant directional moves because they represent a moment of equilibrium before one side wins.
1//@version=52indicator("Doji Detection", overlay=true)34dojiThresh = input.float(10.0, "Doji Body/Range %", minval=1, maxval=30, step=1)56body = math.abs(close - open)7range_ = high - low8ratio = range_ > 0 ? body / range_ * 100 : 100910isDoji = ratio <= dojiThresh and range_ > 01112// Doji subtypes based on wick proportions13upperWick = high - math.max(close, open)14lowerWick = math.min(close, open) - low1516// Gravestone doji: long upper wick, tiny lower wick (bearish)17gravestone = isDoji and upperWick > range_ * 0.6 and lowerWick < range_ * 0.151819// Dragonfly doji: long lower wick, tiny upper wick (bullish)20dragonfly = isDoji and lowerWick > range_ * 0.6 and upperWick < range_ * 0.152122// Standard doji: balanced wicks23standard = isDoji and not gravestone and not dragonfly2425plotshape(standard, "Doji", shape.cross,26 location.abovebar, color.yellow, size=size.tiny)27plotshape(gravestone, "Gravestone", shape.triangledown,28 location.abovebar, color.red, size=size.tiny, text="GS")29plotshape(dragonfly, "Dragonfly", shape.triangleup,30 location.belowbar, color.green, size=size.tiny, text="DF")| Standard Doji | Equal-length upper and lower wicks. Signals pure indecision. Context determines direction. |
| Gravestone Doji | Long upper wick, almost no lower wick. Buyers pushed up but were rejected. Bearish at tops. |
| Dragonfly Doji | Long lower wick, almost no upper wick. Sellers pushed down but were rejected. Bullish at bottoms. |
| Body/Range Ratio | The threshold that defines a doji. 10% is standard. Lower values are stricter and produce fewer signals. |
Hammer and Shooting Star Detection
A hammer is a single-bar reversal pattern at the bottom of a downtrend: a small body at the top of the range with a long lower wick (at least 2x the body length) and little to no upper wick. It shows that sellers pushed price down but buyers absorbed the selling and drove price back up. A shooting star is the mirror: a small body at the bottom of the range with a long upper wick, signaling rejection at higher prices.
1//@version=52indicator("Hammer / Shooting Star", overlay=true)34wickRatio = input.float(2.0, "Min Wick/Body Ratio", step=0.5)5trendLen = input.int(10, "Trend Lookback", minval=5)6maxBodyPct = input.float(33.0, "Max Body % of Range", step=5)78body = math.abs(close - open)9range_ = high - low10upperWick = high - math.max(close, open)11lowerWick = math.min(close, open) - low12bodyPct = range_ > 0 ? body / range_ * 100 : 1001314// Trend context using simple slope15inDowntrend = close < ta.sma(close, trendLen) and16 ta.sma(close, trendLen) < ta.sma(close, trendLen)[trendLen / 2]17inUptrend = close > ta.sma(close, trendLen) and18 ta.sma(close, trendLen) > ta.sma(close, trendLen)[trendLen / 2]1920// Hammer: small body at top, long lower wick, in a downtrend21isHammer = bodyPct <= maxBodyPct and22 body > 0 and23 lowerWick >= body * wickRatio and24 upperWick <= body * 0.5 and25 inDowntrend2627// Shooting star: small body at bottom, long upper wick, in an uptrend28isShootingStar = bodyPct <= maxBodyPct and29 body > 0 and30 upperWick >= body * wickRatio and31 lowerWick <= body * 0.5 and32 inUptrend3334plotshape(isHammer, "Hammer", shape.labelup,35 location.belowbar, color.new(color.green, 10),36 text="H", textcolor=color.white, size=size.small)37plotshape(isShootingStar, "Shooting Star", shape.labeldown,38 location.abovebar, color.new(color.red, 10),39 text="SS", textcolor=color.white, size=size.small)4041alertcondition(isHammer, "Hammer Detected",42 "Hammer candle in downtrend context")43alertcondition(isShootingStar, "Shooting Star Detected",44 "Shooting star candle in uptrend context")Trend context is mandatory
inDowntrend / inUptrend variables above use SMA slope as a simple trend proxy, but you can replace this with any trend definition.Gap Detection: Gap Up, Gap Down, Gap Fill
True gaps are rare in 24/7 crypto markets, but they do appear on CME Bitcoin futures (which close on weekends) and on lower timeframes during extreme volatility. A gap up occurs when the current bar's low is above the previous bar's high. A gap fill occurs when price later trades back into the gap zone. Unfilled gaps act as magnets: the market tends to revisit them, making gap fill detection a high-probability trading setup on BTC futures.
1//@version=52indicator("Gap Detector", overlay=true, max_boxes_count=50)34minGapPct = input.float(0.5, "Min Gap Size (%)", step=0.1)5showFills = input.bool(true, "Highlight Gap Fills")67// Gap up: current low > previous high8gapUpSize = low - high[1]9gapUpPct = gapUpSize / high[1] * 10010isGapUp = gapUpPct >= minGapPct1112// Gap down: current high < previous low13gapDownSize = low[1] - high14gapDownPct = gapDownSize / low[1] * 10015isGapDown = gapDownPct >= minGapPct1617// Track unfilled gaps18var float gapUpTop = na19var float gapUpBottom = na20var bool gapUpActive = false21var float gapDnTop = na22var float gapDnBottom = na23var bool gapDnActive = false2425if isGapUp26 gapUpTop := low27 gapUpBottom := high[1]28 gapUpActive := true2930if isGapDown31 gapDnTop := low[1]32 gapDnBottom := high33 gapDnActive := true3435// Gap fill detection36gapUpFilled = gapUpActive and low <= gapUpBottom37gapDnFilled = gapDnActive and high >= gapDnTop3839if gapUpFilled40 gapUpActive := false41if gapDnFilled42 gapDnActive := false4344// Visual markers45plotshape(isGapUp, "Gap Up", shape.arrowup,46 location.belowbar, color.green, size=size.tiny)47plotshape(isGapDown, "Gap Down", shape.arrowdown,48 location.abovebar, color.red, size=size.tiny)4950bgcolor(showFills and gapUpFilled ? color.new(color.green, 88) :51 showFills and gapDnFilled ? color.new(color.red, 88) : na)Piercing Line and Dark Cloud Cover
The piercing line is a bullish two-bar reversal: a bearish bar followed by a bullish bar that opens below the prior low and closes above the midpoint of the prior bar's body. The dark cloud cover is the bearish mirror: a bullish bar followed by a bearish bar that opens above the prior high and closes below the midpoint of the prior body. The penetration threshold (how far into the prior body the close must reach) is a configurable parameter that controls signal quality.
1//@version=52indicator("Piercing / Dark Cloud", overlay=true)34penetration = input.float(50.0, "Min Penetration %", minval=30, maxval=90, step=5)56prevBody = math.abs(close[1] - open[1])7prevMid = math.min(close[1], open[1]) + prevBody * (penetration / 100)8prevMidBear = math.max(close[1], open[1]) - prevBody * (penetration / 100)910// Piercing line: bearish prev bar, bullish current bar11// Opens below prev low, closes above penetration threshold into prev body12piercingLine = close[1] < open[1] and13 close > open and14 open <= low[1] and15 close >= prevMid and16 close < open[1]1718// Dark cloud cover: bullish prev bar, bearish current bar19// Opens above prev high, closes below penetration threshold into prev body20darkCloud = close[1] > open[1] and21 close < open and22 open >= high[1] and23 close <= prevMidBear and24 close > open[1]2526plotshape(piercingLine, "Piercing Line", shape.labelup,27 location.belowbar, color.new(color.teal, 10),28 text="PL", textcolor=color.white, size=size.small)29plotshape(darkCloud, "Dark Cloud", shape.labeldown,30 location.abovebar, color.new(color.purple, 10),31 text="DC", textcolor=color.white, size=size.small)Higher % = stricter filter (fewer but higher-quality signals). 50% is the classic definition.
Piercing line must open below the previous low; dark cloud must open above the previous high.
The close must not exceed the previous open, otherwise it becomes an engulfing pattern instead.
These patterns work best on 4H+ timeframes on BTC and ETH. Lower timeframes produce too much noise.
Complete Advanced Pattern Detector
This indicator combines all advanced patterns (doji subtypes, hammer/shooting star, gaps, piercing line, and dark cloud cover) into a single production-ready script with per-pattern toggles.
1//@version=52indicator("Advanced Candle Patterns", overlay=true, max_labels_count=200)34// Inputs per pattern group5grpDoji = "Doji Settings"6dojiOn = input.bool(true, "Show Doji", group=grpDoji)7dojiThr = input.float(10, "Body/Range %", group=grpDoji, step=1)89grpHam = "Hammer / Shooting Star"10hamOn = input.bool(true, "Show Hammer/SS", group=grpHam)11wkRatio = input.float(2.0, "Wick/Body Ratio", group=grpHam, step=0.5)12trdLen = input.int(10, "Trend Lookback", group=grpHam)1314grpGap = "Gap Detection"15gapOn = input.bool(true, "Show Gaps", group=grpGap)16minGap = input.float(0.5, "Min Gap %", group=grpGap, step=0.1)1718grpPC = "Piercing / Dark Cloud"19pcOn = input.bool(true, "Show Piercing/DC", group=grpPC)20penThr = input.float(50, "Penetration %", group=grpPC, step=5)2122// Shared calculations23body = math.abs(close - open)24range_ = high - low25bodyHigh = math.max(close, open)26bodyLow = math.min(close, open)27upperWick = high - bodyHigh28lowerWick = bodyLow - low29bodyPct = range_ > 0 ? body / range_ * 100 : 1003031// Trend context32smaVal = ta.sma(close, trdLen)33upTrend = close > smaVal and smaVal > smaVal[trdLen / 2]34dnTrend = close < smaVal and smaVal < smaVal[trdLen / 2]3536// Doji37isDoji = dojiOn and bodyPct <= dojiThr and range_ > 038gravestone = isDoji and upperWick > range_ * 0.6 and lowerWick < range_ * 0.1539dragonfly = isDoji and lowerWick > range_ * 0.6 and upperWick < range_ * 0.1540stdDoji = isDoji and not gravestone and not dragonfly4142// Hammer / Shooting Star43ham = hamOn and bodyPct <= 33 and body > 0 and44 lowerWick >= body * wkRatio and upperWick <= body * 0.5 and dnTrend45ss = hamOn and bodyPct <= 33 and body > 0 and46 upperWick >= body * wkRatio and lowerWick <= body * 0.5 and upTrend4748// Gaps49gapUp = gapOn and (low - high[1]) / high[1] * 100 >= minGap50gapDown = gapOn and (low[1] - high) / low[1] * 100 >= minGap5152// Piercing / Dark Cloud53prevBody_ = math.abs(close[1] - open[1])54pMid = math.min(close[1], open[1]) + prevBody_ * (penThr / 100)55dMid = math.max(close[1], open[1]) - prevBody_ * (penThr / 100)56pier = pcOn and close[1] < open[1] and close > open and57 open <= low[1] and close >= pMid and close < open[1]58dark = pcOn and close[1] > open[1] and close < open and59 open >= high[1] and close <= dMid and close > open[1]6061// Plot all patterns62plotshape(stdDoji, "Doji", shape.cross, location.abovebar,63 color.yellow, size=size.tiny)64plotshape(gravestone, "Gravestone", shape.xcross, location.abovebar,65 color.red, size=size.tiny)66plotshape(dragonfly, "Dragonfly", shape.xcross, location.belowbar,67 color.green, size=size.tiny)68plotshape(ham, "Hammer", shape.labelup, location.belowbar,69 color.new(color.green, 10), text="H", textcolor=color.white, size=size.small)70plotshape(ss, "Shooting Star", shape.labeldown, location.abovebar,71 color.new(color.red, 10), text="SS", textcolor=color.white, size=size.small)72plotshape(gapUp, "Gap Up", shape.arrowup, location.belowbar,73 color.teal, size=size.tiny)74plotshape(gapDown, "Gap Down", shape.arrowdown, location.abovebar,75 color.orange, size=size.tiny)76plotshape(pier, "Piercing", shape.labelup, location.belowbar,77 color.new(color.teal, 10), text="PL", textcolor=color.white, size=size.small)78plotshape(dark, "Dark Cloud", shape.labeldown, location.abovebar,79 color.new(color.purple, 10), text="DC", textcolor=color.white, size=size.small)Parameter tuning for crypto
Put this lesson to work
Free on Thrive, no account needed.