Chart Pattern Recognition System Development (Head and Shoulders, Triangles, Wedges)
Automatic recognition of classical chart patterns is a task that at first glance looks simple, but in practice requires serious engineering work. The "head and shoulders" pattern is easy to see on a static chart, but detecting it algorithmically in real-time across thousands of instruments is a different story.
Pattern Classification and Algorithmic Representation
All patterns can be divided into three categories by detection complexity:
Reversal patterns (Head & Shoulders, Inverse H&S, Double Top/Bottom, Triple Top/Bottom) — formed at significant levels, signal trend reversals.
Continuation patterns (Ascending/Descending/Symmetrical Triangle, Bull/Bear Flag, Pennant, Wedge) — formed in the middle of a trend as consolidation before continuation.
Channel patterns (Rising/Falling Channel, Rectangle) — price moves between parallel levels.
Detection Algorithm: Pivot Points Method
The main approach is identifying local extremes (swing highs and swing lows) and analyzing their sequence.
Stage 1: Finding pivot points
def find_pivots(highs, lows, window=5):
pivot_highs = []
pivot_lows = []
for i in range(window, len(highs) - window):
if highs[i] == max(highs[i-window:i+window+1]):
pivot_highs.append((i, highs[i]))
if lows[i] == min(lows[i-window:i+window+1]):
pivot_lows.append((i, lows[i]))
return pivot_highs, pivot_lows
Window size affects the "scale" of detected patterns. Small window gives patterns on short timeframes, large — on long-term.
Stage 2: Pattern Matching
For "head and shoulders" we need to find a sequence of 5 pivot points: left shoulder (high), neckline left (low), head (highest high), neckline right (low), right shoulder (high).
Conditions for valid H&S:
- Head higher than both shoulders (tolerance ±2%)
- Both shoulders approximately same height (difference < 5%)
- Neckline relatively horizontal (slope < 15°)
- Right shoulder does not exceed Head
Stage 3: Triangle Detection
Triangles are defined through linear regression on pivot points:
- Symmetrical: descending line on highs + ascending on lows, converging to one point
- Ascending: horizontal resistance + ascending support
- Descending: horizontal support + descending resistance
Triangle quality is assessed through R² coefficient of linear regressions. R² > 0.85 — good triangle.
Stage 4: Wedges
Wedge differs from triangle in that both lines point in one direction:
- Rising Wedge (bearish signal): both lines ascending, upper more gradual
- Falling Wedge (bullish signal): both lines descending, lower more gradual
Key condition: lines converge (angle between them decreases).
Pattern Quality Assessment and Target Projection
Each detected pattern receives a score (0–100) based on:
- Symmetry (for H&S)
- R² of lines (for triangles)
- Volume ratio (volume should decrease during formation)
- Pattern completeness (has breakout occurred or not)
Target projection — calculation of target after breakout:
- H&S: target = neckline level − head height
- Triangle: target = apex point ± base height
- Wedge: target = wedge start
Backtesting and Reliability Assessment
System includes backtesting module: for each historically detected pattern we check if target worked within the next N candles and with what success rate. This allows calibrating threshold values for specific asset and timeframe.
Typical reliability in crypto market (based on historical BTC/ETH data):
| Pattern | Win Rate | Average R/R |
|---|---|---|
| H&S (confirmed) | 55–65% | 1:1.5 |
| Ascending Triangle | 60–70% | 1:1.8 |
| Symmetrical Triangle | 50–55% | 1:1.2 |
| Falling Wedge | 60–68% | 1:2.0 |
System Architecture
Backend: Python (pandas, numpy, scipy for linear regression), OHLCV data processing from exchange APIs (CCXT library). Scan across all instruments is scheduled (cron) on each candle close.
Database: PostgreSQL for storing detected patterns with parameters, status (forming/confirmed/failed) and results.
Frontend: React + TradingView Lightweight Charts. Patterns are drawn as SVG overlays on the price chart — shoulder lines, neckline, triangle lines with labels.
Alerts: When a new confirmed pattern is detected or a level is broken — notification via Telegram or webhook.
Scaling
For scanning 500+ instruments in real-time, parallel processing is used through Python multiprocessing or Celery. Results are cached in Redis. Full scan of 500 instruments on 4h timeframe takes 15–30 seconds on standard server.
We develop a system with configurable detector sensitivity, support for multiple timeframes simultaneously, pattern visualization on chart and built-in backtesting for assessing algorithm quality on historical data.







