Function of Nth highest function in AFL

Ayone can of the Nth highest in AFL, thanks in advance

1 Like

It’s not clear from your question exactly what you are after, but Amibroker does have several ways to perform ranking. They are listed in detail here: https://www.amibroker.com/guide/h_ranking.html

1 Like

Hi Friend,

Thanks for yoru reply. I want to write a revised KD function which using the average of 1st hhv and 2nd hhv to smoothen the extreme high or low point in normal KD calculation. In tradestation, it has a nthhighest function but in amibroker, I can only found the hhv function which can only find out the highest value within a certain of time, but I need 2nd highest value or even 3rd highest value as well. Thanks in advance.

1 Like

Here's one possible way to achieve it, although there may well also be a simpler approach out there.

Period = 30;
HighestValue = HHV(H, Period);

// Calc second highest, excluding highs when highest high is made
SecHighestInput = IIf(H < HighestValue, H, Null); 
SecondHighestValue = HHV(SecHighestInput, Period);

// When a new highest high is returned for the lookback period, take one bar less for lookback for second highest
SecondHighestValue = IIf(SecondHighestValue == HighestValue, HHV(SecHighestInput, HHVBars(H, Period) - 1), SecondHighestValue);

// When a new highest high is made, the second highest is the previous highest high
PreviousHighestHigh = ValueWhen(HighestValue != Ref(HighestValue, -1), Ref(HighestValue, -1));
SecondHighestValue = IIf(H == HighestValue, PreviousHighestHigh, SecondHighestValue);

Filter = 1;
AddColumn(H, "High");
AddColumn(HighestValue, "HighestValue");
AddColumn(SecondHighestValue, "SecondHighestValue");

image

3 Likes

Many thanks for your help.

  1. Your code is wrong as it returns wrong values (see table below).
  2. And yes, there is simpler way by just using Percentile function outputting correct values
function NthHV( array, period, nth ) {
    return Percentile( array, period, (period-nth) / (period-1) * 100 );
}

Period = 30;
HighestValue = HHV(H, Period);
NthHighest = NthHV(H, period, nth=2 );

Filter = 1;
format = 1.2;
AddColumn(H, "High", format );
AddColumn(HighestValue, "HighestValue", format );
AddColumn(NthHighest, "NthHighestValue("+nth+")", format );

16 Likes

Thanks for your revision, this solve my problem completely. Thanks again for your help.

Sorry, this is my first question asked in here so don’t know the rule. Just clicked your reply as solution. Thanks again for your help.

Is it possible to find the bar in which this second maximum is found?
I need first and second maximum in a period and the bar in which it occurs.
for the first maximum there is no problem. but for the second I use this percentile function, but I can't think of how to see in which bar it occurs.
I'm looking to see if the first maximum is older than the second maximum.
Thank you

1 Like

I provided the answer already here: Translating EasyLanguage to AFL - in general

// NthLowest(Bars) AFL code copyright (C)2020 Tomasz Janeczko
// The formula can be used only if copyright notice is retained
// Nth starts from 1, so second lowest is 2

function NthLowest( array, Nth, period )
{
	return Percentile( array, period, 100 * ( Nth - 1 ) / ( period - 1 ) );
}

function NthLowestBars( array, Nth, period )
{
	nlv = Percentile( array, period, 100 * ( Nth - 1 )/ ( period - 1 ) );
	
	bars = Null;
	
	for( i = 0; i < period; i++ )
	{
		bars = IIf( Ref( array, -i ) == nlv, i, bars ); 
	}
	
	return bars;
}

Filter = 1;
AddColumn( Close, "Close");
AddColumn( LLV( Close, 10 ), "1st Lowest");
AddColumn( LLVBars( Close, 10 ), "1st Lowest bars");
AddColumn( NthLowest( Close, 2, 10 ), "2nd Lowest" );
AddColumn( NthLowestBars( Close, 2, 10 ), "2nd Lowest bars" );

For highest, replace

Percentile( array, period, 100 * ( Nth - 1 ) / ( period - 1 ) );

with

Percentile( array, period, 100 * (period - Nth) / ( period - 1) );
9 Likes

I need a way to find second, third highest But my period parameter is an array. The given solution works with period as number. Any suggestion?

Write yourself, or use search on this forum:

1 Like

Thanks, i will give it a go and post the code here.

1 Like