Trading Dev AcademyFree quant education

Free lesson · Technical analysis

Harmonic patterns: ratio constraints and competing candidates

Open interactive lessonPractice calculationsExplore labs

Start with the idea

A harmonic candidate is a sequence of turning points that satisfies a named set of leg-ratio constraints.

Symbols, units & horizon
  • X,A,B: ordered confirmed pivot prices
  • q_(AB/XA): dimensionless ratio of AB length to XA length
  • r₀: selected template target ratio
  • ε: nonnegative ratio tolerance
  • | |: absolute magnitude
  • XA: nonzero initial leg

When and why to use this

Use a harmonic detector to create auditable pattern events or ratio-distance features for a model.

  • Start with an ordered X–A–B–C–D pivot sequence. Each pivot must have a recorded confirmation time.
  • Gartley, Bat, Butterfly and Crab templates use different combinations of retracements and extensions. Publish the exact template table and tolerances used.
  • For a common Gartley convention, B is near .618 of XA and D near .786 of XA; other leg constraints must also pass. Two ratios alone are not a full detector.
  • A Bat convention often uses a shallower B retracement and D near .886 of XA; Butterfly/Crab conventions extend D beyond X. Definitions vary across implementations.
  • Check pivot order, alternating direction, leg length, time spacing and every required ratio. Group near-duplicate candidates from overlapping pivot choices.
  • Use tolerance bands fixed on validation. Compare a ratio-constrained detector against the same swing geometry without the Fibonacci restrictions.
  • A complete pattern at D is observable only when the D pivot confirmation rule passes. Historical plotting at the D extremum does not mean trading there was possible.
qABXA=|B−A||A−X|,|qABXA−r0|≤ϵ
Specified chart rule · derivation and arithmetic

Harmonic patterns: ratio constraints and competing candidates

  1. For X=100,A=120,B=107.64, AB length=12.36 and XA length=20.
  2. Divide: 12.36/20=.618.
  3. With target .618 and tolerance .03, absolute error is zero and this one constraint passes. Verify the remaining pattern conditions separately.
Work it by hand

B=110 gives ratio .5, differing from .618 by .118; it fails a .03 tolerance. This is a constraint check, not a payoff estimate.

Use the rule

  • Fix the definition, units and information timestamp.
  • Compute the example and inspect the graph.
  • Compare with a simple baseline on untouched periods after costs.

Before moving on

Write the exact rule, its availability time, an invalidation condition and a fair out-of-sample test.

Python implementation

Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.

def harmonic_leg(x,a,b,target=.618,tolerance=.03):
    if a==x or tolerance<0: raise ValueError("Nonzero anchor leg and nonnegative tolerance required")
    ratio=abs(b-a)/abs(a-x)
    return ratio,abs(ratio-target)<=tolerance

print(harmonic_leg(100,120,107.64)) # one constraint, not a full Gartley detector

Continue learning

Technical Analysis: Geometry, Structure & Evidence — all lessons
  1. Support, resistance and reversal: start with a price zone
  2. Breakout detection, false breaks and retests
  3. Fibonacci retracements: anchors before ratios
  4. Harmonic patterns: ratio constraints and competing candidates
  5. Elliott Wave: count hypotheses, rules and invalidation
  6. Fair value gaps (FVG): three-bar geometry and fill measurement
  7. Heikin-Ashi: smoothed candles are synthetic prices
  8. Renko: price-driven bricks and the missing time axis
  9. Dynamic support, trend lines and Gann angles
  10. Momentum indicators, oscillators and divergence
  11. Volume, supply/demand zones and what OHLCV cannot reveal
  12. Market structure, BOS and CHOCH as a state machine
  13. Moon phases: encode a calendar hypothesis and try to falsify it

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations