Repaints ZigZag High Low Stats for ThinkorSwim

Repaints
Code:
def   price               = close;

def   priceH              = high;    # swing high

def   priceL              = low;     # swing low

input ATRreversalfactor   = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust



input ATRlength           = 5;#Hint ATRlength: 5 is standard, adjust to whatever



input zigzagpercent       = 0.80;#LAR original is 0.2, but modified in testing



input zigzagamount        = .15;

def ATR                   = reference ATR(length = ATRlength);

def reversalAmount        = if (close * zigzagpercent / 100) > Max

(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then

(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then

ATRreversalfactor * ATR else zigzagamount;

input showSupplyDemand    = {Off, default Arrow, Pivot};

input showArrows          = yes; #orignal by LAR was no

input useAlerts           = no; #orignal by LAR was no

 

#Original TOS ZigZag code Modified by Linus

def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(price), 0, barNumber));



rec state = {default init, undefined, uptrend, downtrend};

rec minMaxPrice;



if (GetValue(state, 1) == GetValue(state.init, 0)) {

    minMaxPrice = price;

    state = state.undefined;

} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.undefined;

        minMaxPrice = GetValue(minMaxPrice, 1);

    }

} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else {

        state = state.uptrend;

        minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));

    }

} else {

    if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.downtrend;

        minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));

    }

}



def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;

def futureDepth =  barCount - barNumber;

def tmpLastPeriodBar;

if (isCalculated) {

    if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {

        tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with

lastPeriodBarAcc = 1

            while lastPeriodBarAcc > 0

            do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))

                then -lastPeriodBarAcc

                else lastPeriodBarAcc + 1;

    } else {

        tmpLastPeriodBar = 0;

    }

} else {

    tmpLastPeriodBar = Double.NaN;

}



def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue

(tmpLastPeriodBar) else -futureDepth;



rec currentPriceLevel;

rec currentPoints;

if (state == state.uptrend and isCalculated) {

    currentPriceLevel =

        fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc

= minMaxPrice

            do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,

barWithMaxOnPeriodI));

    currentPoints =

        fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =

Double.NaN

            while IsNaN(maxPointOnPeriodAcc)

            do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)

                then maxPointOnPeriodI

                else maxPointOnPeriodAcc;

} else if (state == state.downtrend and isCalculated) {

    currentPriceLevel =

        fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc

= minMaxPrice

            do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,

barWithMinOnPeriodI));

    currentPoints =

        fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =

Double.NaN

            while IsNaN(minPointOnPeriodAcc)

            do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)

                then minPointOnPeriodI

                else minPointOnPeriodAcc;

} else if (!isCalculated and (state == state.uptrend or state ==

state.downtrend)) {

    currentPriceLevel = GetValue(currentPriceLevel, 1);

    currentPoints = GetValue(currentPoints, 1) + 1;

} else {

    currentPoints = 1;

    currentPriceLevel = GetValue(price, currentPoints);

}



plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==

state.uptrend then priceH else priceL else if (currentPoints == 0) then

currentPriceLevel else Double.NaN;



rec zzSave =  if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)

then if IsNaN(barNumber[-1]) and  state == state.uptrend then priceH else priceL

else currentPriceLevel else GetValue(zzSave, 1);



def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if

barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)

- GetValue(zzSave, 1);



def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and

GetValue(isConf, 1));



"ZZ$".EnableApproximation();

"ZZ$".DefineColor("Up Trend", Color.GREEN);

"ZZ$".DefineColor("Down Trend", Color.RED);

"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"));

"ZZ$".SetLineWeight(2);

DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);

DefineGlobalColor("Up", Color.GREEN);

DefineGlobalColor("Down", Color.RED);

 

#Arrows



def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else

GetValue(zzL, 1);



def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue

(zzH, 1);



def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1

else dir[1], 0);



def signal = CompoundValue(1,

    if dir > 0 and low > zzL then

        if signal[1] <= 0 then 1 else signal[1]

    else if dir < 0 and high < zzH then

        if signal[1] >= 0 then -1 else signal[1]

    else signal[1]

, 0);



plot U1 = showArrows and signal > 0 and signal[1] <= 0;

U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

U1.SetDefaultColor(Color.GREEN);

U1.SetLineWeight(4);



