OPEN-SOURCE SCRIPT
Cody Zig Zag

Cody Zig Zag Indicator - Complete Explanation
This is a Zig Zag indicator that replicates the MetaTrader 4 (MT4) version, which works differently than TradingView's built-in Zig Zag. Here's what it does:
🎯 What is a Zig Zag Indicator?
It filters out minor price movements to show only significant swing highs and swing lows, helping you identify trends and chart patterns (head & shoulders, double tops/bottoms, etc.).
⚙️ User Settings (Inputs)
Setting Default What It Does
Depth 6 Lookback period to find local highs/lows
Deviation 5 Minimum price change (in ticks) to form a new swing point
Backstep 2 Minimum bars between swing points
Line Thickness 2 Zig Zag line width
Bull Color Lime Color for upward moves
Bear Color Red Color for downward moves
Repaint Levels true If ON, lines/labels adjust real-time; if OFF, they lock on close
🔍 How It Works (The "Bake" Section)
Step 1: Find Potential Pivot Points
p_lw = Most recent low within Depth bars
p_hg = Most recent high within Depth bars
Checks if price moved enough (Deviation * tick size) to qualify
Step 2: Determine Direction
down = Boolean (true = bearish/downward leg, false = bullish/upward leg)
Tracks whether we're connecting a high→low (down) or low→high (up)
Step 3: Draw Zig Zag Lines
Connects alternating swing highs and swing lows
Labels each point as:
HH = Higher High
LH = Lower High
LL = Lower Low
HL = Higher Low
🏷️ Labels Explained
When price makes a new swing point, you'll see:
HH (Higher High) - Bullish continuation signal
LH (Lower High) - Bearish reversal signal
LL (Lower Low) - Bearish continuation signal
HL (Higher Low) - Bullish reversal signal
📊 Visual Features
Colored Zig Zag lines: Green for up-legs, Red for down-legs
Background color: Green during bullish legs, Red during bearish legs (90% transparent)
Labels: Tiny triangles pointing up/down with text
⚠️ Repaint Behavior (Important!)
Repaint = ON (default) Repaint = OFF
Lines/labels update in real-time Locks on bar close
Can "repaint" history More reliable for backtesting
Good for real-time trading No historical repainting
Warning: Zig Zag indicators naturally repaint because they need future bars to confirm pivot points. The repaint option controls HOW they repaint.
🚨 Alerts
This script triggers alerts when:
Direction Changed - Zig Zag switches from up to down or down to up
Bullish Direction - Switches from down to up
Bearish Direction - Switches from up to down
📈 How to Use
Identify Trend: Upward Zig Zag = Uptrend; Downward = Downtrend
Find Patterns: Look for HH/HL patterns (bullish) or LH/LL patterns (bearish)
Divergence: Compare Zig Zag swings with oscillator indicators
Support/Resistance: Swing points act as natural S/R levels
⚡ Quick Summary
This indicator draws a line connecting significant price swings, ignoring small noise. It's great for:
Visualizing trend structure
Identifying chart patterns
Finding entry/exit levels
Spotting trend reversals
This is a Zig Zag indicator that replicates the MetaTrader 4 (MT4) version, which works differently than TradingView's built-in Zig Zag. Here's what it does:
🎯 What is a Zig Zag Indicator?
It filters out minor price movements to show only significant swing highs and swing lows, helping you identify trends and chart patterns (head & shoulders, double tops/bottoms, etc.).
⚙️ User Settings (Inputs)
Setting Default What It Does
Depth 6 Lookback period to find local highs/lows
Deviation 5 Minimum price change (in ticks) to form a new swing point
Backstep 2 Minimum bars between swing points
Line Thickness 2 Zig Zag line width
Bull Color Lime Color for upward moves
Bear Color Red Color for downward moves
Repaint Levels true If ON, lines/labels adjust real-time; if OFF, they lock on close
🔍 How It Works (The "Bake" Section)
Step 1: Find Potential Pivot Points
p_lw = Most recent low within Depth bars
p_hg = Most recent high within Depth bars
Checks if price moved enough (Deviation * tick size) to qualify
Step 2: Determine Direction
down = Boolean (true = bearish/downward leg, false = bullish/upward leg)
Tracks whether we're connecting a high→low (down) or low→high (up)
Step 3: Draw Zig Zag Lines
Connects alternating swing highs and swing lows
Labels each point as:
HH = Higher High
LH = Lower High
LL = Lower Low
HL = Higher Low
🏷️ Labels Explained
When price makes a new swing point, you'll see:
HH (Higher High) - Bullish continuation signal
LH (Lower High) - Bearish reversal signal
LL (Lower Low) - Bearish continuation signal
HL (Higher Low) - Bullish reversal signal
📊 Visual Features
Colored Zig Zag lines: Green for up-legs, Red for down-legs
Background color: Green during bullish legs, Red during bearish legs (90% transparent)
Labels: Tiny triangles pointing up/down with text
⚠️ Repaint Behavior (Important!)
Repaint = ON (default) Repaint = OFF
Lines/labels update in real-time Locks on bar close
Can "repaint" history More reliable for backtesting
Good for real-time trading No historical repainting
Warning: Zig Zag indicators naturally repaint because they need future bars to confirm pivot points. The repaint option controls HOW they repaint.
🚨 Alerts
This script triggers alerts when:
Direction Changed - Zig Zag switches from up to down or down to up
Bullish Direction - Switches from down to up
Bearish Direction - Switches from up to down
📈 How to Use
Identify Trend: Upward Zig Zag = Uptrend; Downward = Downtrend
Find Patterns: Look for HH/HL patterns (bullish) or LH/LL patterns (bearish)
Divergence: Compare Zig Zag swings with oscillator indicators
Support/Resistance: Swing points act as natural S/R levels
⚡ Quick Summary
This indicator draws a line connecting significant price swings, ignoring small noise. It's great for:
Visualizing trend structure
Identifying chart patterns
Finding entry/exit levels
Spotting trend reversals
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.