Trouble identifying lowest bar number after variable

greco26

Active member
For some reason I continue to have trouble itenditying the lowest bar number after the variable YZ. I tried 7 different ways. I am trying to isolate only the first pink bubble after the yellow bubble which is what ZZ is supposed to do. Anyone know what Im doing wrong? Thank you as always!


Code:
input LBPeriod = 5;
def BN = barnumber();

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

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

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

def Resistance1 = lastMarkedHigh1;
def ResistanceBN = if Resistance1 then bn else ResistanceBN[1];
#--------------------------------------------------------------

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

def Support1 = lastMarkedLow1 ;

def stronglow = if Support1 < Support1[1] then low else stronglow[1];
def Prevstronglow = if stronglow != stronglow[1] then stronglow[1] else Prevstronglow[1];

def weaklow = if low > stronglow and Support1 != stronglow then Support1 else weaklow[1];
def prevweaklow = if weaklow != weaklow[1] then weaklow[1] else prevweaklow[1];

def strongHigh = if Resistance1 > Resistance1[1] then high else strongHigh[1];
def prevstronghigh = if strongHigh != strongHigh[1] then strongHigh[1] else prevstronghigh[1];

def weakHigh = if high < strongHigh and Resistance1 != strongHigh then Resistance1 else weakHigh[1];
def prevweakhigh = if weakHigh != weakHigh[1] then weakHigh[1] else prevweakhigh[1];


def peak = high > high[1] and high[-1] < high;
def valley = low < low[1] and low[-1] > low;

def y = if high crosses above stronghigh and peak then bn else y[1];
def yz = highestall(y);
def yy = if bn > yz and valley then bn else yy[1];
def zz = if bn == lowestall(yy) then bn else zz[1];

addchartbubble(bn==y, high,y,color.yellow);
addchartbubble(bn==yy, low,yy,color.pink);
addchartbubble(bn == zz, low,zz,color.magenta);
 
For some reason I continue to have trouble itenditying the lowest bar number after the variable YZ. I tried 7 different ways. I am trying to isolate only the first pink bubble after the yellow bubble which is what ZZ is supposed to do. Anyone know what Im doing wrong? Thank you as always!

EDIT =============

sorry , i tried to put together a partial study earlier without testing.
i had to change a couple of your formulas, and 1 of mine

here is a working version

Code:
# one_dip_after_peak_0

input LBPeriod = 5;
def BN = barnumber();

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

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

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

def Resistance1 = lastMarkedHigh1;
def ResistanceBN = if Resistance1 then bn else ResistanceBN[1];
#--------------------------------------------------------------

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

def Support1 = lastMarkedLow1 ;

def stronglow = if Support1 < Support1[1] then low else stronglow[1];
def Prevstronglow = if stronglow != stronglow[1] then stronglow[1] else Prevstronglow[1];

def weaklow = if low > stronglow and Support1 != stronglow then Support1 else weaklow[1];
def prevweaklow = if weaklow != weaklow[1] then weaklow[1] else prevweaklow[1];

def strongHigh = if Resistance1 > Resistance1[1] then high else strongHigh[1];
def prevstronghigh = if strongHigh != strongHigh[1] then strongHigh[1] else prevstronghigh[1];

def weakHigh = if high < strongHigh and Resistance1 != strongHigh then Resistance1 else weakHigh[1];
def prevweakhigh = if weakHigh != weakHigh[1] then weakHigh[1] else prevweakhigh[1];


def peak = high > high[1] and high[-1] < high;
def valley = low < low[1] and low[-1] > low;

#def y = if high crosses above stronghigh and peak then bn else y[1];
# chg y formula
def y = if bn == 1 then 0 else if high crosses above stronghigh and peak then bn else y[1];
def yz = highestall(y);
def yy = if bn > yz and valley then bn else yy[1];
def zz = if bn == lowestall(yy) then bn else zz[1];

addchartbubble(bn==y, high,y,color.yellow, yes);
addchartbubble(bn==yy, low,yy,color.pink, no);
# disable this and use one below
#addchartbubble(bn == zz, low,zz,color.magenta, no);

# new code------------------------

# add counter of yy
# only allow first yy to have a bubble
# if y is different than previous value, then reset counter

def yycnt = if bn == 1 then 0
 else if y[1] != y then 0
 else if (yz > 0 and bn > yz and valley) then yycnt[1] + 1
 else yycnt[1];

def tt = if bn == 1 then 0 
 else if yycnt == 1 and valley then bn
 else tt[1];

# add 5th parameter , no,  to place bubble below insertion point. 
addchartbubble(bn == tt,  low, tt, color.magenta, no);


addchartbubble(0, high*1.01,
bn + "\n" +
y + " y\n" +
yz + " yz\n" +
valley + " dip\n" +
yycnt + " cnt\n" +
tt + " tt\n" 
, color.cyan, yes);
#
 
Last edited:

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

