#// Swing Bands
#// Free for use. Header credits must be included when any form of the code included in this package is used.
#// Any indicator built on this indicator needs to attribute the original author's work
#// v1.0 - barbaros
#indicator("Swing Bands", overlay = true)
# Converted and Mod by Sam4Cok@Samer800 - 12/2022 - Not Typical
declare upper;
input BarColor = yes;
input ShowBand = yes;
input useChartTime = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input pvtLenL = 7; # "Pivot Length Left Hand Side"
input pvtLenR = 7; # "Pivot Length Right Hand Side"
input maxcnt = 5; # "Max history")
#def h = high; def c = close; def l = low; def o = open;
def h; def c; def l; def o;
def Max = if maxcnt>10 then 10 else
if maxcnt<2 then 2 else maxcnt;
if useChartTime {
h = high;
c = close;
l = low;
o = open;
} else {
h = high(Period=Aggregation);
c = close(Period=Aggregation);
l = low(Period=Aggregation);
o = open(Period=Aggregation);
}
def na = Double.NaN; # non-numeric values
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def pvthi = findpivots(H, 1, pvtLenL, pvtLenR);
def pvtlo = findpivots(L, -1, pvtLenL, pvtLenR);
def highs1;def highs2;def highs3;def highs4;def highs5;def highs6;def highs7;def highs8;def highs9;def highs10;
def lows1;def lows2;def lows3;def lows4;def lows5;def lows6;def lows7;def lows8;def lows9;def lows10;
if !isNaN(pvthi) {
highs1 = pvthi;
highs2 = highs1[1];
highs3 = highs2[1];
highs4 = highs3[1];
highs5 = highs4[1];
highs6 = highs5[1];
highs7 = highs6[1];
highs8 = highs7[1];
highs9 = highs8[1];
highs10 = highs9[1];
} else {
highs1 = if isNaN(highs1[1]) or highs1[1]== 0 then Highest(h,pvtLenL + 1) else highs1[1];
highs2 = if isNaN(highs2[1]) or highs2[1]== 0 then highs1[1] else highs2[1];
highs3 = if isNaN(highs3[1]) or highs3[1]== 0 then highs2[1] else highs3[1];
highs4 = if isNaN(highs4[1]) or highs4[1]== 0 then highs3[1] else highs4[1];
highs5 = if isNaN(highs5[1]) or highs5[1]== 0 then highs4[1] else highs5[1];
highs6 = if isNaN(highs6[1]) or highs6[1]== 0 then highs5[1] else highs6[1];
highs7 = if isNaN(highs7[1]) or highs7[1]== 0 then highs6[1] else highs7[1];
highs8 = if isNaN(highs8[1]) or highs8[1]== 0 then highs7[1] else highs8[1];
highs9 = if isNaN(highs9[1]) or highs9[1]== 0 then highs8[1] else highs9[1];
highs10 = if isNaN(highs10[1]) or highs10[1]== 0 then highs9[1] else highs10[1];
}
if !isNaN(pvtlo) {
lows1 = pvtlo;
lows2 = lows1[1];
lows3 = lows2[1];
lows4 = lows3[1];
lows5 = lows4[1];
lows6 = lows5[1];
lows7 = lows6[1];
lows8 = lows7[1];
lows9 = lows8[1];
lows10 = lows9[1];
} else {
lows1 = if isNaN(lows1[1]) or lows1[1]== 0 then Lowest(l,pvtLenL + 1) else lows1[1];
lows2 = if isNaN(lows2[1]) or lows2[1]== 0 then lows1[1] else lows2[1];
lows3 = if isNaN(lows3[1]) or lows3[1]== 0 then lows2[1] else lows3[1];
lows4 = if isNaN(lows4[1]) or lows4[1]== 0 then lows3[1] else lows4[1];
lows5 = if isNaN(lows5[1]) or lows5[1]== 0 then lows4[1] else lows5[1];
lows6 = if isNaN(lows6[1]) or lows6[1]== 0 then lows5[1] else lows6[1];
lows7 = if isNaN(lows7[1]) or lows7[1]== 0 then lows6[1] else lows7[1];
lows8 = if isNaN(lows8[1]) or lows8[1]== 0 then lows7[1] else lows8[1];
lows9 = if isNaN(lows9[1]) or lows9[1]== 0 then lows8[1] else lows9[1];
lows10 = if isNaN(lows10[1]) or lows10[1]== 0 then lows9[1] else lows10[1];
}
def HiPvtNo = if maxcnt>9 then
highs1 + highs2 + highs3 + highs4 + highs5 + highs6 + highs7 + highs8 + highs9 + highs10 else
if maxcnt>8 then
highs1 + highs2 + highs3 + highs4 + highs5 + highs6 + highs7 + highs8 + highs9 else
if maxcnt>7 then
highs1 + highs2 + highs3 + highs4 + highs5 + highs6 + highs7 + highs8 else
if maxcnt>6 then
highs1 + highs2 + highs3 + highs4 + highs5 + highs6 + highs7 else
if maxcnt>5 then
highs1 + highs2 + highs3 + highs4 + highs5 + highs6 else
if maxcnt>4 then
highs1 + highs2 + highs3 + highs4 + highs5 else
if maxcnt>3 then
highs1 + highs2 + highs3 + highs4 else
if maxcnt>2 then
highs1 + highs2 + highs3 else highs1 + highs2;
def LoPvtNo = if maxcnt>9 then
lows1 + lows2 + lows3 + lows4 + lows5 + lows6 + lows7 + lows8 + lows9 + lows10 else
if maxcnt>8 then
lows1 + lows2 + lows3 + lows4 + lows5 + lows6 + lows7 + lows8 + lows9 else
if maxcnt>7 then
lows1 + lows2 + lows3 + lows4 + lows5 + lows6 + lows7 + lows8 else
if maxcnt>6 then
lows1 + lows2 + lows3 + lows4 + lows5 + lows6 + lows7 else
if maxcnt>5 then
lows1 + lows2 + lows3 + lows4 + lows5 + lows6 else
if maxcnt>4 then
lows1 + lows2 + lows3 + lows4 + lows5 else
if maxcnt>3 then
lows1 + lows2 + lows3 + lows4 else
if maxcnt>2 then
lows1 + lows2 + lows3 else lows1 + lows2;
def hh = HiPvtNo / Max;
def ll = LoPvtNo / Max;
def mid = (hh + ll) / 2;
def dir = if c > hh and c > o then 2 else
if c > mid then 1 else
if c < ll and c < o then -2 else
if c < mid then -1 else dir[1];
#--- Plots
plot avghighs = if isNaN(c) or !ShowBand then na else hh[pvtLenR];
plot avglows = if isNaN(c) or !ShowBand then na else ll[pvtLenR];
plot midpoint = if isNaN(c) then na else mid[pvtLenR];
avghighs.SetDefaultColor(Color.DARK_GREEN);
avglows.SetDefaultColor(Color.DARK_RED);
midpoint.SetDefaultColor(Color.WHITE);
#--- Cloud and Bar Color.
AddCloud(avghighs,avglows,Color.DARK_GRAY, Color.DARK_GRAY);
AssignPriceColor(if !BarColor then Color.CURRENT else
if Dir==2 then Color.GREEN else
if Dir==1 then Color.DARK_GREEN else
if Dir==-1 then Color.DARK_RED else
if Dir==-2 then Color.RED else Color.GRAY);
#--- END