#
# TD Ameritrade IP Company, Inc. (c) 2011-2024
#
input display = {default LeftRight, RightLeft};
input price = close;
input reversalAmount = 1.0;
input showBubbles = no;
input showLabel = no;
input showpercent_labels = yes;
Assert(reversalAmount > 0, "'reversal amount' should be positive: " + reversalAmount);
plot "ZZ%" = reference ZigZagHighLow(price, price, reversalAmount, 0, 1, 0);
def zzSave = if !IsNaN("ZZ%") then price else GetValue(zzSave, 1);
def chg = (price / GetValue(zzSave, 1) - 1) * 100;
def isUp = chg >= 0;
def isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ%", 1)) and GetValue(isConf, 1));
"ZZ%".EnableApproximation();
"ZZ%".DefineColor("Up Trend", Color.UPTICK);
"ZZ%".DefineColor("Down Trend", Color.DOWNTICK);
"ZZ%".DefineColor("Undefined", Color.DARK_ORANGE);
"ZZ%".AssignValueColor(if !isConf then "ZZ%".Color("Undefined") else if isUp then "ZZ%".Color("Up Trend") else "ZZ%".Color("Down Trend"));
DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);
DefineGlobalColor("Up", Color.UPTICK);
DefineGlobalColor("Down", Color.DOWNTICK);
def barNumber = BarNumber();
AddChartBubble(showBubbles and !IsNaN("ZZ%") and barNumber != 1, price, Round(chg) + "%", if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"), isUp);
AddLabel(showLabel and barNumber != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + Round(chg) + "%", if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"));
def z1 = if !IsNaN("ZZ%") then chg else z1[1];
def z2 = if z1 != z1[1] then z1[1] else z2[1];
def z3 = if z2 != z2[1] then z2[1] else z3[1];
def z4 = if z3 != z3[1] then z3[1] else z4[1];
def z5 = if z4 != z4[1] then z4[1] else z5[1];
def z6 = if z5 != z5[1] then z5[1] else z6[1];
def z7 = if z6 != z6[1] then z6[1] else z7[1];
def z8 = if z7 != z7[1] then z7[1] else z8[1];
def z9 = if z8 != z8[1] then z8[1] else z9[1];
def z10 = if z9 != z9[1] then z9[1] else z10[1];
def w1 = if !IsNaN("ZZ%") then isUp else w1[1];
def w2 = if w1 != w1[1] then w1[1] else w2[1];
def w3 = if w2 != w2[1] then w2[1] else w3[1];
def w4 = if w3 != w3[1] then w3[1] else w4[1];
def w5 = if w4 != w4[1] then w4[1] else w5[1];
def w6 = if w5 != w5[1] then w5[1] else w6[1];
def w7 = if w6 != w6[1] then w6[1] else w7[1];
def w8 = if w7 != w7[1] then w7[1] else w8[1];
def w9 = if w8 != w8[1] then w8[1] else w9[1];
def w10 = if w9 != w9[1] then w9[1] else w10[1];
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z10) else Round(z1)) + "%", if (if display == display.LeftRight then w10 else w1) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z9) else Round(z2)) + "%", if (if display == display.LeftRight then w9 else w2) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z8) else Round(z3)) + "%", if (if display == display.LeftRight then w8 else w3) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z7) else Round(z4)) + "%", if (if display == display.LeftRight then w7 else w4) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z6) else Round(z5)) + "%", if (if display == display.LeftRight then w6 else w5) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z5) else Round(z6)) + "%", if (if display == display.LeftRight then w5 else w6) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z4) else Round(z7)) + "%", if (if display == display.LeftRight then w4 else w7) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z3) else Round(z8)) + "%", if (if display == display.LeftRight then w3 else w8) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z2) else Round(z9)) + "%", if (if display == display.LeftRight then w2 else w9) then Color.LIGHT_GREEN else Color.LIGHT_RED );
AddLabel(showpercent_labels, (if display == display.LeftRight then Round(z1) else Round(z10)) + "%", if (if display == display.LeftRight then w1 else w10) then Color.LIGHT_GREEN else Color.LIGHT_RED );
#