plot D1 = showArrows and signal < 0 and signal[1] >= 0;

D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

D1.SetDefaultColor(Color.RED);

D1.SetLineWeight(4);



Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);

Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);



#Supply Demand Areas

def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;

def rLow;

def rHigh;



if barNumber() == 1 {

    rLow = Double.NaN;

    rHigh = Double.NaN;

} else if signal crosses 0 {

    rLow = low[idx];

    rHigh = high[idx];

} else {

    rLow = rLow[1];

    rHigh = rHigh[1];

}



plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else

Double.NaN;

HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;

LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



def hlUp = if signal > 0 then HighLine else Double.NaN;

def hlDn = if signal < 0 then HighLine else Double.NaN;



AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);

AddCloud(hlDn, LowLine, Color.RED, Color.RED);



## END CODE
Hi can i get the zigzag indicator as simple labels on tos charts. Looking for it to only say HH,HL,LL,LH on the peaks and troughs. I don't want the whole zigzag line. Can anyone help
 
Code:
def   price               = close;

def   priceH              = high;    # swing high

def   priceL              = low;     # swing low

input ATRreversalfactor   = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust



input ATRlength           = 5;#Hint ATRlength: 5 is standard, adjust to whatever



input zigzagpercent       = 0.80;#LAR original is 0.2, but modified in testing



input zigzagamount        = .15;

def ATR                   = reference ATR(length = ATRlength);

def reversalAmount        = if (close * zigzagpercent / 100) > Max

(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then

(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then

ATRreversalfactor * ATR else zigzagamount;

input showSupplyDemand    = {Off, default Arrow, Pivot};

input showArrows          = yes; #orignal by LAR was no

input useAlerts           = no; #orignal by LAR was no

 

#Original TOS ZigZag code Modified by Linus

def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(price), 0, barNumber));



rec state = {default init, undefined, uptrend, downtrend};

rec minMaxPrice;



if (GetValue(state, 1) == GetValue(state.init, 0)) {

    minMaxPrice = price;

    state = state.undefined;

} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.undefined;

        minMaxPrice = GetValue(minMaxPrice, 1);

    }

} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else {

        state = state.uptrend;

        minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));

    }

} else {

    if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.downtrend;

        minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));

    }

}



def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;

def futureDepth =  barCount - barNumber;

def tmpLastPeriodBar;

if (isCalculated) {

    if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {

        tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with

lastPeriodBarAcc = 1

            while lastPeriodBarAcc > 0

            do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))

                then -lastPeriodBarAcc

                else lastPeriodBarAcc + 1;

    } else {

        tmpLastPeriodBar = 0;

    }

} else {

    tmpLastPeriodBar = Double.NaN;

}



def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue

(tmpLastPeriodBar) else -futureDepth;



rec currentPriceLevel;

rec currentPoints;

if (state == state.uptrend and isCalculated) {

    currentPriceLevel =

        fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc

= minMaxPrice

            do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,

barWithMaxOnPeriodI));

    currentPoints =

        fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =

Double.NaN

            while IsNaN(maxPointOnPeriodAcc)

            do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)

                then maxPointOnPeriodI

                else maxPointOnPeriodAcc;

} else if (state == state.downtrend and isCalculated) {

    currentPriceLevel =

        fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc

= minMaxPrice

            do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,

barWithMinOnPeriodI));

    currentPoints =

        fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =

Double.NaN

            while IsNaN(minPointOnPeriodAcc)

            do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)

                then minPointOnPeriodI

                else minPointOnPeriodAcc;

} else if (!isCalculated and (state == state.uptrend or state ==

state.downtrend)) {

    currentPriceLevel = GetValue(currentPriceLevel, 1);

    currentPoints = GetValue(currentPoints, 1) + 1;

} else {

    currentPoints = 1;

    currentPriceLevel = GetValue(price, currentPoints);

}



plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==

state.uptrend then priceH else priceL else if (currentPoints == 0) then

currentPriceLevel else Double.NaN;



rec zzSave =  if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)

then if IsNaN(barNumber[-1]) and  state == state.uptrend then priceH else priceL

else currentPriceLevel else GetValue(zzSave, 1);



def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if

barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)

- GetValue(zzSave, 1);



def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and

