RicardoSantos

[RS]Simple ZigZag V3

EXPERIMENTAL:
optional:
custom timeframe
custom fib ab_extension/bc_retrace zone

Display base zigzag
Display zigzag
Display Tops and Bottoms Lines
Display Fib. Ret./Ext.
Display Pattern Recognition
Remove from Favorite Scripts Add to Favorite Scripts
study("[RS]Simple ZigZag V3", overlay=true)
TF = input('240')
ab_extension = input(0.618)
bc_retrace = input(0.382)
showBaseZigZag = input(true)
showZigZag = input(true)
showChannel = input(true)
showZones = input(true)
showPatterns = input(true)

f_zigzag(_TF)=>
    _hh = security(tickerid, _TF, high)
    _ll = security(tickerid, _TF, low)
    _output = high >= _hh ? high : low <= _ll ? low : na
f_tops(_TF)=>
    _hh = security(tickerid, _TF, high)
    _ll = security(tickerid, _TF, low)
    _output = high >= _hh ? high : na
f_bots(_TF)=>
    _hh = security(tickerid, _TF, high)
    _ll = security(tickerid, _TF, low)
    _output = low <= _ll ? low : na

//tops = f_tops(TF)
//bots = f_bots(TF)
zigzag = f_zigzag(TF)

//plot(tops, color=gray, linewidth=2)
//plot(bots, color=gray, linewidth=2)
plot(not showBaseZigZag ? na : zigzag, color=black, linewidth=3)

f_fix_zigzag(_zigzag)=>
    _hh = _zigzag ? close : high >= nz(_hh[1]) ? highest(2) : nz(_hh[1])
    _ll = _zigzag ? close : low <= nz(_ll[1]) ? lowest(2) : nz(_ll[1])
    _isBhigh = valuewhen(_zigzag, _zigzag >= high, 1)
    _isChigh = valuewhen(_zigzag, _zigzag >= high, 0)
    _isBlow = valuewhen(_zigzag, _zigzag <= low, 1)
    _isClow = valuewhen(_zigzag, _zigzag <= low, 0)
    _output = _zigzag and _isBhigh and _isChigh ? _ll[1] :_zigzag[1] and _isBhigh[1] and _isChigh[1] ? _hh :
        _zigzag and _isBlow and _isClow ? _hh[1] : _zigzag[1] and _isBlow[1] and _isClow[1] ? _ll : _zigzag
fix_zz = f_fix_zigzag(zigzag)
plot(not showZigZag ? na : fix_zz, color=aqua, linewidth=2)

last_zz_a = valuewhen(fix_zz, fix_zz, 2)
last_zz_b = valuewhen(fix_zz, fix_zz, 1)
last_zz_c = valuewhen(fix_zz, fix_zz, 0)

upper = fix_zz and last_zz_c > last_zz_b ? last_zz_c : na
lower = fix_zz and last_zz_c < last_zz_b ? last_zz_c : na

plot(not showChannel ? na : upper, color=gray, linewidth=2)
plot(not showChannel ? na : lower, color=gray, linewidth=2)

ab_dif = abs(last_zz_a-last_zz_b)
bc_dif = abs(last_zz_b-last_zz_c)

fib1 = last_zz_c >= high ? last_zz_c - ab_dif*ab_extension : last_zz_c <= low ? last_zz_c + ab_dif*ab_extension : nz(fib1[1])
fib2 = last_zz_c >= high ? last_zz_c - bc_dif*bc_retrace : last_zz_c <= low ? last_zz_c + bc_dif*bc_retrace : nz(fib2[1])

f1 = plot(not showZones ? na : fib1, color=gray, style=cross, linewidth=1)
f2 = plot(not showZones ? na : fib2, color=silver, style=cross, linewidth=1)
fill(f1, f2, color=silver, transp=75)

//  ||---   Pattern Recognition:

x = valuewhen(fix_zz, fix_zz, 4) 
a = valuewhen(fix_zz, fix_zz, 3) 
b = valuewhen(fix_zz, fix_zz, 2) 
c = valuewhen(fix_zz, fix_zz, 1) 
d = valuewhen(fix_zz, fix_zz, 0)

xab = (abs(b-a)/abs(x-a))
xad = (abs(a-d)/abs(x-a))
abc = (abs(b-c)/abs(a-b))
bcd = (abs(c-d)/abs(b-c))

//  ||-->   Functions:
isBat(_mode)=>
    _xab = xab >= 0.382 and xab <= 0.5
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad <= 0.886
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAltBat(_mode)=>
    _xab = xab <= 0.382
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.0 and bcd <= 3.618
    _xad = xad <= 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isButterfly(_mode)=>
    _xab = xab <= 0.786
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad >= 1.27 and xad <= 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isABCD(_mode)=>
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _abc and _bcd and (_mode == 1 ? d < c : d > c)

isGartley(_mode)=>
    _xab = xab >= 0.5 and xab <= 0.618 // 0.618
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _xad = xad >= 0.75 and xad <= 0.875 // 0.786
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isCrab(_mode)=>
    _xab = xab >= 0.75 and xab <= 0.875 // 0.886
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.0 and bcd <= 3.618
    _xad = xad >= 1.5 and xad <= 1.625 // 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isShark(_mode)=>
    _xab = xab >= 0.5 and xab <= 0.875 // 0.886
    _abc = abc >= 1.13 and abc <= 1.618
    _bcd = bcd >= 1.27 and bcd <= 2.24
    _xad = xad >= 0.88 and xad <= 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

is5o(_mode)=>
    _xab = xab >= 1.13 and xab <= 1.618
    _abc = abc >= 1.618 and abc <= 2.24
    _bcd = bcd >= 0.5 and bcd <= 0.625 // 0.5
    _xad = xad >= 0.0 and xad <= 0.236 // negative?
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isWolf(_mode)=>
    _xab = xab >= 1.27 and xab <= 1.618
    _abc = abc >= 0 and abc <= 5
    _bcd = bcd >= 1.27 and bcd <= 1.618
    _xad = xad >= 0.0 and xad <= 5
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

plotshape(not showPatterns ? na : isABCD(-1) and not isABCD(-1)[1], text="\nAB=CD", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0, offset=-2)
plotshape(not showPatterns ? na : isBat(-1) and not isBat(-1)[1], text="Bat", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0, offset=-2)
plotshape(not showPatterns ? na : isAltBat(-1) and not isAltBat(-1)[1], text="Alt Bat", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isButterfly(-1) and not isButterfly(-1)[1], text="Butterfly", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isGartley(-1) and not isGartley(-1)[1], text="Gartley", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isCrab(-1) and not isCrab(-1)[1], text="Crab", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isShark(-1) and not isShark(-1)[1], text="Shark", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : is5o(-1) and not is5o(-1)[1], text="5-O", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isWolf(-1) and not isWolf(-1)[1], text="Wolf Wave", style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)

plotshape(not showPatterns ? na : isABCD(1) and not isABCD(1)[1], text="AB=CD\n", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isBat(1) and not isBat(1)[1], text="Bat", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAltBat(1) and not isAltBat(1)[1], text="Alt Bat", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isButterfly(1) and not isButterfly(1)[1], text="Butterfly", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isGartley(1) and not isGartley(1)[1], text="Gartley", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isCrab(1) and not isCrab(1)[1], text="Crab", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isShark(1) and not isShark(1)[1], text="Shark", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : is5o(1) and not is5o(1)[1], text="5-O", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isWolf(1) and not isWolf(1)[1], text="Wolf Wave", style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)