{"id":1324,"date":"2016-01-30T01:22:05","date_gmt":"2016-01-30T00:22:05","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1324"},"modified":"2016-01-29T16:34:58","modified_gmt":"2016-01-29T15:34:58","slug":"separate-ranks-for-categories-that-can-be-used-in-backtesting","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/01\/30\/separate-ranks-for-categories-that-can-be-used-in-backtesting\/","title":{"rendered":"Separate ranks for categories that can be used in backtesting"},"content":{"rendered":"

When we want to develop a trading system, which enters only N top-scored symbols from each of the sectors, industries or other sub-groups of symbols ranked separately, we should build appropriate ranks for each of such categories. This can be done with ranking functionalities provided by 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>), <\/span>score<\/span>, <\/span>scoreNoRotate <\/span>);
<\/span>SetPositionSize<\/span>( <\/span>1<\/span>, <\/span>spsPercentOfEquity <\/span>)<\/code>

Our test should be applied to a watchlist, which contains all symbols we want to include in our ranking code:<\/p>

\"Watch<\/p>

Running the exploration will show two top-ranked symbols for each of the sectors:<\/p>

\"Ranking\"<\/p>

We can also change Filter variable definition to<\/p>Filter <\/span>= <\/span>1<\/span><\/code>

and show all ranked symbols instead.<\/p>

Such ranking information can be used in backtest and sample rules included at the end of the code use rank information to allow only two top-scored symbols to be traded.<\/p>","protected":false},"excerpt":{"rendered":"

When we want to develop a trading system, which enters only N top-scored symbols from each of the sectors, industries or other sub-groups of symbols ranked separately, we should build appropriate ranks for each of such categories. This can be done with ranking functionalities provided by StaticVarGenerateRanks function.The formula presented below iterates though the list […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[39],"tags":[53,15,97],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1324"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1328,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1324\/revisions\/1328"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}