Help finding previous peaks

GUIDO MENG

New member
Hi All!

I've been trying to do the following for 3 days with just partial success:

  1. On a daily chart with the current close price, I want to find the previous bar number that complies with 3 conditions. Those being: a) has a higher HIGH than the current close price b) is a peak in the sense that the previous bar of the peak has a lower high and the following bar also has a lower high than the peak.
  2. I want the code to be able to find not just one but at least 3 peaks but the first previous peak should be higher than the current close, the second previous peak should be higher than the first previous peak and the third previous peak should be higher than the second previous peak.
  3. I have been able to find the first previous peak via a script but when I summon the script for the second time the code simply doesn't produce any results. The desired way would be using the code that is deactivated instead of summoning the script the second time using the known parameter that is the result of the first calling of the script function.

    Here is my code:
def totalBars = HighestAll(BarNumber());


script prevPeak_Val {

input refBarNumber = 0;
input refBarValue = 0;
input numBarsInChart = 0;

def peak_Val = fold i = refBarNumber + 1 to numBarsInChart - 1

with barHigh_Val = Double.NaN

While IsNaN(barHigh_Val) do

if i == numBarsInChart - 1 and
GetValue(high, i) >= refBarValue
or
i < numBarsInChart - 1 and
GetValue(high, i) >= refBarValue and
GetValue(high, i) >= GetValue(high, i + 1) and
GetValue(high, i) >= GetValue(high, i - 1) then
#GetValue(high, i)
i
else
Double.NaN;

plot prevPeak_Val = peak_Val;
}


def firstBar = highestAll(if !IsNaN(close[0]) and IsNaN(close[-1]) then 0 else Double.NaN);


def prueba01 = if !IsNaN(firstBar) then prevPeak_Val(refBarNumber = firstBar, refBarValue = GetValue(close, firstBar), numBarsInChart = totalBars) else Double.NaN;

#def prueba02 = if !IsNaN(prueba01) then
# prevPeak_Val(refBarNumber = prueba01, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
# else
# Double.NaN;


def prueba02 = if !IsNaN(prueba01) then # (HERE THE VALUE OF THE FIRST PARAMETER = 4 IS THE KNOWN RESULT OF prueba01 BUT IT SHOULD USE THE VARIABLE prueba01 TO FIND THE SECOND PREVIOUS PEAK.)

prevPeak_Val(refBarNumber = 4, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
else
Double.NaN;


AddLabel(if !IsNaN(prueba01) then yes else no, prueba01, color.WHITE);

AddLabel(if !IsNaN(prueba02) then yes else no, prueba02, color.WHITE);
 
Last edited:
Solution
defining the last peak is usually somewhat simple, finding previous "peaks" maybe different for some people. i attempted to load your code to get an idea however it didnt plot anything on the chart which makes it difficult to interpret what you are attempting to do. if you can provide a working code that plots what you see as "peaks" and then perhaps circle them at the same time in a picture then it would make it easier in helping you.
defining the last peak is usually somewhat simple, finding previous "peaks" maybe different for some people. i attempted to load your code to get an idea however it didnt plot anything on the chart which makes it difficult to interpret what you are attempting to do. if you can provide a working code that plots what you see as "peaks" and then perhaps circle them at the same time in a picture then it would make it easier in helping you.
 
Solution
Hi All!

I've been trying to do the following for 3 days with just partial success:

  1. On a daily chart with the current close price, I want to find the previous bar number that complies with 3 conditions. Those being: a) has a higher HIGH than the current close price b) is a peak in the sense that the previous bar of the peak has a lower high and the following bar also has a lower high than the peak.
  2. I want the code to be able to find not just one but at least 3 peaks but the first previous peak should be higher than the current close, the second previous peak should be higher than the first previous peak and the third previous peak should be higher than the second previous peak.
  3. I have been able to find the first previous peak via a script but when I summon the script for the second time the code simply doesn't produce any results. The desired way would be using the code that is deactivated instead of summoning the script the second time using the known parameter that is the result of the first calling of the script function.

    Here is my code:
def totalBars = HighestAll(BarNumber());


script prevPeak_Val {

input refBarNumber = 0;
input refBarValue = 0;
input numBarsInChart = 0;

def peak_Val = fold i = refBarNumber + 1 to numBarsInChart - 1

with barHigh_Val = Double.NaN

While IsNaN(barHigh_Val) do

if i == numBarsInChart - 1 and
GetValue(high, i) >= refBarValue
or
i < numBarsInChart - 1 and
GetValue(high, i) >= refBarValue and
GetValue(high, i) >= GetValue(high, i + 1) and
GetValue(high, i) >= GetValue(high, i - 1) then
#GetValue(high, i)
i
else
Double.NaN;

plot prevPeak_Val = peak_Val;
}


def firstBar = highestAll(if !IsNaN(close[0]) and IsNaN(close[-1]) then 0 else Double.NaN);


def prueba01 = if !IsNaN(firstBar) then prevPeak_Val(refBarNumber = firstBar, refBarValue = GetValue(close, firstBar), numBarsInChart = totalBars) else Double.NaN;

#def prueba02 = if !IsNaN(prueba01) then
# prevPeak_Val(refBarNumber = prueba01, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
# else
# Double.NaN;


