Function : StaticVarGenerateRanks Bottom rank mode

How can I use function StaticVarGenerateRank in bottom mode.
from this link : https://www.amibroker.com/guide/afl/staticvargenerateranks.html

when I’m code
StaticVarGenerateRanks( “bot”, “ValuesToSort”, -3, 1224 ); // bottom-N mode

then plot rank into chart It’s return empty.

Thank you very much

You need to post entire formula that you are using

symlist = "2s,ptt,scb,scc,tpipl,ktb,kbank"; 

// delete static variables 
StaticVarRemove( "ItemScore*" ); 

// fill input static arrays 

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

// perform ranking 
StaticVarGenerateRanks( "bot", "ItemScore", -3, 1224 ); // bottom-N mode 

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

sdt = SelectedValue( DateTime() ); 

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


Thank you very much and apologize that I haven’t post clearly. So this is my code.

As documented StaticVarGenerateRanks has THREE modes

  • normal rank mode (topranks = 0)
  • top-N (topranks > 0 )
  • bottom-N (topranks < 0).

It generates those variables that you use for Plot() only with topranks = 0 mode.

But this is NOT obstacle, because to get ranks from bottom, you just subtract the rank from quantity.

/////////////////////////////////
// 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

qty = 0;

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

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

// read ranking (subtract from quantitiy gives you order from BOTTOM)
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    Plot( qty - 1 - StaticVarGet( "RankValuesToSort" + sym ), sym, colorCustom10 + i );
}
7 Likes

Thank you very much for your supporting ^^