scrip to scan for 2 point higher high and higher low

samhanoh

New member
VIP
hi
atatch ZIGZAG that work for me fine to see clear the trend ,i need help to :
1) modefy the scirp to show only last 4 boble
2) scan scrip to scan for 2 point higher high and higher low :

Code:
input atrLength = 14;
input atrReversal = 2;

# Weekly high, low, and close
def priceH = high(period = AggregationPeriod.WEEK);
def priceL = low(period = AggregationPeriod.WEEK);
def closeW = close(period = AggregationPeriod.WEEK);

# ATR and pivot calculation
def atr = WildersAverage(TrueRange(priceH, closeW, priceL), atrLength);
def hlPivot = atr / closeW * atrReversal;

# State machine logic
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

# Initial state
if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;

# Undefined state: check for new high or low
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }

# Uptrend state: check for trend reversal or new high
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }

# Downtrend state: check for trend reversal or new low
} else {
    if priceH >= prevMinL + prevMinL * hlPivot {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

# Bar tracking and zigzag logic
def bn = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, bn));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - bn + 1;

# Identify current high or low pivot points
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

# Calculate last valid high/low using fold
def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH)
            do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH
               then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL)
            do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL
               then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

# Plot ZigZag line
plot ZZ;
if bn == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP)
         do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL
            else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH
            else Double.NaN;
} else if bn == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH
         else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL
         else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.SetLineWeight(2);
ZZ.EnableApproximation();

# Detect confirmed zigzag highs/lows
def isHigh = !IsNaN(ZZ) and ZZ == priceH;
def isLow  = !IsNaN(ZZ) and ZZ == priceL;

# Track the last two unique high and low bars
def lastHighBar1 = if isHigh then bn else lastHighBar1[1];
def lastHighBar2 = if isHigh and bn != lastHighBar1[1] then lastHighBar1[1] else lastHighBar2[1];
def lastLowBar1  = if isLow then bn else lastLowBar1[1];
def lastLowBar2  = if isLow and bn != lastLowBar1[1] then lastLowBar1[1] else lastLowBar2[1];

# Show chart bubbles only on the 2 most recent highs/lows
AddChartBubble(bn == lastHighBar1, priceH, "H: " + priceH, Color.GREEN, yes);
AddChartBubble(bn == lastHighBar2, priceH, "H: " + priceH, Color.DARK_GREEN, yes);

AddChartBubble(bn == lastLowBar1, priceL, "L: " + priceL, Color.RED, no);
AddChartBubble(bn == lastLowBar2, priceL, "L: " + priceL, Color.DARK_RED, no);
 

Attachments

  • ScreenHunter 907.jpg
    ScreenHunter 907.jpg
    75.3 KB · Views: 132
Last edited by a moderator:
Solution
hi
atatch ZIGZAG that work for me fine to see clear the trend ,i need help to :
1) modefy the scirp to show only last 4 boble
2) scan scrip to scan for 2 point higher high and higher low :

Code:
input atrLength = 14;
input atrReversal = 2;

# Weekly high, low, and close
def priceH = high(period = AggregationPeriod.WEEK);
def priceL = low(period = AggregationPeriod.WEEK);
def closeW = close(period = AggregationPeriod.WEEK);

# ATR and pivot calculation
def atr = WildersAverage(TrueRange(priceH, closeW, priceL), atrLength);
def hlPivot = atr / closeW * atrReversal;

# State machine logic
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH...
hi
atatch ZIGZAG that work for me fine to see clear the trend ,i need help to :
1) modefy the scirp to show only last 4 boble
2) scan scrip to scan for 2 point higher high and higher low :

Code:
input atrLength = 14;
input atrReversal = 2;

# Weekly high, low, and close
def priceH = high(period = AggregationPeriod.WEEK);
def priceL = low(period = AggregationPeriod.WEEK);
def closeW = close(period = AggregationPeriod.WEEK);

# ATR and pivot calculation
def atr = WildersAverage(TrueRange(priceH, closeW, priceL), atrLength);
def hlPivot = atr / closeW * atrReversal;

# State machine logic
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

# Initial state
if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;

# Undefined state: check for new high or low
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }

# Uptrend state: check for trend reversal or new high
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }

# Downtrend state: check for trend reversal or new low
} else {
    if priceH >= prevMinL + prevMinL * hlPivot {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

# Bar tracking and zigzag logic
def bn = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, bn));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - bn + 1;

