Implementing a ZigZag Indicator Using Highs and Lows in ProBuilder

19 Feb 2017
0 comment
0 attachment

This code snippet demonstrates how to implement a ZigZag indicator in ProBuilder, focusing on using high and low prices instead of closing prices. The ZigZag indicator is useful for identifying price trends by filtering out minor fluctuations and focusing on significant changes.


DEFPARAM DRAWONLASTBARONLY=TRUE

//Variables:
//Points=20 //Pips
//Perc=0.25 //%
//Type=0 //0=pips; 1=perc
//Segments=10 //n. segments up/dw to show

PI=max(0,(Segments-1)) //points top/low back to store

VarY=CustomClose //close,tpicalprice, etc

//inizializzo primo punto come TOP
once LastPoint = 1
once $TX[0] = barindex
once $TY[0] = High

//Tipo ZZ in Punti o Percentuale
If Type=0 then //ZZ in pips
    DeltaY=max(2*pipsize,Points*pipsize)
elsif Type=1 then //ZZ in %
    DeltaY=max(0.001,Perc/100*Dclose(1)*pipsize) //Dclose used as ref.
endif

//ZZ in fase 1
if LastPoint=1 then //lastpoint was a top
    if High>$TY[0] then //update last top and stay in
        LastPoint=1
        $TY[0]=High
        $TX[0]=barindex
    elsif Low<=($TY[0]-DeltaY) then //first point in low
        for i=PI downto 0 do //shift memory previous points
            $LX[i+1]=$LX[i]
            $LY[i+1]=$LY[i]
        next
        $LY[0]=Low //store new low and change lastpoint
        $LX[0]=barindex
        LastPoint=-1
    endif
endif

//ZZ in fase -1
if LastPoint=-1 then //lastpoint was a low
    if Low<$LY[0] then //update last low and stay in
        LastPoint=-1
        $LY[0]=Low
        $LX[0]=barindex
    elsif High>=($LY[0]+DeltaY) then //first point in top
        for i=PI downto 0 do //shift memory previous points
            $TX[i+1]=$TX[i]
            $TY[i+1]=$TY[i]
        next
        $TY[0]=High //store new top and change lastpoint
        $TX[0]=barindex
        LastPoint=1
    endif
endif

//---GRAPH---
If LastPoint=1 then
    drawsegment (barindex,Low,$TX[0],$TY[0]) style (dottedline,2) COLOURED (0,0,200) //segment in progress
    for i=0 to PI do //final segments
        drawsegment ($TX[i],$TY[i],$LX[i],$LY[i]) style (line,2) COLOURED (0,0,200)
        drawsegment ($LX[i],$LY[i],$TX[i+1],$TY[i+1]) style (line,2) COLOURED (0,0,200)
    next
endif
If LastPoint=-1 then
    drawsegment (barindex,High,$LX[0],$LY[0]) style (dottedline,2) COLOURED (0,0,200) //segment in progress
    for i=0 to PI do //final segments
        drawsegment ($LX[i],$LY[i],$TX[i],$TY[i]) style (line,2) COLOURED (0,0,200)
        drawsegment ($TX[i],$TY[i],$LX[i+1],$LY[i+1]) style (line,2) COLOURED (0,0,200)
    next
endif
return

The ZigZag indicator is constructed by alternating between tracking highs and lows based on a defined deviation amount, which can be set in pips or percentage. The indicator updates its points only when a significant reversal is detected.

  • Initialization: Sets up initial parameters and defines whether the first significant point is a high or low.
  • Directional Phases: The code handles two scenarios – when the last point was a high (phase 1) and when it was a low (phase -1). It updates the current high or low if the new price extends the current trend. If a reversal is detected (price moves enough in the opposite direction), it stores this new point and switches phases.
  • Graphical Representation: Draws segments between significant points. Ongoing segments are dotted, indicating that they are still in formation, while completed segments are solid.

This example helps in understanding how to manipulate arrays and conditional logic in ProBuilder to track and visualize significant price movements effectively.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/zigzag-indicator-using-highs-and-lows/#post-220573

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.
LaMaille Junior
I usually let my code do the talking, which explains why my bio is as empty as a newly created file. Bio to be initialized...
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...