Exploration Ranks

In an exploration I would like to combine the rank (not actual values) of two or more ROCs for different time periods and display the results as an addition column.

Can get close by creating individual columns for ROC values, setting sort columns, and adding rank columns. But have not figured out how to combine the rank column values into one value such as RankColFirst + RankColSecond for a final score column to sort by.

Anyone know how to do thils?

@Trader500 it may have helped to see what calculations you have attempted so far. Perhaps using StaticVarGenerateRanks can help you.

http://www.amibroker.com/guide/afl/staticvargenerateranks.html

Maybe something like this is what you are looking for (I haven't tried this before but it appears to be working)?


// choose WL in Analysis window
wlnum	= GetOption( "FilterIncludeWatchlist" );
List	= CategoryGetSymbols( categoryWatchlist, wlnum ) ;

if( Status( "stocknum" ) == 0 )
{
    StaticVarRemove( "ShortTermValues*" );
    StaticVarRemove( "LongTermValues*" );

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );
        
        ShortTermValues = ROC( C, 20 );
        LongTermValues 	= ROC( C, 200 );

        RestorePriceArrays();
        
        StaticVarSet( "ShortTermValues" + symbol, ShortTermValues );
        StaticVarSet( "LongTermValues" + symbol, LongTermValues );
    }

    StaticVarGenerateRanks( "rank", "ShortTermValues", 0, 1224 );
    StaticVarGenerateRanks( "rank", "LongTermValues", 0, 1224 );
}

symbol = Name();

svShortTermValues 	= StaticVarGet( "ShortTermValues" + symbol );
svLongTermValues 	= StaticVarGet( "LongTermValues" + symbol );

ShortTermRank 		= StaticVarGet( "rankShortTermValues" + symbol );
LongTermRank 		= StaticVarGet( "rankLongTermValues" + symbol );

// from here you can choose whatever manipulation of the two rankings you desire
// example below is to average the two rankings
AverageRank = ( ShortTermRank + LongTermRank ) / 2;

// Exploration of calculations //
Filter = 1;
AddtextColumn(FullName(), "Name");
AddColumn( svShortTermValues, "Short Term Values" );
AddColumn( svLongTermValues, "Long Term Values" );
AddColumn( ShortTermRank, "Short Term Rank", 1.0 );
AddColumn( LongTermRank, "Long Term Rank", 1.0 );
AddColumn( AverageRank, "Average Rank", 1.1 );
//AddColumn( ROC( C, 20 ), "ROC( C, 20)", 1.2 );
//AddColumn( ROC( C, 200 ), "ROC( C, 200)", 1.2 );
SetSortColumns( 8 );

Produces this type of calculation
image

If this isn't what you were looking for just search the forum as there are many ranking examples. For example perhaps you are looking for a "2 pass" ranking

3 Likes

Thanks for you help.

Can probably accomplish what I want using your example with StaticVarGenerateRanks multiple times and then adding them together but was hoping there might be a simpler way using the ranking capabilities built into Explorations.

Should be able to just change @Portfoliobuilder code if you want to Sum the ranks.

Change

AverageRank = ( ShortTermRank + LongTermRank ) / 2;

To

SumofRanks = ShortTermRank + LongTermRank;

and change

AddColumn( AverageRank, "Average Rank", 1.1 );

to

AddColumn(SumofRanks, " Sum of Ranks ",1.1);

It will sort by the addition of the ranks as you see in his SetSortColumns( 8 );

Can also add a

AddRankColumn() After the SetSortColumns().

1 Like

I was trying to figure out a way of generating the combined ranks in an exploration without having to create a lot of static variables. Ultimately would like combine ROC ranks for multiple periods like monthly from 1 to 9 months and add inverse ranking of volatility for 1 or more periods.

Using what is available in exploration functions I can easily get the rankings for multiple time periods with something like this

AddRankColumn(); // without prior sorting AddRankColumn just adds line number 
SetSortColumns( -4 ); 
AddRankColumn(); // rank according to 4th column (descending) 
SetSortColumns( -3 ); 
AddRankColumn(); // rank according to 3rd column (ascending)

So most of what I need is there in the exploration, just can't figure out how to add the Rank Column values together to produce a summary column of the combined rank columns.

The AddSummaryRows() function provides a column sum or a total for the column, but what I want to do would be something like an AddColumns() cell on each row that would add the values from other columns on the same row.