This is same as existing zigzag library with respect to functionality. But, there is a small update with respect to how arrays are used internally. This also leads to issues with backward compatibility. Hence I decided to make this as new library instead of updating the older one.

Below are the major changes:

  • Earlier version uses array.unshift for adding new elements and array.pop for removing old elements. But, since array.unshift is considerably slower than alternative method array.push. Hence, this library makes use of array.push method to achieve performance.
  • While array.push increases the performance significantly, there is also an issue with removing as we no longer will be able to remove the element using pop which is again faster than shift (which need to shit all the elements by index). Hence, have removed the logic of removing elements for zigzag pivots after certain limit. Will think further about it once I find better alternative of handling it.
  • These implementation change also mean that zigzag pivots received by calling method will be ordered in reverse direction. Latest pivots will be stored with higher array index whereas older pivots are stored with lower array index. This is also the reason why backward compatibility is not achievable with this code change.


Library "zigzagplus"
Library dedicated to zigzags and related indicators

zigzag(length, useAlternativeSource, source, oscillatorSource, directionBias) zigzag: Calculates zigzag pivots and generates an array
  Parameters:
    length: : Zigzag Length
    useAlternativeSource: : If set uses the source for genrating zigzag . Default is false
    source: : Alternative source used only if useAlternativeSource is set to true. Default is close
    oscillatorSource: : Oscillator source for calculating divergence
    directionBias: : Direction bias for calculating divergence
  Returns: zigzagpivots : Array containing zigzag pivots
zigzagpivotbars : Array containing zigzag pivot bars
zigzagpivotdirs : Array containing zigzag pivot directions (Lower High : 1, Higher High : 2, Lower Low : -2 and Higher Low : -1)
zigzagpivotratios : Array containing zigzag retracement ratios for each pivot
zigzagoscillators : Array of oscillator values at pivots . Will have valid values only if valid oscillatorSource is provided as per input.
zigzagoscillatordirs: Array of oscillator directions (HH, HL, LH, LL) at pivots . Will have valid values only if valid oscillatorSource is provided as per input.
zigzagtrendbias : Array of trend bias at pivots . Will have valid value only if directionBias series is sent in input parameters
zigzagdivergence : Array of divergence sentiment at each pivot . Will have valid values only if oscillatorSource and directionBias inputs are provided
newPivot : Returns true if new pivot created
doublePivot : Returns true if two new pivots are created on same bar (Happens in case of candles with long wicks and shorter zigzag lengths)

drawzigzag(length, , source, linecolor, linewidth, linestyle, oscillatorSource, directionBias, showHighLow, showRatios, showDivergence) drawzigzag: Calculates and draws zigzag pivots
  Parameters:
    length: : Zigzag Length
    : useAlternativeSource: If set uses the source for genrating zigzag . Default is false
    source: : Alternative source used only if useAlternativeSource is set to true. Default is close
    linecolor: : zigzag line color
    linewidth: : zigzag line width
    linestyle: : zigzag line style
    oscillatorSource: : Oscillator source for calculating divergence
    directionBias: : Direction bias for calculating divergence
    showHighLow: : show highlow label
    showRatios: : show retracement ratios
    showDivergence: : Show divergence on label (Only works if divergence data is available - that is if we pass valid oscillatorSource and directionBias input)
  Returns: zigzagpivots : Array containing zigzag pivots
zigzagpivotbars : Array containing zigzag pivot bars
zigzagpivotdirs : Array containing zigzag pivot directions (Lower High : 1, Higher High : 2, Lower Low : -2 and Higher Low : -1)
zigzagpivotratios : Array containing zigzag retracement ratios for each pivot
zigzagoscillators : Array of oscillator values at pivots . Will have valid values only if valid oscillatorSource is provided as per input.
zigzagoscillatordirs: Array of oscillator directions (HH, HL, LH, LL) at pivots . Will have valid values only if valid oscillatorSource is provided as per input.
zigzagtrendbias : Array of trend bias at pivots . Will have valid value only if directionBias series is sent in input parameters
zigzagdivergence : Array of divergence sentiment at each pivot . Will have valid values only if oscillatorSource and directionBias inputs are provided
zigzaglines : Returns array of zigzag lines
zigzaglabels : Returns array of zigzag labels
Release Notes: Added numberOfPivots back and using remove to remove the older elements instead of shift as this is found to be yielding better performance over testing.

Request trial access: https://www.trendoscope.com.au/contact-us
Reach me on telegram: https://t.me/HeWhoMustNotBeNaamed
Join tradingview: https://www.tradingview.com/gopro/?share_your_love=sudh
USDT (TRC20): TCaz4CG9aZyR4jp3Ygf7MESWJPmcJgoep5
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.

Disclaimer

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.