# Learner's Questions

Hey there! I have created my account recently but have been reading the forum for a while. I am a novice to AFL and to the coding world. Without any further ado, I just would like to invite the experts to extend their help by explaining the code. I will not asking any one to code for me but instead I will post the doubt which I come across when understanding the already built codes.

I understand the IIF, Sum and Ref, HHV functions. But what I don't understand is Ref(HHV( Close,LK),-5)

Below is the code to calculate the number of new highs (or lows) that a stock has made during the last 50 days

LK=50;
A1= IIf( L> Ref(HHV( Close,LK),-5), 1, 0 );
A2= Sum( A1, LK ) ;

I understand that Ref(Close, -5) refers the value of close 5 candles ago. When you say (L>Ref(HHV(close,LK),-5) It means value of L is greater than previous highest close value in last 50 candles but what it means -5?

Please No harsh words asking me to learn from the other pages. I have spent an ample time searching the web to understand the code.

Users guide explains everything:

http://www.amibroker.com/f?ref

References a previous or subsequent element in a ARRAY. A positive period references "n" periods in the future; a negative period references "n" periods ago.

So obviously `-5` means that you reference values "5 bars ago"

@gokul I don't think your code is calculating what you think it is calculating. I assume you just copied this from some web site that is full of mistakes.

I interpret this as higher HIGH's, is that correct? If so then why are you comparing current bar LOW to the highest value of the CLOSE for 50 bars, but that calculation is done on the value 5 bars in the past??

Perhaps you are looking for higher Low or higher Close values?

A suggestion for when you are trying to understand what your code is actually calculating read the ideas in the post.

Since I don't understand what your code is trying to do I am just making a guess and posting a sample.

``````///@link https://forum.amibroker.com/t/learners-questions/15229/3

// use a smaller number than 50 for ease of debugging your calculations
// change it back to 50 when you are relatively certain the code is correct
// and then check your calculations again

Periods	= 10; // define the number lookback periods

// is this bar's HIGH greater than the highest HIGH in the past "Periods" bars
NewHigh = H > Ref( HHV( High, Periods ), -1 );

// total number of new HIGH's in the period
TotNH 	= Sum( NewHigh, Periods );

// Explore to debug these calculations //
NewHigh_Color = IIf( NewHigh, colorGreen, colorDefault );
Filter = 1; // all bars and symbols in the analysis
AddColumn( High, "High", 1.2, colorDefault, NewHigh_Color );
AddColumn( TotNH, " Total # of New Highs", 1.0 );

``````

Run an exploration and start manually counting to see if you are actually achieving your goals with the code.

Welcome to AmiBroker and good luck!

4 Likes

Thanks Mr.Tomasz, I will read the referred article.

Absolutely yes, I was going through the program on a website and I happen to don't understand this piece of code.

``````Periods	= 10; // define the number lookback periods

// is this bar's HIGH greater than the highest HIGH in the past "Periods" bars
NewHigh = H > Ref( HHV( High, Periods ), -1 );
``````

When you are comparing the current bar's H with the highest high with in the latest 10 bars why do we need to add -1 in the end? what is the purpose of -1?

I know Ref function needs period parameter - Ref(Array, Period)

Is -1 means, it's going to count 10 bars from the previous bar? If I put it as -5, then Is it going to leave the previous 5 bars from the current bar and calculate the highest high from the previous 6th bar to 16th bar?

@Gokul You want the â€śnew highâ€ť. So compare the current bar high to the highest values of the previous Highest High. That value was calculated on the previous bar. You canâ€™t compare the current High with itself and ask is the current High higher than itself (H > H) can you?

So as of the previous bar what is the method for finding the Highest High:

`````` Ref( HHV(High, Periods ), -1)
``````

Your question is not â€śdumbâ€ť but you could find the answers yourself by debugging the code as was suggested to you in my previous post. Create an exploration and try finding what each calculation is producing. Add Columns with each variable and calculation that you are uncertain about. This will help your understanding of the afl code enormously.

Add columns in my above Exploration with,

``````HHV(High, Periods )
Ref( HHV(High, Periods ), -1)
``````

And then experiment by altering the Ref(â€¦., -1) to -2 or -3. Repeatedly run your explorations and examine the outputs of each calculation.

Same with â€śPeriodsâ€ť, try 3 bars, 5 bars, 10 bars etc and examine how and why the outputs change. This will teach you how to use those functions better than someone writing on this forum can.

2 Likes

Thanks for briefing me patiently. I will debug the code as suggested.

So I did what you suggested in your post.

``````Filter = 1;
highesthigh = Ref(HHV(High,2),-5);