October 23, 2014
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
if ( Status( "action" ) == actionPortfolio )
bo = GetBacktesterObject();
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
bo.ProcessTradeSignals( bar );
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.