For some reason I continue to have trouble itenditying the lowest bar number after the variable YZ. I tried 7 different ways. I am trying to isolate only the first pink bubble after the yellow bubble which is what ZZ is supposed to do. Anyone know what Im doing wrong? Thank you as always!


Code:
input LBPeriod = 5;
def BN = barnumber();

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

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

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

def Resistance1 = lastMarkedHigh1;
def ResistanceBN = if Resistance1 then bn else ResistanceBN[1];
#--------------------------------------------------------------

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

def Support1 = lastMarkedLow1 ;

def stronglow = if Support1 < Support1[1] then low else stronglow[1];
def Prevstronglow = if stronglow != stronglow[1] then stronglow[1] else Prevstronglow[1];

def weaklow = if low > stronglow and Support1 != stronglow then Support1 else weaklow[1];
def prevweaklow = if weaklow != weaklow[1] then weaklow[1] else prevweaklow[1];

def strongHigh = if Resistance1 > Resistance1[1] then high else strongHigh[1];
def prevstronghigh = if strongHigh != strongHigh[1] then strongHigh[1] else prevstronghigh[1];

def weakHigh = if high < strongHigh and Resistance1 != strongHigh then Resistance1 else weakHigh[1];
def prevweakhigh = if weakHigh != weakHigh[1] then weakHigh[1] else prevweakhigh[1];


def peak = high > high[1] and high[-1] < high;
def valley = low < low[1] and low[-1] > low;

def y = if high crosses above stronghigh and peak then bn else y[1];
def yz = highestall(y);
def yy = if bn > yz and valley then bn else yy[1];
def zz = if bn == lowestall(yy) then bn else zz[1];

addchartbubble(bn==y, high,y,color.yellow);
addchartbubble(bn==yy, low,yy,color.pink);
addchartbubble(bn == zz, low,zz,color.magenta);

This seems to work

Capture.jpg
Ruby:
input LBPeriod = 5;
def BN = BarNumber();

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

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

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

def Resistance1 = lastMarkedHigh1;
def ResistanceBN = if Resistance1 then BN else ResistanceBN[1];
#--------------------------------------------------------------

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

def Support1 = lastMarkedLow1 ;

def stronglow = if Support1 < Support1[1] then low else stronglow[1];
def Prevstronglow = if stronglow != stronglow[1] then stronglow[1] else Prevstronglow[1];

def weaklow = if low > stronglow and Support1 != stronglow then Support1 else weaklow[1];
def prevweaklow = if weaklow != weaklow[1] then weaklow[1] else prevweaklow[1];

def strongHigh = if Resistance1 > Resistance1[1] then high else strongHigh[1];
def prevstronghigh = if strongHigh != strongHigh[1] then strongHigh[1] else prevstronghigh[1];

def weakHigh = if high < strongHigh and Resistance1 != strongHigh then Resistance1 else weakHigh[1];
def prevweakhigh = if weakHigh != weakHigh[1] then weakHigh[1] else prevweakhigh[1];


def peak = high > high[1] and high[-1] < high;
def valley = low < low[1] and low[-1] > low;

def y = if high crosses above strongHigh and peak then BN else y[1];
def yz = HighestAll(y);
def yy = if BN > yz and valley then BN else yy[1];
def zz = if yy[1] == 0 and yy then yy else Min(yy, zz[1]);

AddChartBubble(BN == y, high, y, Color.YELLOW);
AddChartBubble(BN == yy, low, yy, Color.PINK);
AddChartBubble(BN == zz, low, zz, Color.MAGENTA, up = no);
 
Hi Guys,

Any idea why the following code won't find OneAgoHighestPeak1BN?

Code:
def BN = Barnumber();
def peak = high > high[1] and high[-1] < high;
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def Peak1Val = if bn == Peak1BN then high else Peak1Val[1];
def HighestPeak1BN = highestall(Peak1BN);
def OneAgoHighestPeak1BN= if HighestPeak1BN != HighestPeak1BN[1] then HighestPeak1BN[1] else OneAgoHighestPeak1BN[1];
addchartbubble(bn ==HighestPeak1BN,high,"Hightest Peak",color.violet);
addchartbubble(bn ==OneAgoHighestPeak1BN,high,"1 Ago Highest Peak",color.violet);
 
Hi Guys,

Any idea why the following code won't find OneAgoHighestPeak1BN?

Code:
def BN = Barnumber();
def peak = high > high[1] and high[-1] < high;
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def Peak1Val = if bn == Peak1BN then high else Peak1Val[1];
def HighestPeak1BN = highestall(Peak1BN);
def OneAgoHighestPeak1BN= if HighestPeak1BN != HighestPeak1BN[1] then HighestPeak1BN[1] else OneAgoHighestPeak1BN[1];
addchartbubble(bn ==HighestPeak1BN,high,"Hightest Peak",color.violet);
addchartbubble(bn ==OneAgoHighestPeak1BN,high,"1 Ago Highest Peak",color.violet);

I have made a mod to def OneAgoHighestPeak1BN and related bubble to display what I think you want.

