Harmonic & Chart Patterns [AI-AlgoCode]

1 490
Harmonic & Chart Patterns finds 38 classic price patterns on a ZigZag, draws them cleanly on the chart and adds a ready trade plan for the latest pattern: potential reversal zone, entry, stop loss and two targets with risk/reward.

━━━━━━━━━━━━━━━━━━━━
WHAT IT DETECTS
━━━━━━━━━━━━━━━━━━━━
Harmonic patterns (22, bullish and bearish)
• Bat, ALT Bat, Butterfly, Crab, Gartley, Deep Crab, Shark
• AB=CD, ALT AB=CD, 5-0, Three Drives

Reversal patterns (10)
• Double Top / Double Bottom
• Head & Shoulders / Inverse Head & Shoulders
• Triple Top / Triple Bottom
• Rising Wedges, Expanding Triangles

Continuation patterns (6)
• Bull / Bear Pennant, Falling Wedge, Rising Wedge, Bull / Bear Flag

Every pattern can be switched on or off individually, bullish and bearish separately, or by whole family.

━━━━━━━━━━━━━━━━━━━━
HOW IT WORKS
━━━━━━━━━━━━━━━━━━━━
1. Swings. A ZigZag marks swing highs and lows: a swing high is the highest high of the last "Depth" bars, a swing low is the lowest low. An optional "Minimum swing, %" ignores small moves.

2. Pattern rules. Every time a new swing appears, the last 11 swing points are tested against all enabled patterns:
• harmonic patterns — the ratios of consecutive legs (AB/XA, BC/AB, CD/BC) and of XD/XA must fall inside the tolerance range of each pattern;
• chart patterns — the relative position of highs and lows (equal tops, a higher head, converging or expanding boundaries, a pole and a channel for flags) plus a close beyond the breakout level.
If several patterns match the same point D, the first one is drawn and the label shows "+N" for the others.

3. Confirmed vs forming.
• Confirmed: point D is fixed because the next swing in the opposite direction has already formed. Such a pattern is drawn once and never moves or disappears.
• Forming (dashed): point D is the live swing that is still being built. It is recalculated on the last bar and can change or disappear. Use it as an early warning, not as a final signal.

━━━━━━━━━━━━━━━━━━━━
WHAT YOU SEE ON THE CHART
━━━━━━━━━━━━━━━━━━━━
• The latest pattern is the main drawing: filled wings, point letters X A B C D, the actual leg ratios on small tags and a colored name label at point D.
• Older patterns stay as thin outlines with a small name, so the history is visible but the chart stays clean. The number of kept patterns is adjustable.
• Chart patterns are drawn as two boundary lines (through the highs and through the lows) and a dashed breakout level, instead of a long polyline through every swing.
• Harmonic and chart patterns use separate colors for bullish and bearish setups.

━━━━━━━━━━━━━━━━━━━━
TRADE PLAN
━━━━━━━━━━━━━━━━━━━━
For the forming pattern, or for the latest confirmed pattern if its point D is recent (30 bars by default), the indicator draws a plan to the right of point D:
• PRZ — the potential reversal zone around point D (harmonic patterns);
• entry, stop loss, TP1 and TP2 as price tags with R multiples;
• risk (red) and reward (green) areas, similar to the Long/Short Position tool.

How the levels are calculated (all coefficients are adjustable):
• Harmonic patterns: entry at D, stop loss beyond D at 0.13 × AD, TP1 and TP2 at the 38.2% and 61.8% retracement of the AD leg.
• Chart patterns: entry at the breakout level, stop loss at the middle of the pattern (0.5 × height), TP1 and TP2 at 1.0 and 1.618 × the pattern height (measured move).

The plan shows geometric reference levels. It is not a trading recommendation.

━━━━━━━━━━━━━━━━━━━━
ALERTS
━━━━━━━━━━━━━━━━━━━━
• Alert conditions: "Bullish pattern confirmed", "Bearish pattern confirmed", "Bullish pattern forming", "Bearish pattern forming".
• "Any alert() function call" sends a detailed message with the symbol, timeframe, pattern name and the price of point D. Confirmed-pattern alerts are sent once per bar close.

━━━━━━━━━━━━━━━━━━━━
SETTINGS TIPS
━━━━━━━━━━━━━━━━━━━━
• Depth 3–5: many smaller patterns, suits lower timeframes. Depth 8–15: fewer, larger structures.
• Raise "Minimum swing, %" on volatile instruments to filter noise.
• Disable the pattern families you do not use: fewer drawings, a cleaner chart.
• The trade plan coefficients let you match the levels to your own rules (for example, a wider stop or a 1:2 target).

━━━━━━━━━━━━━━━━━━━━
REPAINTING AND LIMITATIONS
━━━━━━━━━━━━━━━━━━━━
• Confirmed patterns do not repaint. They are drawn on the bar where the next swing confirms point D, so they always appear with a delay of at least one swing.
• The forming pattern, its trade plan and the "forming" alerts repaint by design, because point D is not final until the next swing appears.
• Pattern tolerances are wide enough to catch real-market shapes, so not every detected pattern is a textbook example. Combine the signals with trend, levels and your own analysis.

This script is for educational and analytical purposes and is not financial advice.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.