Filter symbols for StaticVarGenerateRanks

Hi,
I want to use ranking with limit orders, and from the forum I understood that I need to use StaticVarGenarateRanks. However I couldn't find out how to filter my symbol list by my desired entry signal before sending them to StaticVarGenarateRanks for ranking.

Pointing me in the right direction or providing a simple example will be very much appreciated.

@orshk ranking may be done in several ways depending on what you are trying to accomplish. PositionScore is a variable you can define and when investing in a portfolio you can use it to rank which securities take priority.

StaticVarGenerateRanks is a special function that generates new set of static variables that hold calculated ranks.

Read the User Guide info here, and look through this forum for many examples.

http://www.amibroker.com/guide/h_ranking.html

Thank you for the reply, I see that I should've been much clearer.
Say for example that I want my signal to filter by to be RSI(4) < 25, in that case I've written the ranking as:

if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL 
{ 
    StaticVarRemove( "values*" ); 

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        Sig = RSI(4) < 25;
        values = IIf(Sig, ROC(C,10), -10000);
        RestorePriceArrays(); 
        StaticVarSet (  "values"  +  symbol, values ); 
    } 

    StaticVarGenerateRanks( "rank", "values", 0, 1224 ); 
} 

I'm wondering if there is a better (and less resource intensive) way to filter the Symbols by my desired signal other then using IIF inside the loop.

Sorry for being vague.

@orshk I guess I still don't understand you.

Are you trying to test a trading strategy on a watch list of symbols? And your signal is a low RSI(4), and knowing you may get too many signals you want to rank which signals will be executed by AmiBroker, so you want to take the signals on the symbols with the highest ROC(C, 10)? Once again I suggest PositionScore.

PositionScore determines the rank. If you have more than one trade signal, Amibroker will take the trade that is scored the highest. This is quite important, particularly if your system generates lots of signals on the same day/ bar.

http://www.amibroker.com/guide/h_portfolio.html

@portfoliobuilder thanks for sticking with we :slight_smile:
As I wrote in my original massage, I want to use limit orders, so I can't use PositionScore as it will rank only the signals which the limit occurred.
I managed to write a code which works like I want, but I'm wondering if there's a better way of filtering the symbols by the signal I want, as the way I wrote it seems inefficient and resources intensive.

See the full example code:


SetOption("InitialEquity", 10000);
SetOption("MaxOpenPositions", 10);
SetPositionSize( 10, spsShares );


if ( GetOption( "ApplyTo" ) == 2 ) 
{ 
     wlnum = GetOption( "FilterIncludeWatchlist" ); 
     List = CategoryGetSymbols( categoryWatchlist, wlnum ) ; 
} 
else 
if ( GetOption( "ApplyTo" ) == 0 ) 
{ 
     List = CategoryGetSymbols( categoryAll, 0 ); 
} 
else 
{ 
     Error( "The formula works fine if your ApplyTo setting is 'Filter' or 'All' " ); 
} 

if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL 
{ 
    StaticVarRemove( "values*" ); 

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        Sig = RSI(4) < 25;
        values = IIf(Sig, ROC(C,10), -10000);
        RestorePriceArrays(); 
        StaticVarSet (  "values"  +  symbol, values ); 
    } 

    StaticVarGenerateRanks( "rank", "values", 0, 1224 ); 
} 

symbol = Name(); 

values = StaticVarGet ( "values" +  symbol ); 
rank = StaticVarGet ( "rankvalues" +  symbol ); 

PositionScore = values;

Limit_Price = Ref(C, -1) * 0.98;
Buy = L < Limit_Price AND rank <= 10;
BuyPrice = Min(Open, Limit_Price);
Sell = 1; 
SellPrice = C;

Filter = 1;
AddColumn( Buy, "Buy" );
AddColumn( PositionScore, "PositionScore" );
AddColumn ( values, "values");           
AddColumn ( rank, "rank"); 
AddColumn( ROC(C,10), "ROC" );
AddColumn( RSI(4) < 25, "Signal" );
2 Likes

If you don't want to write a Custom Backtest (CBT), then your approach is probably the best way to solve the problem. However, you should be using the previous bar's rank in your Buy assignment, otherwise you're looking ahead to the close of the bar for ranking but entering intrabar on a limit.

2 Likes

@mradtke thanks for pointing out my mistake.
I'm still new to Amibroker, so I want to gain more experience with it before diving into CBT.

1 Like