Zigzag SAR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mildSnail31034

//@version=4
study("Zigzag SAR",overlay=true)
showsar = input(title="Show Parabolic SAR", type=input.bool, defval=true)
start = input(title="SAR Start", type=input.float, step=0.001,defval=0.02)
increment = input(title="SAR increment", type=input.float, step=0.001,defval=0.02)
maxsar = input(title="SAR max", type=input.float, step=0.01, defval=0.2)
psar = sar(start,increment, maxsar)
dir = psar<close ? true : false
up = dir and dir[1]==false
dn = dir==false and dir[1]
sarcolor = dir ? color.green : color.red
lineupcolor = input(title="Zigzag up color", defval=color.green)
linedncolor = input(title="Zigzag down color", defval=color.red)
linewidth = input(title="Zigzag line width",type=input.integer,minval=1,maxval=4,defval=2)
plotsar = plot(showsar ? psar : na, title= "PSAR", style=plot.style_circles, color=sarcolor, linewidth=2 )
var line zzup = na
var line zzdn = na
var bt1 = 0
var bt2 = 0
var float hi = na
var float lo = na
if up
    hi :=high
    bt1 := time
    zzup := line.new(bt2,lo,bt1,hi,color=lineupcolor, width =linewidth,xloc=xloc.bar_time)
if dn
    lo :=low
    bt2 := time
    zzdn := line.new(bt1,hi,bt2,lo,color=linedncolor, width=linewidth,xloc=xloc.bar_time)
if (dir and high > hi)
    hi :=high
    bt1 := time
    line.set_xy2(zzup,bt1,hi)
    
if (dir==false and low < lo)
    lo := low
    bt2 := time
    line.set_xy2(zzdn,bt2,lo)
