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,
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.
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.
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.
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
/// @link https://forum.amibroker.com/t/how-to-find-out-the-2nd-highest-or-2nd-lowest-value/8913/7
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);
/// ###############################################################################################
/// @link https://forum.amibroker.com/t/how-to-find-out-the-2nd-highest-or-2nd-lowest-value/8913/8
/// 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 );