I have also included some testing code to follow how the changes work. Using the highestpeakBN code to find the previous one did not work as there is only one highestall bn. Using the peak1bn to find previous peaks was used instead as it includes data for all peaks.

Ruby:
def BN = BarNumber();
def peak = high > high[1] and high[-1] < high;
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def Peak1Val = if BN == Peak1BN then high else Peak1Val[1];
def HighestPeak1BN = HighestAll(Peak1BN);
def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];

AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
AddChartBubble(BN == HighestAll(OneAgoHighestPeak1BN), high, "1 Ago Highest Peak", Color.VIOLET);

#Testing
plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
AddLabel(1, HighestAll(OneAgoHighestPeak1BN));
def twoAgoHighestPeak1BN = if OneAgoHighestPeak1BN != OneAgoHighestPeak1BN[1] then OneAgoHighestPeak1BN[1] else twoAgoHighestPeak1BN[1];
AddChartBubble(BN == HighestAll(twoAgoHighestPeak1BN), high, "2 Ago Highest Peak", Color.VIOLET);
AddChartBubble(BN == x, high, "peak");
 
This is perfect! Made some changes but I have 1 more question if possible. If I want to identify the highest peak in a run, is there an easier way to code that? The logical thing for me is to list everything out like if peak 1 is > than peak then peak1 else if... Basically, once I hit a peak that is lower than the previous peak, I want to stop. Hopefully this would not require a fold because my brain doesn't like those. :)

Code:
def BN = BarNumber();
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];

def HighestPeak1BN = HighestAll(Peak1BN);
def HighestPeak1Val = if BN == HighestAll(Peak1BN) then High else HighestPeak1Val[1];

def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];
def OneAgoHighestPeak1Val = if BN == HighestAll(OneAgoHighestPeak1BN) then High else OneAgoHighestPeak1Val[1];

def TwoAgoHighestPeak1BN = if OneAgoHighestPeak1BN != OneAgoHighestPeak1BN[1] then OneAgoHighestPeak1BN[1] else TwoAgoHighestPeak1BN[1];
def TwoAgoHighestPeak1Val = if BN == HighestAll(TwoAgoHighestPeak1BN) then High else TwoAgoHighestPeak1Val[1];

def ThreeAgoHighestPeak1BN = if TwoAgoHighestPeak1BN != TwoAgoHighestPeak1BN[1] then TwoAgoHighestPeak1BN[1] else ThreeAgoHighestPeak1BN[1];
def ThreeAgoHighestPeak1Val = if BN == HighestAll(ThreeAgoHighestPeak1BN) then High else ThreeAgoHighestPeak1Val[1];

def FourAgoHighestPeak1BN = if ThreeAgoHighestPeak1BN != ThreeAgoHighestPeak1BN[1] then ThreeAgoHighestPeak1BN[1] else FourAgoHighestPeak1BN[1];
def FourAgoHighestPeak1Val = if BN == HighestAll(FourAgoHighestPeak1BN) then High else FourAgoHighestPeak1Val[1];

def FiveAgoHighestPeak1BN = if FourAgoHighestPeak1BN != FourAgoHighestPeak1BN[1] then FourAgoHighestPeak1BN[1] else FiveAgoHighestPeak1BN[1];
def FiveAgoHighestPeak1Val = if BN == HighestAll(FiveAgoHighestPeak1BN) then High else FiveAgoHighestPeak1Val[1];


def HighestInRunVal = If
OneAgoHighestPeak1Val > HighestPeak1Val then OneAgoHighestPeak1Val
else 0;

AddChartBubble(high == HighestInRunVal, HighestInRunVal, "xxxxxx", Color.red);

AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
AddChartBubble(BN == HighestAll(OneAgoHighestPeak1BN), high, "1 Ago Highest Peak", Color.plum);
AddChartBubble(BN == HighestAll(twoAgoHighestPeak1BN), high, "2 Ago Highest Peak", Color.yellow);
AddChartBubble(BN == HighestAll(threeAgoHighestPeak1BN), high, "3 Ago Highest Peak", Color.orange);
AddChartBubble(BN == HighestAll(fourAgoHighestPeak1BN), high, "4 Ago Highest Peak", Color.green);
AddChartBubble(BN == HighestAll(fiveAgoHighestPeak1BN), high, "5 Ago Highest Peak", Color.magenta);

plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
Here is what I have figured out so far. I will keep working at it because its not quite right.

def HighestInRunBN = If HighestPeak1BN >= OneAgoHighestPeak1Val then bn else HighestInRunBN[1];
def HighestInRunVal =If BN == HighestInRunBN then High else HighestInRunVal[1];
AddChartBubble(bn == highestall(HighestInRunBN), HighestInRunVal, highestall(HighestInRunBN), Color.red);
 
Tried this too but it didn't work. Im at a loss......


Code:
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];
def OneAgoHighestPeak1Val = if BN == HighestAll(OneAgoHighestPeak1BN) then High else OneAgoHighestPeak1Val[1];

