StaticVarGenerateRanks
- generate ranking of multiple symbols and store it to static variables

Miscellaneous functions
(AFL 3.70)


SYNTAX StaticVarGenarateRanks( "outputprefix", "inputprefix", topranks, tiemode )
RETURNS NOTHING
FUNCTION The function implements general-purpose multiple symbol bar-by-bar ranking.

StaticVarGenarateRanks( "outputprefix", "inputprefix", topranks, tiemode )

"inputprefix" is a prefix that defines names of static variables that will be used as input for ranking. AmiBroker will search for all static variables that begin with that prefix and assume that remaining part of the variable name is a stock symbol. Say you want to rank stocks by ROC (rate of change). All you need to do is to store values into static variables.

Let us say that we will use static variable names like "ValuesToSortAPPL", "ValuesToSortMSFT", and so on.

To fill input static variables you can use this loop:

for( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign(sym );
Value =
ROC( C, 10 );
RestorePriceArrays();
StaticVarSet( "ValuesToSort" + sym, Value );
}

Now you are ready to perform sorting/ranking. There are two modes, normal ranking mode and Top/Bottom Rank mode.

Normal ranking mode is performed when toprank argument is set to zero.

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

In this case StaticVarGenerateRanks call would generate set of static variables starting with prefix defined by 2nd argument each variable holding the rank of particular symbol, so in this case

RankValuesToSortMSFT will hold ranking of MSFT
RankValuesToSortAAPL will hold ranking of AAPL

Note that in AmiBroker rank count start from ONE.

Third argument (topranks) is zero in normal ranking mode

Fourth argument (tiemode) defines how ties are ranked. Supported modes are 1234 and 1224. In 1224 mode ties are numbered with equal rank.

Top/bottom ranking mode (that generates top/bottom ranking tables that hold indexes to top ranking values. When topranks > 0 top ranked values are used, when topranks < 0 then bottom ranked values are used. The values are stored in variables that have format of: OutputprefixInputprefixN where N is a number 1, 2, 3 representing top/bottom ranks. Let us assume that OutputPrefix parameter is "Top" and Inputprefix parameter is ROC. In such case variable TopROC1 would hold the index of top rated value. TopROC2 would hold second top rated value, and so on.

StaticVarGenerateRanks function uses rank numbering that starts from ONE.

In top ranking mode StaticVarGenerateRanks will also prepare static variable that contains comma separated list of variable names that can be used to find out which index refers to which symbol. So if TopROC1 holds 1 you would lookup first substring in TopROCSymbols variable to find out what variable (symbol) ranked at the top. Additionally StaticVarGetRankedSymbols gives easy-to-use method to retrieve comma separated list of ranked symbols for particular datetime.

EXAMPLE /////////////////////////////////
// Example 1. code for normal ranking mode
// (everything done is done in one pass, can be used in indicator):
/////////////////////////////////

symlist =
"C,CAT,DD,GE,IBM,INTC,MSFT";

// delete static variables - DO NOT forget the asterisk (wildcard) at the end
StaticVarRemove( "ValuesToSort*" );

// fill input static arrays

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    
SetForeign( sym );
     Value =
ROC( C, 10 );
    
RestorePriceArrays();
    
StaticVarSet( "ValuesToSort" + sym, Value );
}

// perform ranking
StaticVarGenerateRanks( "rank", "ValuesToSort", 0, 1224 ); // normal rank mode

// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    
Plot( StaticVarGet( "RankValuesToSort" + sym ), sym, colorCustom10 + i );
}

/////////////////////////////////
// Example 2. Code for top ranking mode
// (everything done is done in one pass, can be used in indicator):
/////////////////////////////////


symlist =
"C,CAT,DD,GE,IBM,INTC,MSFT";

// delete static variables - DO NOT forget the asterisk (wildcard) at the end
StaticVarRemove( "ValuesToSort*" );

// fill input static arrays

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    
SetForeign( sym );
     Value =
ROC( C, 10 );
    
RestorePriceArrays();
    
StaticVarSet( "ValuesToSort" + sym, Value );
}

// perform ranking
StaticVarGenerateRanks( "rank", "ValuesToSort", 0, 1224 ); // normal rank mode

StaticVarGenerateRanks( "top", "ValuesToSort", 3, 1224 ); // top-N mode

StaticVarGenerateRanks( "bot", "ValuesToSort", -3, 1224 ); // bottom-N mode

// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    
Plot( StaticVarGet( "RankValuesToSort" + sym ), sym, colorCustom10 + i );
}

sdt =
SelectedValue( DateTime() );

Title = "{{NAME}} -{{DATE}} - {{VALUES}} TOP: " + StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt ) +
        
" BOT: " + StaticVarGetRankedSymbols( "bot", "ValuesToSort", sdt ) ;

SEE ALSO StaticVarGetRankedSymbols() function

References:

The StaticVarGenerateRanks function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference