If I run the exploration on 10/20/17 for American airlines (AAL) the columns return the following:
Bars High=8
Bars low=41
High=53.03
Low=42.92
Range=33
All the above are correct.
The HHV r, however , is 54.22 which wrong.
This is the HHV for a range of 63 and not 33.
The right value for HHVR in this range (33) and 13 (8+5) days in the past is 50.51 and not 54.22.
Could anybody tell me what am I doing wrong?

Moderator comment: Your post was INCORRECTLY formatted and I had to fix it. Next time follow the forum rules and format message correctly!

@markos I suggest breaking your code and exploration into smaller segments to see if the calculations are what you intended. Since I am not sure what you are exactly trying to code I have just added an extra line for your exploration and looked back the "13 bars" to see what the values were. They seem to be correct, so perhaps you are attempting to look at something different.

( I think that I have a different data feed so my numbers will not be exactly the same as yours as the August 10, 2017 $0.10 dividend is treated differently)

Larry thanks for the reply. What I am trying to do is find the second Highest value before the most recent Highest (HIGH1) . In the attached chart of AAL I am trying to find the HHV between points C (10/3/17) and E (8/16/17) . Distance CE is 33 days and point C is ref 13 days from today's date 10/20/17 and 5 days from the most recent high point A.
In this case the Highest is 50.51 (point C) , my code, however, returns 54.22 which is point D on 7/14/17 which is outside the range.

@markos The problem is the RANGE keeps changing. So when you look back 13 bars and want to calculate the Highest for a RANGE , the RANGE is no longer 33, but it is the RANGE value of 103 (that was the value of RANGE 13 bars ago!). So the calculation looks back for 103 bars and finds the highest high which is of course not what you are looking for.

Iâve attempted a few solutions but keep coming up against the same problem. Your calculations keep changing the variables every bar. Sorry, if I figure out a solution I will post it.

@markos Ok, if this works you owe me a beer! Because your variable RANGE keeps changing we need to set the latest value for RANGE to a number as opposed to an array.

HighB = Ref( HHVBARS( C, 30 ), -2 ) + 2;
LowB = LLVBars( C, 120 ) + 1;
High1 = Ref( HHV( C, 30 ), -2 );
Low1 = LLV( C, 120 );
Range = LowB - HighB;
LatestRange = EndValue( Range );
HHVR = Ref( HHV( C, LatestRange ), -( HighB + 5 ) );
// Explore to reveal what calculations are creating //
Filter = 1;
AddColumn( HighB, "BARS HIGH", 1.0 );
AddColumn( LowB, "BARS LOW", 1.0 );
AddColumn( High1, "HIGH", 1.2 );
AddColumn( Low1, "LOW", 1.2 );
AddColumn( Range, "Range", 1.0 );
AddColumn( HHVR, "HHVR", 1.2 );
// for completeness sake, let us look at what EndValue has created
AddColumn(LatestRange, "EndValue( Range )", 1.0, colorDefault, colorLightYellow);

To be honest, each of the following functions seemed to generate similar results. The advantages and disadvantages of each for your problem are beyond my knowledge of AmiBroker.

@portfoliobuilder Bravo Larry. I think Markos is looking for a Cup and handle patterns at this moment. That is One of my favourite patterns
Something like
"Identifying Cup Formations Early" article Giorgos E. Siligardos presents a pattern detection formula for cup formations.
S&C Tradersâ Tips Issue 04-2011 http://www.amibroker.com/members/traders/04-2011.html
That is coded by Tomasz
Also there are many on internet AFL about that I think the first One that he wrote afl was Dimitris Tsokakis and the latest good one is by @empottasch Ed. pottasch

ÎÎ±Î»Î·ÏÏÎ”ÏÎ±. Hello Markos and welcome to AB forum

@PanoS Between you, Markos, me (yesâŠLarry came from old-time Canadian Anglo-Saxonâs converting Leonidas to Larry ), Tsokakis and Silgardos we can have an AmiBroker Greek dance some day.

Larry and Panos. This is my first post and I was overwhelmed by your warm welcome and information. Thank you again. By the way the Greek dance video made my day.

@portfoliobuilder, even though it was a couple of years ago, I must thank you for sharing this flash mob video. It was a welcome distraction as I battled with a similar REF problem.