def Count = if BN == 1 then 0 else if ( HighestPeak1Val > OneAgoHighestPeak1Val) then Count[1] + 1 else Count[1];
def HHBN = if BN == 1 then 0 else if Count == 1 and Peak then BN else HHBN[1];
def HHVal = if BN == HHBN then high else HHVal[1];
AddChartBubble(High == HHVal,  High, "xxxxxxxxxx", Color.CYAN, yes);
 
This is perfect! Made some changes but I have 1 more question if possible. If I want to identify the highest peak in a run, is there an easier way to code that? The logical thing for me is to list everything out like if peak 1 is > than peak then peak1 else if... Basically, once I hit a peak that is lower than the previous peak, I want to stop. Hopefully this would not require a fold because my brain doesn't like those. :)

Code:
def BN = BarNumber();
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];

def HighestPeak1BN = HighestAll(Peak1BN);
def HighestPeak1Val = if BN == HighestAll(Peak1BN) then High else HighestPeak1Val[1];

def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];
def OneAgoHighestPeak1Val = if BN == HighestAll(OneAgoHighestPeak1BN) then High else OneAgoHighestPeak1Val[1];

def TwoAgoHighestPeak1BN = if OneAgoHighestPeak1BN != OneAgoHighestPeak1BN[1] then OneAgoHighestPeak1BN[1] else TwoAgoHighestPeak1BN[1];
def TwoAgoHighestPeak1Val = if BN == HighestAll(TwoAgoHighestPeak1BN) then High else TwoAgoHighestPeak1Val[1];

def ThreeAgoHighestPeak1BN = if TwoAgoHighestPeak1BN != TwoAgoHighestPeak1BN[1] then TwoAgoHighestPeak1BN[1] else ThreeAgoHighestPeak1BN[1];
def ThreeAgoHighestPeak1Val = if BN == HighestAll(ThreeAgoHighestPeak1BN) then High else ThreeAgoHighestPeak1Val[1];

def FourAgoHighestPeak1BN = if ThreeAgoHighestPeak1BN != ThreeAgoHighestPeak1BN[1] then ThreeAgoHighestPeak1BN[1] else FourAgoHighestPeak1BN[1];
def FourAgoHighestPeak1Val = if BN == HighestAll(FourAgoHighestPeak1BN) then High else FourAgoHighestPeak1Val[1];

def FiveAgoHighestPeak1BN = if FourAgoHighestPeak1BN != FourAgoHighestPeak1BN[1] then FourAgoHighestPeak1BN[1] else FiveAgoHighestPeak1BN[1];
def FiveAgoHighestPeak1Val = if BN == HighestAll(FiveAgoHighestPeak1BN) then High else FiveAgoHighestPeak1Val[1];


def HighestInRunVal = If
OneAgoHighestPeak1Val > HighestPeak1Val then OneAgoHighestPeak1Val
else 0;

AddChartBubble(high == HighestInRunVal, HighestInRunVal, "xxxxxx", Color.red);

AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
AddChartBubble(BN == HighestAll(OneAgoHighestPeak1BN), high, "1 Ago Highest Peak", Color.plum);
AddChartBubble(BN == HighestAll(twoAgoHighestPeak1BN), high, "2 Ago Highest Peak", Color.yellow);
AddChartBubble(BN == HighestAll(threeAgoHighestPeak1BN), high, "3 Ago Highest Peak", Color.orange);
AddChartBubble(BN == HighestAll(fourAgoHighestPeak1BN), high, "4 Ago Highest Peak", Color.green);
AddChartBubble(BN == HighestAll(fiveAgoHighestPeak1BN), high, "5 Ago Highest Peak", Color.magenta);

plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

please edit your post7 to clarify,
find highest peak what? price or barnumber.
what defines a 'run' ?
 
please edit your post7 to clarify,
find highest peak what? price or barnumber.
what defines a 'run' ?
Sorry about that. Here is the updated code. I shorted my naming convention in this version. What I want to do is have code identify the Highest Peak from a (price perspective) in a run. I'm defining a run as the the highest point in the most recent swing (or I guess the swing high). In the image below, the label "2 Ago Highest Peak" should be the "Highest Peak" if my code was working properly. The code would start from the most recent bar number and work backwards to stop at a point where the Highest Peak > the 1 Ago Highest Peak. Hope this makes sense.


Code:
def BN = BarNumber();
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def Peak = high > high[1] and high[-1] < high;

def HighestPeak1BN = HighestAll(Peak1BN);
def HighestPeak1Val = if BN == HighestAll(Peak1BN) then High else HighestPeak1Val[1];

def _1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else _1BN[1];
def _1Val = if BN == HighestAll(_1BN) then High else _1Val[1];

def _2BN = if _1BN != _1BN[1] then _1BN[1] else _2BN[1];
def _2Val = if BN == HighestAll(_2BN) then High else _2Val[1];

def _3BN = if _2BN != _2BN[1] then _2BN[1] else _3BN[1];
def _3Val = if BN == HighestAll(_3BN) then High else _3Val[1];