GetValue(isConf, 1));



"ZZ$".EnableApproximation();

"ZZ$".DefineColor("Up Trend", Color.GREEN);

"ZZ$".DefineColor("Down Trend", Color.RED);

"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"));

"ZZ$".SetLineWeight(2);

DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);

DefineGlobalColor("Up", Color.GREEN);

DefineGlobalColor("Down", Color.RED);

 

#Arrows



def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else

GetValue(zzL, 1);



def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue

(zzH, 1);



def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1

else dir[1], 0);



def signal = CompoundValue(1,

    if dir > 0 and low > zzL then

        if signal[1] <= 0 then 1 else signal[1]

    else if dir < 0 and high < zzH then

        if signal[1] >= 0 then -1 else signal[1]

    else signal[1]

, 0);



plot U1 = showArrows and signal > 0 and signal[1] <= 0;

U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

U1.SetDefaultColor(Color.GREEN);

U1.SetLineWeight(4);



plot D1 = showArrows and signal < 0 and signal[1] >= 0;

D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

D1.SetDefaultColor(Color.RED);

D1.SetLineWeight(4);



Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);

Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);



#Supply Demand Areas

def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;

def rLow;

def rHigh;



if barNumber() == 1 {

    rLow = Double.NaN;

    rHigh = Double.NaN;

} else if signal crosses 0 {

    rLow = low[idx];

    rHigh = high[idx];

} else {

    rLow = rLow[1];

    rHigh = rHigh[1];

}



plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else

Double.NaN;

HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;

LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



def hlUp = if signal > 0 then HighLine else Double.NaN;

def hlDn = if signal < 0 then HighLine else Double.NaN;



AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);

AddCloud(hlDn, LowLine, Color.RED, Color.RED);



## END CODE
thank you this was very helpful. Is there any way you can make the zigzag Peaks and Troughs show just a label that says HH,HL,LL,LH. Please let me know
 
thank you this was very helpful. Is there any way you can make the zigzag Peaks and Troughs show just a label that says HH,HL,LL,LH. Please let me know

Added the following bold code to the code you posted to provide the label/bubbles.

Capture.jpg
def price = close;

def priceH = high; # swing high

def priceL = low; # swing low

input ATRreversalfactor = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust



input ATRlength = 5;#Hint ATRlength: 5 is standard, adjust to whatever



input zigzagpercent = 0.80;#LAR original is 0.2, but modified in testing



input zigzagamount = .15;

def ATR = reference ATR(length = ATRlength);

def reversalAmount = if (close * zigzagpercent / 100) > Max

(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then

(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then

ATRreversalfactor * ATR else zigzagamount;

input showSupplyDemand = {Off, default Arrow, Pivot};

input showArrows = yes; #orignal by LAR was no

input useAlerts = no; #orignal by LAR was no



#Original TOS ZigZag code Modified by Linus

def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(price), 0, barNumber));



rec state = {default init, undefined, uptrend, downtrend};

rec minMaxPrice;



if (GetValue(state, 1) == GetValue(state.init, 0)) {

minMaxPrice = price;

state = state.undefined;

} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {

if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

state = state.downtrend;

minMaxPrice = priceL;

} else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

state = state.uptrend;

minMaxPrice = priceH;

} else {

state = state.undefined;

minMaxPrice = GetValue(minMaxPrice, 1);

}

} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {

if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

state = state.downtrend;

minMaxPrice = priceL;

} else {

state = state.uptrend;

minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));

}

} else {

if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

state = state.uptrend;

minMaxPrice = priceH;

} else {

state = state.downtrend;

minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));

}

}



def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;

def futureDepth = barCount - barNumber;

def tmpLastPeriodBar;

if (isCalculated) {

if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {

tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with

lastPeriodBarAcc = 1

while lastPeriodBarAcc > 0

do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))

then -lastPeriodBarAcc

else lastPeriodBarAcc + 1;

} else {

tmpLastPeriodBar = 0;

}

} else {

tmpLastPeriodBar = Double.NaN;

}



def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue

(tmpLastPeriodBar) else -futureDepth;



rec currentPriceLevel;

rec currentPoints;

if (state == state.uptrend and isCalculated) {

currentPriceLevel =

fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc

= minMaxPrice

do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,

barWithMaxOnPeriodI));

