input showPivotLines = yes; input showCompressionZones = yes; input highlightAlternateDays = yes; def length = 13; def bn = BarNumber(); def lastBar = HighestAll(if IsNaN(close) then 0 else bn); def osc = Round(10 * Log(Sum((Max(high, close[1]) - Min(low, close[1])), length) / (Highest(high, length) - Lowest(low, length))) / Log(length), 1); def compressed = osc > 6 or compressed[1] and osc == 6; def extended = osc < 4 or extended[1] and osc == 4; def osR = CompoundValue(1, osR[1] + 2, bn - lastBar); def revH = GetValue(high, osR); def revL = GetValue(low, osR); def revCom = getvalue(compressed, osr); def revEnd = revcom[1] and !revcom; def preCom = if isnan(close) then 0 else getvalue(revend,osr); def rcHi = if bn == 1 and revcom then revh else if !revcom[1] and revcom then revh else if revcom and revh > rchi[1] then revh else rchi[1]; def rcLo = if bn == 1 and revcom then revl else if !revcom[1] and revcom then revl else if revcom and revl < rclo[1] then revl else rclo[1]; def count = compoundvalue(1,if compressed then 0 else count[1] + 1, double.nan); def compHigh = if compressed and !compressed[1] then getvalue(rchi,osr) else if !compressed and count > 20 then double.nan else comphigh[1]; def compLow = if compressed and !compressed[1] then getvalue(rclo,osr) else if !compressed and count > 20 then double.nan else complow[1]; plot upper = if !showCompressionZones or precom then double.nan else if isnan(close) then getvalue(comphigh,bn-lastbar) else comphigh; upper.setpaintingStrategy(12); upper.setdefaultColor(createcolor(102,145,205)); plot lower = if !showCompressionZones or precom then double.nan else if isnan(close) then getvalue(complow,bn-lastbar) else complow; lower.setpaintingStrategy(12); lower.setdefaultColor(createcolor(102,145,205)); addcloud(upper,lower,createcolor(168,168,225)); # --- swing lines --- def swingLength = 5; def lastClose = GetValue(close, bn - lastBar); def nan = Double.NaN; def os1 = Min(swingLength - 1, lastBar - bn); def aHi = high > Highest(high[1], swingLength - 1) and high == GetValue(Highest(high, swingLength), -os1); def aLo = low < Lowest(low[1], swingLength - 1) and low == GetValue(Lowest(low, swingLength), -os1); def aHiCount = if aHi[1] then 1 else aHiCount[1] + 1; def aLoCount = if aLo[1] then 1 else aLoCount[1] + 1; def bHi = (aHi and !aLo) or (aHi and aLo and aLoCount < aHiCount); def bLo = (aLo and !aHi) or (aLo and aHi and aHiCount < aLoCount); def bHiBn = if bHi then bn else 0; def bLoBn = if bLo then bn else 0; def lastBhi = HighestAll(bHiBn); def lastBlo = HighestAll(bLoBn); def rHi = GetValue(bHi, osR); def rLo = GetValue(bLo, osR); def rHiCount = if rHi[1] then 1 else rHiCount[1] + 1; def rLoCount = if rLo[1] then 1 else rLoCount[1] + 1; def fH1 = GetValue(rHiCount, osR); def fL1 = GetValue(rLoCount, osR); def mHi = if bLo[1] then high else if high > mHi[1] then high else mHi[1]; def mLo = if bHi[1] then low else if low < mLo[1] then low else mLo[1]; def mHb = if aHiCount < aLoCount then 0 else if bLo[1] or mHi <> mHi[1] then bn else mHb[1]; def mLb = if aLoCount < aHiCount then 0 else if bHi[1] or mLo <> mLo[1] then bn else mLb[1]; def cHi = bn < lastBlo and !bHi and !bLo and bn == GetValue(mHb[1], -fL1); def cLo = bn < lastBhi and !bHi and !bLo and bn == GetValue(mLb[1], -fH1); def dHi = if IsNaN(cHi) then 0 else cHi; def dLo = if IsNaN(cLo) then 0 else cLo; def lastLow = GetValue(low, bn - lastBar); def lastHigh = GetValue(high, bn - lastBar); def swingHigh = bHi or dHi; def swingLow = bLo or dLo; def newest = HighestAll(if swingHigh or swingLow then bn else 0); def sh1 = HighestAll(if swingHigh and high > lasthigh and bn < newest then bn else 0); def vh1 = if sh1 == 0 then double.nan else GetValue(high, bn - sh1); def sh2 = HighestAll(if swingHigh and high > lasthigh and bn < sh1 and high > vh1 then bn else 0); def vh2 = if sh2 == 0 then double.nan else GetValue(high, bn - sh2); def sh3 = HighestAll(if swingHigh and high > lasthigh and bn < sh2 and high > vh2 then bn else 0); def vh3 = if sh3 == 0 then double.nan else GetValue(high, bn - sh3); def sh4 = HighestAll(if swingHigh and high > lasthigh and bn < sh3 and high > vh3 then bn else 0); def vh4 = if sh4 == 0 then double.nan else GetValue(high, bn - sh4); def sh5 = HighestAll(if swingHigh and high > lasthigh and bn < sh4 and high > vh4 then bn else 0); def vh5 = if sh5 == 0 then double.nan else GetValue(high, bn - sh5); def sl1 = HighestAll(if swingLow and low < lastLow and bn < newest then bn else 0); def vl1 = if sl1 == 0 then Double.NaN else GetValue(low, bn - sl1); def sl2 = HighestAll(if swingLow and low < lastLow and bn < sl1 and low < vl1 then bn else 0); def vl2 = if sl2 == 0 then Double.NaN else GetValue(low, bn - sl2); def sl3 = HighestAll(if swingLow and low < lastLow and bn < sl2 and low < vl2 then bn else 0); def vl3 = if sl3 == 0 then Double.NaN else GetValue(low, bn - sl3); def sl4 = HighestAll(if swingLow and low < lastLow and bn < sl3 and low < vl3 then bn else 0); def vl4 = if sl4 == 0 then Double.NaN else GetValue(low, bn - sl4); def sl5 = HighestAll(if swingLow and low < lastLow and bn < sl4 and low < vl4 then bn else 0); def vl5 = if sl5 == 0 then Double.NaN else GetValue(low, bn - sl5); plot r1 = if showPivotLines and bn >= sh1 then vh1 else Double.NaN; plot r2 = if showPivotLines and bn >= sh2 then vh2 else Double.NaN; plot r3 = if showPivotLines and bn >= sh3 then vh3 else Double.NaN; plot r4 = if showPivotLines and bn >= sh4 then vh4 else Double.NaN; plot r5 = if showPivotLines and bn >= sh5 then vh5 else Double.NaN; r1.SetDefaultColor(Color.GREEN); r2.SetDefaultColor(Color.MAGENTA); r3.SetDefaultColor(Color.MAGENTA); r4.SetDefaultColor(Color.MAGENTA); r5.SetDefaultColor(Color.MAGENTA); plot s1 = if showPivotLines and bn >= sl1 then vl1 else Double.NaN; plot s2 = if showPivotLines and bn >= sl2 then vl2 else Double.NaN; plot s3 = if showPivotLines and bn >= sl3 then vl3 else Double.NaN; plot s4 = if showPivotLines and bn >= sl4 then vl4 else Double.NaN; plot s5 = if showPivotLines and bn >= sl5 then vl5 else Double.NaN; s1.SetDefaultColor(Color.GREEN); s2.SetDefaultColor(Color.MAGENTA); s3.SetDefaultColor(Color.MAGENTA); s4.SetDefaultColor(Color.MAGENTA); s5.SetDefaultColor(Color.MAGENTA); def upper1 = if highlightAlternateDays and GetDay() % 2 == 0 then Double.POSITIVE_INFINITY else Double.NaN; def lower1 = if highlightAlternateDays and GetDay() % 2 == 0 then Double.NEGATIVE_INFINITY else Double.NaN; AddCloud(upper1, lower1, Color.DARK_GRAY);