declare upper;
input price = close;
input reversalAmount = 1.0;
input reversalMode = {default price, percent};
def mode = if reversalMode == reversalMode.price then ZigZagTrendSign(price = price, reversalAmount = reversalAmount) else ZigZagTrendPercent(price = price, reversalAmount = reversalAmount);
def inflection = if reversalMode == reversalMode.price then if !IsNaN(ZigZagSign(price = price, reversalAmount = reversalAmount)) then 1 else 0 else if !IsNaN(ZigZagPercent(price = price, reversalAmount = reversalAmount)) then 1 else 0;
rec trend = if inflection == 1 and mode == -1 then 1 else if inflection == 1 and mode == 1 then -1 else trend[1];
plot wave = if reversalMode == reversalMode.price then ZigZagSign(price = price, reversalAmount = reversalAmount, "show bubbles" = Yes) else ZigZagPercent(price = price, reversalAmount = reversalAmount);
wave.EnableApproximation();
wave.AssignValueColor(if trend[1] == 1 then color.red else color.red);
wave.SetDefaultColor(Color.RED);
wave.SetLineWeight(1);
def zzoth = if !IsNaN(wave) then wave else zzoth[1];
def chgzzoth = AbsValue(zzoth[1] - zzoth);
def count = if !IsNaN(wave) then BarNumber() else count[1];
input show_price_bubble = yes;
input show_change_bubble = yes;
input show_count_bubble = yes;
input show_wave_bubble = yes;
input bubbleoffset = .0005;
AddChartBubble(!IsNaN(wave) and show_count_bubble,
if zzoth>zzoth[1]
then high * (1 + bubbleoffset)
else low * (1 - bubbleoffset) ,
"Bars: " + (count - count[1]),
color.yellow,
if zzoth>zzoth[1] then yes else no);
AddChartBubble(!IsNaN(wave) and show_change_bubble,
if zzoth>zzoth[1]
then high * (1 + bubbleoffset)
else low * (1 - bubbleoffset) ,
"Chg: " + AsText(chgzzoth) ,
Color.CYAN,
if zzoth>zzoth[1] then yes else no);
AddChartBubble(!IsNaN(wave) and show_price_bubble,
if zzoth>zzoth[1]
then high * (1 + bubbleoffset)
else low * (1 - bubbleoffset) ,
(if wave == close then "C: " else "C: ") + AsText(wave) ,
if !isnan(wave) > !isnan(wave[1]) then Color.GREEN else Color.RED,
if zzoth>zzoth[1] then yes else no);
#Prior Bubbles defined--------------------------------------------
def zzoth1 = if zzoth!=zzoth[1] then zzoth[1] else zzoth1[1];
def zzoth2 = if zzoth1!=zzoth1[1] then zzoth1[1] else zzoth2[1];
def zzoth3 = if zzoth2!=zzoth2[1] then zzoth2[1] else zzoth3[1];
input debug = no;
addlabel(debug,zzoth+" "+zzoth1+" "+zzoth2);
#Waves where higher high or lower high are cumulative
def wavehighext = if zzoth>zzoth[1] and zzoth>zzoth2 then wavehighext[1]+chgzzoth else if zzoth>zzoth[1] and zzoth<=zzoth2 then 0 else wavehighext[1];
addchartBubble(!IsNaN(wave) and show_wave_bubble,
if zzoth>zzoth[1]
then high * (1 + bubbleoffset)
else double.nan ,
(if wave == close then "W: " else "W: ") + AsText(wavehighext) ,
if !isnan(wave) > !isnan(wave[1]) then Color.GREEN else Color.RED,
if zzoth>zzoth[1] then yes else no);
def wavelowext = if zzoth<zzoth[1] and zzoth>zzoth2 then wavelowext[1]+chgzzoth else if zzoth<zzoth[1] and zzoth<=zzoth2 then 0 else wavelowext[1];
addchartBubble(!IsNaN(wave) and show_wave_bubble,
if zzoth<zzoth[1]
then low * (1 - bubbleoffset)
else double.nan ,
(if wave == close then "W: " else "W: ") + AsText(wavelowext) ,
if !isnan(wave) > !isnan(wave[1]) then Color.GREEN else Color.RED,
if zzoth>zzoth[1] then yes else no);