currentPoints =

fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =

Double.NaN

while IsNaN(maxPointOnPeriodAcc)

do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)

then maxPointOnPeriodI

else maxPointOnPeriodAcc;

} else if (state == state.downtrend and isCalculated) {

currentPriceLevel =

fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc

= minMaxPrice

do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,

barWithMinOnPeriodI));

currentPoints =

fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =

Double.NaN

while IsNaN(minPointOnPeriodAcc)

do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)

then minPointOnPeriodI

else minPointOnPeriodAcc;

} else if (!isCalculated and (state == state.uptrend or state ==

state.downtrend)) {

currentPriceLevel = GetValue(currentPriceLevel, 1);

currentPoints = GetValue(currentPoints, 1) + 1;

} else {

currentPoints = 1;

currentPriceLevel = GetValue(price, currentPoints);

}



plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==

state.uptrend then priceH else priceL else if (currentPoints == 0) then

currentPriceLevel else Double.NaN;



rec zzSave = if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)

then if IsNaN(barNumber[-1]) and state == state.uptrend then priceH else priceL

else currentPriceLevel else GetValue(zzSave, 1);



def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if

barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)

- GetValue(zzSave, 1);



def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and

GetValue(isConf, 1));



"ZZ$".EnableApproximation();

"ZZ$".DefineColor("Up Trend", Color.GREEN);

"ZZ$".DefineColor("Down Trend", Color.RED);

"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"));

"ZZ$".SetLineWeight(2);

DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);

DefineGlobalColor("Up", Color.GREEN);

DefineGlobalColor("Down", Color.RED);



#Arrows



def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else

GetValue(zzL, 1);



def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue

(zzH, 1);



def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1

else dir[1], 0);



def signal = CompoundValue(1,

if dir > 0 and low > zzL then

if signal[1] <= 0 then 1 else signal[1]

else if dir < 0 and high < zzH then

if signal[1] >= 0 then -1 else signal[1]

else signal[1]

, 0);



plot U1 = showArrows and signal > 0 and signal[1] <= 0;

U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

U1.SetDefaultColor(Color.GREEN);

U1.SetLineWeight(4);



plot D1 = showArrows and signal < 0 and signal[1] >= 0;

D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

D1.SetDefaultColor(Color.RED);

D1.SetLineWeight(4);



Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);

Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);



#Supply Demand Areas

def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;

def rLow;

def rHigh;



if BarNumber() == 1 {

rLow = Double.NaN;

rHigh = Double.NaN;

} else if signal crosses 0 {

rLow = low[idx];

rHigh = high[idx];

} else {

rLow = rLow[1];

rHigh = rHigh[1];

}



plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else

Double.NaN;

HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;

LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



def hlUp = if signal > 0 then HighLine else Double.NaN;

def hlDn = if signal < 0 then HighLine else Double.NaN;



AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);

AddCloud(hlDn, LowLine, Color.RED, Color.RED);

def zzsave1 = if zzSave != zzSave[1] then zzSave[1] else zzsave1[1];
def zzsave2 = if zzsave1 != zzsave1[1] then zzsave1[1] else zzsave2[1];
def lastbar = if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN;
input debug = no;
AddLabel(debug, zzSave + " " + zzsave1 + " " + zzsave2, Color.WHITE);

AddChartBubble(BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high, if zzSave > zzsave2 then "HH" else "LH", if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low, if zzSave > zzsave2 then "HL" else "LL", if zzSave > zzsave2 then Color.GREEN else Color.RED, no);



## END CODE
 
Last edited:
Added the following bold code to the code you posted to provide the label/bubbles.
what is zig zag amount in the code. I'm now trying to adjust it so I won't see as many HH,HL,LL,LH's. Looking to get it to show Higher highs, lower lows, Lower highs, like every 5% moves. Its showing a HH,HL,LL,LH, on almost every 3 candles. Any way you can help me with that too?
 
what is zig zag amount in the code. I'm now trying to adjust it so I won't see as many HH,HL,LL,LH's. Looking to get it to show Higher highs, lower lows, Lower highs, like every 5% moves. Its showing a HH,HL,LL,LH, on almost every 3 candles. Any way you can help me with that too?
I added the HHLL code to the code that you liked from 123man above. Go to the input screen and change the factors to your liking.
 
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#

input aggregationPeriod = AggregationPeriod.hour;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;

plot DailyHigh;
plot DailyLow;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    DailyHigh = Double.NaN;
    DailyLow = Double.NaN;
} else {
    DailyHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    DailyLow = Lowest(low(period = aggregationPeriod)[-displace], length);
}

DailyHigh.SetDefaultColor(GetColor(4));
DailyHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailyLow.SetDefaultColor(GetColor(4));
DailyLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

What needs to be added to make this into an indicator that will detect higher lows and lower highs?
Should be really easy. Thanks!
 
What needs to be added to make this into an indicator that will detect higher lows and lower highs?
Should be really easy. Thanks!
@ezrollin Try replacing the last 4 lines of code with the below section - this will change the color of the indicator to indicate higher lows or lower highs.
Code:
DailyHigh.assignvaluecolor(if DailyHigh < DailyHigh[1] then color.red else color.green);
DailyHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DailyLow.AssignValueColor(if DailyLow > DailyLow[1] then color.green else color.red);
DailyLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Just as an additional reference, might want to look into Donchian Channels. Sounds similar to what you're looking for.

Code:
input BandLength = 20;

####  Create Donchian ####
plot upperBand = Highest(high[1], BandLength);
plot lowerBand = Lowest(low[1], BandLength);
plot middleBand = (upperBand + lowerBand) / 2;

addCloud(upperBand, lowerBand, color.black, color.black);

upperBand.SetDefaultColor(Color.gray);
lowerBand.SetDefaultColor(Color.gray);
middleBand.SetDefaultColor(Color.dark_gray);
middleBand.SetStyle(Curve.LONG_DASH);
 
@SleepyZ is there a way to add (Deviation values in point) in the above code?

See if this helps. Using the bold code from below, add the def dev and replace the addchartbubble code in the original code

Rich (BB code):
def zzsave1 = if zzSave != zzSave[1] then zzSave[1] else zzsave1[1];
def zzsave2 = if zzsave1 != zzsave1[1] then zzsave1[1] else zzsave2[1];
def lastbar = if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN;
input debug = no;
AddLabel(debug, zzSave + " " + zzsave1 + " " + zzsave2, Color.WHITE);

def dev = (zzsave - zzsave2) / zzsave;

AddChartBubble(BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high,  aspercent(dev) + "\n" + (if zzSave > zzsave2 then "HH" else "LH"), if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low, aspercent(dev) + "\n" + (if zzSave > zzsave2 then "HL" else "LL"), if zzSave > zzsave2 then Color.GREEN else Color.RED, no);
 
Thank you..I'll ask for help if it doesn't work..I'm not coder... I want to add deviation from price (3 )value.. plus I want to add atr multiplier (2) value.. I hope I can do it..
 
So whoever moved my post to this post.... I'm not looking for something with "horizontal lines" as the title says. Thanks
 
Code:
####### ZigZag Lines



input Period = AggregationPeriod.FIFTEEN_MIN;

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(period = Period);

def pricelow = low(period = Period);

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(period=period) * percentamount / 100) > Max(revAmount < atrreversal * reference ATR(atrlength), revAmount) then (close(period=period) * 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;

plot EnhancedLines = if EId <= 1 then EI else Double.NaN;

EnhancedLines.AssignValueColor(if EId == 1 then Color.GREEN else if EId == 0 then Color.RED else Color.DARK_ORANGE);

EnhancedLines.SetStyle(Curve.FIRM);

EnhancedLines.EnableApproximation();

EnhancedLines.HideBubble();

I guess what I'm looking for more resembles the zig zag?
I'm wanting to turn it into a scan eventually thanks
 
Can you make the HH LL with the price on it? Like the photos showing below?


Screenshot%202022-02-10%20004755.png

Modified code below to add input to show price bubble in addition to "HH/LH" | "HL/LL" bubble.

[
Ruby:
def price = close;

def priceH = high; # swing high

def priceL = low; # swing low

input ATRreversalfactor = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust



input ATRlength = 5;#Hint ATRlength: 5 is standard, adjust to whatever



input zigzagpercent = 0.80;#LAR original is 0.2, but modified in testing



input zigzagamount = .15;

def ATR = reference ATR(length = ATRlength);

def reversalAmount = if (close * zigzagpercent / 100) > Max

(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then

(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then

ATRreversalfactor * ATR else zigzagamount;

input showSupplyDemand = {Off, default Arrow, Pivot};

input showArrows = yes; #orignal by LAR was no

input useAlerts = no; #orignal by LAR was no



#Original TOS ZigZag code Modified by Linus

def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(price), 0, barNumber));



rec state = {default init, undefined, uptrend, downtrend};

rec minMaxPrice;



if (GetValue(state, 1) == GetValue(state.init, 0)) {

    minMaxPrice = price;

    state = state.undefined;

} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.undefined;

        minMaxPrice = GetValue(minMaxPrice, 1);

    }

} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {

    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

        state = state.downtrend;

        minMaxPrice = priceL;

    } else {

        state = state.uptrend;

        minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));

    }

} else {

    if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

        state = state.uptrend;

        minMaxPrice = priceH;

    } else {

        state = state.downtrend;

        minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));

    }

}