def _4BN = if _3BN != _3BN[1] then _3BN[1] else _4BN[1];
def _4Val = if BN == HighestAll(_4BN) then High else _4Val[1];

def _5BN = if _4BN != _4BN[1] then _4BN[1] else _5BN[1];
def _5Val = if BN == HighestAll(_4BN) then High else _5Val[1];


def Count = if BN == 1 then 0 else if ( HighestPeak1Val > _1Val) then Count[1] + 1 else Count[1];
def HHBN = if BN == 1 then 0 else if Count == 1 and Peak then BN else HHBN[1];
def HHVal = if BN == HHBN then high else HHVal[1];
AddChartBubble(High == HHVal,  High, "xxxxxxxxxx", Color.CYAN, yes);



AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
AddChartBubble(BN == HighestAll(_1BN), high, "1 Ago Highest Peak", Color.plum);
AddChartBubble(BN == HighestAll(_2BN), high, "2 Ago Highest Peak", Color.yellow);
AddChartBubble(BN == HighestAll(_3BN), high, "3 Ago Highest Peak", Color.orange);
AddChartBubble(BN == HighestAll(_4BN), high, "4 Ago Highest Peak", Color.green);
AddChartBubble(BN == HighestAll(_5BN), high, "5 Ago Highest Peak", Color.magenta);

plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
Sorry about that. Here is the updated code. I shorted my naming convention in this version. What I want to do is have code identify the Highest Peak from a (price perspective) in a run. I'm defining a run as the the highest point in the most recent swing (or I guess the swing high). In the image below, the label "2 Ago Highest Peak" should be the "Highest Peak" if my code was working properly. The code would start from the most recent bar number and work backwards to stop at a point where the Highest Peak > the 1 Ago Highest Peak. Hope this makes sense.
no need for sorry. i'm just trying to help you explain your process.
i try to ask specific questions to pull relevant info from people.

don't take this the wrong way. some people have trouble explaining what is in their head, and some people get stuck on understanding someone else. this may apply to both of us.
i asked a couple of questions, and i'm still confused.
no idea what defines a run. i think of a run as a collection of numbers, not a peak.
is a run the bars spanning across the 6 peak bubbles?
is the highest peak always before 1 ago peak? ( Highest Peak > the 1 Ago Highest Peak.) or does that statement just apply to this chart?
can the most recent peak also be the highest peak? or just one of the x ago peaks ?
 
Sorry about that. Here is the updated code. I shorted my naming convention in this version. What I want to do is have code identify the Highest Peak from a (price perspective) in a run. I'm defining a run as the the highest point in the most recent swing (or I guess the swing high). In the image below, the label "2 Ago Highest Peak" should be the "Highest Peak" if my code was working properly. The code would start from the most recent bar number and work backwards to stop at a point where the Highest Peak > the 1 Ago Highest Peak. Hope this makes sense.

here is a different code that may do what you want
it's long and a little messy. i left your original code in it, but commented out.
it doesn't use highestall()
you can pick how many peaks to find
it finds the highest peak

Ruby:
# find_highest_peak_1

#greco26
#7
# sleepyz code...
#This is perfect! Made some changes but I have 1 more question if possible. If I want to identify the highest peak in a run, is there an easier way to code that? The logical thing for me is to list everything out like if peak 1 is > than peak then peak1 else if...
#Basically, once I hit a peak that is lower than the previous peak, I want to stop. Hopefully this would not require a fold because my brain doesn't like those. :)


#def BN = BarNumber();
#def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];

#def HighestPeak1BN = HighestAll(Peak1BN);
#def HighestPeak1Val = if BN == HighestAll(Peak1BN) then High else HighestPeak1Val[1];

#def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];
#def OneAgoHighestPeak1Val = if BN == HighestAll(OneAgoHighestPeak1BN) then High else OneAgoHighestPeak1Val[1];

#def TwoAgoHighestPeak1BN = if OneAgoHighestPeak1BN != OneAgoHighestPeak1BN[1] then OneAgoHighestPeak1BN[1] else TwoAgoHighestPeak1BN[1];
#def TwoAgoHighestPeak1Val = if BN == HighestAll(TwoAgoHighestPeak1BN) then High else TwoAgoHighestPeak1Val[1];

#def ThreeAgoHighestPeak1BN = if TwoAgoHighestPeak1BN != TwoAgoHighestPeak1BN[1] then TwoAgoHighestPeak1BN[1] else ThreeAgoHighestPeak1BN[1];
#def ThreeAgoHighestPeak1Val = if BN == HighestAll(ThreeAgoHighestPeak1BN) then High else ThreeAgoHighestPeak1Val[1];

#def FourAgoHighestPeak1BN = if ThreeAgoHighestPeak1BN != ThreeAgoHighestPeak1BN[1] then ThreeAgoHighestPeak1BN[1] else FourAgoHighestPeak1BN[1];
#def FourAgoHighestPeak1Val = if BN == HighestAll(FourAgoHighestPeak1BN) then High else FourAgoHighestPeak1Val[1];