# Identify current high or low pivot points
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

# Calculate last valid high/low using fold
def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH)
            do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH
               then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL)
            do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL
               then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

# Plot ZigZag line
plot ZZ;
if bn == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP)
         do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL
            else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH
            else Double.NaN;
} else if bn == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH
         else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL
         else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.SetLineWeight(2);
ZZ.EnableApproximation();

# Detect confirmed zigzag highs/lows
def isHigh = !IsNaN(ZZ) and ZZ == priceH;
def isLow  = !IsNaN(ZZ) and ZZ == priceL;

# Track the last two unique high and low bars
def lastHighBar1 = if isHigh then bn else lastHighBar1[1];
def lastHighBar2 = if isHigh and bn != lastHighBar1[1] then lastHighBar1[1] else lastHighBar2[1];
def lastLowBar1  = if isLow then bn else lastLowBar1[1];
def lastLowBar2  = if isLow and bn != lastLowBar1[1] then lastLowBar1[1] else lastLowBar2[1];

# Show chart bubbles only on the 2 most recent highs/lows
AddChartBubble(bn == lastHighBar1, priceH, "H: " + priceH, Color.GREEN, yes);
AddChartBubble(bn == lastHighBar2, priceH, "H: " + priceH, Color.DARK_GREEN, yes);

AddChartBubble(bn == lastLowBar1, priceL, "L: " + priceL, Color.RED, no);
AddChartBubble(bn == lastLowBar2, priceL, "L: " + priceL, Color.DARK_RED, no);
Modified code to show ZigZag with last x (via input) zigs and zags.
Enjoy!

Code:
input atrLength = 14;
input atrReversal = 2;

# Weekly high, low, and close
def priceH = high(period = AggregationPeriod.WEEK);
def priceL = low(period = AggregationPeriod.WEEK);
def closeW = close(period = AggregationPeriod.WEEK);

# ATR and pivot calculation
def atr = WildersAverage(TrueRange(priceH, closeW, priceL), atrLength);
def hlPivot = atr / closeW * atrReversal;

# State machine logic
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

# Initial state
if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;

# Undefined state: check for new high or low
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }

# Uptrend state: check for trend reversal or new high
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }

# Downtrend state: check for trend reversal or new low
} else {
    if priceH >= prevMinL + prevMinL * hlPivot {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

# Bar tracking and zigzag logic
def bn = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, bn));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - bn + 1;

# Identify current high or low pivot points
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

# Calculate last valid high/low using fold
def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH)
            do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH
               then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL)
            do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL
               then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

# Plot ZigZag line
plot ZZ;
if bn == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP)
         do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL
            else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH
            else Double.NaN;
} else if bn == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH
         else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL
         else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.SetLineWeight(2);
ZZ.EnableApproximation();


##### Added Code #####

input NoOfHighLows = 2;

def ZZHBar = if ZZ == priceH then 1 else double.NaN;
def ZZLBar = if ZZ == priceL then 1 else double.NaN;

def ZZHcnt = if !isNaN(ZZHBar) then ZZHcnt[1]+1 else ZZHcnt[1];
def ZZLcnt = if !isNaN(ZZLBar) then ZZLcnt[1]+1 else ZZLcnt[1];

def cntZZHHi = highestAll(ZZHcnt);
def cntZZLHi = highestAll(ZZLcnt);

def cntZZHCond = if (ZZHcnt) > (cntZZHHi) - NoOfHighLows AND ZZHBar then priceH else double.NaN;
def cntZZLCond = if (ZZLcnt) > (cntZZLHi) - NoOfHighLows AND ZZLBar then priceL else double.NaN;

def LastZZ = if !isNaN(cntZZHCond)then cntZZHCond else if !isNaN(cntZZLCond) then cntZZLCond else double.NaN;

plot LastXZZ = LastZZ;
LastXZZ.EnableApproximation();

AddChartBubble(cntZZHCond, priceH, "H: " + priceH, Color.GREEN, yes);
AddChartBubble(cntZZLCond, priceL, "L: " + priceL, Color.RED, yes);

Below image of the ZZ function colored dashed cyan line, with the last 4 zig and zag bubbles showing a filled cyan line.

ZZ_ShowX.jpg
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
353 Online
Create Post

Similar threads

Similar threads

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