mcpField
Z
zigzag-pattern-classifier

zigzag-pattern-classifier

github.com/terrylicaData & databases170 installsUpdated today
Repository

About zigzag-pattern-classifier

zigzag-pattern-classifier is a reference skill for classifying two- and three-pivot ZigZag swing patterns by price relationships and market regime. It defines an exhaustive pattern taxonomy, tolerance rules, and an FD-based method for finer subdivisions.

Key features

  1. 01Two- and three-pivot pattern taxonomy
  2. 02Higher, equal, and lower level comparisons
  3. 03Epsilon tolerance guidance
  4. 04Freedman-Diaconis sub-classification
  5. 05Market regime labeling
  6. 06qta Rust crate implementation reference

Use cases

  1. 01Classify confirmed ZigZag swings
  2. 02Label patterns in Rust or Python pipelines
  3. 03Interpret market regimes from swing structures
  4. 04Set tolerance and binning rules

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/terrylica/cc-skills --skill zigzag-pattern-classifier

After installing, type/zigzag-pattern-classifierin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
binning-methodology.md
data-pipeline.md
epsilon-tolerance-detail.md
eurusd-validation-scenarios.md
notation-definitions.md
three-pivot-variants.md
two-pivot-variants.md

Details

From the project README

ZigZag Swing Pattern Classifier

Complete taxonomy for classifying ZigZag swing patterns by structure and market regime. Every confirmed ZigZag sequence maps to exactly one variant — no gaps, no overlaps.

Self-Evolving Skill: This skill improves through use. If instructions are wrong, parameters drifted, or a workaround was needed — fix this file immediately, don't defer. Only update for real, reproducible issues.

When to Use

  • Classifying a confirmed L₀→H₁→L₂ (two-pivot) or L₀→H₁→L₂→H₃ (three-pivot) swing
  • Looking up market regime implications of a specific pattern
  • Answering "how many distinct patterns exist?" and proving exhaustiveness
  • Implementing pattern labeling in code (Rust qta crate, Python pipelines)
  • Understanding the epsilon tolerance band that defines "equal"
  • Applying Freedman-Diaconis binning for sub-classification depth

Notation (Single Source of Truth)

SymbolDefinitionExample
L₀Initial low (first confirmed pivot)1.0800
H₁Swing high (reversal peak)1.0850
L₂Second low (retracement)1.0810
H₃Second high (three-pivot only)1.0870
WSwing magnitude: H₁ − L₀0.0050
zNormalized retracement: (L₂ − L₀) / (H₁ − L₀)0.20
oVolatility-normalized overshoot: (L₀ − L₂) / ATR₁₄0.35
εTolerance band for "equal" classification5 pips
τZigZag reversal threshold6.6 pips
Price Level Comparisons
CodeMeaningCondition
HLHigher LowL₂ > L₀ + ε
ELEqual Low|L₂ − L₀| ≤ ε
LLLower LowL₂ < L₀ − ε
HHHigher HighH₃ > H₁ + ε
EHEqual High|H₃ − H₁| ≤ ε
LHLower HighH₃ < H₁ − ε

Part 1: Two-Pivot Patterns (UP-DOWN)

Pattern: L₀ → H₁ → L₂ (one up-leg, one down-leg)

Base Classification (3 Classes)

Every UP-DOWN triplet falls into exactly one:

ClassConditionMeaningFrequency
EL|L₂ − L₀| ≤ εEqual Low (retest)~20–30%
HLL₂ > L₀ + εHigher Low (pullback)~50–60%
LLL₂ < L₀ − εLower Low (undercut)~10–20%
Granular Classification: 9 FD-Binned Variants

HL and LL each decompose into 4 sub-classes using Freedman-Diaconis binning on normalized coordinates:

  • HL bins use z = (L₂ − L₀) / (H₁ − L₀), ranging 0 to 1
  • LL bins use o = (L₀ − L₂) / ATR₁₄, ranging 0 to ∞
