Candlestick Pattern Recognition System Development
Candlestick analysis is a Japanese methodology that is centuries old, but in the context of algorithmic trading it has gained a second life. Automatic recognition of patterns across thousands of instruments simultaneously enables what is impossible to do manually.
Classification of Candlestick Patterns
Single candlestick patterns (simplest, high frequency):
- Doji (cross, gravestone, dragonfly)
- Hammer / Inverted Hammer
- Shooting Star
- Spinning Top
- Marubozu (fully filled candle)
Two candlestick patterns:
- Bullish/Bearish Engulfing
- Harami (bullish/bearish)
- Piercing Line / Dark Cloud Cover
- Tweezer Top/Bottom
Three candlestick patterns (most reliable):
- Morning Star / Evening Star
- Three White Soldiers / Three Black Crows
- Three Inside Up/Down
- Abandoned Baby
Recognition Algorithm
Each pattern is described by a set of mathematical conditions relative to candle parameters (open, high, low, close, volume).
Example: Bullish Engulfing
def is_bullish_engulfing(prev, curr):
prev_bearish = prev['close'] < prev['open']
curr_bullish = curr['close'] > curr['open']
curr_body_size = curr['close'] - curr['open']
prev_body_size = prev['open'] - prev['close']
engulfs = (curr['open'] <= prev['close'] and
curr['close'] >= prev['open'])
min_body_ratio = curr_body_size / prev_body_size >= 1.1
return prev_bearish and curr_bullish and engulfs and min_body_ratio
Example: Doji
def is_doji(candle, threshold=0.1):
body = abs(candle['close'] - candle['open'])
range_ = candle['high'] - candle['low']
return (body / range_) <= threshold if range_ > 0 else False
Candle normalization — absolute values of body and wicks are compared through coefficients, not absolute numbers. Body > 70% of range = strong candle. Body < 10% = doji. Wicks > 2× body = hammer/shooting star.
Contextual Filtering
Raw pattern detector produces many false signals. Contextual filtering significantly improves quality:
Trend context — Hammer is valid only in downtrend, Shooting Star — in uptrend. Trend is determined through EMA(20) or linear regression over last N candles.
Support/Resistance levels — pattern near key level has higher weight than in middle of range.
Volume — pattern with above-average volume is significantly more reliable. Morning Star with high volume on third candle — strong reversal signal.
ATR filter — during low volatility periods (ATR below N%) patterns are ignored as statistical noise.
Backtesting and Win Rate
TA-Lib library (Python/C) contains ready-made functions for recognizing most classical patterns. We use it as baseline and supplement with own implementations with contextual filtering.
Backtesting on BTC/USDT (1h, 2020–2024):
| Pattern | Number of Signals | Win Rate (without context) | Win Rate (with trend filter) |
|---|---|---|---|
| Bullish Engulfing | 1840 | 52% | 61% |
| Morning Star | 412 | 56% | 67% |
| Hammer | 2190 | 49% | 58% |
| Three White Soldiers | 186 | 64% | 71% |
Multi-Timeframe Scanning
System scans all specified instruments simultaneously on multiple timeframes (15m, 1h, 4h, 1d). Pattern confirmed on multiple timeframes simultaneously — priority signal.
Priority hierarchy: daily > 4h > 1h > 15m. Signal from 4h pattern with confirmation on 1d receives score +30%.
Architecture and Stack
Python: pandas for OHLCV operations, TA-Lib for basic patterns, own functions for advanced patterns with context. CCXT for connecting to exchange APIs.
Scheduler: APScheduler or Celery Beat for regular scanning at candle close.
Pattern database: PostgreSQL — table with fields: instrument, timeframe, pattern_type, candle_timestamp, score, context (JSON), status (active/expired/triggered).
Notifications: Telegram Bot with formatted messages: pattern name, instrument, timeframe, current price, possible target.
Visualization: highlighting pattern candles with color/icons on price chart. TradingView Lightweight Charts or custom canvas renderer.
Result — scalable system that monitors hundreds of instruments and sends notifications about quality candlestick signals with contextual filtering.







