Can't get my code to work to find prior last two peaks.

greco26

Active member
Hi All,

I'm need to identify the second most recent peak as well as the third most recent peak however the methods (#Attempt 1 and 2 in code) I've tried aren't working. This study isn't one I created but im trying to modify it to fit a need I have however I need to reference the 2nd and 3rd peaks (any valleys) in variables I will write. Any ideas?

Code:
script PrevHigh {
    input x = 0;
    input H = high;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevH = GetValue(H, cnt[1]);
    plot count = cnt[1];
} # end PrevHigh

script PrevLow {
    input x = 0;
    input L = low;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevL = GetValue(L, cnt[1]);
    plot count = cnt[1];
} # end PrevLow


script NextHigh {
    input X = 0;
    input H = high;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or H <> GetValue(H, -i)) then i else Double.NaN;
    }
    plot high = if cnt == 0 then 0 else GetValue(H, -cnt);
    plot count = cnt;
} # end NextHigh

script NextLow {
    input X = 0;
    input L = low;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or L <> GetValue(L, -i)) then i else Double.NaN;
    }
    plot low = if cnt == 0 then 999999 else GetValue(L, -cnt);
    plot count = cnt;
} # end NextLow

script RecentHighBar {
    input x = 0;
    input y = 0;
    input h = high;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rH;
    def bar;
    if (BarNumber() == 1) {
        rH = if x then h else 0;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rH = 0;
        bar = 0;
    } else if (x and h > rH[1]) {
        rH = h;
        bar = BarNumber();
    } else {
        rH = rH[1];
        bar = bar[1];
    }
    def count = nextlow(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end REcentHighBar

script RecentLowBar {
    input x = 0;
    input y = 0;
    input l = low;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rl;
    def bar;
    if (BarNumber() == 1) {
        rl = if x then l else 999999;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rl = 999999;
        bar = 0;
    } else if (x and l < rl[1]) {
        rl = l;
        bar = BarNumber();
    } else {
        rl = rl[1];
        bar = bar[1];
    }
    def count = nexthigh(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end RecentLowBar

# define high / low point based on either wicks or candle bodies
input drawFromWicksOrBodies = {default "Wicks", "Bodies"};
def H;
def L;
switch (drawFromWicksOrBodies) {
case "Wicks":
    H = high;
    L = low;
case "Bodies":
    H = Max(open, close);
    L = Min(open, close);
} # end switch

# first order pivots
def p1 = H > H[1] and H > nexthigh(H, H, 0);
def v1 = L < L[1] and L < nextlow(L, L, 0);

# second order pivots
def pp2 = p1 and H > prevhigh(p1, H) and H > nexthigh(p1, H, 0);
def pv2 = v1 and L < prevlow(v1, L) and L < nextlow(v1, L, 0);
#addchartbubble(p1,h,p1,color.magenta);
#addchartbubble(v1,l,v1,color.cyan);

def countPH = prevhigh(pp2).count;
def countPL = prevlow(pv2).count;
def countNH = nexthigh(pp2).count;
def countNL = nextlow(pv2).count;
def b = pp2 and pv2;
def isCloserPrevP = countPH < countPL;
def isCloserPrevV = countPL < countPH;
def isCloserNextP = countNH < countNL;
def isCloserNextV = countNL < countNH;

def pHigh = prevhigh(pp2, H);
def pLow = prevlow(pv2, L);
def vf2;
def pf2;

if (b and isCloserPrevP and pHigh > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and isCloserNextP and pHigh < H and nexthigh(pp2, H) > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and isCloserNextV and pLow > L and nextlow(pv2, L) < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and pHigh < H and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevV and pHigh > H and pLow > L) {
    pf2 = 0;
    vf2 = 1;
} else if (b) {
    pf2 = 0;
    vf2 = 0;
} else {
    pf2 = pp2;
    vf2 = pv2;
}


def p2 = pf2 and BarNumber() == recenthighbar(pf2, vf2, H);
addchartbubble(p2,h,p2,color.cyan);
def v2 = vf2 and BarNumber() == recentlowbar(vf2, pf2, L);
addchartbubble(v2,L,v2,color.magenta);
plot minorLine = if p2 then H else if v2 then L else Double.NaN;
minorLine.EnableApproximation();
minorLine.SetDefaultColor(Color.WHITE);


def BN = Barnumber();
#Attempt 1
def P2BN = if p2 then BN else P2BN[1];
def OneAgoP2BN = if P2BN != P2BN[1] then P2BN[1] else OneAgoP2BN[1];
def TwoAgoP2BN = if OneAgoP2BN != OneAgoP2BN[1] then OneAgoP2BN[1] else TwoAgoP2BN[1];

#troubleshooting bubbles
#addchartbubble(bn==OneAgoP2BN,h,OneAgoP2BN,color.green);
#addchartbubble(bn==TwoAgoP2BN,h,TwoAgoP2BN,color.light_green);

#Attempt 2
def OneAgoP2 = if p2 != p2[1] then p2[1] else OneAgoP2[1];
def TwoAgoP2 = if OneAgoP2 != OneAgoP2[1] then OneAgoP2[1] else TwoAgoP2[1];

#troubleshooting bubbles
#addchartbubble(OneAgoP2,h,OneAgoP2,color.green);
#addchartbubble(TwoAgoP2,h,TwoAgoP2,color.light_green);

 
Hi All,

I'm need to identify the second most recent peak as well as the third most recent peak however the methods (#Attempt 1 and 2 in code) I've tried aren't working. This study isn't one I created but im trying to modify it to fit a need I have however I need to reference the 2nd and 3rd peaks (any valleys) in variables I will write. Any ideas?

Code:
script PrevHigh {
    input x = 0;
    input H = high;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevH = GetValue(H, cnt[1]);
    plot count = cnt[1];
} # end PrevHigh

script PrevLow {
    input x = 0;
    input L = low;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevL = GetValue(L, cnt[1]);
    plot count = cnt[1];
} # end PrevLow


script NextHigh {
    input X = 0;
    input H = high;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or H <> GetValue(H, -i)) then i else Double.NaN;
    }
    plot high = if cnt == 0 then 0 else GetValue(H, -cnt);
    plot count = cnt;
} # end NextHigh

script NextLow {
    input X = 0;
    input L = low;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or L <> GetValue(L, -i)) then i else Double.NaN;
    }
    plot low = if cnt == 0 then 999999 else GetValue(L, -cnt);
    plot count = cnt;
} # end NextLow

script RecentHighBar {
    input x = 0;
    input y = 0;
    input h = high;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rH;
    def bar;
    if (BarNumber() == 1) {
        rH = if x then h else 0;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rH = 0;
        bar = 0;
    } else if (x and h > rH[1]) {
        rH = h;
        bar = BarNumber();
    } else {
        rH = rH[1];
        bar = bar[1];
    }
    def count = nextlow(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end REcentHighBar

script RecentLowBar {
    input x = 0;
    input y = 0;
    input l = low;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rl;
    def bar;
    if (BarNumber() == 1) {
        rl = if x then l else 999999;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rl = 999999;
        bar = 0;
    } else if (x and l < rl[1]) {
        rl = l;
        bar = BarNumber();
    } else {
        rl = rl[1];
        bar = bar[1];
    }
    def count = nexthigh(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end RecentLowBar

# define high / low point based on either wicks or candle bodies
input drawFromWicksOrBodies = {default "Wicks", "Bodies"};
def H;
def L;
switch (drawFromWicksOrBodies) {
case "Wicks":
    H = high;
    L = low;
case "Bodies":
    H = Max(open, close);
    L = Min(open, close);
} # end switch

# first order pivots
def p1 = H > H[1] and H > nexthigh(H, H, 0);
def v1 = L < L[1] and L < nextlow(L, L, 0);

# second order pivots
def pp2 = p1 and H > prevhigh(p1, H) and H > nexthigh(p1, H, 0);
def pv2 = v1 and L < prevlow(v1, L) and L < nextlow(v1, L, 0);
#addchartbubble(p1,h,p1,color.magenta);
#addchartbubble(v1,l,v1,color.cyan);

def countPH = prevhigh(pp2).count;
def countPL = prevlow(pv2).count;
def countNH = nexthigh(pp2).count;
def countNL = nextlow(pv2).count;
def b = pp2 and pv2;
def isCloserPrevP = countPH < countPL;
def isCloserPrevV = countPL < countPH;
def isCloserNextP = countNH < countNL;
def isCloserNextV = countNL < countNH;

def pHigh = prevhigh(pp2, H);
def pLow = prevlow(pv2, L);
def vf2;
def pf2;

if (b and isCloserPrevP and pHigh > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and isCloserNextP and pHigh < H and nexthigh(pp2, H) > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and isCloserNextV and pLow > L and nextlow(pv2, L) < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and pHigh < H and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevV and pHigh > H and pLow > L) {
    pf2 = 0;
    vf2 = 1;
} else if (b) {
    pf2 = 0;
    vf2 = 0;
} else {
    pf2 = pp2;
    vf2 = pv2;
}


def p2 = pf2 and BarNumber() == recenthighbar(pf2, vf2, H);
addchartbubble(p2,h,p2,color.cyan);
def v2 = vf2 and BarNumber() == recentlowbar(vf2, pf2, L);
addchartbubble(v2,L,v2,color.magenta);
plot minorLine = if p2 then H else if v2 then L else Double.NaN;
minorLine.EnableApproximation();
minorLine.SetDefaultColor(Color.WHITE);


def BN = Barnumber();
#Attempt 1
def P2BN = if p2 then BN else P2BN[1];
def OneAgoP2BN = if P2BN != P2BN[1] then P2BN[1] else OneAgoP2BN[1];
def TwoAgoP2BN = if OneAgoP2BN != OneAgoP2BN[1] then OneAgoP2BN[1] else TwoAgoP2BN[1];

#troubleshooting bubbles
#addchartbubble(bn==OneAgoP2BN,h,OneAgoP2BN,color.green);
#addchartbubble(bn==TwoAgoP2BN,h,TwoAgoP2BN,color.light_green);

#Attempt 2
def OneAgoP2 = if p2 != p2[1] then p2[1] else OneAgoP2[1];
def TwoAgoP2 = if OneAgoP2 != OneAgoP2[1] then OneAgoP2[1] else TwoAgoP2[1];

#troubleshooting bubbles
#addchartbubble(OneAgoP2,h,OneAgoP2,color.green);
#addchartbubble(TwoAgoP2,h,TwoAgoP2,color.light_green);

i'm not at my computer so i didn't study your code. it does seem overly complicated.


here are a couple of ways to look back to find a value,
------------------
valuewhen
https://usethinkscript.com/threads/...tos-script-not-pine-studies.11424/#post-99310

------------------
use a reverse count, and look back for a match to the reverse count number,

increment a variable on each peak.
find max quantity of the counter.
create a reverse count, =max - count# +1
rev count will be numbered occurance of signal, looking backwards, from last bar.

https://usethinkscript.com/threads/...remove-it-once-price-hits-it.5257/#post-75706
look at daycntrev formula

Code:
# count desired days on chart
def daycnt = if (bn == 1 and (dayen and (dayen[1] <> dayen))) then 1
 else if bn == 1 then 0
else if (dayen and (dayen[1] <> dayen)) then daycnt[1] + 1
else daycnt[1];
# get qty of desired days on the chart
def daycntmax = HighestAll(daycnt);
# rev the day count, the last day (on the right) is #1
def daycntrev = (daycntmax - daycnt) + 1;
def dayrev_en = (daycntrev <= dayqty);
 
Last edited:

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

i'm not at my computer so i didn't study your code. it does seem overly complicated.


here are a couple of ways to look back to find a value,
------------------
valuewhen
https://usethinkscript.com/threads/...tos-script-not-pine-studies.11424/#post-99310

------------------
use a reverse count, and look back for a match to the reverse count number,

increment a variable on each peak.
find max quantity of the counter.
create a reverse count, =max - count# +1
rev count will be numbered occurance of signal, looking backwards, from last bar.

https://usethinkscript.com/threads/...remove-it-once-price-hits-it.5257/#post-75706
look at daycntrev formula

# count desired days on chart
def daycnt = if (bn == 1 and (dayen and (dayen[1] <> dayen))) then 1
else if bn == 1 then 0
else if (dayen and (dayen[1] <> dayen)) then daycnt[1] + 1
else daycnt[1];
# get qty of desired days on the chart
def daycntmax = HighestAll(daycnt);
# rev the day count, the last day (on the right) is #1
def daycntrev = (daycntmax - daycnt) + 1;
def dayrev_en = (daycntrev <= dayqty);
I tried the reverse count and that looks to work! I should now be able to isolated each of the occurrences. You are so helpful so thank you as always!!

def P2Count = if p2 then P2Count[1] + 1 else P2Count[1];
def P2CountMax = highestall(P2Count);
def P2RevCount = P2CountMax-P2Count +1;
addchartbubble(P2RevCount,high,P2RevCount);
 
I tried the reverse count and that looks to work! I should now be able to isolated each of the occurrences. You are so helpful so thank you as always!!

def P2Count = if p2 then P2Count[1] + 1 else P2Count[1];
def P2CountMax = highestall(P2Count);
def P2RevCount = P2CountMax-P2Count +1;
addchartbubble(P2RevCount,high,P2RevCount);

FYI, your 1st attempt was also working. Here it is with a labels, bubbles.

Capture.jpg
Ruby:
def BN = BarNumber();
#Attempt 1
def P2BN = if p2 then BN else P2BN[1];
def OneAgoP2BN = if P2BN != P2BN[1] then P2BN[1] else OneAgoP2BN[1];
def TwoAgoP2BN = if OneAgoP2BN != OneAgoP2BN[1] then OneAgoP2BN[1] else TwoAgoP2BN[1];
def p2h = if BN == P2BN then H else p2h[1];
def p2h1 = if BN == HighestAll(OneAgoP2BN) then H else p2h1[1];
def p2h2 = if BN == HighestAll(TwoAgoP2BN) then H else p2h2[1];
AddLabel(1, P2BN + " " + OneAgoP2BN + " " + TwoAgoP2BN, Color.YELLOW);
AddLabel(1, p2h + " " + p2h1 + " " + p2h2, Color.WHITE);
AddChartBubble(p2, H, H + "\n" + P2BN, Color.CYAN);
 
FYI, your 1st attempt was also working. Here it is with a labels, bubbles.
yea, I see that now! what I wasn't doing was putting the Highestall for the twoagop2bn. Thats what was causing the issue. Well, now I have 2 ways of doing it! Awesome stuff. Seriously, you guys are so helpful in my learning process and I can't thank you enough!!
 
@SleepyZ @halcyonguy

Either of you guys happen to know why my Buys and Sells aren't being picked up in the strategy? I'm wondering if its because of my reverse counting for the pivot highs/lows.

Thanks, as always, for any advice you can provide!!

~Tim


Code:
def BN = Barnumber();
script PrevHigh {
    input x = 0;
    input H = high;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevH = GetValue(H, cnt[1]);
    plot count = cnt[1];
} # end PrevHigh

script PrevLow {
    input x = 0;
    input L = low;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevL = GetValue(L, cnt[1]);
    plot count = cnt[1];
} # end PrevLow


script NextHigh {
    input X = 0;
    input H = high;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or H <> GetValue(H, -i)) then i else Double.NaN;
    }
    plot high = if cnt == 0 then 0 else GetValue(H, -cnt);
    plot count = cnt;
} # end NextHigh

script NextLow {
    input X = 0;
    input L = low;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or L <> GetValue(L, -i)) then i else Double.NaN;
    }
    plot low = if cnt == 0 then 999999 else GetValue(L, -cnt);
    plot count = cnt;
} # end NextLow