Variantz or o RangeMarket RegimeProbability
EL|z| ≤ ε_rRetest — support re-established20–30%
HL-FD10.75 < z < 1.0Shallow pullback — buyers in control20–25%
HL-FD20.50 < z ≤ 0.75Moderate pullback — Fib 38–50%15–20%
HL-FD30.25 < z ≤ 0.50Deep pullback — Fib 61.8%10–15%
HL-FD40 < z ≤ 0.25Near-complete retrace — barely held5–10%
LL-FD1o ≤ q₂₀Micro undercut — brief false break8–12%
LL-FD2q₂₀ < o ≤ q₄₀Shallow undercut — moderate panic4–8%
LL-FD3q₄₀ < o ≤ q₆₀Deep undercut — structural break2–5%
LL-FD4o > q₆₀Extreme undercut — tail event<2%
Trading Implications
VariantEntry SignalStop LossTarget
ELLong from L₀+εL₀−εH₁ + ΔH
HL-FD1Long from L₂L₂−εH₁ + ΔH
HL-FD2Long at confirmationL₂−εPrior H + ΔH
HL-FD3Reduced size; waitL₂−εSupport + ΔH
HL-FD4Extreme risk; avoidL₂−εCritical
LL-FD1Short spike tradeL₂+spikeL₀
LL-FD2Wait for reversalBreakoutL₀
LL-FD3Short continuationReversalNew lows
LL-FD4Crisis mode; hedgeCapitulationTBD
Optional Sub-Classification Flags

Attach to any variant for richer context:

FlagMeaningSignal
+CAny close < L₀ between H₁→L₂Stronger bearish commitment
+SL₂ occurs in single bar (spike)Sharp reversal; mean reversion
+XWicks below L₀ only, no closeLiquidity grab; false break

Example labels: HL-FD2+C, LL-FD4+S+C, EL+X


Part 2: Three-Pivot Patterns (UP-DOWN-UP)

Pattern: L₀ → H₁ → L₂ → H₃ (up-leg, down-leg, up-leg)

9 Exhaustive Variants

Two independent dimensions × 3 values each = 3×3 = 9 mutually exclusive, collectively exhaustive variants.

Dimension 1 — L₂ vs L₀: {HL, EL, LL} Dimension 2 — H₃ vs H₁: {HH, EH, LH}

#L₂ vs L₀H₃ vs H₁NameMarket Regime
1HLHHContinuation impulseBull trend continuation
2HLEHDouble-top testRange, bullish bias
3HLLHTriangle compressionNeutral consolidation
4ELHHRange break upBullish transition
5ELEHRectangleBalanced range
6ELLHLower-high at flat baseRange, bearish bias
7LLHHV-reversal / springBullish reversal
8LLEHUndercut then stallVolatile range
9LLLHRally failureBear trend continuation
Exhaustiveness Proof

