HHV returns the wrong value in the past

The HHV function when used with Ref in the past. For example in the following exploration code

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;
HHVR=Ref(HHV(C,RANGE),-(HIGHB+5));

Filter=C>0;
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);`

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)

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;
HHVR = Ref( HHV( C, RANGE ), -( HIGHB + 5 ) ); // Markos initial line
HighestDuringRange  = HHV( C, RANGE ); // additional line

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 );
AddColumn( HighestDuringRange, "HHV( C, RANGE )", 1.2, colorDefault, colorLightYellow );

image

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.aal

@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.

Good luck.

1 Like

@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.

So perhaps this will work,
https://www.amibroker.com/guide/afl/endvalue.html

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

image

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.

http://amibroker.com/guide/afl/endvalue.html
http://amibroker.com/guide/afl/selectedvalue.html
http://amibroker.com/guide/afl/valuewhen.html

1 Like

@portfoliobuilder Bravo Larry. I think Markos is looking for a Cup and handle patterns at this moment. That is One of my favourite patterns :smiley:
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

1 Like

Thanks Larry .
The endvalue function did the trick. It is a pity that the code can’t be backtested though.

Panos thank you for the information

@markos Though it is beyond my abilities, I think with proper coding most formations can be back tested (maybe check for help, Third party services, blogs, courses, books, add-ons)

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

4 Likes

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.

1 Like