Skip to main content

Module harmonic

Module harmonic 

Source
Expand description

Harmonic pattern detection (AB=CD, Alternate AB=CD, 5-0, and the XABCD Gartley family: Gartley, Bat, Butterfly, Crab, Alternate Bat).

Harmonic patterns are Fibonacci-ratio-constrained price structures. This module builds them on the shared swing foundation (MarketStructure, Part 21) exactly like [GeometricPatternScanner]: confirmed swing pivots are collected into an alternating sequence, and the most recent 4 (AB=CD) or 5 (5-0 and the XABCD patterns) pivots are tested against the pattern’s ratio gates. A pattern is emitted only once its completion pivot D is a confirmed swing — which lags the pivot by swing_strength bars — so detection never uses information from beyond D (anti-lookahead).

§Attribution

Harmonic Trading is the work of Scott M. Carney (HarmonicTrader.com). Carney named and defined these patterns; “Harmonic Trading” and several pattern names are his trademarks. The ratio definitions here follow his primary sources and are implemented for interoperability with attribution — no source text is reproduced:

  • AB=CD reciprocal ratio table (C retracement {0.382, 0.50, 0.618, 0.707, 0.786, 0.886} ↔ BC projection {2.618/2.24, 2.0, 1.618, 1.41, 1.27, 1.13}): Carney, Harmonic Trading: Volume One (2010), Ch. 4 “The AB=CD Pattern”.
  • Alternate AB=CD (CD = 1.27 or 1.618 × AB): Volume One Ch. 4 / harmonictrader.com “Alternate ABCD Pattern”.
  • 5-0 pattern (B = 1.13–1.618 of XA; C = 1.618–2.24 of AB; D = 50% of BC and the reciprocal AB=CD): Carney, Harmonic Trading: Volume Two (2010), Ch. 3 “New Harmonic Patterns”, pp. 78–79 / harmonictrader.com “5-0”.
  • XABCD reversal patterns, distinguished by D’s retracement/extension of the XA leg (see [XABCD_SPECS]): Gartley (D 0.786), Bat (D 0.886), Butterfly (D 1.27), Crab (D 1.618) — Volume One Ch. 5–8 — and Alternate Bat (D 1.13) — Volume Two Ch. 3.

Signal output is a rich HarmonicPattern struct (pivots, measured ratios, fit score, PRZ), kept separate from any visualization per the project’s PA design philosophy.

Structs§

HarmonicConfig
Tunable detection thresholds.
HarmonicPattern
A detected harmonic pattern. Points are labelled X, A, B, C, D; x_* is populated only for the 5-0 (four-point AB=CD patterns have no X). is_bull is true when the pattern completes at a low (a buy setup).
HarmonicPatternScanner
Streaming harmonic pattern scanner. Feed (high, low) per bar; each call returns the patterns (usually none) whose completion pivot confirmed on that bar.

Enums§

HarmonicKind
Which harmonic pattern was detected.

Functions§

harmonic_patterns_batch
Batch harmonic detection: fold HarmonicPatternScanner over (high, low) bars, returning every detected pattern in completion order.