def prueba02 = if !IsNaN(prueba01) then # (HERE THE VALUE OF THE FIRST PARAMETER = 4 IS THE KNOWN RESULT OF prueba01 BUT IT SHOULD USE THE VARIABLE prueba01 TO FIND THE SECOND PREVIOUS PEAK.)

prevPeak_Val(refBarNumber = 4, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
else
Double.NaN;


AddLabel(if !IsNaN(prueba01) then yes else no, prueba01, color.WHITE);

AddLabel(if !IsNaN(prueba02) then yes else no, prueba02, color.WHITE);


this finds 3 peaks, with each one lower than the previous one.
it draws bubbles on them 3,2,1
can draw a cyan dot on all peaks

main signal. when close after peak #1 is lower than peak #1 high,
..draws cyan wedges (small arrows), above and below the bar.
..can draw a vertical line.

during the 3 peaks, these 3 variables hold the barnumbers of the peak bars,
bn3 , bn2 , bn1


Code:
#prev_3peaks

#https://usethinkscript.com/threads/help-finding-previous-peaks.19023/
#Help finding previous peaks
# find 3 peaks, 3,2,1  each 1 lower than prev
# is high on 3rd peak higher than that next bar close?

def na = double.nan;
def bn = barnumber();

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

# based on prev bar , the peak bar
def r1 = (high > close[-1]);
def r2 = peak;


input show_peaks = yes;
plot zp = if show_peaks and peak then high else na;
zp.SetPaintingStrategy(PaintingStrategy.POINTS);
#zp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zp.SetDefaultColor(Color.cyan);
zp.setlineweight(3);
zp.hidebubble();


def n = 100;
def seqmax = 3;
# start on a peak.  look at future bars. 
#  find 3 peaks,  3,2,1,  each one lower than prev one
def pk3bn;
def pk2off;
def pk2bn;
def pk1off;
def pk1bn;
def seq;
if bn == 1 then {
 pk3bn = 0;
 pk2off = 0;
 pk2bn = 0;
 pk1off = 0;
 pk1bn = 0;
 seq = 0;

} else if peak and seq[1] == 0 then {
 # 3rd peak
 pk3bn = bn;

 # 2nd peak
 pk2off = fold a = 1 to n
  with b = 1
  while !getvalue(peak, -a)
  do b + 1;
 pk2bn = if pk2off > 0 and getvalue(high,-pk2off) < high then pk2off + bn else 0;

 # 1st peak
 pk1off = fold c = (pk2off + 1) to n
  with d = 1
  while !getvalue(peak, -c)
  do d + 1;

 pk1bn = if pk2bn > 0 and pk1off > 0 and getvalue(high,-pk2off) > getvalue(high,-(pk2off + pk1off)) then (pk2off + pk1off + bn) else 0;
 seq = if pk2bn > 0 and pk1bn > 0 then 1 else 0;

} else {
 pk3bn = 0;
 pk2off = 0;
 pk2bn = 0;
 pk1off = 0;
 pk1bn = 0;
 seq = 0;
}

def droppingpeaks = (peak and pk3bn > 0 and pk2bn > 0 and pk1bn > 0);

# check if all 3 peaks recorded a bn
def bn3;
def bn2;
def bn1;
if bn == 1 then {
 bn3 = 0;
 bn2 = 0;
 bn1 = 0;
} else if bn1[1] > 0 and bn > bn1[1] then {
 bn3 = 0;
 bn2 = 0;
 bn1 = 0;
} else if droppingpeaks and bn3[1] == 0 then {
 bn3 = pk3bn;
 bn2 = pk2bn;
 bn1 = pk1bn;
} else {
 bn3 = bn3[1];
 bn2 = bn2[1];
 bn1 = bn1[1];
}


def pk2 = (bn3 == bn or bn2 == bn or bn1 == bn);

addchartbubble(pk2, high*1.001,
(if bn3 == bn then "3" else if bn2 == bn then "2" else if bn1 == bn then "1" else "-")
, color.yellow, yes);


# signal on bar after peak#1
def clsloprev = close < high[1] and bn1[1] == bn[1];


# mark current bar with a close lower than prev high, after 3 peaks
plot zout1 = clsloprev;
zout1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zout1.SetDefaultColor(Color.cyan);
zout1.setlineweight(4);
zout1.hidebubble();

plot zout2 = clsloprev;
zout2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_down);
zout2.SetDefaultColor(Color.cyan);
zout2.setlineweight(4);
zout2.hidebubble();


input show_vert_line_signal = yes;
addverticalline(show_vert_line_signal and clsloprev, "-", color.cyan);

#----------------------------------
# test data

addchartbubble(0, low,
 bn + "\n" +
 droppingpeaks + "\n" +
 bn3 + "\n" +
 bn2 + "\n" +
 bn1 + "\n" 
, color.yellow, no);


addchartbubble(0 and peak, low*0.998,
 bn + " bn\n" +
 pk3bn + "\n" +
pk2off + "\n" +
 pk2bn + "\n" +
pk1off + "\n" +
 pk1bn + "\n" 
, color.yellow, no);
#
 

Attachments

  • img2.JPG
    img2.JPG
    62.8 KB · Views: 82

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
293 Online
Create Post

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