def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;

def futureDepth = barCount - barNumber;

def tmpLastPeriodBar;

if (isCalculated) {

    if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {

        tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with

lastPeriodBarAcc = 1

while lastPeriodBarAcc > 0

do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))

then -lastPeriodBarAcc

else lastPeriodBarAcc + 1;

    } else {

        tmpLastPeriodBar = 0;

    }

} else {

    tmpLastPeriodBar = Double.NaN;

}



def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue

(tmpLastPeriodBar) else -futureDepth;



rec currentPriceLevel;

rec currentPoints;

if (state == state.uptrend and isCalculated) {

    currentPriceLevel =

fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc

= minMaxPrice

do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,

barWithMaxOnPeriodI));

    currentPoints =

fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =

Double.NaN

while IsNaN(maxPointOnPeriodAcc)

do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)

then maxPointOnPeriodI

else maxPointOnPeriodAcc;

} else if (state == state.downtrend and isCalculated) {

    currentPriceLevel =

fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc

= minMaxPrice

do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,

barWithMinOnPeriodI));

    currentPoints =

fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =

Double.NaN

while IsNaN(minPointOnPeriodAcc)

do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)

then minPointOnPeriodI

else minPointOnPeriodAcc;

} else if (!isCalculated and (state == state.uptrend or state ==

state.downtrend)) {

    currentPriceLevel = GetValue(currentPriceLevel, 1);

    currentPoints = GetValue(currentPoints, 1) + 1;

} else {

    currentPoints = 1;

    currentPriceLevel = GetValue(price, currentPoints);

}



plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==

state.uptrend then priceH else priceL else if (currentPoints == 0) then

currentPriceLevel else Double.NaN;



rec zzSave = if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)

then if IsNaN(barNumber[-1]) and state == state.uptrend then priceH else priceL

else currentPriceLevel else GetValue(zzSave, 1);



def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if

barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)

- GetValue(zzSave, 1);



def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and

GetValue(isConf, 1));



"ZZ$".EnableApproximation();

"ZZ$".DefineColor("Up Trend", Color.GREEN);

"ZZ$".DefineColor("Down Trend", Color.RED);

"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"));

"ZZ$".SetLineWeight(2);

DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);

DefineGlobalColor("Up", Color.GREEN);

DefineGlobalColor("Down", Color.RED);



#Arrows



def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else

GetValue(zzL, 1);



def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue

(zzH, 1);



def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1

else dir[1], 0);



def signal = CompoundValue(1,

if dir > 0 and low > zzL then

if signal[1] <= 0 then 1 else signal[1]

else if dir < 0 and high < zzH then

if signal[1] >= 0 then -1 else signal[1]

else signal[1]

, 0);



plot U1 = showArrows and signal > 0 and signal[1] <= 0;

U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

U1.SetDefaultColor(Color.GREEN);

U1.SetLineWeight(4);



plot D1 = showArrows and signal < 0 and signal[1] >= 0;

D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

D1.SetDefaultColor(Color.RED);

D1.SetLineWeight(4);



Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);

Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);



#Supply Demand Areas

def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;

def rLow;

def rHigh;