script RecentHighBar {
    input x = 0;
    input y = 0;
    input h = high;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rH;
    def bar;
    if (BarNumber() == 1) {
        rH = if x then h else 0;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rH = 0;
        bar = 0;
    } else if (x and h > rH[1]) {
        rH = h;
        bar = BarNumber();
    } else {
        rH = rH[1];
        bar = bar[1];
    }
    def count = nextlow(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end REcentHighBar

script RecentLowBar {
    input x = 0;
    input y = 0;
    input l = low;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rl;
    def bar;
    if (BarNumber() == 1) {
        rl = if x then l else 999999;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rl = 999999;
        bar = 0;
    } else if (x and l < rl[1]) {
        rl = l;
        bar = BarNumber();
    } else {
        rl = rl[1];
        bar = bar[1];
    }
    def count = nexthigh(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end RecentLowBar

# define high / low point based on either wicks or candle bodies
input drawFromWicksOrBodies = {default "Wicks", "Bodies"};
def H;
def L;
switch (drawFromWicksOrBodies) {
case "Wicks":
    H = high;
    L = low;
case "Bodies":
    H = Max(open, close);
    L = Min(open, close);
} # end switch

# first order pivots
def p1 = H > H[1] and H > nexthigh(H, H, 0);
def v1 = L < L[1] and L < nextlow(L, L, 0);

# second order pivots
def pp2 = p1 and H > prevhigh(p1, H) and H > nexthigh(p1, H, 0);
def pv2 = v1 and L < prevlow(v1, L) and L < nextlow(v1, L, 0);
#addchartbubble(p1,h,p1,color.magenta);
#addchartbubble(v1,l,v1,color.cyan);

def countPH = prevhigh(pp2).count;
def countPL = prevlow(pv2).count;
def countNH = nexthigh(pp2).count;
def countNL = nextlow(pv2).count;
def b = pp2 and pv2;
def isCloserPrevP = countPH < countPL;
def isCloserPrevV = countPL < countPH;
def isCloserNextP = countNH < countNL;
def isCloserNextV = countNL < countNH;

def pHigh = prevhigh(pp2, H);
def pLow = prevlow(pv2, L);
def vf2;
def pf2;

if (b and isCloserPrevP and pHigh > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and isCloserNextP and pHigh < H and nexthigh(pp2, H) > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and isCloserNextV and pLow > L and nextlow(pv2, L) < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and pHigh < H and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevV and pHigh > H and pLow > L) {
    pf2 = 0;
    vf2 = 1;
} else if (b) {
    pf2 = 0;
    vf2 = 0;
} else {
    pf2 = pp2;
    vf2 = pv2;
}



# For reversal back long

def v2 = if !isnan(close) and isnan(close[1]) then double.nan else vf2 and BarNumber() == recentlowbar(vf2, pf2, L);
def V2Count = if bn == 1 then 0 else if v2 then v2Count[1] + 1 else v2Count[1];
def V2CountMax = highestall(V2Count);
def V2RevCount = V2CountMax-V2Count +1;
#addchartbubble(V2RevCount,low,V2RevCount,color.red,no);

def ThirdPL = V2RevCount[1]== 4 and V2RevCount == 3 ;
def ThirdPLbn = if ThirdPL then bn else ThirdPLbn[1];
def ThirdPLval = if bn == highestall(ThirdPLbn) then low else ThirdPLval[1];
addchartbubble(ThirdPL,ThirdPLval,V2RevCount,color.orange);

def SecondPL = V2RevCount[1]== 3 and V2RevCount == 2 ;
def SecondPLbn = if SecondPL then bn else secondPLbn[1];
def SecondPLVal = if bn == highestall(SecondPLbn) then low else SecondPLVal[1];
addchartbubble(SecondPL,SecondPLVal,V2RevCount,color.orange);

def FirstPL = V2RevCount[1]== 2 and V2RevCount == 1;
def FirstPLbn = if FirstPL then bn else FirstPLbn[1];
def FirstPLVal = if bn == highestall(FirstPLbn) then low else FirstPLVal[1];
addchartbubble(FirstPL,FirstPLVal,V2RevCount,color.orange);



# For reversal back short

def p2 = if !isnan(close) and isnan(close[1]) then double.nan else pf2 and BarNumber() == recenthighbar(pf2, vf2, H);
def P2Count = if bn == 1 then 0 else if p2 then P2Count[1] + 1 else P2Count[1];
def P2CountMax = highestall(P2Count);
def P2RevCount = P2CountMax-P2Count +1;

def ThirdPH = P2RevCount[1]== 4 and P2RevCount == 3 ;
def ThirdPHbn = if ThirdPH then bn else ThirdPHbn[1];
def ThirdPHVal = if bn == highestall(ThirdPHbn) then high else ThirdPHVal[1];
addchartbubble(ThirdPH,ThirdPHVal,P2RevCount,color.green);

def SecondPH = P2RevCount[1]== 3 and P2RevCount == 2 ;
def SecondPHbn = if SecondPH then bn else secondPHbn[1];
def SecondPHVal = if bn == highestall(SecondPHbn) then high else SecondPHVal[1];
addchartbubble(SecondPH,SecondPHVal,P2RevCount,color.green);

def FirstPH = P2RevCount[1]== 2 and P2RevCount == 1;
def FirstPHbn = if FirstPH then bn else FirstPHbn[1];
def FirstPHVal = if bn == highestall(FirstPHbn) then High else FirstPHVal[1];
addchartbubble(FirstPH,FirstPHVal,P2RevCount,color.green);

#Pattern
def outside = FirstPHVal > SecondPHVal and FirstPLVal < SecondPLVal;
def OutsideBullish =  firstPLbn > firstPHbn and outside and high[1] < high[2] and close crosses above high[1];
def OutsideBearish = firstPLbn < firstPHbn and outside and Low[1] > Low[2] and close crosses below low[1];

def LE = OutsideBullish ;
def SE = OutsideBearish;

def LXStop = low crosses below low[1] - .25 ;
def SXStop = high crosses above high[1] + .25 ;

AddOrder(OrderType.BUY_TO_OPEN, LE[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = high);
AddOrder(OrderType.SELL_TO_CLOSE,  LXStop[-1], tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX", price = low);

AddOrder(OrderType.SELL_TO_OPEN, SE[-1], arrowcolor = Color.WHITE, tickcolor = Color.MAGENTA, name = "SE", price = low);
AddOrder(OrderType.BUY_TO_CLOSE, SXStop[-1], tickcolor = Color.WHITE, arrowcolor = Color.CYAN, name = "SX", price = high);
 
@SleepyZ @halcyonguy

Either of you guys happen to know why my Buys and Sells aren't being picked up in the strategy? I'm wondering if its because of my reverse counting for the pivot highs/lows.

Thanks, as always, for any advice you can provide!!

~Tim


Code:
def BN = Barnumber();
script PrevHigh {
    input x = 0;
    input H = high;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevH = GetValue(H, cnt[1]);
    plot count = cnt[1];
} # end PrevHigh

script PrevLow {
    input x = 0;
    input L = low;
    def cnt = if x then 1 else cnt[1] + 1;
    plot prevL = GetValue(L, cnt[1]);
    plot count = cnt[1];
} # end PrevLow


script NextHigh {
    input X = 0;
    input H = high;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or H <> GetValue(H, -i)) then i else Double.NaN;
    }
    plot high = if cnt == 0 then 0 else GetValue(H, -cnt);
    plot count = cnt;
} # end NextHigh

script NextLow {
    input X = 0;
    input L = low;
    input Z = 1;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def barX = if X then BarNumber() else 0;
    def lastX = HighestAll(barX);
    def cnt;
    if BarNumber() >= lastX
    then
    {
        cnt = 0;
    } else {
        cnt = fold i = 1 to lastBar with a = Double.NaN while IsNaN(a) do
                if IsNaN(GetValue(close, -i)) then 0 else
                if GetValue(X, -i) and (Z or L <> GetValue(L, -i)) then i else Double.NaN;
    }
    plot low = if cnt == 0 then 999999 else GetValue(L, -cnt);
    plot count = cnt;
} # end NextLow

script RecentHighBar {
    input x = 0;
    input y = 0;
    input h = high;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rH;
    def bar;
    if (BarNumber() == 1) {
        rH = if x then h else 0;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rH = 0;
        bar = 0;
    } else if (x and h > rH[1]) {
        rH = h;
        bar = BarNumber();
    } else {
        rH = rH[1];
        bar = bar[1];
    }
    def count = nextlow(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end REcentHighBar

script RecentLowBar {
    input x = 0;
    input y = 0;
    input l = low;
    def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());
    def rl;
    def bar;
    if (BarNumber() == 1) {
        rl = if x then l else 999999;
        bar = if x then BarNumber() else 0;
    } else if (y) {
        rl = 999999;
        bar = 0;
    } else if (x and l < rl[1]) {
        rl = l;
        bar = BarNumber();
    } else {
        rl = rl[1];
        bar = bar[1];
    }
    def count = nexthigh(y).count;
    plot barNumber = if count == 0 then GetValue(bar, BarNumber() - lastBar) else GetValue(bar[1], -count);
} # end RecentLowBar

# define high / low point based on either wicks or candle bodies
input drawFromWicksOrBodies = {default "Wicks", "Bodies"};
def H;
def L;
switch (drawFromWicksOrBodies) {
case "Wicks":
    H = high;
    L = low;
case "Bodies":
    H = Max(open, close);
    L = Min(open, close);
} # end switch

# first order pivots
def p1 = H > H[1] and H > nexthigh(H, H, 0);
def v1 = L < L[1] and L < nextlow(L, L, 0);

# second order pivots
def pp2 = p1 and H > prevhigh(p1, H) and H > nexthigh(p1, H, 0);
def pv2 = v1 and L < prevlow(v1, L) and L < nextlow(v1, L, 0);
#addchartbubble(p1,h,p1,color.magenta);
#addchartbubble(v1,l,v1,color.cyan);

def countPH = prevhigh(pp2).count;
def countPL = prevlow(pv2).count;
def countNH = nexthigh(pp2).count;
def countNL = nextlow(pv2).count;
def b = pp2 and pv2;
def isCloserPrevP = countPH < countPL;
def isCloserPrevV = countPL < countPH;
def isCloserNextP = countNH < countNL;
def isCloserNextV = countNL < countNH;

def pHigh = prevhigh(pp2, H);
def pLow = prevlow(pv2, L);
def vf2;
def pf2;

if (b and isCloserPrevP and pHigh > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and isCloserNextP and pHigh < H and nexthigh(pp2, H) > H) {
    pf2 = 0;
    vf2 = 1;
} else if (b and isCloserPrevV and isCloserNextV and pLow > L and nextlow(pv2, L) < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevP and pHigh < H and pLow < L) {
    pf2 = 1;
    vf2 = 0;
} else if (b and isCloserPrevV and pHigh > H and pLow > L) {
    pf2 = 0;
    vf2 = 1;
} else if (b) {
    pf2 = 0;
    vf2 = 0;
} else {
    pf2 = pp2;
    vf2 = pv2;
}



# For reversal back long

def v2 = if !isnan(close) and isnan(close[1]) then double.nan else vf2 and BarNumber() == recentlowbar(vf2, pf2, L);
def V2Count = if bn == 1 then 0 else if v2 then v2Count[1] + 1 else v2Count[1];
def V2CountMax = highestall(V2Count);
def V2RevCount = V2CountMax-V2Count +1;
#addchartbubble(V2RevCount,low,V2RevCount,color.red,no);

def ThirdPL = V2RevCount[1]== 4 and V2RevCount == 3 ;
def ThirdPLbn = if ThirdPL then bn else ThirdPLbn[1];
def ThirdPLval = if bn == highestall(ThirdPLbn) then low else ThirdPLval[1];
addchartbubble(ThirdPL,ThirdPLval,V2RevCount,color.orange);

def SecondPL = V2RevCount[1]== 3 and V2RevCount == 2 ;
def SecondPLbn = if SecondPL then bn else secondPLbn[1];
def SecondPLVal = if bn == highestall(SecondPLbn) then low else SecondPLVal[1];
addchartbubble(SecondPL,SecondPLVal,V2RevCount,color.orange);

def FirstPL = V2RevCount[1]== 2 and V2RevCount == 1;
def FirstPLbn = if FirstPL then bn else FirstPLbn[1];
def FirstPLVal = if bn == highestall(FirstPLbn) then low else FirstPLVal[1];
addchartbubble(FirstPL,FirstPLVal,V2RevCount,color.orange);



# For reversal back short

def p2 = if !isnan(close) and isnan(close[1]) then double.nan else pf2 and BarNumber() == recenthighbar(pf2, vf2, H);
def P2Count = if bn == 1 then 0 else if p2 then P2Count[1] + 1 else P2Count[1];
def P2CountMax = highestall(P2Count);
def P2RevCount = P2CountMax-P2Count +1;

def ThirdPH = P2RevCount[1]== 4 and P2RevCount == 3 ;
def ThirdPHbn = if ThirdPH then bn else ThirdPHbn[1];
def ThirdPHVal = if bn == highestall(ThirdPHbn) then high else ThirdPHVal[1];
addchartbubble(ThirdPH,ThirdPHVal,P2RevCount,color.green);

def SecondPH = P2RevCount[1]== 3 and P2RevCount == 2 ;
def SecondPHbn = if SecondPH then bn else secondPHbn[1];
def SecondPHVal = if bn == highestall(SecondPHbn) then high else SecondPHVal[1];
addchartbubble(SecondPH,SecondPHVal,P2RevCount,color.green);

def FirstPH = P2RevCount[1]== 2 and P2RevCount == 1;
def FirstPHbn = if FirstPH then bn else FirstPHbn[1];
def FirstPHVal = if bn == highestall(FirstPHbn) then High else FirstPHVal[1];
addchartbubble(FirstPH,FirstPHVal,P2RevCount,color.green);

#Pattern
def outside = FirstPHVal > SecondPHVal and FirstPLVal < SecondPLVal;
def OutsideBullish =  firstPLbn > firstPHbn and outside and high[1] < high[2] and close crosses above high[1];
def OutsideBearish = firstPLbn < firstPHbn and outside and Low[1] > Low[2] and close crosses below low[1];

def LE = OutsideBullish ;
def SE = OutsideBearish;

def LXStop = low crosses below low[1] - .25 ;
def SXStop = high crosses above high[1] + .25 ;

AddOrder(OrderType.BUY_TO_OPEN, LE[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = high);
AddOrder(OrderType.SELL_TO_CLOSE,  LXStop[-1], tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX", price = low);

AddOrder(OrderType.SELL_TO_OPEN, SE[-1], arrowcolor = Color.WHITE, tickcolor = Color.MAGENTA, name = "SE", price = low);
AddOrder(OrderType.BUY_TO_CLOSE, SXStop[-1], tickcolor = Color.WHITE, arrowcolor = Color.CYAN, name = "SX", price = high);

Replacing the Pattern code with this produced results. Without the highestall function the values were not populating

Capture.jpg
Ruby:
#Pattern
def outside = highestall(FirstPHVal) > highestall(SecondPHVal) and highestall(FirstPLVal) < highestall(SecondPLVal);
def OutsideBullish =  highestall(firstPLbn) > highestall(firstPHbn) and outside and high[1] < high[2] and close crosses above high[1];
def OutsideBearish = highestall(firstPLbn) < highestall(firstPHbn) and outside and Low[1] > Low[2] and close crosses below low[1];
 
@SleepyZ
@halcyonguy

I'm having an issue where my signals in my strategy aren't being recognized on some charts/timesframes. It seems like the only time a historical signal populates is when the signal is currently active. If there is no current signal, nothing populates in the P/L back testing section nor does the test bubble illuminate. This is what I've been troubleshooting for about 2 straight days but can't figure it out. Hopefully something jumps out at you guys. Example: AAL on the 15 min chart. Thanks in advance!!

def FracLong = Prev2DnL < Prev1DnL and Prev1DnL > DnL and DnL > Prev2DnL and h[1] < h[2] and h > h[1] ;
def FracShort = Prev2UpH > Prev1UpH and Prev1UpH < UpH and UpH < Prev2UpH and l[1] > l[2] and l < l[1] ;
#Test Bubbles
#AddChartBubble(FracLong, high, "Signal Long", Color.GREEN);
#AddChartBubble(FracShort, low, "Signal Short", Color.RED);


Code:
input TakeProfitPct = .5; #hint, 1 is 1 pct, .5 is 1/2 percent, etc
def takeprofitconverstionLong = 1 + (TakeProfitPct / 100);
addlabel(1,(takeprofitconverstionLong*close)-close, color.white);
def takeprofitconverstionshort = 1 - (TakeProfitPct / 100);
addlabel(1,(takeprofitconverstionshort*close)-close, color.white);


input Major_bubbleoffset = .0005;
input Major_percentamount = .15;
input Major_revAmount = .2;
input Major_atrreversal = 3.75;
input Major_atrlength = 5;
def BN = BarNumber();
def zz = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = Major_percentamount, "absolute reversal" = Major_revAmount, "atr length" = Major_atrlength, "atr reversal" = Major_atrreversal);
def reversalAmount        = if (close * Major_percentamount / 100) > Max(Major_revAmount < Major_atrreversal * Major_atrlength, Major_revAmount) then (close * Major_percentamount / 100) else if Major_revAmount < Major_atrreversal * Major_atrlength then Major_atrreversal * Major_atrlength else Major_revAmount;
rec zzSave = if !IsNaN(zz) then zz else GetValue(zzSave, 1);
def chg = (if zzSave == high then high else low) - GetValue(zzSave, 1);
def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue(zz, 1)) and GetValue(isConf, 1));
def zzd = if isUp then 1 else 0;
plot zzp = if zzd <= 1 then zz else Double.NaN;
zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp.SetStyle(Curve.FIRM);
zzp.EnableApproximation();
zzp.SetLineWeight(1);
 
#Price Change between zigzags
def xxhigh = if zzSave == high then  high else xxhigh[1];
def chghigh = high - xxhigh[1];
def xxlow = if zzSave == low then low else xxlow[1];
def chglow = low - xxlow[1];
input showMagnitudeBubbles = yes;
AddChartBubble(showMagnitudeBubbles and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + Major_bubbleoffset)  else low * (1 - Major_bubbleoffset)   , "$" + chg , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);
 
 
#Price at High/Low
input Major_showBubblesprice = no;
AddChartBubble(Major_showBubblesprice and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + Major_bubbleoffset)  else low * (1 - Major_bubbleoffset)   , if isUp then "$" + high else "$" + low , if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, isUp);

input Minor_bubbleoffset2 = .0005;
input Minor_percentamount2 = .01;
input Minor_revAmount2 = .1;
input Minor_atrreversal2 = 2.0;
input Minor_atrlength2 = 9;
def zz2 = ZigZagHighLow("price h" = high, "price l" = low, "percentage reversal" = Minor_percentamount2, "absolute reversal" = Minor_revAmount2, "atr length" = Minor_atrlength2, "atr reversal" = Minor_atrreversal2);
def reversalAmount2        = if (close * Minor_percentamount2 / 100) > Max(Minor_revAmount2 < Minor_atrreversal2 * Minor_atrlength2, Minor_revAmount2) then (close * Minor_percentamount2 / 100) else if Minor_revAmount2 < Minor_atrreversal2 * Minor_atrlength2 then Minor_atrreversal2 * Minor_atrlength2 else Minor_revAmount2;
rec zzSave2 = if !IsNaN(zz2) then zz2 else GetValue(zzSave2, 1);
def chg2 = (if zzSave2 == high then high else low) - GetValue(zzSave2, 1);
def isUp2 = chg2 >= 0;
rec isConf2 = AbsValue(chg2) >= reversalAmount2 or (IsNaN(GetValue(zz2, 1)) and GetValue(isConf2, 1));
def zzd2 = if isUp2 then 1 else 0;
plot zzp2 = if zzd2 <= 1 then zz2 else Double.NaN;
#zzp.AssignValueColor(if zzd == 1 then Color.GREEN else if zzd == 0 then Color.RED else Color.DARK_ORANGE);
zzp2.SetDefaultColor(Color.WHITE);
zzp2.SetStyle(Curve.FIRM);
zzp2.EnableApproximation();
zzp2.SetLineWeight(1);


#Arrows
def zzL = if !IsNaN(zz) and !isUp then low else GetValue(zzL, 1);
def zzH = if !IsNaN(zz) and isUp then high else GetValue(zzH, 1);
def dir = CompoundValue(1, if zzL != zzL[1] or low == zzL[1] and low == zzSave then 1 else if zzH != zzH[1] or high == zzH[1] and high == zzSave 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);
input showarrows = yes;
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);
 
 
#Alerts
input usealerts = no;
Alert(usealerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(usealerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);
 
 
#Supply Demand Areas
rec data1 = CompoundValue(1, if (zzSave == high or zzSave == low) then data1[1] + 1 else data1[1], 0);
def datacount1 = (HighestAll(data1) - data1[1]);
input numbersuppdemandtoshow = 1;
input showSupplyDemand = {default Pivot, Arrow, None};
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;
if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}

plot HighLine = if datacount1 <= numbersuppdemandtoshow and showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rHigh != 0 then rHigh else Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);

