Zig Zag Stochastic (ZZS)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Zig Zag Stochastic", "ZZS")
import lastguru/DominantCycle/2 as d

bes(float source = close, float length = 9)=>
    alpha = 2 / (length + 1)
    var float smoothed = na
    smoothed := alpha * source + (1 - alpha) * nz(smoothed[1])

smoothing_length = input.int(14, "Smoothing length", 0, 3000, 1, tooltip = "1 represents no smoothing, 0 represents auto smoothing.")

high_left  = input.int(50, "High Zig Zag Length", 0) 
low_left   = input.int(50, "Low Zig Zag Length", 0)

var float high_scale = na
var float low_scale  = na

pivot_high = ta.pivothigh(high, high_left, 0)
pivot_low  = ta.pivotlow(low, low_left, 0)

high_scale := na(pivot_high) ? nz(high_scale[1]) : pivot_high
low_scale  := na(pivot_low) ? nz(low_scale[1]) : pivot_low

smoothed    = ta.sma(100 * (close - low_scale)/(high_scale - low_scale), smoothing_length < 1 ? 1 : smoothing_length )
scale_price = 100 * (close - low_scale)/(high_scale - low_scale)
k_length    = d.mamaPeriod(scale_price, 2, 4096)
k_line      = bes(scale_price, k_length)
d_length    = d.mamaPeriod(smoothing_length == 0 ? k_line : smoothed, 2, 4096)
d_line      = bes(smoothing_length == 0 ? k_line : smoothed, d_length)

plot(smoothing_length == 0 ? k_line : smoothed, "Zig Zag Stochastic", color.blue)
plot(d_line, "ZZS D Line", color.orange)

top = hline(80, linestyle = hline.style_solid)
hline(50)
bot = hline(20, linestyle = hline.style_solid)

fill(top, bot, top_color = color.new(color.navy, 90), top_value = 80, bottom_value = 20, bottom_color = color.new(color.purple, 90))