amibroker

Home ▸ Knowledge Base

How to exclude top ranked symbol(s) in rotational backtest

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 are closed.

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.

ExcludeTopN = 1; // how many top positions to exclude
SetCustomBacktestProc("");

if ( 
Status( "action" ) == actionPortfolio )
{
    
bo = GetBacktesterObject();
    
bo.PreProcess();

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

            
Cnt++;
        }

        
bo.ProcessTradeSignals( bar );
    }

    
bo.PostProcess();
}

EnableRotationalTrading( True );

SetOption( "MaxOpenPositions", 5 );
SetOption( "WorstRankHeld", 10 );
SetPositionSize( 20, spsPercentOfEquity );
PositionScore = 1 / RSI( 14 )

The code is pretty straightforward mid-level custom backtest 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.

Comments are closed.