OPEN-SOURCE SCRIPT
Telah dikemas kini Custom ZigZag Indicator

Overview
The Custom ZigZag Indicator is a technical analysis tool built in Pine Script (version 5) for TradingView. It overlays on price charts to visualize market trends by connecting significant swing highs and lows, filtering out minor price noise. This helps identify the overall market direction (uptrends or downtrends), potential reversal points, and key support/resistance levels. Unlike standard price lines, it "zigzags" only between meaningful pivots, making trends clearer.
Core Logic and How It Works
The script uses a state-machine approach to track market direction and pivots:
Initialization
Starts assuming an upward trend on the first bar.
sets initial high/low prices and bar indices based on the current bar's high/low.
Direction Tracking:
Upward Trend (direction = 1):
Monitors for new highs: If the current high exceeds the tracked high, update the high price and bar.
Checks for reversal: If the low drops below the high by the deviation percentage (e.g., high * (1 - 0.05) for 5%), it signals a downtrend reversal.
Draws a green line from the last pivot (low) to the new high.
If labels are enabled, adds a label: "HH" (Higher High if above previous high), "LH" (Lower High if below), or "H" (for the first one).
Updates the last high and switches to downward direction.
Downward Trend (direction = -1):
Monitors for new lows: If the current low is below the tracked low, update the low price and bar.
Checks for reversal: If the high rises above the low by the deviation percentage (e.g., low * (1 + 0.05)), it signals an uptrend reversal.
Draws a red line from the last pivot (high) to the new low.
If labels are enabled, adds a label: "LL" (Lower Low if below previous low), "HL" (Higher Low if above), or "L" (for the first one).
Updates the last low and switches to upward direction.
The Custom ZigZag Indicator is a technical analysis tool built in Pine Script (version 5) for TradingView. It overlays on price charts to visualize market trends by connecting significant swing highs and lows, filtering out minor price noise. This helps identify the overall market direction (uptrends or downtrends), potential reversal points, and key support/resistance levels. Unlike standard price lines, it "zigzags" only between meaningful pivots, making trends clearer.
Core Logic and How It Works
The script uses a state-machine approach to track market direction and pivots:
Initialization
Starts assuming an upward trend on the first bar.
sets initial high/low prices and bar indices based on the current bar's high/low.
Direction Tracking:
Upward Trend (direction = 1):
Monitors for new highs: If the current high exceeds the tracked high, update the high price and bar.
Checks for reversal: If the low drops below the high by the deviation percentage (e.g., high * (1 - 0.05) for 5%), it signals a downtrend reversal.
Draws a green line from the last pivot (low) to the new high.
If labels are enabled, adds a label: "HH" (Higher High if above previous high), "LH" (Lower High if below), or "H" (for the first one).
Updates the last high and switches to downward direction.
Downward Trend (direction = -1):
Monitors for new lows: If the current low is below the tracked low, update the low price and bar.
Checks for reversal: If the high rises above the low by the deviation percentage (e.g., low * (1 + 0.05)), it signals an uptrend reversal.
Draws a red line from the last pivot (high) to the new low.
If labels are enabled, adds a label: "LL" (Lower Low if below previous low), "HL" (Higher Low if above), or "L" (for the first one).
Updates the last low and switches to upward direction.
Nota Keluaran
OverviewThe Custom ZigZag Indicator is a technical analysis tool built in Pine Script (version 5) for TradingView. It overlays on price charts to visualize market trends by connecting significant swing highs and lows, filtering out minor price noise. This helps identify the overall market direction (uptrends or downtrends), potential reversal points, and key support/resistance levels. Unlike standard price lines, it "zigzags" only between meaningful pivots, making trends clearer.
The indicator is dynamic and can "repaint" (update past lines) as new price data arrives, which is common for ZigZag tools since they rely on future confirmation of pivots.
Key Inputs and Customization
Users can adjust settings via TradingView's indicator inputs:
Deviation (%): The core sensitivity parameter (default 5%). It determines how much price must reverse (as a percentage) to form a new pivot. Higher values ignore smaller fluctuations for a smoother, longer-term view; lower values capture more short-term swings.
Up Leg Color: Color for lines showing upward moves (default green).
Down Leg Color: Color for downward moves (default red).
Line Width: Thickness of the zigzag lines (default 2, minimum 1).
Line Style: Choose "Solid" (default), "Dashed", or "Dotted" for line appearance.
Show High/Low Labels: Toggle to display labels at pivots (default true).
These allow full customization of appearance without changing the code.
Core Logic and How It Works
The script uses a state-machine approach to track market direction and pivots:
Initialization:
Starts assuming an upward trend on the first bar.
Sets initial high/low prices and bar indices based on the current bar's high/low.
Direction Tracking:
Upward Trend (direction = 1):
Monitors for new highs: If the current high exceeds the tracked high, update the high price and bar.
Checks for reversal: If the low drops below the high by the deviation percentage (e.g., high * (1 - 0.05) for 5%), it signals a downtrend reversal.
Draws a green line from the last pivot (low) to the new high.
If labels are enabled, adds a label: "HH" (Higher High if above previous high), "LH" (Lower High if below), or "H" (for the first one).
Updates the last high and switches to downward direction.
Downward Trend (direction = -1):
Monitors for new lows: If the current low is below the tracked low, update the low price and bar.
Checks for reversal: If the high rises above the low by the deviation percentage (e.g., low * (1 + 0.05)), it signals an uptrend reversal.
Draws a red line from the last pivot (high) to the new low.
If labels are enabled, adds a label: "LL" (Lower Low if below previous low), "HL" (Higher Low if above), or "L" (for the first one).
Updates the last low and switches to upward direction.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.