Exclude top rankings / percentile? ( Rockstar beater )

Hi guys,
I was trying to use the percentile function to rank stock in different stack but I check around and there wasn't a lot of example around to learn from so was trying to get some idea from the site here. The logic behind is the stock that have the biggest gain often doesn't continue the momentum but it's the stock that are the next 1 or 2 percentile that often beat the high gain stock. Same of course is to be on the biggest loss side. It's kind of a rock star thing.. while it attract all the attention, it's often the 2nd or 3nd liner that earn the biggest money. Any idea anyone?

The Percentile() and PercentRank() functions compare each element of an array to some number of previous elements in the same array. If you want to compare values for the same bar (array element) from different securities, then you would need to use the StaticVarGenerateRanks() function.


@zeichai in addition to @mradtke's advice you may want to look over this article which presents an example of excluding a specific number of top ranked symbols (in this example in a rotational system).



Hi Guys,
Thanks for the great tips... so it seem I've looking at the wrong tool for the jobs.. I take a look at what portfoliobuilder suggest but I always can't understand the loop kind of stuff as it always get me in a loop... :sweat_smile: must be those curly brackets... in any case, the linked article talk about excluding the top rank but I'm more interested in to trying out the different stack concept. I thought it the stack are most likely hidden few stack down so it's nowhere near the top so most search won't find them... just wondering is the staticvargenerateranks() function the way to go...?

@zeichai I would consider renaming this thread so in future it will be easier for users to search for a topic like "ranking", "exclude top rankings" or "percentile ranking".

There are many examples of ranking on the forum and in the user guide,

If I understand your purpose of excluding top ranked stocks perhaps something like this,

///@link https://forum.amibroker.com/t/rockstar-beater/11598/4
wlnumber  = GetOption( "FilterIncludeWatchlist" );
SymbolList = CategoryGetSymbols( categoryWatchlist, wlnumber );
NumberOfTickers = StrCount( SymbolList, "," ) + 1;

CutOffRank = 80; // set your own preferred cutoff

if( Status( "stocknum" ) == 0 )
    StaticVarRemove( "ValuesToSort*" );

    for( i = 0; ( Symbol = StrExtract( SymbolList, i ) ) != ""; i++ )
        SetForeign( Symbol );
        Values = RSI( 20 ); // plug in your preferred method
        StaticVarSet( "Values" + Symbol, Values );

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

symbol = Name(); 

svValues = StaticVarGet( "Values"  + Symbol);
svRankValues = StaticVarGet( "rankValues" + Name() );
PercentileRankR = 100 * ( NumberOfTickers - svRankValues ) / ( NumberOfTickers - 1 );

// now use cutoff to manipulate the "Score"
Score = IIf( PercentileRankR < CutOffRank, svValues, 0 );

// Explore to illustrate calculations //
Filter = 1;
HiRankColor = IIf( PercentileRankR >= 80, colorRed, colorDefault );
AddColumn( RSI( 20 ), "RSI( 20 )", 1.2 ); // out of curiosity
//AddColumn( svValues, "svValues", 1.2 ); 
AddColumn( PercentileRankR, "PercentileRankR", 1.2, colorDefault, HiRankColor );
// in this example have chosen to use same value to create a 'score" variable
AddColumn( Score, "Score", 1.2, colorDefault, colorLightYellow );
AddColumn( NumberOfTickers, "NumberOfTickers", 1.0 );
SetSortColumns( -5, -4 );

When run on today's DJIA (30 stocks) the calculations are producing this type of result.

Hope that helps, good luck and let us know if you find something useful.


Hi @portfoliobuilder, thanks for the great link and code.. it sure look like something I'm looking for. .. I'll do some further study and share back with the site if I do have some interesting finding...

In case I haven't mention it.. thanks a lot for all the pointer and help so far.. it shorten my learning and search by quite a bit..:+1:

1 Like

@zeichai, This is definitely a "further study" forum. The more work you put into your efforts the better the help you'll receive around here. No lazy coders!