# How to find out the 2nd highest or 2nd lowest value

It is very much easier to find highest and lowest values using MAX and MIN functions but how to use the same function to get the second highest or third lowest value from the example below,

``````HH1 = Max(Max( Max(1, 2 ), Max(3, 4)),Max( Max(5, 6 ), Max(7, 8)))   ;
LL1 = MIN(MIN( MIN(1, 2 ), MIN(3, 4)),MIN( MIN(5, 6 ), MIN(7, 8)))   ;
``````

Thank you.

You could put the values into an array or a matrix, and then use `Sort()` or `MxSort()`.

1 Like

Are you talking about symbol array or custom array?

If you are talking about nth-highest/lowest of 1-dim array of your symbol over some period (which includes array size too) then look here

NthLV would be

``````function NthLV( array, period, nth ) {
return Percentile( array, period, (nth - 1) / (period - 1) * 100 );
}
``````

If you just have some custom vector then

``````// unsorted custom array
mat = MxFromString( "[2;4;1;7;3;5;8;6]" );

// sorted custom array
matsort = MxSort(mat, 1);
MxToString(matsort);

rownum = MxGetSize(mat,0);

// second highest and lowest values of custom array
sec_lowest = matsort[1][0];
sec_highest = matsort[rownum-2][0];

printf( "second highest: %g, second lowest: %g", sec_highest, sec_lowest );
``````
6 Likes

....Deleted.....

Dear forum members thanks for responding to my query, but my query is not sorted out.
Thanks fxchart for giving few examples but those didn't workout for me.
from my little knowledge what i understood from your 1st example is,
it can see the previous 30 candles and gives the output with the 2nd highest value.
from the 2nd example, the values within the string works only when it has integers.

my code is like this:
f1p = 0.80;
f2p = 0.52;
f3p = -1.20;
f4p = 2.15;
f5p = 0.90;
f6p = 0.10;
f7p = -1.28;
f8p = 0.75;
f9p = -1.80;
f10p = -0.05;
f11p = -0.85;

from the above set of arrays how to find the second or third highest value?
will it not be possible using max and min functions?

It is sorted out! You just do not understand yet.

No, it can see any look-back period making sense but not just 30 bars.

That's what you asked for! See here:

First of all what you have there is not a set of arrays! It is a set of numbers each assigned to a variable being of type number again.

What for? You are not looking for max/min but 2nd, 3rd, 4th,....

That's incorrect!
First of all given matrix creation was example to create custom array out of some values. I used the same values as you did in your first example! That's all.

Secondly you can create and fill matrix via several ways! And you can fill a matrix with any type of numbers!

So back to your "problem" which is a zero problem in reality.

``````/// @link https://forum.amibroker.com/t/how-to-find-out-the-2nd-highest-or-2nd-lowest-value/8913/6
f1p = 0.80;
f2p = 0.52;
f3p = -1.20;
f4p = 2.15;
f5p = 0.90;
f6p = 0.10;
f7p = -1.28;
f8p = 0.75;
f9p = -1.80;
f10p = -0.05;
f11p = -0.85;

mat = Matrix( 11, 1 );
rownum = MxGetSize(mat,0);

for ( i = 0; i < rownum; i++ ) {
var = VarGet("f"+(i+1)+"p");
if ( typeof(var) == "number" )
mat[i][0] = var;
}

"Source array:";
MxToString(mat);

// sorted custom array
matsort = MxSort(mat, 1);

"Sorted array:";
MxToString(matsort);

nth = 2; // e.g second highest and second lowest values

// nth highest and lowest values of custom array
nth = abs(nth);
if ( rownum >= nth && nth > 0 ) {
nth_lowest = matsort[nth-1][0];
nth_highest = matsort[rownum-nth][0];
printf( "second highest: %g, second lowest: %g", nth_highest, nth_lowest );
}
``````

So as you can see I did the same thing as before once the custom array was created.

Result output:

4 Likes

seeing the way you have replied i have gone dumbstruck, what a clinical explanation! As I'm a physician, i have no knowledge about coding but slowly I'm getting to learn from this wonderful forum.

You have sorted out my query for what i have asked but it is limited to only set of numbers i guess. check the below code in which i have dropped your code, as well as mine for example. interpretation works with your code only, not with mine! what went wrong for me, maybe i have not understood varget, please check.

``````f1p = MA(C,3);
f2p = MA(C,5);
f3p = MA(C,7);
f4p = MA(C,10);
f5p = MA(C,15);
f6p = MA(C,20);
f7p = MA(C,30);
f8p = MA(C,45);
f9p = MA(C,60);
f10p = MA(C,90);
f11p = MA(C,120);

mat = Matrix( 11, 1 );
rownum = MxGetSize(mat,0);
for ( i = 0; i < rownum; i++ ) {

var = VarGet("f"+(i+1)+"p");

if ( typeof(var) == "number" )

mat[i][0] = var;

}
"Source array:";
MxToString(mat);
// sorted custom array
matsort = MxSort(mat, 1);
"Sorted array:";
MxToString(matsort);
// second highest and lowest values of custom array