#def FiveAgoHighestPeak1BN = if FourAgoHighestPeak1BN != FourAgoHighestPeak1BN[1] then FourAgoHighestPeak1BN[1] else FiveAgoHighestPeak1BN[1];
#def FiveAgoHighestPeak1Val = if BN == HighestAll(FiveAgoHighestPeak1BN) then High else FiveAgoHighestPeak1Val[1];


#def HighestInRunVal = If OneAgoHighestPeak1Val > HighestPeak1Val then OneAgoHighestPeak1Val else 0;

#AddChartBubble(high == HighestInRunVal, HighestInRunVal, "xxxxxx", Color.red);

#AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
#AddChartBubble(BN == HighestAll(OneAgoHighestPeak1BN), high, "1 Ago Highest Peak", Color.plum);
#AddChartBubble(BN == HighestAll(twoAgoHighestPeak1BN), high, "2 Ago Highest Peak", Color.yellow);
#AddChartBubble(BN == HighestAll(threeAgoHighestPeak1BN), high, "3 Ago Highest Peak", Color.orange);
#AddChartBubble(BN == HighestAll(fourAgoHighestPeak1BN), high, "4 Ago Highest Peak", Color.green);
#AddChartBubble(BN == HighestAll(fiveAgoHighestPeak1BN), high, "5 Ago Highest Peak", Color.magenta);

#plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
#x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

# ---------------------------------
def na = double.nan;

# charts with more than 1000 bars may not calculate correctly
# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
# Line At Price  Mobius
# mod to find last bn
def bn = barnumber();
# find last bar number
input barsBack = 1000;
def d = if !IsNaN(close) and IsNaN(close[-1])
        then bn
        else d[1];
def lastbn = if isNaN(close[-barsBack])
            then d[-barsBack]
            else Double.NaN;

# -------------------------------------
# find peaks
input peak_bar_length = 1;
def peak = if (high > high[peak_bar_length] and high[-peak_bar_length] < high) then 1 else 0;
def peak_bn = if peak then bn else 0;

# count peaks
def peak_cnt = if bn == 1 then 0 else if ( bn == (lastbn - peak_bar_length + 1)) then peak_cnt[1] else if peak then peak_cnt[1] + 1 else peak_cnt[1];

# -------------------------------------
# find highest cnt #
input barsBack2 = 1000;
def e = if !IsNaN(close) and IsNaN(close[-1])
        then peak_cnt
        else e[1];

# 2 of these codes in 1 study cause error offset -2001 , too big
# switching offsets to getvalue, removes error
#def peak_max = if isNaN(close[-barsBack2])
#            then e[-barsBack2]
#            else Double.NaN;

def peak_max = if isNaN(getvalue(close, -barsBack2))
            then getvalue(e, -barsBack2)
            else Double.NaN;

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

def revpeakcnt = peak_max - peak_cnt + 1;
input max_number_prev_peaks = 5;

AddChartBubble(
 (peak and ((revpeakcnt-1)  <= max_number_prev_peaks)),
 high,
 (if revpeakcnt == 1 then "Highest\nPeak" else (revpeakcnt-1) + " Ago\nHighest\nPeak"),
 Color.magenta,
 yes);

input show_peak_bn = yes;
plot bnx = if (show_peak_bn and Peak) then bn else Double.NaN;
bnx.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
bnx.setdefaultcolor(color.cyan);


# find highest peak
def x = if bn > lastbn then 0 else ((revpeakcnt-1) <= max_number_prev_peaks);
addverticalline(0 and x, "-", color.cyan);
# or bn > lastbn
def hipeak2 = if bn == 1 then 0
 else if bn > lastbn then  hipeak2[1]
 else if (x and high > hipeak2[1]) then high else hipeak2[1];

def hipeak = if x then hipeak2[-50] else 0;
plot hip = if (x and hipeak == high) then 1 else na;
hip.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
hip.SetDefaultColor(Color.white);
hip.setlineweight(4);
hip.hidebubble();

# -------------------------------
# test stuff


addchartbubble(0, low*0.996,
x + "\n" +
high + "\n" +
hipeak2 + "\n" +
hipeak + "\n" +
hip
, color.yellow, no);


addchartbubble(0, low*0.996,
 peak + "\n" +
 bn + "\n" +
 lastbn
, (if peak then color.magenta else color.gray), no);


addchartbubble(0, low*0.996,
 peak + "\n" +
 peak_cnt + "\n" +
 peak_max + "\n" +
 revpeakcnt
, (if peak then color.magenta else color.gray), yes);

#
 
Thank you, I appreciate the understanding. Lets totally forget the word Run in this example. Im going to draw it out on a screenshot and post it. I think thats the easiest way to explain.
 
here is a different code that may do what you want
it's long and a little messy. i left your original code in it, but commented out.
it doesn't use highestall()
you can pick how many peaks to find
it finds the highest peak

Ruby:
# find_highest_peak_1

