Implementing a Zigzag Indicator Using Donchian Channels in ProBuilder

14 Mar 2021
0 comment
0 attachment

This code snippet demonstrates how to create a Zigzag indicator based on the Donchian channel values in ProBuilder. The Zigzag indicator is used to filter out market noise and identify significant trends and changes in market prices.


defparam drawonlastbaronly=true
// --- period = 40 // --------------
du = DonchianChannelUp[period]
dd = DonchianChannelDown[period]
mid = DonchianChannelCenter[period]

if high > du then
    ihigh = high
    ihighbar = barindex
endif

if low < dd then
    ilow = low
    ilowbar = barindex
endif

if close crosses over mid then
    $zz[zz] = ilow
    $zzbar[zz] = ilowbar
    $zzdir[zz] = -1
    zz=zz+1
endif

if close crosses under mid then
    $zz[zz] = ihigh
    $zzbar[zz] = ihighbar
    $zzdir[zz] = 1
    zz=zz+1
endif

if islastbarupdate then
    for i = zz-1 downto 0 do
        drawsegment($zzbar[i],$zz[i],$zzbar[i+1],$zz[i+1])
    next
endif

return //du,dd

The code snippet above is structured to calculate and plot a Zigzag indicator using the upper, lower, and center bands of the Donchian channel. Here's a step-by-step explanation:

  • Initialization: The drawonlastbaronly parameter is set to true, ensuring that drawing commands are executed only on the last bar update to optimize performance.
  • Donchian Channel Calculation: The variables du, dd, and mid are assigned the upper, lower, and center values of the Donchian channel, respectively, for the specified period.
  • High and Low Detection: The script checks if the current high is greater than the upper Donchian band (du) or if the current low is less than the lower Donchian band (dd). If true, it records the bar index and the high/low values.
  • Zigzag Calculation: The Zigzag points are determined based on the crossing of the close price over or under the mid Donchian band. If the close crosses over, a downward Zigzag point is recorded; if it crosses under, an upward Zigzag point is noted.
  • Plotting: If it's the last bar update, the script draws segments between recorded Zigzag points using the drawsegment function, visually representing the Zigzag on the chart.

This implementation helps in visualizing significant price movements, filtering out smaller price changes, and potentially identifying trend reversals.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/zigzag-loi-de-dow/#post-193423

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...