sec_lowest = matsort[1][0];
sec_highest = matsort[rownum-2][0];
printf( "second highest: %g, second lowest: %g", sec_highest, sec_lowest );
/////////////////
z1p = 0.80;
z2p = 0.52;
z3p = -1.20;
z4p = 2.15;
z5p = 0.90;
z6p = 0.10;
z7p = -1.28;
z8p = 0.75;
z9p = -1.80;
z10p = -0.05;
z11p = -0.85;

mat1 = Matrix( 11, 1 );
rownum1 = MxGetSize(mat1,0);

for ( i = 0; i < rownum1; i++ ) {
var1 = VarGet("z"+(i+1)+"p");
if ( typeof(var1) == "number" )
mat1[i][0] = var1;
}

"Source array:";
MxToString(mat1);

// sorted custom array
matsort1 = MxSort(mat1, 1);

"Sorted array:";
MxToString(matsort1);

nth1 = 2; // e.g second highest and second lowest values

// nth highest and lowest values of custom array
nth1 = abs(nth1);
if ( rownum1 >= nth1 && nth1 > 0 ) {
nth_lowest1 = matsort1[nth1-1][0];
nth_highest1 = matsort1[rownum1-nth1][0];
printf( "second highest: %g, second lowest: %g", nth_highest1, nth_lowest1 );
}
``````

The actual coding screenshot is below;

So how do i sort them as there are no direct numbers.

Why have you mentioned numbers all the time if all you are after are arrays in the first place?

If you do not know the difference between number and array then read here. Even as physician you should know about existence of documented material and how to get there (-> search engines).

Why do you waste time of people who have given you working example (yes, for numbers as you did use them in the first place(!) but same concept will work the for arrays with few adjustments!).

Why do you remove link from original code solution? It is not your code solution. It is not you who have come up with it. It is solely based on my brain factory using AFL solely based on brain factory of amibroker.com.
If you remove link again you will be ignored by me in future because I consider it as disrespect. If you want to remove links then please come up with solution yourself next time.

No, it is not your code solution. Just adding different input variables does not make it yours.

``````/// sample set of arrays
f1p = MA(C,3);
f2p = MA(C,5);
f3p = MA(C,7);
f4p = MA(C,10);
f5p = MA(C,15);
f6p = MA(C,20);
f7p = MA(C,30);
f8p = MA(C,45);
f9p = MA(C,60);
f10p = MA(C,90);
f11p = MA(C,120);

/// ###############################################################################################
/// code solution by fxshrat@gmail.com
/// code is available only at forum.amibroker.com
/// code is only for personal non-commercial use
/// ###############################################################################################
mat_source = Matrix( BarCount, 11 );
rownum = MxGetSize(mat_source, 0 );
colnum = MxGetSize(mat_source,1);
for ( j = 0; j < colnum; j++ ) {
var = VarGet("f"+(j+1)+"p");
mat_source = MxSetBlock(mat_source, 0, rownum-1, j, j, var);
}

// sorted custom array
mat_sort = MxSort(mat_source, 0);

nth = 2; // e.g second highest and second lowest values

mat_nth = Matrix(rownum, 2);
nth = Min(colnum, Max(1, nth));
for ( i = 0; i < rownum; i++ ) {
nth_lowest = mat_sort[i][nth-1];
nth_highest = mat_sort[i][colnum-nth];
mat_nth[i][0] = nth_lowest;
mat_nth[i][1] = nth_highest;
}

nth_lowest_array = MxGetBlock(mat_nth, 0, rownum-1, 0, 0, True);
nth_highest_array = MxGetBlock(mat_nth, 0, rownum-1, 1, 1, True);

/// Output #######################################################################
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

if ( Status( "actionex" ) == actionExInterpret ) {
printf( "Source multidim-array of visible chart:\n");
printf( MxToString(MxGetBlock(mat_source, startrow = Max(0,fvb), endrow = Min(rownum-1,lvb), 0, colnum-1 )));

printf( "\n\nSorted multidim-array of visible chart:\n");
printf( MxToString(MxGetBlock(mat_sort, startrow, endrow, 0, colnum-1 )));

printf( "\n\nnth-value: %g", nth);
printf( "\nN-th highest/lowest multidim-array of visible chart:");
printf( "\n(First colum: nth-lowest, second column: nth-highest)\n");
printf( MxToString(MxGetBlock(mat_nth, startrow, endrow, 0, 1 )));
}

Plot( C, "", colorDefault, styleCandle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g \nn-th: %g{{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 ) ), V, nth ) );

Plot( nth_highest_array, "\nn-th_highest", colorGreen );
Plot( nth_lowest_array, "n-th_lowest", colorRed );

for( i = 1; i <= 11; i++ )
Plot( VarGet("f"+i+"p"), "\nMA"+i, colorYellow );
``````

7 Likes