# Best way to determine last occurrence of a pattern without HighestAll()

#### armybender

##### Active member
Hi All,

I have a study that looks for peaks and valleys (i.e. high > high[1] and high > high[-1]). Of course, these occur quite often, and I'm interested in finding the LAST occurrence of the pattern WITHOUT USING HIGHESTALL().

Reason for not wanting to use HighestAll() is that I want it to update on every tick, and HighestAll() puts it into a once-per-bar mode.
I've tested this thoroughly and know that to be the case.

What's the best method for this?

Any help is appreciated.

Thanks!

Last edited by a moderator:
Solution
Hi All,

I have a study that looks for peaks and valleys (i.e. high > high[1] and high > high[-1]). Of course, these occur quite often, and I'm interested in finding the LAST occurrence of the pattern WITHOUT USING HIGHESTALL().

Reason for not wanting to use HighestAll() is that I want it to update on every tick, and HighestAll() puts it into a once-per-bar mode.
I've tested this thoroughly and know that to be the case.

What's the best method for this?

Any help is appreciated.
hal_last
Thanks!

find the last signal on a chart

define a signal
find the signal barnumbers
define a...
Hi All,

I have a study that looks for peaks and valleys (i.e. high > high[1] and high > high[-1]). Of course, these occur quite often, and I'm interested in finding the LAST occurrence of the pattern WITHOUT USING HIGHESTALL().

Reason for not wanting to use HighestAll() is that I want it to update on every tick, and HighestAll() puts it into a once-per-bar mode.
I've tested this thoroughly and know that to be the case.

What's the best method for this?

Any help is appreciated.
hal_last
Thanks!

find the last signal on a chart

define a signal
find the signal barnumbers
define a bar 50 bars from the last bar
jump to the last bar and find the highest signal barnumber
plot an arrow when highest signal barnumber = barnumber

draw cyan dots on all signals
draw arrow on last signal

Code:
``````# last_signal_0

#find last signal

def bn = BarNumber();
def na = Double.NaN;

#def signal = (bn % 12 ) == 0;
#def signal = (high > high[1] and high > high[-1]);
def signal = (high >= high[1] and high >= high[-1]);

# use a number, same as negative offset in signal formula
def sig_offset = 1;

# running count of signal
def cnt = if bn == 1 then 0 else if signal then cnt[1] + 1 else cnt[1];

def sigbn = if bn == 1 then 0
else if signal then bn
else sigbn[1];

def n = 50;

def x = !IsNaN(close[-(n - 1)]) and
IsNaN(close[-n]);

input vert_line = no;
addverticalline(vert_line and x, "-", color.cyan);

def lastbn = if bn == 1 then 0
else if x then Highest(sigbn[-(n - 1 - sig_offset)], (n - sig_offset))
else lastbn[1];

def last = if lastbn == bn then 1 else 0;

plot z1 = if last then high*1.0001 else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_down);
z1.SetDefaultColor(Color.green);
z1.setlineweight(3);
z1.hidebubble();

input show_all_signals = yes;
plot z2 = if show_all_signals and signal then low*0.999 else na;
z2.SetPaintingStrategy(PaintingStrategy.points);
z2.SetDefaultColor(Color.cyan);
z2.setlineweight(2);
z2.hidebubble();

input test1 = no;
bn + "\n" +
signal + " s\n" +
sigbn + " sbn\n" +
x + " x\n" +
lastbn + " Lbn\n" +
last + " L\n"
, (if signal then color.yellow else color.gray), no);
#``````

i randomly picked 50 because i guessed there should be at least 1 signal in the last 50 bars on chart. if not, make it bigger.

Last edited:

### Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
411 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?