Limit Explorer results and rearrange

Explorer provides the results based on Filter criteria. I need to limit / restrict the explorer results to say 10 or select top 10 explored results and re-arrange as per another column. Need help.

You can automatically sort by multiple columns using SetSortColumns(): https://www.amibroker.com/guide/afl/setsortcolumns.html

However, if you want to limit your results based on dynamic criteria, you will likely need to use StaticVarGenarateRanks(): https://www.amibroker.com/guide/afl/staticvargenerateranks.html

Hello everyone,

Picking up on this topic to avoid creating a similar one.

Please read until the end. This bump is a variation of what the OP requested but addresses an implication of the StaticVarGenerateRanks() function which has to my knowledge not yet been covered in the forum.

I am trying to limit the Exploration output to the top N results when the output used for final ranking is a composite of those static variables created with StaticVarGenerateRanks()? What I mean by composite is a formula (involving said static variables) that I am calculating ex-post.

At this point, my results are sorted accurately and I added an AddRankColumn() to index them. But AddRankColumn() is not parametric. So I am stuck with indexing the entire array of composite results and I am obviously getting more than I wish. I thought about something as simple as capping the number of output rows but to no avail.

My question: how can I cap these results to the top N when I cannot natively control the output limit with StaticVarGenerateRanks()?

Thank you.

Not sure if I fully understand your question but does this help?

StaticVarGenerateRanks("Rank", "Values", 0, 1224);

nTop = 5;
pass = StaticVarGet ("RankValues" + ticker) <= nTop; // ticker = Name()

Filter = pass; // Explorer Filter

@seb without some code example I do not understand your question. Show us what you are trying to do and where you are stuck.

1 Like

Unfortunately it does not. What I have is this at the moment.

factorA_rank = StaticVarGet( "rankfactorA" + ticker);
factorB_rank = StaticVarGet( "rankfactorB" + ticker);
factorC_rank = StaticVarGet( "rankfactorC" + ticker);
composite_rank = 0.25 * factorA_rank + 0.25 * factorB_rank + 0.50 * factorC_rank;

My issue is that I am trying to cap the number of composite_rank outputs to the top 5, and not the number of my factor's outputs taking individually which I indeed could control using your method.

Perform another 'StaticVarGenerateRanks' on your 'composite_rank'?

Thanks @TrendSurfer. I russian-dolled the StaticVarGenerateRanks() like you suggested and I think that I am almost there. I did cut my output to the Top 8.

However, I have an issue with my Filter now. I am also screening for stocks above their MA(200) which I defined as MA200. So...

If I do Filter = pass >> I am getting the Top 8 (expected).
If I do Filter = Close > MA200 >> I am getting too many results as before (expected).
If I do Filter = Close > MA200 AND pass >> I am getting a blank exploration table (not expected).

What am I doing wrong? I thought I could combine several filtering criteria.

// COMPOSITE
score = 0.25 * factorA_rank + 0.25 * factorB_rank + 0.50 * factorC_rank;
StaticVarSet( "score" + ticker, score);
score_values = StaticVarGet( "score" + ticker);
StaticVarGenerateRanks( "rank", "score", 0, 1224);


// OUTPUT FILTERING
nTop = 8;
pass = StaticVarGet( "rankscore" + ticker) <= nTop;
Filter = Close > MA200 AND pass;

You can!

Do any your Symbols in your object Watchlist pass 'Close > MA200'?

You probably need to add your MA200 filter earlier in the process. That might be when you rank by factors A, B, and C (if you want initial ranks to always be consecutive starting at 1), or it might be when you create your composite value. For example, you could do something like this:

score = IIf(C > MA200, 0.25 * factorA_rank + 0.25 * factorB_rank + 0.50 * factorC_rank, -999);

Now all of the top-ranked scores will also have passed the MA(200) filter.

It should also be noted that you are reversing your ranking between the first pass (factors A, B, C) and the second pass (score). So the things that have the best ranks for the first pass will have low rank numbers (best rank is 1). So now when you combine ranks A, B, and C, the overall "best" symbol will have the lowest composite score. When you rank by score, that symbol will have a poorer rank, i.e. a higher value because StaticVarGenerateRanks() always ranks from highest to lowest. I suspect that is not what you want. To fix that, you might change the code above to something like this:

score = 0.25 * factorA_rank + 0.25 * factorB_rank + 0.50 * factorC_rank;
rankableScore = IIf(C > MA200, 1/score, -999);
...
StaticVarGenerateRanks( "rank", "rankableScore", 0, 1224);

Sir,
I need to limit my results to top 10 results in ROC, Please help this code.

dailyCLOSE = TimeFrameCompress(C, inDaily, compressLast);
prevDayCLOSE = TimeFrameExpand(Ref(dailyCLOSE ,-1), inDaily, expandFirst);
CHANGE = Ref(C,0)*100/PREVDAYCLOSE-100;

Plot(C,"",47,128);
Filter=1;
SetSortColumns( -3 );
AddColumn(CHANGE,"ROC",1.1, textcolor=IIf( Ref(C,- 1)>C,colorGreen,colorRed));