Candlestick Pattern Libraries & Filters
Using community pattern libraries, building volume and trend filters for candlestick signals, and combining patterns with technical indicators for higher accuracy.
From Thrive on YouTube
Watch the matching Thrive video
Swing Failure Pattern (SFP) Explained: Trade the Fake Breakout
Part of Charting and Market Structure. Education only. Not financial advice.
Why Raw Candlestick Patterns Produce Too Many False Signals
If you run any of the pattern detectors from the previous lessons on a BTC daily chart, you will see dozens of signals per month. Most of them lead nowhere: the pattern fires, price moves a little in the expected direction, then reverses. The problem is not the pattern itself. It is the lack of context. A bullish engulfing candle on below-average volume during a strong downtrend is just noise. That same engulfing candle on 2x average volume at a major support level with RSI in oversold territory is a high-probability setup.
Filters transform raw pattern detection into a tradeable system. Each filter eliminates a category of false signals. Combining multiple filters creates a confluence-based approach where only the highest-quality patterns survive.
Volume Filter: Above-Average Volume Confirmation
Volume is the single most important confirmation for any candle pattern. A bullish engulfing on high volume means real capital is behind the move. The same pattern on low volume is just random noise with no institutional participation. The standard approach is to require volume to be above its 20-period simple moving average, but you can set a multiplier for stricter filtering.
1//@version=52indicator("Volume Filter", overlay=true)34volLen = input.int(20, "Volume SMA Length", minval=5)5volMult = input.float(1.0, "Volume Multiplier", step=0.1,6 tooltip="1.0 = above average, 1.5 = 50% above average")78avgVol = ta.sma(volume, volLen)9aboveAvgVol = volume >= avgVol * volMult1011// Example: engulfing + volume filter12body = math.abs(close - open)13bodyHigh = math.max(close, open)14bodyLow = math.min(close, open)15prevBodyHigh = math.max(close[1], open[1])16prevBodyLow = math.min(close[1], open[1])1718bullEngulf = close[1] < open[1] and close > open and19 bodyLow <= prevBodyLow and bodyHigh >= prevBodyHigh2021// Unfiltered vs filtered22unfilteredSignal = bullEngulf23filteredSignal = bullEngulf and aboveAvgVol2425plotshape(unfilteredSignal and not filteredSignal, "Unfiltered",26 shape.cross, location.belowbar,27 color.new(color.gray, 50), size=size.tiny)28plotshape(filteredSignal, "Vol Confirmed",29 shape.triangleup, location.belowbar,30 color.green, size=size.small)3132// Volume bars in separate pane33plot(volume, "Volume", aboveAvgVol ? color.green : color.gray,34 style=plot.style_histogram, display=display.pane)35plot(avgVol * volMult, "Vol Threshold", color.orange,36 display=display.pane)Volume multiplier tuning
Trend Filter: Only Take Patterns in the Right Direction
One of the most effective filters is directional alignment. Only take bullish candle patterns when the broader trend is bullish, and only take bearish patterns when the trend is bearish. Trading counter-trend patterns is possible but requires much higher confluence. For most traders, trend-aligned patterns have significantly better win rates.
1//@version=52indicator("Trend Filter", overlay=true)34trendMA = input.int(50, "Trend MA Length", minval=10)5slopeLen = input.int(5, "Slope Lookback", minval=1)67ema = ta.ema(close, trendMA)8slope = ema - ema[slopeLen]9upTrend = slope > 0 and close > ema10dnTrend = slope < 0 and close < ema1112// Filter function: returns true if pattern direction matches trend13bullFilter = upTrend14bearFilter = dnTrend1516// Example application17body = math.abs(close - open)18bodyHigh = math.max(close, open)19bodyLow = math.min(close, open)20prevBodyHigh = math.max(close[1], open[1])21prevBodyLow = math.min(close[1], open[1])2223bullEngulf = close[1] < open[1] and close > open and24 bodyLow <= prevBodyLow and bodyHigh >= prevBodyHigh25bearEngulf = close[1] > open[1] and close < open and26 bodyLow <= prevBodyLow and bodyHigh >= prevBodyHigh2728trendBull = bullEngulf and bullFilter29trendBear = bearEngulf and bearFilter3031plot(ema, "Trend EMA", upTrend ? color.green : dnTrend ? color.red : color.gray, 2)32plotshape(trendBull, "Trend Bull", shape.triangleup,33 location.belowbar, color.green, size=size.small)34plotshape(trendBear, "Trend Bear", shape.triangledown,35 location.abovebar, color.red, size=size.small)3637bgcolor(upTrend ? color.new(color.green, 95) :38 dnTrend ? color.new(color.red, 95) : na)ATR-Based Filter: Minimum Candle Size
Small candles produce weak signals. If an engulfing pattern forms with a range smaller than 0.5x the 14-period ATR, it lacks the volatility to be meaningful. ATR-based filters ensure that patterns only fire on candles that represent genuine price movement relative to recent volatility. This is especially important on ETH during low-volume weekend sessions.
1//@version=52indicator("ATR Filter", overlay=true)34atrLen = input.int(14, "ATR Length")5atrMult = input.float(0.5, "Min Candle Size (ATR mult)", step=0.1,6 tooltip="Minimum candle range as a multiple of ATR")78atr = ta.atr(atrLen)9range_ = high - low10minRange = atr * atrMult1112// The candle must be at least atrMult * ATR in size13sizeFilter = range_ >= minRange1415// Also useful: body must be a minimum % of ATR16bodyAtrFilter = math.abs(close - open) >= atr * 0.31718// Combined size filter19validCandle = sizeFilter and bodyAtrFilter2021// Visual: mark candles that pass the size filter22bgcolor(not validCandle ? color.new(color.gray, 95) : na,23 title="Weak Candle Fade")Moving Average Slope as a Directional Filter
Rather than just checking if price is above or below a moving average, measuring the slope of the MA tells you whether the trend is accelerating, decelerating, or flat. A bullish candle pattern during an accelerating uptrend (steepening slope) is far more powerful than one during a flattening trend. The slope is calculated as the difference between the current MA value and its value N bars ago.
1//@version=52indicator("MA Slope Filter", overlay=false)34maLen = input.int(21, "MA Length")5slopeLen = input.int(5, "Slope Period")6slopeThr = input.float(0.0, "Min Slope Threshold",7 tooltip="Absolute minimum slope value")89ema = ta.ema(close, maLen)10slope = (ema - ema[slopeLen]) / ema[slopeLen] * 10011absSlope = math.abs(slope)1213strongUp = slope > slopeThr14strongDown = slope < -slopeThr15flat = absSlope <= slopeThr1617slopeColor = strongUp ? color.green :18 strongDown ? color.red : color.gray1920plot(slope, "EMA Slope %", slopeColor, 2, plot.style_histogram)21hline(slopeThr, "Threshold", color.new(color.green, 70), hline.style_dashed)22hline(-slopeThr, "-Threshold", color.new(color.red, 70), hline.style_dashed)23hline(0, "Zero", color.new(color.gray, 80))Combining Patterns with RSI Zones
RSI zone filtering adds a momentum dimension. A bullish engulfing with RSI below 40 means the reversal is occurring from a momentum extreme: sellers are exhausted. A bullish engulfing with RSI at 70 means the "reversal" is actually a continuation in overbought territory: much less reliable. By requiring RSI to be in the appropriate zone for the pattern direction, you eliminate a large class of low-probability signals.
1//@version=52indicator("RSI Zone Filter", overlay=true)34rsiLen = input.int(14, "RSI Length")5bullZone = input.int(45, "Bull RSI Max", tooltip="Bullish patterns only below this RSI")6bearZone = input.int(55, "Bear RSI Min", tooltip="Bearish patterns only above this RSI")78rsi = ta.rsi(close, rsiLen)910// RSI zone filters11rsiBullFilter = rsi <= bullZone12rsiBearFilter = rsi >= bearZone1314// Example: engulfing + RSI filter15body = math.abs(close - open)16bodyHigh = math.max(close, open)17bodyLow = math.min(close, open)18prevBodyHigh = math.max(close[1], open[1])19prevBodyLow = math.min(close[1], open[1])2021bullEngulf = close[1] < open[1] and close > open and22 bodyLow <= prevBodyLow and bodyHigh >= prevBodyHigh23bearEngulf = close[1] > open[1] and close < open and24 bodyLow <= prevBodyLow and bodyHigh >= prevBodyHigh2526rsiBull = bullEngulf and rsiBullFilter27rsiBear = bearEngulf and rsiBearFilter2829plotshape(rsiBull, "RSI Bull", shape.labelup,30 location.belowbar, color.new(color.green, 10),31 text="RSI", textcolor=color.white, size=size.small)32plotshape(rsiBear, "RSI Bear", shape.labeldown,33 location.abovebar, color.new(color.red, 10),34 text="RSI", textcolor=color.white, size=size.small)Using Community Pattern Libraries
TradingView's community publishes PineScript libraries that contain pre-built pattern detection functions. Instead of writing every pattern from scratch, you can import a library and call its functions directly. This saves development time and lets you leverage battle-tested code written by experienced PineScript developers.
Find a library
Import the library
import statement at the top of your script: import Username/LibraryName/1 as candles. The /1 is the version number.Call library functions
candles.isBullishEngulfing(). Each function returns a boolean that you can use in your own filter chain.Apply your own filters on top
Creating Your Own Reusable Pattern Library
Once you have refined your pattern and filter functions, package them into a library so you can reuse them across all your indicators and strategies without copy-pasting code.
1//@version=52// @description Candle pattern detection with built-in filters3library("CandlePatterns", overlay=false)45export isEngulfing(bool bullish) =>6 body = math.abs(close - open)7 bodyHigh = math.max(close, open)8 bodyLow = math.min(close, open)9 pBodyHigh = math.max(close[1], open[1])10 pBodyLow = math.min(close[1], open[1])11 engulf = bodyLow <= pBodyLow and bodyHigh >= pBodyHigh and body > math.abs(close[1] - open[1])12 if bullish13 close[1] < open[1] and close > open and engulf14 else15 close[1] > open[1] and close < open and engulf1617export isHammer(float wickRatio) =>18 body = math.abs(close - open)19 lowerWick = math.min(close, open) - low20 upperWick = high - math.max(close, open)21 body > 0 and lowerWick >= body * wickRatio and upperWick <= body * 0.52223export isDoji(float threshold) =>24 range_ = high - low25 range_ > 0 and math.abs(close - open) / range_ * 100 <= threshold2627export volumeFilter(int length, float mult) =>28 volume >= ta.sma(volume, length) * mult2930export atrFilter(int length, float mult) =>31 (high - low) >= ta.atr(length) * multComprehensive Filtered Pattern System
Here is a complete indicator that applies all filter types to engulfing patterns. Toggle each filter independently to see how it affects signal count and quality.
1//@version=52indicator("Filtered Patterns", overlay=true)34// Filter toggles5useVolFilter = input.bool(true, "Volume Filter")6useTrendFilter = input.bool(true, "Trend Filter")7useAtrFilter = input.bool(true, "ATR Size Filter")8useRsiFilter = input.bool(false, "RSI Zone Filter")910// Filter parameters11volLen = input.int(20, "Volume SMA Length")12volMult = input.float(1.0, "Volume Multiplier", step=0.1)13trendLen = input.int(50, "Trend EMA Length")14atrLen = input.int(14, "ATR Length")15atrMult = input.float(0.5, "Min Range (ATR mult)", step=0.1)16rsiLen = input.int(14, "RSI Length")17rsiBull = input.int(45, "Bull RSI Max")18rsiBear = input.int(55, "Bear RSI Min")1920// Calculations21avgVol = ta.sma(volume, volLen)22ema = ta.ema(close, trendLen)23slope = ema - ema[5]24atr = ta.atr(atrLen)25rsi = ta.rsi(close, rsiLen)2627// Pattern detection28body = math.abs(close - open)29bodyHigh = math.max(close, open)30bodyLow = math.min(close, open)31pBodyHigh = math.max(close[1], open[1])32pBodyLow = math.min(close[1], open[1])33engulf = bodyLow <= pBodyLow and bodyHigh >= pBodyHigh and body > math.abs(close[1] - open[1])3435rawBull = close[1] < open[1] and close > open and engulf36rawBear = close[1] > open[1] and close < open and engulf3738// Apply filters39volPass = not useVolFilter or volume >= avgVol * volMult40trendBull = not useTrendFilter or (slope > 0 and close > ema)41trendBear = not useTrendFilter or (slope < 0 and close < ema)42atrPass = not useAtrFilter or (high - low) >= atr * atrMult43rsiBullOk = not useRsiFilter or rsi <= rsiBull44rsiBearOk = not useRsiFilter or rsi >= rsiBear4546filteredBull = rawBull and volPass and trendBull and atrPass and rsiBullOk47filteredBear = rawBear and volPass and trendBear and atrPass and rsiBearOk4849// Count filters passed (for label)50bullFilters = (volPass ? 1 : 0) + (trendBull ? 1 : 0) + (atrPass ? 1 : 0) + (rsiBullOk ? 1 : 0)51bearFilters = (volPass ? 1 : 0) + (trendBear ? 1 : 0) + (atrPass ? 1 : 0) + (rsiBearOk ? 1 : 0)5253plotshape(filteredBull, "Filtered Bull", shape.labelup,54 location.belowbar, color.new(color.green, 0),55 text="BUY", textcolor=color.white, size=size.small)56plotshape(filteredBear, "Filtered Bear", shape.labeldown,57 location.abovebar, color.new(color.red, 0),58 text="SELL", textcolor=color.white, size=size.small)5960// Show rejected patterns in gray for comparison61plotshape(rawBull and not filteredBull, "Rejected Bull",62 shape.cross, location.belowbar,63 color.new(color.gray, 60), size=size.tiny)64plotshape(rawBear and not filteredBear, "Rejected Bear",65 shape.cross, location.abovebar,66 color.new(color.gray, 60), size=size.tiny)6768plot(ema, "Trend EMA", slope > 0 ? color.green : color.red, 2)6970alertcondition(filteredBull, "Filtered Bullish",71 "High-quality bullish engulfing detected")72alertcondition(filteredBear, "Filtered Bearish",73 "High-quality bearish engulfing detected")Backtesting filter combinations
Put this lesson to work
Free on Thrive, no account needed.