plot LowLine = if datacount1 <= numbersuppdemandtoshow and  showSupplyDemand != showSupplyDemand.None and !IsNaN(close) and rLow != 0 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;



def zzL_Minor = if !IsNaN(zz2) and !isUp2 then low else GetValue(zzL_Minor, 1);
def zzH_Minor = if !IsNaN(zz2) and isUp2 then high else GetValue(zzH_Minor, 1);
def dir_Minor = CompoundValue(1, if zzL_Minor != zzL_Minor[1] or low == zzL_Minor[1] and low == zzSave2 then 1 else if zzH_Minor != zzH_Minor[1] or high == zzH_Minor[1] and high == zzSave2 then -1 else dir_Minor[1], 0);
def signal_Minor = CompoundValue(1, if dir_Minor > 0 and low > zzL_Minor then if signal_Minor[1] <= 0 then 1 else signal_Minor[1] else if dir_Minor < 0 and high < zzH_Minor then if signal_Minor[1] >= 0 then -1 else signal_Minor[1]    else signal_Minor[1], 0);

#Minor Supply Demand Areas
rec data1_Minor = CompoundValue(1, if (zzSave2 == high or zzSave2 == low) then data1_Minor [1] + 1 else data1_Minor [1], 0);
def datacount1_Minor = (HighestAll(data1_Minor) - data1_Minor[1]);
input numbersuppdemandtoshow_Minor = 3;
input showSupplyDemand_Minor = {default Pivot, Arrow, None};
def idx_Minor = if showSupplyDemand_Minor  == showSupplyDemand_Minor .Pivot then 1 else 0;
def rLow_Minor;
def rHigh_Minor;
if signal_Minor crosses 0 {
    rLow_Minor = low[idx_Minor];
    rHigh_Minor = high[idx_Minor];
} else {
    rLow_Minor = rLow_Minor[1];
    rHigh_Minor = rHigh_Minor[1];
}



#Minor


def zzh_minorBN = if high == zzH_Minor then BN else zzh_minorBN[1];
#addchartbubble(bn == zzh_minorBN, zzh_minorval, zzh_minorval,color.yellow);

def zzhcount = if BN == 1 then 0 else if BN == zzh_minorBN then zzhcount[1] + 1 else zzhcount[1];
def zzhcountMax = HighestAll(zzhcount);
def zzhRevCount = zzhcountMax - zzhcount + 1;


def ThirdPHCount = if zzhRevCount[1] == 5 and zzhRevCount == 4 then high else ThirdPHCount[1] ;
def ThirdPHCountBN = if zzhRevCount[1] == 5 and zzhRevCount == 4 then BN else ThirdPHCountBN[1] ;
AddChartBubble(BN == ThirdPHCountBN, ThirdPHCount, " 3 ", Color.GREEN);
def ThirdPH = ThirdPHCount;

def SecondPHCount = if zzhRevCount[1] == 4 and zzhRevCount == 3 then high else SecondPHCount[1] ;
def SecondPHCountBN = if zzhRevCount[1] == 4 and zzhRevCount == 3 then BN else SecondPHCountBN[1] ;
AddChartBubble(BN == SecondPHCountBN, SecondPHCount, " 2 ", Color.GREEN);
def SecondPH = SecondPHCount;

def FirstPHCount = if zzhRevCount[1] == 3 and zzhRevCount == 2 then high else FirstPHCount[1];
def FirstPHCountbn = if zzhRevCount[1] == 3 and zzhRevCount == 2 then BN else FirstPHCountbn[1] ;
AddChartBubble(BN == FirstPHCountbn, FirstPHCount, " 1 ", Color.GREEN);
def FirstPHHighestBN = highestall(FirstPHCountBN);
#def FirstPH = if bn == FirstPHCountbn then high else FirstPH[1];;
def FirstPH = FirstPHCount;

def zzl_minorBN = if low == zzL_Minor then BN else zzl_minorBN[1];
#addchartbubble(bn == zzl_minorBN, zzl_minorval, zzl_minorval,color.yellow);

def zzlcount = if BN == 1 then 0 else if BN == zzl_minorBN then zzlcount[1] + 1 else zzlcount[1];
def zzlcountMax = HighestAll(zzlcount);
def zzlRevCount = zzlcountMax - zzlcount + 1;

def ThirdPLCount = if zzlRevCount[1] == 5 and zzlRevCount == 4 then low else ThirdPLCount[1] ;
def ThirdPLCountBN = if zzlRevCount[1] == 5 and zzlRevCount == 4 then BN else ThirdPLCountBN[1] ;
AddChartBubble(BN == ThirdPLCountBN, ThirdPLCount, " 3 ", Color.RED, no);
def ThirdPL = ThirdPLCount;

def SecondPLCount = if zzlRevCount[1] == 4 and zzlRevCount == 3 then low else SecondPLCount[1] ;
def SecondPLCountBN = if zzlRevCount[1] == 4 and zzlRevCount == 3 then BN else SecondPLCountBN[1] ;
AddChartBubble(BN == SecondPLCountBN, SecondPLCount, " 2 ", Color.RED, no);
def SecondPL = SecondPLCount;

def FirstPLCount = if zzlRevCount[1] == 3 and zzlRevCount == 2 then low else FirstPLCount[1] ;
def FirstPLCountbn = if zzlRevCount[1] == 3 and zzlRevCount == 2 then BN else FirstPLCountBN[1] ;
AddChartBubble(BN == FirstPLCountbn, FirstPLCount, " 1 ", Color.RED, no);
def FirstPL = FirstPLCount;

input showsupplydemandcloud = yes;
AddCloud(if showsupplydemandcloud then hlUp else Double.NaN, LowLine, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showsupplydemandcloud then hlDn else Double.NaN, LowLine, Color.LIGHT_RED, Color.LIGHT_RED);
 
 
#Store Previous Major Data
def zzsave1Major = if !IsNaN(zzSave) then zzSave else zzsave1Major[1];
def zzsave2Major = zzsave1Major;
rec priorzz1Major = if zzsave2Major  != zzsave2Major[1]  then zzsave2Major[1]  else priorzz1Major[1];
rec priorzz2Major = if priorzz1Major != priorzz1Major[1] then priorzz1Major[1] else priorzz2Major[1];
rec priorzz3Major = if priorzz2Major != priorzz2Major[1] then priorzz2Major[1] else priorzz3Major[1];



#Highestall and Lowestall Major lines
def zzsave2highbn = if high == HighestAll(zzsave2Major) then BN else zzsave2highbn[1];
def highestall = HighestAll(zzsave2highbn);
def highestallprice = if BN == highestall then high else highestallprice[1];
plot highestallpriceline = if BN > highestall then  highestallprice else Double.NaN;
#addchartbubble(bn==highestall,high,highestall,color.yellow);

highestallpriceline.SetDefaultColor(Color.LIGHT_GREEN);
highestallpriceline.SetPaintingStrategy(PaintingStrategy.LINE);
highestallpriceline.SetStyle(Curve.SHORT_DASH);

def zzsave2lowbn = if low == LowestAll(zzsave2Major) then BN else zzsave2lowbn[1];
def lowestall = HighestAll(zzsave2lowbn);
def lowestallprice = if BN == lowestall then low else lowestallprice[1];
plot lowestallpriceline = if BN > lowestall then  lowestallprice else Double.NaN;
#addchartbubble(bn==lowestall,low,lowestall,color.white);

lowestallpriceline.SetDefaultColor(Color.PINK);
lowestallpriceline.SetPaintingStrategy(PaintingStrategy.LINE);
lowestallpriceline.SetStyle(Curve.SHORT_DASH);


#Volume at Reversals
def vol = if BarNumber() == 0 then 0 else volume + vol[1];
def vol1 = if BarNumber() == 1 then volume else vol1[1];
def xxvol = if zzSave == high or zzSave == low then TotalSum(volume) else xxvol[1];
def chgvol =  if xxvol - xxvol[1] + vol1 == vol then vol else xxvol - xxvol[1];
input showBubblesVolume = no;
AddChartBubble(showBubblesVolume and !IsNaN(zz) and BarNumber() != 1, if isUp then high * (1 + Major_bubbleoffset)  else low * (1 - Major_bubbleoffset), chgvol, if isUp and chghigh > 0 then Color.GREEN else if isUp and chghigh < 0 then Color.RED else if isUp then Color.YELLOW else if !isUp and chglow > 0 then Color.GREEN else if !isUp and chglow < 0 then Color.RED else Color.YELLOW, if isUp then yes else no );



########################################################

def h = high;
def L = low;

def UpH = HighestAll(FirstPH);
def Prev1UpH = HighestAll(SecondPH);
def Prev2UpH = HighestAll(ThirdPH);


def DnL = highestall(FirstPL);
def Prev1DnL = highestall(SecondPL);
def Prev2DnL = highestall(ThirdPL);


def FracLong =  Prev2DnL < Prev1DnL and Prev1DnL > DnL and DnL > Prev2DnL and h[1] < h[2] and h > h[1] ;
def FracShort =  Prev2UpH > Prev1UpH and Prev1UpH < UpH and UpH < Prev2UpH and l[1] > l[2] and l < l[1] ;


#Test Bubbles
#AddChartBubble(FracLong, high, "Signal Long", Color.GREEN);
#AddChartBubble(FracShort, low, "Signal Short", Color.RED);

def Entry = EntryPrice();
def EntryBN = if Entry then BN else EntryBN[1];


def FracLongAlert = Prev2DnL < Prev1DnL and Prev1DnL > L and L > Prev2DnL;
def FracShortAlert = Prev2UpH > Prev1UpH and Prev1UpH < h and h < Prev2UpH;

#def LE = FracLong;
def LE = if IsNaN(Entry) then FracLong else 0 ;
def LEBN = if LE then BN else LEBN[1];
def LowStop = if BN == LEBN then l else LowStop[1];

#def SE = FracShort;
def SE = if IsNaN(Entry) then FracShort else 0 ;
def SEBN = if SE then BN else SEBN[1];
def HighStop = if BN == SEBN then h else HighStop[1];

def lxstop = L < LowStop;
def lx = h > Entry * takeprofitconverstionLong ;

def sxstop = h > HighStop;
def sx = L < Entry * takeprofitconverstionshort ;