#greco26
#7
# sleepyz code...
#This is perfect! Made some changes but I have 1 more question if possible. If I want to identify the highest peak in a run, is there an easier way to code that? The logical thing for me is to list everything out like if peak 1 is > than peak then peak1 else if...
#Basically, once I hit a peak that is lower than the previous peak, I want to stop. Hopefully this would not require a fold because my brain doesn't like those. :)


#def BN = BarNumber();
#def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];

#def HighestPeak1BN = HighestAll(Peak1BN);
#def HighestPeak1Val = if BN == HighestAll(Peak1BN) then High else HighestPeak1Val[1];

#def OneAgoHighestPeak1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else OneAgoHighestPeak1BN[1];
#def OneAgoHighestPeak1Val = if BN == HighestAll(OneAgoHighestPeak1BN) then High else OneAgoHighestPeak1Val[1];

#def TwoAgoHighestPeak1BN = if OneAgoHighestPeak1BN != OneAgoHighestPeak1BN[1] then OneAgoHighestPeak1BN[1] else TwoAgoHighestPeak1BN[1];
#def TwoAgoHighestPeak1Val = if BN == HighestAll(TwoAgoHighestPeak1BN) then High else TwoAgoHighestPeak1Val[1];

#def ThreeAgoHighestPeak1BN = if TwoAgoHighestPeak1BN != TwoAgoHighestPeak1BN[1] then TwoAgoHighestPeak1BN[1] else ThreeAgoHighestPeak1BN[1];
#def ThreeAgoHighestPeak1Val = if BN == HighestAll(ThreeAgoHighestPeak1BN) then High else ThreeAgoHighestPeak1Val[1];

#def FourAgoHighestPeak1BN = if ThreeAgoHighestPeak1BN != ThreeAgoHighestPeak1BN[1] then ThreeAgoHighestPeak1BN[1] else FourAgoHighestPeak1BN[1];
#def FourAgoHighestPeak1Val = if BN == HighestAll(FourAgoHighestPeak1BN) then High else FourAgoHighestPeak1Val[1];

#def FiveAgoHighestPeak1BN = if FourAgoHighestPeak1BN != FourAgoHighestPeak1BN[1] then FourAgoHighestPeak1BN[1] else FiveAgoHighestPeak1BN[1];
#def FiveAgoHighestPeak1Val = if BN == HighestAll(FiveAgoHighestPeak1BN) then High else FiveAgoHighestPeak1Val[1];


#def HighestInRunVal = If OneAgoHighestPeak1Val > HighestPeak1Val then OneAgoHighestPeak1Val else 0;

#AddChartBubble(high == HighestInRunVal, HighestInRunVal, "xxxxxx", Color.red);

#AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
#AddChartBubble(BN == HighestAll(OneAgoHighestPeak1BN), high, "1 Ago Highest Peak", Color.plum);
#AddChartBubble(BN == HighestAll(twoAgoHighestPeak1BN), high, "2 Ago Highest Peak", Color.yellow);
#AddChartBubble(BN == HighestAll(threeAgoHighestPeak1BN), high, "3 Ago Highest Peak", Color.orange);
#AddChartBubble(BN == HighestAll(fourAgoHighestPeak1BN), high, "4 Ago Highest Peak", Color.green);
#AddChartBubble(BN == HighestAll(fiveAgoHighestPeak1BN), high, "5 Ago Highest Peak", Color.magenta);

#plot x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;
#x.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

# ---------------------------------
def na = double.nan;

# charts with more than 1000 bars may not calculate correctly
# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
# Line At Price  Mobius
# mod to find last bn
def bn = barnumber();
# find last bar number
input barsBack = 1000;
def d = if !IsNaN(close) and IsNaN(close[-1])
        then bn
        else d[1];
def lastbn = if isNaN(close[-barsBack])
            then d[-barsBack]
            else Double.NaN;

# -------------------------------------
# find peaks
input peak_bar_length = 1;
def peak = if (high > high[peak_bar_length] and high[-peak_bar_length] < high) then 1 else 0;
def peak_bn = if peak then bn else 0;

# count peaks
def peak_cnt = if bn == 1 then 0 else if ( bn == (lastbn - peak_bar_length + 1)) then peak_cnt[1] else if peak then peak_cnt[1] + 1 else peak_cnt[1];

# -------------------------------------
# find highest cnt #
input barsBack2 = 1000;
def e = if !IsNaN(close) and IsNaN(close[-1])
        then peak_cnt
        else e[1];

# 2 of these codes in 1 study cause error offset -2001 , too big
# switching offsets to getvalue, removes error
#def peak_max = if isNaN(close[-barsBack2])
#            then e[-barsBack2]
#            else Double.NaN;

def peak_max = if isNaN(getvalue(close, -barsBack2))
            then getvalue(e, -barsBack2)
            else Double.NaN;

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

def revpeakcnt = peak_max - peak_cnt + 1;
input max_number_prev_peaks = 5;

