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.
Harmonic patterns: ratio constraints and competing candidates
- For X=100,A=120,B=107.64, AB length=12.36 and XA length=20.
- Divide: 12.36/20=.618.
- With target .618 and tolerance .03, absolute error is zero and this one constraint passes. Verify the remaining pattern conditions separately.
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.
- Research context: review the related evidence checkpoint. The numerical convention here defines a candidate feature; that related research does not validate this exact rule.
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 detectorContinue learning
Technical Analysis: Geometry, Structure & Evidence — all lessons- Support, resistance and reversal: start with a price zone
- Breakout detection, false breaks and retests
- Fibonacci retracements: anchors before ratios
- Harmonic patterns: ratio constraints and competing candidates
- Elliott Wave: count hypotheses, rules and invalidation
- Fair value gaps (FVG): three-bar geometry and fill measurement
- Heikin-Ashi: smoothed candles are synthetic prices
- Renko: price-driven bricks and the missing time axis
- Dynamic support, trend lines and Gann angles
- Momentum indicators, oscillators and divergence
- Volume, supply/demand zones and what OHLCV cannot reveal
- Market structure, BOS and CHOCH as a state machine
- Moon phases: encode a calendar hypothesis and try to falsify it
Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations