# Repainting Reversal Pivots Based on ZigZag
# original base code by Bayside of Enhanced Investor
# highly edited - shows the 'zone' between pivot point and actual signal trigger point
input method = {default average, high_low};
def bubbleoffset = .0005;
def percentamount = .01;
def revAmount = .05;
def atrreversal = 2.0;
def atrlength = 5;
def pricehigh = high;
def pricelow = low;
def averagelength = 5;
def averagetype = AverageType.EXPONENTIAL;
def mah = MovingAverage(averagetype, pricehigh, averagelength);
def mal = MovingAverage(averagetype, pricelow, averagelength);
def priceh = if method == method.high_low then pricehigh else mah;
def pricel = if method == method.high_low then pricelow else mal;
def EI = ZigZagHighLow("price h" = priceh, "price l" = pricel, "percentage reversal" = percentamount, "absolute reversal" = revAmount, "atr length" = atrlength, "atr reversal" = atrreversal);
def reversalAmount = if (close * percentamount / 100) > Max(revAmount < atrreversal * reference ATR(atrlength), revAmount) then (close * percentamount / 100) else if revAmount < atrreversal * reference ATR(atrlength) then atrreversal * reference ATR(atrlength) else revAmount;
rec EISave = if !IsNaN(EI) then EI else GetValue(EISave, 1);
def chg = (if EISave == priceh then priceh else pricel) - GetValue(EISave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(EI, 1)) and GetValue(isConf, 1));
def EId = if isUp then 1 else 0;
def xxhigh = if EISave == priceh then priceh else xxhigh[1];
def chghigh = priceh - xxhigh[1];
def xxlow = if EISave == pricel then pricel else xxlow[1];
def chglow = pricel - xxlow[1];
#Arrows
def EIL = if !IsNaN(EI) and !isUp then pricel else GetValue(EIL, 1);
def EIH = if !IsNaN(EI) and isUp then priceh else GetValue(EIH, 1);
def dir = CompoundValue(1, if EIL != EIL[1] or pricel == EIL[1] and pricel == EISave then 1 else if EIH != EIH[1] or priceh == EIH[1] and priceh == EISave then -1 else dir[1], 0);
def signal = CompoundValue(1, if dir > 0 and pricel > EIL then if signal[1] <= 0 then 1 else signal[1] else if dir < 0 and priceh < EIH then if signal[1] >= 0 then -1 else signal[1] else signal[1], 0);
def showarrows = yes;
plot Up = if showarrows and signal > 0 and signal[1] <= 0 then close else double.nan;
Up.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
Up.SetDefaultColor(color.cyan);
Up.SetLineWeight(2);
plot Dn = if showarrows and signal < 0 and signal[1] >= 0 then close else double.nan;
Dn.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
Dn.SetDefaultColor(color.magenta);
Dn.SetLineWeight(2);
def barnumber = BarNumber()[10];
input show_bubbles = no;
AddChartBubble(show_bubbles and barnumber and Up, if isUp then low[1] else high[1], if showarrows and signal > 0 and signal[1] <= 0 then "up" else "" ,Color.UPTICK, no);
AddChartBubble(show_bubbles and barnumber and Dn, if isUp then low[1] else high[1], if showarrows and signal < 0 and signal[1] >= 0 then "dn" else "" , Color.DOWNTICK, yes);
def PivotHigh;
def PivotLow;
if barnumber and Dn{PivotLow = Double.NaN; PivotHigh = high[1];}
else if barnumber and Up{PivotHigh = Double.NaN; PivotLow = low[1];}
else if !IsNaN(PivotLow[1]){PivotLow = PivotLow[1]; PivotHigh = Double.NaN;}
else if !IsNaN(PivotHigh[1]){PivotHigh = PivotHigh[1]; PivotLow = Double.NaN;}
else{PivotHigh = Double.NaN; PivotLow = Double.NaN;}
plot PL = PivotLow[-1];
PL.SetDefaultColor(Color.GREEN);
PL.SetLineWeight(3);
plot PH = PivotHigh[-1];
PH.SetDefaultColor(Color.RED);
PH.SetLineWeight(3);
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def EntryHigh;
def EntryLow;
if barnumber and Dn{EntryLow = Double.NaN;EntryHigh = close;}
else if barnumber and Up {EntryHigh = Double.NaN;EntryLow = close;}
else if !IsNaN(EntryLow[1]){EntryLow = EntryLow[1];EntryHigh = Double.NaN;}
else if !IsNaN(EntryHigh[1]){EntryHigh = EntryHigh[1];EntryLow = Double.NaN;}
else{EntryHigh = Double.NaN;EntryLow = Double.NaN;}
plot EL = EntryLow[-1];
EL.SetDefaultColor(Color.green);
plot EH = EntryHigh[-1];
EH.SetDefaultColor(Color.red);
input show_cloud = yes;
addcloud(if show_cloud then EL else double.nan,PL,color.dark_green,color.dark_green);
addcloud(if show_cloud then EH else double.nan,PH,color.dark_red,color.dark_red);
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~