AddChartBubble(
 (peak and ((revpeakcnt-1)  <= max_number_prev_peaks)),
 high,
 (if revpeakcnt == 1 then "Highest\nPeak" else (revpeakcnt-1) + " Ago\nHighest\nPeak"),
 Color.magenta,
 yes);

input show_peak_bn = yes;
plot bnx = if (show_peak_bn and Peak) then bn else Double.NaN;
bnx.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
bnx.setdefaultcolor(color.cyan);


# find highest peak
def x = if bn > lastbn then 0 else ((revpeakcnt-1) <= max_number_prev_peaks);
addverticalline(0 and x, "-", color.cyan);
# or bn > lastbn
def hipeak2 = if bn == 1 then 0
 else if bn > lastbn then  hipeak2[1]
 else if (x and high > hipeak2[1]) then high else hipeak2[1];

def hipeak = if x then hipeak2[-50] else 0;
plot hip = if (x and hipeak == high) then 1 else na;
hip.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
hip.SetDefaultColor(Color.white);
hip.setlineweight(4);
hip.hidebubble();

# -------------------------------
# test stuff


addchartbubble(0, low*0.996,
x + "\n" +
high + "\n" +
hipeak2 + "\n" +
hipeak + "\n" +
hip
, color.yellow, no);


addchartbubble(0, low*0.996,
 peak + "\n" +
 bn + "\n" +
 lastbn
, (if peak then color.magenta else color.gray), no);


addchartbubble(0, low*0.996,
 peak + "\n" +
 peak_cnt + "\n" +
 peak_max + "\n" +
 revpeakcnt
, (if peak then color.magenta else color.gray), yes);

#
Im going to review this now and see if its what im looking for. I will let you know shortly. Thx!
 
Ok, so here goes. I want to compare each peak to the peak prior to it. I will use the bar numbers in the screenshot below and write out the rule in logical wording. When I refer to the High, I am referring the High price of the bar and not the bar number.

Let me know if this helps. Thank you, as always, for everything.

this helps. so, comparing 2 sequencial peaks to determine which one is higher.
then what?

unfortunetely, i can't read your words on the chart images. you used a font that is too small. when i zoom in, it is blurry.

---------------------

keep an eye on this thread. i will try to post something this weekend.
i converted a pine valuewhen() function, that i think can be used in your study, to look back and find a previous signal.

https://usethinkscript.com/threads/convert-tradingview-demark-9.11212/
 
I edited the images and zoomed them in. Below is what the commentary says on the slide.

The numbers in my example below are the bar numbers that appeared on the chart screenshot. Hope this helps. Thank you so much!

Is peak 775 < peak 770? Yes
Then is peak 770 < peak 767? No
Return the value high of peak 770.






Until @halcyonguy gets a chance to work on his idea, this may be close to what you described.

It will look back currently based upon the code you added for prior peaks. If it doesn't find the higest peak within the look back code, it will put the yellow bubble on the last peak..

Capture.jpg
Ruby:
def BN = BarNumber();
def peak = high > high[1] and high[-1] < high;
def Peak1BN = if high > high[1] and high[-1] < high then BN else Peak1BN[1];
def Peak1Val = if BN == Peak1BN then high else Peak1Val[1];
def HighestPeak1BN = HighestAll(Peak1BN);

def _1BN = if Peak1BN != Peak1BN[1] then Peak1BN[1] else _1BN[1];
def _1Val = if BN == HighestAll(_1BN) then High else _1Val[1];

def _2BN = if _1BN != _1BN[1] then _1BN[1] else _2BN[1];
def _2Val = if BN == HighestAll(_2BN) then High else _2Val[1];

def _3BN = if _2BN != _2BN[1] then _2BN[1] else _3BN[1];
def _3Val = if BN == HighestAll(_3BN) then High else _3Val[1];

def _4BN = if _3BN != _3BN[1] then _3BN[1] else _4BN[1];
def _4Val = if BN == HighestAll(_4BN) then High else _4Val[1];

def _5BN = if _4BN != _4BN[1] then _4BN[1] else _5BN[1];
def _5Val = if BN == HighestAll(_4BN) then High else _5Val[1];

#AddChartBubble(BN == HighestPeak1BN, high, "Hightest Peak", Color.VIOLET);
#AddChartBubble(BN == HighestAll(_1bn), high, "1 Ago Highest Peak", Color.VIOLET);

#Testing
def x = if Peak1BN != Peak1BN[1] then Peak1BN else Double.NaN;

AddChartBubble(BN == x, high, "peak");

def xx= if _5val>_4val and _4val > _3val  and _3val>_2val  and _2val>_1val   then
        _5BN else
        if _4val>_3val and _3val>_2val  and _2val>_1val  then
        _4BN else
        if _3val>_2val and _2val>_1val then
        _3BN else
        if _2val>_1val and _2val>peak1val then
        _2BN else
        if  _1Val>Peak1val then
        _1bn else
        peak1bn;

addchartBubble( bn==highestall(xx), high, "xxxx" + highestall(xx),color.yellow);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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