StaticVarGenerateRanks<\/a> function.<\/p>The formula presented below iterates though the list of symbols included in the test, then calculates the scores used for ranking and writes them into static variables. The static variables names are based on category number (sectors in this example) and that allows to create separate ranks for each sector.<\/p><\/span>\/\/ watchlist should contain all symbols included in the test
<\/span>wlnum <\/span>= <\/span>GetOption<\/span>( <\/span>"FilterIncludeWatchlist" <\/span>);
List = <\/span>CategoryGetSymbols<\/span>( <\/span>categoryWatchlist<\/span>, <\/span>wlnum <\/span>) ;
if( <\/span>Status<\/span>( <\/span>"stocknum" <\/span>) == <\/span>0 <\/span>)
{
<\/span>\/\/ cleanup variables created in previous runs (if any)
<\/span>StaticVarRemove<\/span>( <\/span>"rank*" <\/span>);
<\/span>StaticVarRemove<\/span>( <\/span>"values*" <\/span>);
<\/span>categoryList <\/span>= <\/span>","<\/span>;
for( <\/span>n <\/span>= <\/span>0<\/span>; ( <\/span>Symbol <\/span>= <\/span>StrExtract<\/span>( List, <\/span>n <\/span>) ) != <\/span>""<\/span>; <\/span>n<\/span>++ )
{
<\/span>SetForeign<\/span>( <\/span>symbol <\/span>);
<\/span>\/\/ use sectors for ranking
<\/span>category <\/span>= <\/span>sectorID<\/span>();
<\/span>\/\/ add sector to the list
<\/span>if( ! <\/span>StrFind<\/span>( <\/span>categoryList<\/span>, <\/span>"," <\/span>+ <\/span>category <\/span>+ <\/span>"," <\/span>) ) <\/span>categoryList <\/span>+= <\/span>NumToStr<\/span>( <\/span>category<\/span>, <\/span>1<\/span>, <\/span>0 <\/span>) + <\/span>","<\/span>;
<\/span>\/\/ write our ranking criteria to a variable
\/\/ in this example we will use 10-bar rate-of-change
<\/span>values <\/span>= <\/span>Roc<\/span>( <\/span>Close<\/span>, <\/span>10 <\/span>);
<\/span>RestorePriceArrays<\/span>();
<\/span>\/\/ write ranked values to a static variable
<\/span>StaticVarSet<\/span>( <\/span>"values" <\/span>+ <\/span>category <\/span>+ <\/span>"_" <\/span>+ <\/span>symbol<\/span>, <\/span>values <\/span>);
}
<\/span>\/\/ generate separate ranks for each category from the list
<\/span>for( <\/span>i <\/span>= <\/span>1<\/span>; ( <\/span>category <\/span>= <\/span>StrExtract<\/span>( <\/span>categoryList<\/span>, <\/span>i <\/span>) ) != <\/span>""<\/span>; <\/span>i<\/span>++ )
{
<\/span>StaticVarGenerateRanks<\/span>( <\/span>"rank"<\/span>, <\/span>"values" <\/span>+ <\/span>category <\/span>+ <\/span>"_"<\/span>, <\/span>0<\/span>, <\/span>1224 <\/span>);
}
}
<\/span>category <\/span>= <\/span>sectorID<\/span>();
<\/span>symbol <\/span>= <\/span>Name<\/span>();
<\/span>m <\/span>= <\/span>Month<\/span>();
<\/span>values <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"values" <\/span>+ <\/span>category <\/span>+ <\/span>"_" <\/span>+ <\/span>symbol <\/span>);
<\/span>rank <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"rank" <\/span>+ <\/span>"values" <\/span>+ <\/span>category <\/span>+ <\/span>"_" <\/span>+ <\/span>symbol <\/span>);
<\/span>\/\/ exploration code for verification
<\/span>AddColumn<\/span>( <\/span>values<\/span>, <\/span>"values" <\/span>);
<\/span>AddColumn<\/span>( <\/span>rank<\/span>, <\/span>"rank" <\/span>);
<\/span>AddTextColumn<\/span>( <\/span>SectorID<\/span>( <\/span>1 <\/span>), <\/span>"Sector" <\/span>);
<\/span>AddColumn<\/span>( <\/span>SectorID<\/span>(), <\/span>"Sector No"<\/span>, <\/span>1 <\/span>);
<\/span>Filter <\/span>= <\/span>rank <\/span><= <\/span>2<\/span>;
if( <\/span>Status<\/span>( <\/span>"Action" <\/span>) == <\/span>actionExplore <\/span>) <\/span>SetSortColumns<\/span>( <\/span>2<\/span>, <\/span>5<\/span>, <\/span>4 <\/span>);
<\/span>\/\/ sample backtesting rules
<\/span>SetBacktestMode<\/span>( <\/span>backtestRotational <\/span>);
<\/span>score <\/span>= <\/span>IIf<\/span>( <\/span>rank <\/span><= <\/span>2<\/span>, <\/span>values<\/span>, <\/span>0 <\/span>);
<\/span>\/\/ switch symbols at the beginning of the month only
<\/span>PositionScore <\/span>= <\/span>IIf<\/span>( <\/span>m <\/span>!= <\/span>Ref<\/span>( <\/span>m<\/span>, -<\/span>1 <\/span>