All variants satisfy these mandatory constraints:

  • L₀ < H₁ (first uptrend exists)
  • L₂ < H₁ (retracement doesn't exceed peak)
  • H₃ > L₂ (second uptrend exists)

L₂ has exactly 3 relationships to L₀ (higher, equal, lower). H₃ has exactly 3 relationships to H₁ (higher, equal, lower). These dimensions are independent — no constraint eliminates any combination. Therefore 3 × 3 = 9 variants, all feasible, none missing.

Natural Groupings
  • Bullish (4): HL+HH, EL+HH, HL+EH, LL+HH
  • Neutral (3): HL+LH, EL+EH, LL+EH
  • Bearish (2): EL+LH, LL+LH
Regime Assignments
RegimeVariants
Trend ContinuationHL+HH (bull), LL+LH (bear)
Range ConsolidationHL+LH, EL+EH, LL+EH
Bullish TransitionEL+HH, HL+EH
Bearish TransitionEL+LH
ReversalLL+HH

Part 3: 27-Way Extension

Adding H₃ vs L₀ as a third independent dimension:

  • L₂ vs L₀: {HL, EL, LL}
  • H₃ vs H₁: {HH, EH, LH}
  • H₃ vs L₀: {Above, Equal, Below}

This yields 3×3×3 = 27 sub-variants. Some are mathematically impossible due to constraints (e.g., HL+LH+H₃<L₀ requires H₃ < L₀ < L₂ < H₃, a contradiction).

Analysts often simplify this third dimension to a binary: "reclaims L₀" vs "fails to reclaim L₀".


The Epsilon Tolerance Band

The tolerance band ε determines what "equal" means. It adapts to volatility and microstructure.

Core Formula
ε = min(ε_max, max(ε_min, √[(a·S)² + (b·ATR₁₄)²]))

Where:

  • S = rolling median spread (bid-ask)
  • ATR₁₄ = 14-bar Average True Range
  • a = 2.0 (spread scaling)
  • b = 0.05 (M5-M30) or 0.07 (H1-D1)
Bounds (EURUSD defaults)
ε_min = max(3 ticks, 1×S) = max(0.00003, S)
ε_max = min(5 pips, 0.20 × swing) = min(0.00050, 0.20 × W)
Classification Using ε
ε_r = ε / W  (relative tolerance)

EL if |z| ≤ ε_r
HL if z > ε_r
LL if z < −ε_r
Practical Fallbacks

If bid-ask spread unavailable:

  • ATR-only: ε = 0.05 × ATR₁₄ (M5-M30), 0.07 × ATR₁₄ (H1-D1)
  • Fixed band: ε = 3 pips (intraday), 5 pips (swing), 10 pips (daily)

For complete worked examples with sensitivity analysis, read references/epsilon-tolerance-detail.md.


Freedman-Diaconis Binning

The FD rule computes statistically optimal bin edges from data:

h = 2 × IQR(X) × n^(-1/3)    (bin width)
K = clip(⌈(max−min) / h⌉, 3, 6)  (number of bins)
edges = linspace(min, max, K+1)
Procedure for HL Patterns
  1. Collect 2-3 years of UP-DOWN triplets
  2. Filter to HL (z > ε_r)
  3. Winsorize z at 0.5%-99.5%
  4. Compute FD bin edges on z ∈ (ε_r, 1)
  5. Label: HL-FD1 (shallowest) through HL-FDK (deepest)
Procedure for LL Patterns
  1. Filter to LL (z < −ε_r)
  2. Compute o = (L₀ − L₂) / ATR₁₄
  3. Winsorize o at 0.5%-99.5%
  4. Compute FD bin edges on o ∈ (ε_r, ∞)
  5. Label: LL-FD1 (micro) through LL-FDK (extreme)

Recompute bin edges monthly or quarterly to track regime drift. Fall back to quantile binning if sample < 400.


Implementation Reference

The qta Rust crate (crates/qta/) implements the core ZigZag state machine that produces the pivots consumed by this classification framework:

  • ZigZagConfig::new(reversal_depth, epsilon_multiplier, bar_threshold_dbps)
  • ZigZagState::process_bar(&bar) → ZigZagOutput with completed_segment containing base_class (EL/HL/LL) and z score
  • The crate computes τ and ε dynamically per pivot from bar_threshold_dbps

The classification framework in this skill extends the crate's output with FD-binning, three-pivot analysis, and market regime labeling.


Deep Reference Files

For ASCII visualizations, worked examples, and implementation pseudocode, read these reference files as needed:

FileContents
references/notation-definitions.mdSingle source of truth: all symbols, formulas, abbreviations, pattern classes
references/two-pivot-variants.mdAll 9 two-pivot ASCII diagrams, trading rules, flag examples
references/three-pivot-variants.mdAll 9 three-pivot ASCII diagrams, 27-way extension, HL+LH granular sub-variants
references/epsilon-tolerance-detail.mdComplete ε formula, worked calculation, sensitivity analysis, pseudocode
references/binning-methodology.mdFreedman-Diaconis algorithm details, FD vs quantile comparison, worked example
references/data-pipeline.md11-step end-to-end pipeline: raw quotes → OHLC → ATR → pivots → classify → output
references/eurusd-validation-scenarios.md3 worked market scenarios (Normal, Volatile, Crash) validating ε and τ

Post-Execution Reflection

After this skill completes, check before closing:

  1. Did the command succeed? — If not, fix the instruction or error table that caused the failure.
  2. Did parameters or output change? — If the underlying tool's interface drifted, update Usage examples and Parameters table to match.
  3. Was a workaround needed? — If you had to improvise (different flags, extra steps), update this SKILL.md so the next invocation doesn't need the same workaround.

Only update if the issue is real and reproducible — not speculative.

Comments

Markdown supported