# Arranging the past 5 days close either in ascending or descending order

Hi,
Can some one help to find the past 5 days close in ascending or descending order like 1st high,2nd high,3rd high etc...
Regards,
Gloria

Simplest way... use Explorer.
For sorting either use SetSortColumns function or click column header

``````AddTextColumn(Interval(2), "Interval" );

SetSortColumns( -4 );

Filter = 1;
`````` I want to use it inside the afl to develop the strategy..
It would be helpful if it is something other than exploration sorting method.

Thanks.

Why do you make people wasting time to respond?!

Use Sort() function then!

Since you made me waste time I won't respond in more detail on latter one...

@Gloriafilamino there are several posts on the forum that help with finding the order or sorting the order of an array. For example,

There is a similar post for the Nth highest (search the forum). But we can use the above function for your case too.

``````///@link https://forum.amibroker.com/t/function-of-nth-lowest-function-in-afl/6522/2

nBars 		= Param("nBars", 5, 1, 10, 1);
PriceArray	= Close;

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

// examples, this can be put into a loop for additional flexibility
rank1 = 1;
rank2 = 2;
rank3 = 3;
rank4 = 4;
rank5 = 5;

// HighestValue2	= HHV( PriceArray, nBars ); // a simple method for finding the highest value

HighestValue	= NthLowest( PriceArray, nBars, rank5);
SecondHighest	= NthLowest( PriceArray, nBars, rank4);
ThirdHighest	= NthLowest( PriceArray, nBars, rank3);
FourthHighest	= NthLowest( PriceArray, nBars, rank2);
FifthHighest	= NthLowest( PriceArray, nBars, rank1);

// FifthHighest2	= LLV( PriceArray, nBars ); // a simple method for finding the lowest value
`````` 3 Likes

So he told you that he is looking for n-highest ones at same element of array?
Where is that mentioned?

What if he is rather looking for this?
Maybe... maybe not.
But it's getting ascending/descending order too. He was not being specific in what he is looking for?
So IMO he should tell more first.

And BTW if he is looking for highest then why not using n-highest?

1 Like

And there is another one here using different approach than using Percentile
(Including both highest lowest)

1 Like

Just FYI, in more detail...

The suggestion above of using multiple Percentile calls is not a proper solution.

Percentile function is a time consuming function and as such multiple calls will end up in slow(er) AFL execution (as with other ways).

As written in upper post rather use `Sort`() or `MxSort`().
But apparently people don't listen as seen above (because of not doing tests).

See picture below.

Code with multiple Percentile calls (second pane) is much slower than code using MxSort()... 3.5 times slower there! Here you can check yourself how much slower it is

1. Slow way of using Percentile
``````/// ###############################################################################################
/// ###############################################################################################
function NthHighest( array, period, nth ) {
/// Code source
return Percentile(array, period, (period-nth) / (period-1) * 100);
}

GetPerformanceCounter( 1 );

/// SLOW. Rather do not do multiple percentile calls
ranks = 100;
for ( j = 1; j <= ranks; j++ ) {
nth = NthHighest( C, ranks, j );
Plot(nth, "Price"+j, colorRed);
}
/// ....
performance = GetPerformanceCounter();
printf("Performance: %gms", performance);
``````
1. Faster way of using MxSort.
``````/// ###############################################################################################
/// ###############################################################################################
GetPerformanceCounter( 1 );
/// FASTER than doing multiple Percentile calls
ranks = 100;
mat_source = Matrix(BarCount, ranks);
rownum = MxGetSize(mat_source,0);
colnum = MxGetSize(mat_source,1);
for ( j = 0; j < colnum; j++ ) {
mat_source = MxSetBlock(mat_source, 0, rownum-1, j, j, Ref(C,-j));
}

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

// 1-dim array output
for ( j = 0; j < colnum; j++ ) {
block = MxGetBlock(mat_sort, 0, rownum-1, j, j, True);
Plot(block, "Price"+(j+1), colorRed);
}
/// ....
performance = GetPerformanceCounter();
printf("Performance: %gms", performance);
``````

PS: I apologize for wording "smart ass".

3 Likes

Thanks for referring to the right post..
I used the source and modified little to suit my requirement.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.