AddOrder(OrderType.BUY_TO_OPEN, Le[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = h);
AddOrder(OrderType.SELL_TO_CLOSE, lxstop[-1], tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX Stop", price = l);
AddOrder(OrderType.SELL_TO_CLOSE, lx[-1], tickcolor = Color.WHITE, arrowcolor = Color.GREEN, name = "LX Profit", price = h);


AddOrder(OrderType.SELL_TO_OPEN, SE[-1], arrowcolor = Color.WHITE, tickcolor = Color.YELLOW, name = "SE", price = l);
AddOrder(OrderType.BUY_TO_CLOSE, sxstop[-1], tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "SX Stop", price = h);
AddOrder(OrderType.BUY_TO_CLOSE, sx[-1], tickcolor = Color.WHITE, arrowcolor = Color.GREEN, name = "SX Profit", price = l);
 
Hey guys, I figured it out! sweeeeet!

great, please post some code if you can.


fyi
your addchartbubble() functions are missing the last, 5th parameter, up/down.
the default is yes, which places the insertion point below the bubble (it points down)
if set to no, and used on your valleys, the bubble will point up and be below the insertion point, and not cover up candles.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddChartBubble
 
I keep having the same issue where my Strategy won't populate older statistics on the P/L. @SleepyZ , this is the same issue you helped me with a few days ago but I still can't seem to get this version to work properly and populate the historic buys and sells. Let me know if anything jumps out at you.....or @halcyonguy or @Joshua. I honestly dont know what I'd do without the support of you guys and this community. Truly awesome. Thank you as always.


Code:
input TakeProfitPct = .5; #hint, 1 is 1 pct, .5 is 1/2 percent, etc
def takeprofitconverstionLong = 1 + (TakeProfitPct / 100);
AddLabel(1, (takeprofitconverstionLong * close) - close, Color.WHITE);
def takeprofitconverstionshort = 1 - (TakeProfitPct / 100);
AddLabel(1, (takeprofitconverstionshort * close) - close, Color.WHITE);
def bn = BarNumber();
def Latest_bar = if IsNaN(close[-1]) then bn else Latest_bar[1];


input Trend_TimeFrame = {default Current, Five, Ten, Fifteen, Twenty, Thirty, Sixty, Two_Hour, Four_Hour, Day};

def TimeFrame;
switch (Trend_TimeFrame) {
case Current:
    TimeFrame = getAggregationPeriod();
case Five:
    TimeFrame = AggregationPeriod.FIVE_MIN;
case Ten:
    TimeFrame = AggregationPeriod.TEN_MIN;
case Fifteen:
     TimeFrame = AggregationPeriod.FIFTEEN_MIN;
case Twenty:
    TimeFrame = AggregationPeriod.TWENTY_MIN;
case Thirty:
    TimeFrame = AggregationPeriod.THIRTY_MIN;
case Sixty:
    TimeFrame = AggregationPeriod.HOUR;
case Two_Hour:
    TimeFrame = AggregationPeriod.Two_Hours;
case Four_Hour:
    TimeFrame = AggregationPeriod.Four_Hours;
case Day:
    TimeFrame = AggregationPeriod.DAY;
}

AddLabel(yes, Trend_TimeFrame, Color.WHITE);






#######################################################################################################

#TREND SECTION

#######################################################################################################


input LBPeriod = 2;

#--------------------------------------------------------------

def HH = Highest(high(Period = Timeframe), LBPeriod);
def marketHigh1 = if HH > HH[-LBPeriod] then HH else HH[-LBPeriod];
def markedHigh1 = high(Period = Timeframe) == marketHigh1;

def lastMarkedHigh1 = CompoundValue(1, if IsNaN( markedHigh1) then lastMarkedHigh1[1] else if markedHigh1 then high(Period = Timeframe) else lastMarkedHigh1[1], high(Period = Timeframe));

#-------------------------------------------------------------

plot HP = lastMarkedHigh1;
def HPBN = if high == HP then bn else HPBN[1];
def HallHPBN = highestall(HPBN);
#--------------------------------------------------------------

def LL = Lowest(low(Period = Timeframe), LBPeriod);
#--------------------------------------------------------------
def marketLow1 = if LL < LL[-LBPeriod] then LL else LL[-LBPeriod];
def markedLow1 = low(Period = Timeframe) == marketLow1;
def lastMarkedLow1 = CompoundValue(1, if IsNaN(markedLow1) then lastMarkedLow1[1] else if markedLow1 then low(Period = Timeframe) else lastMarkedLow1[1], low(Period = Timeframe));

plot LV = lastMarkedLow1 ;
def LVBN = if low(Period = Timeframe) == LV then bn else LVBN[1];


#--------------------------------------------------------------
HP.SetPaintingStrategy(PaintingStrategy.DASHES);
HP.SetDefaultColor(Color.GREEN);

#--------------------------------------------------------------
LV.SetPaintingStrategy(PaintingStrategy.DASHES);
LV.SetDefaultColor(Color.RED);


####TREND CALCS###

def Phigh = HP;
def HighCount = if bn == 1 then 0 else if High==Phigh then HighCount[1] + 1 else HighCount[1];
def HighCountMax = highestall(HighCount);
def HighRevCount = HighCountMax-HighCount +1;


def High1 =  HighRevCount[1]== 2 and HighRevCount == 1;
def High1BN = if  high1 then bn else High1BN[1];
def High1Val = if bn == High1BN then High else High1Val[1];
addchartbubble(bn == highestall(high1bn),High,HighRevCount,color.violet);


def High2 = HighRevCount[1]== 3 and HighRevCount == 2 ;
def High2BN = if High2 then bn else High2BN[1];
def High2Val = if bn == highestall(High2BN) then High else High2Val[1];
addchartbubble(bn == highestall(high2bn),High,HighRevCount,color.violet);

def High3 = HighRevCount[1]== 4 and HighRevCount == 3 ;
def High3BN = if High3 then bn else High3BN[1];
def High3Val = if bn == highestall(High3BN) then High else High3Val[1];
addchartbubble(bn == highestall(high3bn),High,HighRevCount,color.violet);


def PLow = lv;
def LowCount = if bn == 1 then 0 else if low==Plow then LowCount[1] + 1 else LowCount[1];
def LowCountMax = highestall(LowCount);
def LowRevCount = LowCountMax-LowCount +1;

def Low1 =  LowRevCount[1]== 2 and LowRevCount == 1;
def Low1BN = if Low1 then bn else Low1BN[1];
def Low1Val = if bn == highestall(Low1BN) then low else Low1Val[1];
addchartbubble(bn == highestall(Low1BN),low,LowRevCount,color.green);

def Low2 = LowRevCount[1]== 3 and LowRevCount == 2 ;
def Low2BN = if Low2 then bn else Low2BN[1];
def Low2Val = if bn == highestall(Low2BN) then low else Low2Val[1];
addchartbubble(BN == highestall(low2bn),low,LowRevCount,color.green);

def Low3 = LowRevCount[1]== 4 and LowRevCount == 3 ;
def Low3BN = if Low3 then bn else Low3BN[1];
def Low3Val = if bn == highestall(Low3BN) then low else Low3Val[1];
addchartbubble(bn == highestall(low3bn),low,LowRevCount,color.green);


#####TREND SIGNALS######

def HigherHighs = high3val < high2val and high2val < high1val ;
def HigherLows =  Low3val < low2val and low2val < low1val ;

def LowerHighs =  high3val > high2val and high2val > high1val ;
def LowerLows =  Low3val > low2val and low2val > low1val ;
#def LowerLows =  if highestall(Low3val) > highestall(low2val) and highestall(low2val) > highestall(low1val) then 1 else lowerlows[1];

def TrendLong = HigherHighs or higherlows;
def TrendShort = LowerLows ;
addlabel(1, if trendlong then "Trend Long" else if Trendshort then "Trend Short" else " ", color.white);


###SIGNALS###

input showArrows = yes;
input x = 5; #bars amount

# Calculate the length of the candle's wicks
def UpperWick = high - Max(open, close);
def LowerWick = Min(open, close) - low;

# Calculate the length of the candle's body
def CandleBody = bodyheight();
def range = High - low;
def cond = range == highest(range,x);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = (lowerWick / CandleBody >= 1.25) and (upperWick / CandleBody <= 1);
def Shooter = (upperWick / CandleBody >= 1.25) and (lowerWick / CandleBody <= 1);

def TwoUp = low >= Low[1] and high >= high[1];
def TwoDown = High <= High[1] and Low <= Low[1];
def Inside = high < High[1] and Low > Low[1];
def OutsideRed = high >= High[1] and Low <= Low[1] and close < open;
def OutsideGreen = high >= High[1] and Low <= Low[1] and close > open;

Def TwoTwoShort = trendshort and TwoUp[1] and close[1] > open[1] and twodown;
addchartbubble(TwoTwoShort,low,TwoTwoShort);

Def ShooterTwoShort = trendshort and twoup[2]  and close[2] > open[2] and shooter[1] and TwoDown;
addchartbubble(ShooterTwoShort,low,ShooterTwoShort,color.violet);

Def OutsideRedTwoShort = trendshort and TwoUp[1] and OutsideRed;
addchartbubble(OutsideRedTwoShort,low,OutsideRedTwoShort,color.yellow);

Def TwoTwoLong = trendlong and Twodown[1] and close[1] < open[1] and twoup;
addchartbubble(TwoTwoLong,high,TwoTwoLong);

Def HammerTwoLong = trendlong and twodown[2]  and close[2] < open[2] and hammer[1] and Twoup;
addchartbubble(HammerTwoLong,high,HammerTwoLong,color.violet);

Def OutsideGreenTwoLong = trendlong and TwoDown[1] and OutsideGreen;
addchartbubble(OutsideGreenTwoLong,high,OutsideGreenTwoLong,color.yellow);

################################################################################################################

###TRIGGERS###
def LongTrigger = trendlong and (TwoTwoLong or HammerTwoLong or  OutsideGreenTwoLong) ;
def ShortTrigger = trendshort and (TwoTwoShort or ShooterTwoShort or OutsideRedTwoShort);


def LE = TwoTwoLong or HammerTwoLong or OutsideGreenTwoLong;
def LEBN = if LE then bn else LEBN[1];
def LowStop = if bn == LEBN then low else LowStop[1];

#def SE = FracShort;
def SE = OutsideRedTwoShort ;
def SEBN = if SE then bn else SEBN[1];
def HighStopBN = if bn == SEBN then bn-1 else HighStopBN[1];
def HighStop = if bn == SEBN then high else HighStop[1];
#addchartbubble(high == highstop,high,highstop,color.magenta);

def lxstop = low < LowStop;
#def lx = high > Entry * takeprofitconverstionLong ;

def sxstop = high > HighStop;
#def sx = low < Entry * takeprofitconverstionshort ;

AddOrder(OrderType.BUY_TO_OPEN, LE[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = high);
AddOrder(OrderType.SELL_TO_CLOSE, low[-1] crosses below low, tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX Profit", price = low);
AddOrder(OrderType.SELL_TO_CLOSE, low crosses below LowStop, tickcolor = Color.WHITE, arrowcolor = Color.GREEN, name = "LX Stop", price = high);


AddOrder(OrderType.SELL_TO_OPEN, SE[-1], arrowcolor = Color.WHITE, tickcolor = Color.YELLOW, name = "SE", price = low[0]);
AddOrder(OrderType.BUY_TO_CLOSE, high[-1] crosses above high, tickcolor = Color.WHITE, arrowcolor = Color.Green, name = "SX Profit", price = high);
AddOrder(OrderType.BUY_TO_CLOSE,  high crosses above HighStop, tickcolor = Color.WHITE, arrowcolor = Color.Magenta, name = "SX Stop", price = low);
 
I keep having the same issue where my Strategy won't populate older statistics on the P/L. @SleepyZ , this is the same issue you helped me with a few days ago but I still can't seem to get this version to work properly and populate the historic buys and sells. Let me know if anything jumps out at you.....or @halcyonguy or @Joshua. I honestly dont know what I'd do without the support of you guys and this community. Truly awesome. Thank you as always.


Code:
input TakeProfitPct = .5; #hint, 1 is 1 pct, .5 is 1/2 percent, etc
def takeprofitconverstionLong = 1 + (TakeProfitPct / 100);
AddLabel(1, (takeprofitconverstionLong * close) - close, Color.WHITE);
def takeprofitconverstionshort = 1 - (TakeProfitPct / 100);
AddLabel(1, (takeprofitconverstionshort * close) - close, Color.WHITE);
def bn = BarNumber();
def Latest_bar = if IsNaN(close[-1]) then bn else Latest_bar[1];


input Trend_TimeFrame = {default Current, Five, Ten, Fifteen, Twenty, Thirty, Sixty, Two_Hour, Four_Hour, Day};

def TimeFrame;
switch (Trend_TimeFrame) {
case Current:
    TimeFrame = getAggregationPeriod();
case Five:
    TimeFrame = AggregationPeriod.FIVE_MIN;
case Ten:
    TimeFrame = AggregationPeriod.TEN_MIN;
case Fifteen:
     TimeFrame = AggregationPeriod.FIFTEEN_MIN;
case Twenty:
    TimeFrame = AggregationPeriod.TWENTY_MIN;
case Thirty:
    TimeFrame = AggregationPeriod.THIRTY_MIN;
case Sixty:
    TimeFrame = AggregationPeriod.HOUR;
case Two_Hour:
    TimeFrame = AggregationPeriod.Two_Hours;
case Four_Hour:
    TimeFrame = AggregationPeriod.Four_Hours;
case Day:
    TimeFrame = AggregationPeriod.DAY;
}

AddLabel(yes, Trend_TimeFrame, Color.WHITE);






#######################################################################################################

#TREND SECTION

#######################################################################################################


input LBPeriod = 2;

#--------------------------------------------------------------

def HH = Highest(high(Period = Timeframe), LBPeriod);
def marketHigh1 = if HH > HH[-LBPeriod] then HH else HH[-LBPeriod];
def markedHigh1 = high(Period = Timeframe) == marketHigh1;

def lastMarkedHigh1 = CompoundValue(1, if IsNaN( markedHigh1) then lastMarkedHigh1[1] else if markedHigh1 then high(Period = Timeframe) else lastMarkedHigh1[1], high(Period = Timeframe));

#-------------------------------------------------------------

plot HP = lastMarkedHigh1;
def HPBN = if high == HP then bn else HPBN[1];
def HallHPBN = highestall(HPBN);
#--------------------------------------------------------------

def LL = Lowest(low(Period = Timeframe), LBPeriod);
#--------------------------------------------------------------
def marketLow1 = if LL < LL[-LBPeriod] then LL else LL[-LBPeriod];
def markedLow1 = low(Period = Timeframe) == marketLow1;
def lastMarkedLow1 = CompoundValue(1, if IsNaN(markedLow1) then lastMarkedLow1[1] else if markedLow1 then low(Period = Timeframe) else lastMarkedLow1[1], low(Period = Timeframe));

plot LV = lastMarkedLow1 ;
def LVBN = if low(Period = Timeframe) == LV then bn else LVBN[1];


#--------------------------------------------------------------
HP.SetPaintingStrategy(PaintingStrategy.DASHES);
HP.SetDefaultColor(Color.GREEN);

#--------------------------------------------------------------
LV.SetPaintingStrategy(PaintingStrategy.DASHES);
LV.SetDefaultColor(Color.RED);


####TREND CALCS###

def Phigh = HP;
def HighCount = if bn == 1 then 0 else if High==Phigh then HighCount[1] + 1 else HighCount[1];
def HighCountMax = highestall(HighCount);
def HighRevCount = HighCountMax-HighCount +1;


def High1 =  HighRevCount[1]== 2 and HighRevCount == 1;
def High1BN = if  high1 then bn else High1BN[1];
def High1Val = if bn == High1BN then High else High1Val[1];
addchartbubble(bn == highestall(high1bn),High,HighRevCount,color.violet);


def High2 = HighRevCount[1]== 3 and HighRevCount == 2 ;
def High2BN = if High2 then bn else High2BN[1];
def High2Val = if bn == highestall(High2BN) then High else High2Val[1];
addchartbubble(bn == highestall(high2bn),High,HighRevCount,color.violet);

def High3 = HighRevCount[1]== 4 and HighRevCount == 3 ;
def High3BN = if High3 then bn else High3BN[1];
def High3Val = if bn == highestall(High3BN) then High else High3Val[1];
addchartbubble(bn == highestall(high3bn),High,HighRevCount,color.violet);


def PLow = lv;
def LowCount = if bn == 1 then 0 else if low==Plow then LowCount[1] + 1 else LowCount[1];
def LowCountMax = highestall(LowCount);
def LowRevCount = LowCountMax-LowCount +1;

def Low1 =  LowRevCount[1]== 2 and LowRevCount == 1;
def Low1BN = if Low1 then bn else Low1BN[1];
def Low1Val = if bn == highestall(Low1BN) then low else Low1Val[1];
addchartbubble(bn == highestall(Low1BN),low,LowRevCount,color.green);

def Low2 = LowRevCount[1]== 3 and LowRevCount == 2 ;
def Low2BN = if Low2 then bn else Low2BN[1];
def Low2Val = if bn == highestall(Low2BN) then low else Low2Val[1];
addchartbubble(BN == highestall(low2bn),low,LowRevCount,color.green);

def Low3 = LowRevCount[1]== 4 and LowRevCount == 3 ;
def Low3BN = if Low3 then bn else Low3BN[1];
def Low3Val = if bn == highestall(Low3BN) then low else Low3Val[1];
addchartbubble(bn == highestall(low3bn),low,LowRevCount,color.green);


#####TREND SIGNALS######

def HigherHighs = high3val < high2val and high2val < high1val ;
def HigherLows =  Low3val < low2val and low2val < low1val ;

def LowerHighs =  high3val > high2val and high2val > high1val ;
def LowerLows =  Low3val > low2val and low2val > low1val ;
#def LowerLows =  if highestall(Low3val) > highestall(low2val) and highestall(low2val) > highestall(low1val) then 1 else lowerlows[1];

def TrendLong = HigherHighs or higherlows;
def TrendShort = LowerLows ;
addlabel(1, if trendlong then "Trend Long" else if Trendshort then "Trend Short" else " ", color.white);


###SIGNALS###

input showArrows = yes;
input x = 5; #bars amount

# Calculate the length of the candle's wicks
def UpperWick = high - Max(open, close);
def LowerWick = Min(open, close) - low;

# Calculate the length of the candle's body
def CandleBody = bodyheight();
def range = High - low;
def cond = range == highest(range,x);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = (lowerWick / CandleBody >= 1.25) and (upperWick / CandleBody <= 1);
def Shooter = (upperWick / CandleBody >= 1.25) and (lowerWick / CandleBody <= 1);

def TwoUp = low >= Low[1] and high >= high[1];
def TwoDown = High <= High[1] and Low <= Low[1];
def Inside = high < High[1] and Low > Low[1];
def OutsideRed = high >= High[1] and Low <= Low[1] and close < open;
def OutsideGreen = high >= High[1] and Low <= Low[1] and close > open;

Def TwoTwoShort = trendshort and TwoUp[1] and close[1] > open[1] and twodown;
addchartbubble(TwoTwoShort,low,TwoTwoShort);

Def ShooterTwoShort = trendshort and twoup[2]  and close[2] > open[2] and shooter[1] and TwoDown;
addchartbubble(ShooterTwoShort,low,ShooterTwoShort,color.violet);

Def OutsideRedTwoShort = trendshort and TwoUp[1] and OutsideRed;
addchartbubble(OutsideRedTwoShort,low,OutsideRedTwoShort,color.yellow);

Def TwoTwoLong = trendlong and Twodown[1] and close[1] < open[1] and twoup;
addchartbubble(TwoTwoLong,high,TwoTwoLong);

Def HammerTwoLong = trendlong and twodown[2]  and close[2] < open[2] and hammer[1] and Twoup;
addchartbubble(HammerTwoLong,high,HammerTwoLong,color.violet);

Def OutsideGreenTwoLong = trendlong and TwoDown[1] and OutsideGreen;
addchartbubble(OutsideGreenTwoLong,high,OutsideGreenTwoLong,color.yellow);

################################################################################################################

###TRIGGERS###
def LongTrigger = trendlong and (TwoTwoLong or HammerTwoLong or  OutsideGreenTwoLong) ;
def ShortTrigger = trendshort and (TwoTwoShort or ShooterTwoShort or OutsideRedTwoShort);


def LE = TwoTwoLong or HammerTwoLong or OutsideGreenTwoLong;
def LEBN = if LE then bn else LEBN[1];
def LowStop = if bn == LEBN then low else LowStop[1];

#def SE = FracShort;
def SE = OutsideRedTwoShort ;
def SEBN = if SE then bn else SEBN[1];
def HighStopBN = if bn == SEBN then bn-1 else HighStopBN[1];
def HighStop = if bn == SEBN then high else HighStop[1];
#addchartbubble(high == highstop,high,highstop,color.magenta);

def lxstop = low < LowStop;
#def lx = high > Entry * takeprofitconverstionLong ;

def sxstop = high > HighStop;
#def sx = low < Entry * takeprofitconverstionshort ;

AddOrder(OrderType.BUY_TO_OPEN, LE[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = high);
AddOrder(OrderType.SELL_TO_CLOSE, low[-1] crosses below low, tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX Profit", price = low);
AddOrder(OrderType.SELL_TO_CLOSE, low crosses below LowStop, tickcolor = Color.WHITE, arrowcolor = Color.GREEN, name = "LX Stop", price = high);


AddOrder(OrderType.SELL_TO_OPEN, SE[-1], arrowcolor = Color.WHITE, tickcolor = Color.YELLOW, name = "SE", price = low[0]);
AddOrder(OrderType.BUY_TO_CLOSE, high[-1] crosses above high, tickcolor = Color.WHITE, arrowcolor = Color.Green, name = "SX Profit", price = high);
AddOrder(OrderType.BUY_TO_CLOSE,  high crosses above HighStop, tickcolor = Color.WHITE, arrowcolor = Color.Magenta, name = "SX Stop", price = low);

not at computer, so going to guess it's related to this. this won't always be equal to the last bar.

def Latest_bar = if IsNaN(close[-1]) then bn else Latest_bar[1];

replace it with this

def Latest_bar = highestall( if !IsNaN(close) then bn else 0);
 
not at computer, so going to guess it's related to this. this won't always be equal to the last bar.

def Latest_bar = if IsNaN(close[-1]) then bn else Latest_bar[1];

replace it with this

def Latest_bar = highestall( if !IsNaN(close) then bn else 0);
Actually, I'm not using that line in the code in anything so I should have taken that out. Either way, its not effecting the results. Thanks though.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
462 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