if BarNumber() == 1 {

    rLow = Double.NaN;

    rHigh = Double.NaN;

} else if signal crosses 0 {

    rLow = low[idx];

    rHigh = high[idx];

} else {

    rLow = rLow[1];

    rHigh = rHigh[1];

}



plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else

Double.NaN;

HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;

LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



def hlUp = if signal > 0 then HighLine else Double.NaN;

def hlDn = if signal < 0 then HighLine else Double.NaN;



AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);

AddCloud(hlDn, LowLine, Color.RED, Color.RED);

def zzsave1 = if zzSave != zzSave[1] then zzSave[1] else zzsave1[1];
def zzsave2 = if zzsave1 != zzsave1[1] then zzsave1[1] else zzsave2[1];
def lastbar = if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN;
input debug = no;
AddLabel(debug, zzSave + " " + zzsave1 + " " + zzsave2, Color.WHITE);

input showbubble_price = yes;
AddChartBubble(BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high, if zzSave > zzsave2 then "HH" else "LH" , if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low,  if zzSave > zzsave2 then "HL" else "LL", if zzSave > zzsave2 then Color.GREEN else Color.RED, no);
AddChartBubble(showbubble_price and BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high, if showbubble_price then AsText(high) else "" , if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(showbubble_price and BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low, if showbubble_price then AsText(low) else "", if zzSave > zzsave2 then Color.GREEN else Color.RED, no);


## END CODE
 
  • Like
Reactions: LLP
Can you make the HH LL with the price on it? Like the photos showing below?

Sorry, but I just saw your request. I could not view your screenshot, but I have added an option for price, in addition to the HHLL and dev, all separate optional bubbles at the input screen. You can move the addchartbubble pairs in a different order to change the stacking order display on the chart.

Ruby:
def price = close;

def priceH = high; # swing high

def priceL = low; # swing low

input ATRreversalfactor = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust



input ATRlength = 5;#Hint ATRlength: 5 is standard, adjust to whatever



input zigzagpercent = 0.80;#LAR original is 0.2, but modified in testing



input zigzagamount = .15;

def ATR = reference ATR(length = ATRlength);

def reversalAmount = if (close * zigzagpercent / 100) > Max

(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then

(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then

ATRreversalfactor * ATR else zigzagamount;

input showSupplyDemand = {Off, default Arrow, Pivot};

input showArrows = yes; #orignal by LAR was no

input useAlerts = no; #orignal by LAR was no



#Original TOS ZigZag code Modified by Linus

def barNumber = BarNumber();

def barCount = HighestAll(If(IsNaN(price), 0, barNumber));



rec state = {default init, undefined, uptrend, downtrend};

rec minMaxPrice;



if (GetValue(state, 1) == GetValue(state.init, 0)) {

minMaxPrice = price;

state = state.undefined;

} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {

if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

state = state.downtrend;

minMaxPrice = priceL;

} else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

state = state.uptrend;

minMaxPrice = priceH;

} else {

state = state.undefined;

minMaxPrice = GetValue(minMaxPrice, 1);

}

} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {

if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {

state = state.downtrend;

minMaxPrice = priceL;

} else {

state = state.uptrend;

minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));

}

} else {

if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {

state = state.uptrend;

minMaxPrice = priceH;

} else {

state = state.downtrend;

minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));

}

}



def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;

def futureDepth = barCount - barNumber;

def tmpLastPeriodBar;

if (isCalculated) {

if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {

tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with

lastPeriodBarAcc = 1

while lastPeriodBarAcc > 0

do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))

then -lastPeriodBarAcc

else lastPeriodBarAcc + 1;

} else {

tmpLastPeriodBar = 0;

}

} else {

tmpLastPeriodBar = Double.NaN;

}



def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue

(tmpLastPeriodBar) else -futureDepth;



rec currentPriceLevel;

rec currentPoints;

if (state == state.uptrend and isCalculated) {

currentPriceLevel =

fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc

= minMaxPrice

do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,

barWithMaxOnPeriodI));

currentPoints =

fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =

Double.NaN

while IsNaN(maxPointOnPeriodAcc)

do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)

then maxPointOnPeriodI

else maxPointOnPeriodAcc;

} else if (state == state.downtrend and isCalculated) {

currentPriceLevel =

fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc

= minMaxPrice

do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,

barWithMinOnPeriodI));

