ZigZag Library [TradingFinder]


The "Zig Zag" indicator is an analytical tool that emerges from pricing changes. Essentially, it connects consecutive high and low points in an oscillatory manner. This method helps decipher price changes and can also be useful in identifying traditional patterns.

By sifting through partial price changes, "Zig Zag" can effectively pinpoint price fluctuations within defined time intervals.

🔵Key Features

1.Drawing the Zig Zag based on Pivot points:

The algorithm is based on pivots that operate consecutively and alternately (switch between high and low swing). In this way, zigzag lines are connected from a swing high to a swing low and from a swing low to a swing high.

Also, with a very low probability, it is possible to have both low pivots and high pivots in one candle. In these cases, the algorithm tries to make the best decision to make the most suitable choice.

You can control what period these decisions are based on through the "PiPe" parameter.

2.Naming and labeling each pivot based on its position as "Higher High" (HH), "Lower Low" (LL), "Higher Low" (HL), and "Lower High" (LH).

Additionally, classic patterns such as HH, LH, LL, and HL can be recognized. All traders analyzing financial markets using classic patterns and Elliot Waves can benefit from the "zigzag" indicator to facilitate their analysis.

"HH": When the price is higher than the previous peak (Higher High).
"HL": When the price is higher than the previous low (Higher Low).
"LH": When the price is lower than the previous peak (Lower High).
"LL": When the price is lower than the previous low (Lower Low).

🔵How to Use

First, you can add the library to your code as shown in the example below.
import TFlab/ZigZagLibrary_TradingFinder/1 as ZZ

Function "ZigZag" Parameters:

🟣Logical Parameters

1.HIGH: You should place the "high" value here. High is a float variable.

2.LOW: You should place the "low" value here. Low is a float variable.

3.BAR_INDEX: You should place the "bar_index" value here. Bar_index is an integer variable.

4.PiPe: The desired pivot period for plotting Zig Zag is placed in this parameter. For example, if you intend to draw a Zig Zag with a Swing Period of 5, you should input 5.
PiPe is an integer variable.


Apart from the "PiPe" indicator, which is part of the customization capabilities of this indicator, you can create a multi-time frame mode for the indicator using 3 parameters "High", "Low" and "BAR_INDEX". In this way, instead of the data of the current time frame, use the data of other time frames.

Note that it is better to use the current time frame data, because using the multi-time frame mode is associated with challenges that may cause bugs in your code.

🟣Setting Parameters

5.SHOW_LINE: It's a boolean variable. When true, the Zig Zag line is displayed, and when false, the Zig Zag line display is disabled.

6.STYLE_LINE: In this variable, you can determine the style of the Zig Zag line. You can input one of the 3 options: line.style_solid, line.style_dotted, line.style_dashed. STYLE_LINE is a constant string variable.

7.COLOR_LINE: This variable takes the input of the line color.

8.WIDTH_LINE: The input for this variable is a number from 1 to 3, which is used to adjust the thickness of the line that draws the Zig Zag. WIDTH_LINE is an integer variable.

9.SHOW_LABEL: It's a boolean variable. When true, labels are displayed, and when false, label display is disabled.

10.COLOR_LABEL: The color of the labels is set in this variable.

11.SIZE_LABEL: The size of the labels is set in this variable. You should input one of the following options:, size.tiny, size.small, size.normal, size.large, size.huge.

12.Show_Support: It's a boolean variable that, when true, plots the last support line, and when false, disables its plotting.

13.Show_Resistance: It's a boolean variable that, when true, plots the last resistance line, and when false, disables its plotting.

You can use the following code snippet to import Zig Zag into your code for time efficiency.

//import Library
import TFlab/ZigZagLibrary_TradingFinder/1 as ZZ

// Input and Setting
// Zig Zag Line
ShZ = input.bool(true , 'Show Zig Zag Line', group = 'Zig Zag') //Show Zig Zag

PPZ = ,'Pivot Period Zig Zag Line' , group = 'Zig Zag') //Pivot Period Zig Zag

ZLS = input.string(line.style_dashed , 'Zig Zag Line Style' , options = [line.style_solid ,line.style_dotted , line.style_dashed], group = 'Zig Zag' )

//Zig Zag Line Style
ZLC = input.color(color.rgb(0, 0, 0) , 'Zig Zag Line Color' , group = 'Zig Zag')  //Zig Zag Line Color
ZLW = , 'Zig Zag Line Width' , group = 'Zig Zag')//Zig Zag Line Width 

// Label
ShL = input.bool(true , 'Label', group = 'Label') //Show Label 
LC =  input.color(color.rgb(0, 0, 0) , 'Label Color' , group = 'Label')//Label Color
LS =  input.string(size.tiny , 'Label size' , options = [, size.tiny, size.small, size.normal, size.large, size.huge], group = 'Label' )//Label size

Show_Support= input.bool(false, 'Show Last Support',
 tooltip = 'Last Support' , group = 'Support and Resistance')
Show_Resistance = input.bool(false, 'Show Last Resistance',
 tooltip = 'Last Resistance' , group = 'Support and Resistance')

//Call Function
ZZ.ZigZag(high ,low ,bar_index ,PPZ , ShZ ,ZLS , ZLC, ZLW ,ShL , LC , LS , Show_Support , Show_Resistance )

Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House Rules.


The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this library?

Copy the following line and paste it in your script.