Swing Point Detection
Swing point detection is the foundation of all harmonic and structure pattern detection in the ohlc-service. It identifies significant pivot highs and lows from a series of OHLC candles.
Algorithm
Constant: PIVOT_LOOKBACK = 5
A candle qualifies as:
- Swing high — its
highis ≥ thehighof every candle within 5 candles before and 5 candles after it - Swing low — its
lowis ≤ thelowof every candle within 5 candles before and 5 candles after it
Minimum candle requirement
PIVOT_LOOKBACK * 2 + 5 = 15 candles
Fewer than 15 candles returns an empty array — there is not enough surrounding context to confirm any pivot.
Output
findSwingPoints(candles: OhlcCandle[]): SwingPoint[] (in patternDetector.ts)
The result is an alternating list of highs and lows — a swing high is always followed by a swing low, and vice versa. If two consecutive pivots of the same type are detected, the more extreme one is kept.
Each SwingPoint carries:
| Field | Description |
|---|---|
index | Candle index in the input array |
price | The pivot price (high or low) |
type | 'high' or 'low' |
timestamp | Candle open time |
Role in Pattern Detection
Every harmonic pattern (Gartley, Bat, Butterfly, Crab, Shark, Cypher) and every structure pattern (ABCD, Three Drives, Double Top/Bottom, Head & Shoulders) operates on the swing point list rather than raw candles. The swing points define the X, A, B, C, D legs used in ratio calculations.
See Markets — Pattern Detection Overview for the full pattern family map.