```
# 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);
#
```