public class swinghighlow {
//@version=4
//
// @author batuhantastekin
//
//study(“Higher High Lower Low Strategy by batuhantastekin”, overlay=true)
//lb = input(2, title=”Left Bars”, minval=1)
//rb = input(2, title=”Right Bars”, minval=1)
//showsupres = input(true, title=”Show Support/Resistance”)
//changebarcol = input(true, title=”Change Bar Color”)
//mb = lb + rb + 1
pivothigh = iff(not na(high[mb]), iff(highestbars(high, mb) == -lb, high[lb], na), na) // Pivot High
pivotlow = iff(not na(low[mb]), iff(lowestbars(low, mb) == -lb, low[lb], na), na) // Pivot Low
hl = int(na)
hl := iff(pivothigh, 1, iff(pivotlow, -1, na)) // Trend direction
zz = float(na)
zz := iff(pivothigh, pivothigh, iff(pivotlow, pivotlow, na)) // similar to zigzag but may have multiple highs/lows
zz := iff(pivotlow and hl == -1 and valuewhen(hl, hl, 1) == -1 and pivotlow > valuewhen(zz, zz, 1), na, zz)
zz := iff(pivothigh and hl == 1 and valuewhen(hl, hl, 1) == 1 and pivothigh < valuewhen(zz, zz, 1), na, zz) hl := iff(hl == -1 and valuewhen(hl, hl, 1) == 1 and zz > valuewhen(zz, zz, 1), na, hl)
hl := iff(hl == 1 and valuewhen(hl, hl, 1) == -1 and zz < valuewhen(zz, zz, 1), na, hl) zz := iff(na(hl), na, zz) findprevious() => // finds previous three points (b, c, d, e)
ehl = iff(hl == 1, -1, 1)
loc1 = 0.0
loc2 = 0.0
loc3 = 0.0
loc4 = 0.0
xx = 0
for x = 1 to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc1 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc2 := zz[x]
xx := x + 1
break
ehl := iff(hl == 1, -1, 1)
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc3 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc4 := zz[x]
break
[loc1, loc2, loc3, loc4]
a = float(na)
b = float(na)
c = float(na)
d = float(na)
e = float(na)
if not na(hl)
[loc1, loc2, loc3, loc4] = findprevious()
a := zz
b := loc1
c := loc2
d := loc3
e := loc4
e
_hh = zz and a > b and a > c and c > b and c > d
_ll = zz and a < b and a < c and c < b and c < d _hl = zz and (a >= c and b > c and b > d and d > c and d > e or a < b and a > c and b < d) _lh = zz and (a <= c and b < c and b < d and d < c and d < e or a > b and a < c and b > d)
//plotshape(_hl, text=”HL”, title=”Higher Low”, style=shape.labelup, color=color.lime, textcolor=color.black, location=location.belowbar, transp=0, offset=-lb)
//plotshape(_hh, text=”HH”, title=”Higher High”, style=shape.labeldown, color=color.lime, textcolor=color.black, location=location.abovebar, transp=0, offset=-lb)
//plotshape(_ll, text=”LL”, title=”Lower Low”, style=shape.labelup, color=color.red, textcolor=color.white, location=location.belowbar, transp=0, offset=-lb)
//plotshape(_lh, text=”LH”, title=”Lower High”, style=shape.labeldown, color=color.red, textcolor=color.white, location=location.abovebar, transp=0, offset=-lb)
res = float(na)
sup = float(na)
res := iff(_lh, zz, res[1])
sup := iff(_hl, zz, sup[1])
trend = int(na)
trend := iff(close > res, 1, iff(close < sup, -1, nz(trend[1]))) res := iff(trend == 1 and _hh or trend == -1 and _lh, zz, res) sup := iff(trend == 1 and _hl or trend == -1 and _ll, zz, sup) //plot(showsupres ? res : na, title="Resistance", color=na(res) ? na : color.red, linewidth=2, style=plot.style_circles, offset=-lb) //plot(showsupres ? sup : na, title="Support", color=na(sup) ? na : color.blue, linewidth=2, style=plot.style_circles, offset=-lb) //barcolor(color=iff(changebarcol, iff(trend == 1, color.blue, color.black), na)) }