OPEN-SOURCE SCRIPT
تم تحديثه

Reverse ZigZag

5 923
Reverse ZigZag

A reimagined ZigZag indicator that processes price data from the most recent bar backwards, solving the biggest frustration with traditional ZigZag implementations: the inaccurate last leg.

━━━━━━━━━━━━━━━━━━━━━━━━

THE PROBLEM

Standard ZigZag indicators process bars left-to-right. This means the last leg is always "tentative" — it often anchors to the current bar instead of the actual swing extreme. If the true low was 3 bars ago but price has since bounced, the traditional ZigZag either misses it entirely or draws to the wrong bar. This makes the indicator unreliable precisely where it matters most: at the current price action.

THE SOLUTION

This indicator flips the logic. Instead of walking forward through history, it starts at the most recent bar and walks backwards. The result:

→ The last leg is always accurate — it finds the true extreme first, then works outward
→ No tentative or "pending" pivots — every swing point is drawn to the correct bar
→ No repainting — pivots don't shift around as new bars form
→ Clean, consistent results that match what your eyes see on the chart

━━━━━━━━━━━━━━━━━━━━━━━━

HOW IT WORKS

Phase 1 — Initialization
Starting from the current bar, the indicator walks left until it finds a price range that exceeds the deviation threshold. The more recent extreme becomes the first confirmed pivot.

Phase 2 — Backward Walk
From that first pivot, the algorithm continues left, tracking running highs and lows. When price reverses by more than the deviation %, a pivot is confirmed and the direction flips. This continues across the full lookback window.

Phase 3 — Drawing
Lines and labels are drawn between all confirmed pivots. Labels show the price and the magnitude of each swing move.

━━━━━━━━━━━━━━━━━━━━━━━━

SETTINGS

• Deviation % — Minimum reversal required to confirm a new pivot (default: 5%). Lower values capture smaller swings, higher values show only major structure.
• Lookback Bars — How far back to calculate (default: 500). Increase for longer history.
• Show Price Labels — Toggle pivot labels showing price and swing magnitude.
• Line Color / Width — Customize appearance.
• High/Low Pivot Colors — Separate colors for swing highs and lows.

━━━━━━━━━━━━━━━━━━━━━━━━

USE CASES

• Swing traders identifying reversal points and market structure
• Harmonic pattern recognition where accurate pivot placement is critical
• Elliott Wave analysis requiring precise swing labeling
• Support/resistance identification from confirmed swing levels
• Measuring swing magnitude to gauge momentum shifts

━━━━━━━━━━━━━━━━━━━━━━━━

NOTES

• The indicator recalculates on the last bar, so all drawing happens once per bar update — this is efficient and avoids the overhead of managing state across bars.
• Works on any timeframe and any instrument.

━━━━━━━━━━━━━━━━━━━━━━━━
ملاحظات الأخبار
Reverse ZigZag — Right-to-Left Processing for Accurate Pivots

A reimagined ZigZag indicator that processes price data from the most recent bar backwards, solving the biggest frustration with traditional ZigZag implementations: the inaccurate last leg.

━━━━━━━━━━━━━━━━━━━━━━━━

THE PROBLEM

Standard ZigZag indicators process bars left-to-right. This causes two well-known issues:

1. The last leg is always "tentative" — it often anchors to the current bar instead of the actual swing extreme. If the true low was 3 bars ago but price has since bounced, the traditional ZigZag either misses it entirely or draws to the wrong bar.

2. Large candles create phantom pivots — when a single bar's range exceeds the deviation threshold, standard implementations can register both its high and low as separate pivots, drawing false mini zigzag legs on a single candle.

THE SOLUTION

This indicator flips the logic. Instead of walking forward through history, it starts at the most recent bar and walks backwards. Combined with a single-bar protection rule, the result:

→ The last leg is always accurate — it finds the true extreme first, then works outward
→ No phantom pivots from large candles — a single bar can either extend the current move or confirm a reversal, never both
→ No tentative or "pending" pivots — every swing point is drawn to the correct bar
→ No repainting — pivots don't shift around as new bars form
→ Clean, consistent results that match what your eyes see on the chart

━━━━━━━━━━━━━━━━━━━━━━━━

HOW IT WORKS

The algorithm runs in three phases, all executed on the last bar:

Phase 1 — Initialization
Starting from the current bar, the indicator walks left until it finds a price range that exceeds the deviation threshold. The more recent extreme becomes the first confirmed pivot, and direction is established.

