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