Hull ZigZag
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © djsnowman06

//@version=5
indicator("Hull ZigZag", overlay=true, max_bars_back=250)

src = input.source(close, "Source")
len = input.int(9, "Hull Length", minval=1)
disp = input.bool(false, "Display Hull MA")
tog = input.bool(true, "Extend Latest Trendline")

out = ta.hma(src, len)

pivot = ((out[1] <= out)and(out[1] < out[2])) or ((out[1] >= out)and(out[1] > out[2]))
ZZ = pivot ? out[1] : na

exTog = tog ? extend.right : extend.none

var extend = line.new(x1=bar_index[10], y1=close[10],
     x2=bar_index, y2=close, color=color.gray, width=3)
 
var extend2 = line.new(x1=bar_index[10], y1=close[10],
     x2=bar_index, y2=close, color=color.gray, width=1, 
     extend=exTog, style=line.style_dashed )

var extend3 = line.new(x1=bar_index[10], y1=close[10],
     x2=bar_index, y2=close, color=color.gray, width=1, 
     extend=exTog, style=line.style_dashed )

x1 = ta.barssince(pivot) + 1

col = out > out[1] ? #00FF00 : #FF0000
line.set_color(id=extend, color=col)

line.set_xy1(id=extend, x=bar_index[x1], y=out[x1])
line.set_xy2(id=extend, x=bar_index, y=out)

line.set_color(id=extend3, color=col)

line.set_xy1(id=extend2, x=bar_index[x1], y=out[x1])
line.set_xy2(id=extend2, x=bar_index, y=out)

line.set_xy1(id=extend3, x=bar_index[1], y=out[1])
line.set_xy2(id=extend3, x=bar_index, y=out)

dEna = disp ? #0000FF : na

plot(out, title="Hull Ma", color=dEna, display=display.all - display.price_scale)
plot(ZZ, title="Zig Zag", color=color.gray, linewidth=3, offset=-1) 

//