{"id":599,"date":"2014-10-23T04:16:14","date_gmt":"2014-10-23T09:16:14","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=599"},"modified":"2014-12-04T08:20:21","modified_gmt":"2014-12-04T13:20:21","slug":"how-to-exclude-top-ranked-symbols-in-rotational-backtest","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/10\/23\/how-to-exclude-top-ranked-symbols-in-rotational-backtest\/","title":{"rendered":"How to exclude top ranked symbol(s) in rotational backtest"},"content":{"rendered":"

Rotational trading<\/a> is based on scoring and ranking of multiple symbols based on user-defined criteria. For each symbol a user-definable “score” is assigned on bar by bar basis. Then, each bar, symbols are sorted according to that score and N top ranked symbols are bought, while existing positions that don’t appear in top N rank are closed.<\/p>

Sometimes however, we may want to exclude the highest ranking symbol (or a couple of them) from trading. The code below shows how to do that using custom backtester<\/a>.<\/p>ExcludeTopN <\/span>= <\/span>1<\/span>; <\/span>\/\/ how many top positions to exclude
<\/span>SetCustomBacktestProc<\/span>(<\/span>""<\/span>);

if ( <\/span>Status<\/span>( <\/span>"action" <\/span>) == <\/span>actionPortfolio <\/span>)
{
    <\/span>bo <\/span>= <\/span>GetBacktesterObject<\/span>();
    <\/span>bo<\/span>.<\/span>PreProcess<\/span>();

    for ( <\/span>bar <\/span>= <\/span>0<\/span>; <\/span>bar <\/span>< <\/span>BarCount<\/span>; <\/span>bar<\/span>++ )
    {
        <\/span>Cnt <\/span>= <\/span>0<\/span>;
        for ( <\/span>sig <\/span>= <\/span>bo<\/span>.<\/span>GetFirstSignal<\/span>( <\/span>bar <\/span>); <\/span>sig<\/span>; <\/span>sig <\/span>= <\/span>bo<\/span>.<\/span>GetNextSignal<\/span>( <\/span>bar <\/span>) )
        {
            if ( <\/span>Cnt <\/span>< <\/span>ExcludeTopN <\/span>)
                <\/span>sig<\/span>.<\/span>Price <\/span>= -<\/span>1<\/span>; <\/span>\/\/ exclude

            <\/span>Cnt<\/span>++;
        }

        <\/span>bo<\/span>.<\/span>ProcessTradeSignals<\/span>( <\/span>bar <\/span>);
    }

    <\/span>bo<\/span>.<\/span>PostProcess<\/span>();
}

<\/span>EnableRotationalTrading<\/span>( <\/span>True <\/span>);

<\/span>SetOption<\/span>( <\/span>"MaxOpenPositions"<\/span>, <\/span>5 <\/span>);
<\/span>SetOption<\/span>( <\/span>"WorstRankHeld"<\/span>, <\/span>10 <\/span>);
<\/span>SetPositionSize<\/span>( <\/span>20<\/span>, <\/span>spsPercentOfEquity <\/span>);
<\/span>PositionScore <\/span>= <\/span>1 <\/span>\/ <\/span>RSI<\/span>( <\/span>14 <\/span>)<\/code>

The code is pretty straightforward mid-level custom backtest<\/a> loop but it uses one trick – setting signal price to -1 tells AmiBroker to exclude given signal from further processing. Note also that signals retrieved by GetFirstSignal \/ GetNextSignal are already sorted, so the highest ranked signal appears first in the list.<\/p>","protected":false},"excerpt":{"rendered":"

Rotational trading is based on scoring and ranking of multiple symbols based on user-defined criteria. For each symbol a user-definable “score” is assigned on bar by bar basis. Then, each bar, symbols are sorted according to that score and N top ranked symbols are bought, while existing positions that don’t appear in top N rank […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[53,14,15,30],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/599"}],"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=599"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/599\/revisions\/608"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}