Phase 2 — Backward Walk
From that first pivot, the algorithm continues left, tracking running highs and lows. When price reverses by more than the deviation %, a pivot is confirmed and the direction flips. Crucially, a single bar can only do one thing per iteration: either extend the current extreme or confirm a reversal. This prevents large-range candles from generating false pivots.

Phase 3 — Drawing
Lines and labels are drawn between all confirmed pivots. Labels show the price and the magnitude of each swing move.

━━━━━━━━━━━━━━━━━━━━━━━━

SETTINGS

• Deviation % — Minimum reversal required to confirm a new pivot (default: 5%). Lower values capture smaller swings, higher values show only major structure.
• Lookback Bars — How far back to calculate (default: 500). Increase for longer history.
• Show Price Labels — Toggle pivot labels showing price and swing magnitude.
• Line Color / Width — Customize appearance.
• High/Low Pivot Colors — Separate colors for swing highs and lows.

━━━━━━━━━━━━━━━━━━━━━━━━

USE CASES

• Swing traders identifying reversal points and market structure
• Harmonic pattern recognition where accurate pivot placement is critical
• Elliott Wave analysis requiring precise swing labeling
• Support/resistance identification from confirmed swing levels
• Measuring swing magnitude to gauge momentum shifts

━━━━━━━━━━━━━━━━━━━━━━━━

NOTES

• The indicator recalculates on the last bar, so all drawing happens once per bar update — this is efficient and avoids the overhead of managing state across bars.
• Works on any timeframe and any instrument.

━━━━━━━━━━━━━━━━━━━━━━━━

Feedback and suggestions are welcome
ملاحظات الأخبار
This update addresses several edge cases and adds new controls for cleaner zigzag output.

New Features:
• Depth parameter — Controls how many bars the algorithm scans before establishing the first pivot direction. Higher values produce more stable results across different deviation settings. Default: 10.
• Min Leg Bars — Sets the minimum number of bars required between two pivots. Short legs are automatically merged into longer ones while preserving the most extreme price points. Prevents noisy zigzag legs on volatile price action. Default: 2.
• Confirmed Pivots Only — Optional toggle that hides the last unconfirmed zigzag leg. When enabled, only pivots that have been validated by a full reversal exceeding the deviation threshold are drawn. Useful when you only want to see confirmed market structure.
• Label Size — Configurable label size (tiny, small, normal, large) to suit different chart zoom levels and screen sizes.

Bug Fixes:
• Fixed large candles creating phantom pivots — A single bar whose range exceeded the deviation threshold could generate both a high and low pivot simultaneously. Now a bar can either extend the current move or confirm a reversal, never both.
• Fixed algorithm freezing at low deviation values — At deviations below 5%, the algorithm could get stuck and skip large sections of the chart. Refactored to a clean two-pass architecture: Pass 1 finds all pivots using pure deviation logic, Pass 2 post-processes to merge short legs.
• Fixed Phase 1 instability — Small changes in deviation (e.g. 5.4% vs 5.5%) could produce completely different zigzag patterns. The new depth parameter ensures the algorithm always scans enough bars before committing to an initial direction.
• Fixed Phase 1/Phase 2 overlap — The backward walk could re-scan bars between the two initial extremes, creating duplicate pivots near the most recent price action. Phase 2 now starts strictly after both Phase 1 extremes.
• Fixed percentage change direction — Change values in labels are now calculated left-to-right (chronologically), showing the move from the previous pivot to the current one.
• Lookback bars now works correctly — Added max_bars_back=5000 to the indicator declaration. Previously Pine's default buffer limit prevented lookback values above ~500 from having any effect.
ملاحظات الأخبار
Updated Reverse ZigZag to improve reliability and live-chart behavior. This release fixes line/label accumulation, adds timeframe-based deviation presets, and corrects Confirmed Pivots Only so it now hides the current unconfirmed right-edge leg as intended. It also improves label consistency and keeps rendering within TradingView object limits on dense charts.
ملاحظات الأخبار
Improved right-edge pivot handling in Reverse ZigZag. This update refines how the newest developing leg is detected, makes it respect Min Leg Bars, and improves Confirmed Pivots Only so the newest pivot is removed when it has not yet been confirmed by a sufficient counter-move.
ملاحظات الأخبار
Renamed Confirmed Pivots Only to Show Completed Legs Only for clearer semantics. The old name implied traditional confirmation logic, while the indicator actually uses reverse-resolved leg logic. The setting now reads in a way that better matches its real behavior: hiding the active right-edge extension and showing only completed legs.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.