Use of More Than One Category in Filter Settings for Backtest with Ranking

I have a backtest scan that is directed at testing a formula with various variables, but only on the top 400 stocks each day according to a ranking formula. For my filter setting, I have a watchlist category (for all listed companies) and a market category (for all delisted companies). This way the backtest captures the 400 top ranked stocks on each day without survivorship bias. In other words, if I only tested the watchlist, there would be stocks in there that weren’t in the top 400 in the past at the time because the scan wouldn't include the delisted stocks.

The issue that I am having when I run the scan is that the scan doesn’t pick up the market category of delisted companies. It only picks up the watchlist category of stocks. I am wondering if the issue may be with the code. In other words, even though the filter has both the watchlist and market categories of stocks, it could be that the code is limiting the scan to those on the watchlist and not also including the stocks from the market category of delisted stocks. Or it could be that the ranking can only be done on a watchlist or one category in the filter. There is a comment in the code by whoever wrote it that suggests that the code might be able to be broadened to include more than a watchlist category that would be picked up by the scan through the filter. If anyone knows how to fix the code if it is fixable, that would be great. Here is the code:

//=================================================================================
//Ranking
//=================================================================================
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 ) 
{ 
    StaticVarRemove( "values*" ); 

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        values =MA(C*V,Param("Turnover Period",90,2,1000,1));//Ranking criteria
        RestorePriceArrays(); 
        StaticVarSet (  "values"  +  symbol, values ); 
        _TRACE( symbol ); 
    } 

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

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

Moderator comment: you must use CODE TAGS!

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

Thank you, here is the code:


//=================================================================================
//Ranking
//=================================================================================
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 ) 
{ 
    StaticVarRemove( "values*" ); 

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        values =MA(C*V,Param("Turnover Period",90,2,1000,1));//Ranking criteria
        RestorePriceArrays(); 
        StaticVarSet (  "values"  +  symbol, values ); 
        _TRACE( symbol ); 
    } 

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

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

type or paste code here

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.