HHVBars returns the first occurence


I use the HHVBars to get the distance (in bars) from the highest bar, in a given period of bars.

In some cases I have two bars that are next to each other or very very close (2-3 bars appart) that have the same height and are also the highest bars. HHVBars returns the first occurence (leftmost bar). I would prefer to detect and record the distance from the 2nd of the two bars.

Is there any quick way to do this?

I thought of taking the HHV price, then doing a ValueWhen for that price and get the barindex. This would bring me the barindex of the rightmost bar.

I was looking for something more elegant though.

Can you try BarsSince() over ValueWhen()?

Then you can just check iff HHVBars not equal to BarsSince(), that way you can workaround the BarIndex()
Didn't get enough time to check if I can test it against some sample data but BarsSince() could return the recent most price.

However, logically HHVBars make more sense returning the leftmost bar because you are specifying a period in it.

This thread HHV and Barssince issue is relevant to your question and you shall find an answer inside it.
P.S try to search the forum before creating new post :slight_smile:

1 Like

First high bar within given period..........?

bi = BarIndex();
Period = 30;
HighBar	= HHV(H,Period);
FirstHigh = ExRemSpan((HighBar == High),Period);
GetHighBar = ValueWhen(FirstHigh,bi,1);

You are right. Tomasz's solution described in this post is the most elegant ( HHV and Barssince issue ):
BarsSince( H == HHV( H, 28 ) );