currentPoints =

fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =

Double.NaN

while IsNaN(minPointOnPeriodAcc)

do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)

then minPointOnPeriodI

else minPointOnPeriodAcc;

} else if (!isCalculated and (state == state.uptrend or state ==

state.downtrend)) {

currentPriceLevel = GetValue(currentPriceLevel, 1);

currentPoints = GetValue(currentPoints, 1) + 1;

} else {

currentPoints = 1;

currentPriceLevel = GetValue(price, currentPoints);

}



plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==

state.uptrend then priceH else priceL else if (currentPoints == 0) then

currentPriceLevel else Double.NaN;



rec zzSave = if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)

then if IsNaN(barNumber[-1]) and state == state.uptrend then priceH else priceL

else currentPriceLevel else GetValue(zzSave, 1);



def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if

barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)

- GetValue(zzSave, 1);



def isUp = chg >= 0;

rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and

GetValue(isConf, 1));



"ZZ$".EnableApproximation();

"ZZ$".DefineColor("Up Trend", Color.GREEN);

"ZZ$".DefineColor("Down Trend", Color.RED);

"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"));

"ZZ$".SetLineWeight(2);

DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);

DefineGlobalColor("Up", Color.GREEN);

DefineGlobalColor("Down", Color.RED);



#Arrows



def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else

GetValue(zzL, 1);



def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue

(zzH, 1);



def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1

else dir[1], 0);



def signal = CompoundValue(1,

if dir > 0 and low > zzL then

if signal[1] <= 0 then 1 else signal[1]

else if dir < 0 and high < zzH then

if signal[1] >= 0 then -1 else signal[1]

else signal[1]

, 0);



plot U1 = showArrows and signal > 0 and signal[1] <= 0;

U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

U1.SetDefaultColor(Color.GREEN);

U1.SetLineWeight(4);



plot D1 = showArrows and signal < 0 and signal[1] >= 0;

D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

D1.SetDefaultColor(Color.RED);

D1.SetLineWeight(4);



Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);

Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);



#Supply Demand Areas

def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;

def rLow;

def rHigh;



if BarNumber() == 1 {

rLow = Double.NaN;

rHigh = Double.NaN;

} else if signal crosses 0 {

rLow = low[idx];

rHigh = high[idx];

} else {

rLow = rLow[1];

rHigh = rHigh[1];

}



plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else

Double.NaN;

HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;

LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);



def hlUp = if signal > 0 then HighLine else Double.NaN;

def hlDn = if signal < 0 then HighLine else Double.NaN;



AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);

AddCloud(hlDn, LowLine, Color.RED, Color.RED);

def zzsave1 = if zzSave != zzSave[1] then zzSave[1] else zzsave1[1];
def zzsave2 = if zzsave1 != zzsave1[1] then zzsave1[1] else zzsave2[1];
def lastbar = if !IsNaN(close) and IsNaN(close[-1]) then BarNumber() else Double.NaN;
input debug = no;
AddLabel(debug, zzSave + " " + zzsave1 + " " + zzsave2, Color.WHITE);

def dev = (zzsave - zzsave2) / zzsave;

input showbubble_HHLL  = yes;
input showbubble_dev   = yes;
input showbubble_price = yes;

AddChartBubble(showbubble_HHLL and BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high,  (if zzSave > zzsave2 then "HH" else "LH"), if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(showbubble_HHLL and BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low,  (if zzSave > zzsave2 then "HL" else "LL"), if zzSave > zzsave2 then Color.GREEN else Color.RED, no);

AddChartBubble(showbubble_dev and BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high,  aspercent(dev), if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(showbubble_dev and BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low, aspercent(dev), if zzSave > zzsave2 then Color.GREEN else Color.RED, no);

AddChartBubble(showbubble_price and BarNumber() != HighestAll(lastbar) and priceH == "ZZ$", high, astext(zzsave), if zzSave > zzsave2 then Color.GREEN else Color.RED);
AddChartBubble(showbubble_price and BarNumber() != HighestAll(lastbar) and priceL == "ZZ$", low,  astext(zzsave), if zzSave > zzsave2 then Color.GREEN else Color.RED, no);


## END CODE
 
  • Like
Reactions: LLP

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
399 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top