Rotational trading code

Is it possible to code the following using CBT and Buy/Sell commands? I'd like to have more flexibility than this provides.

EnableRotationalTrading(); 
SetOption("WorstRankHeld",5); 

PositionSize = -25; 
PositionScore = 50 - RSI(); 

@burger,

Yes it is possible but unless you consider using words like "hi", "please" or "thanks" then you probably won't get much further help.

1 Like

My apologies. Could someone please post a link to code that will simulate rotational trading. Otherwise please let me know the function I should be looking at. I don't expect someone to write the code if there is none already. Thank you.

@burger the example you posted is a very preliminary introduction to the rotational capabilities built into AmiBroker.

If you don't want to use built in functionality for rotational trading (and you have not given any details at all to guide the forum as to why that doesn't suffice for you), then you probably need to use the Custom Backtest Interface (CBT).

This should probably be used by advanced AmiBroker users only.

Some references for you to study,

https://www.amibroker.com/guide/a_custombacktest.html

http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

And there are many posts on this forum for you to search through, some in the Users Library and more in the Official Knowledgebase.

1 Like

Rotational trading does not use regular buy/sell/short/cover signals.
Check out here https://www.amibroker.com/guide/afl/enablerotationaltrading.html

Rotational trading is popular method for trading mutual funds. It is also known as fund-switching or scoring&ranking. Its basic permise is to rotate symbols all the time so only top N issues ranked according to some user-definable score are traded. The number of positions open depend on "Max. open positions" setting and available funds / position size. Once position is entered in remains in place until security's rank drops below WorstRankHeld (settable via SetOption("WorstRankHeld", 5 ) ). Regular buy/sell/short/cover signals are not used at all.

In knowledge base there are a few examples of rotational codes involving CBT.
Check out here https://www.amibroker.com/kb/index.php?s=rotational

Thanks much for the replies. I was hoping that I could achieve the same result as EnableRotationalTrading (hold the top 4 ranked symbols based on PostionScore), but with Buy/Sell statements instead so I could add additional filters like only buy if the symbol is also above the 200 day SMA for example.

I'm assuming the ranking part is probably not very easily/efficiently achieved with raw code. Perhaps I should not go down the path of Buy/Sell statements but rather modify the "exclude top ranked" code example and see if I can make that work for other filters instead.

Thanks in advance for any comments.

You do not need CBT for that. E.g.

SetBacktestMode( backtestRotational );

MaxPositions = 4;
SetOption("MaxOpenPositions", MaxPositions);
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); 

// trade on next day open
SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;

Filter = C > MA( C, 200 );

score = 10000 - ROC( C, 250 );

/// @link https://www.amibroker.com/guide/afl/enablerotationaltrading.html
/// the score equal to scoreNoRotate constant means that 
/// already open trades should be kept and no new trades entered
PositionScore = Iif( Filter, score, scoreNoRotate );

/// Or
/// the score of zero means no trade 
/// (exit the trade if there is already open position on given symbol)
//PositionScore = Iif( Filter, score, 0);

Have you read the 1st link been given to you by @codejunkie

The score (PositionScore) for all securities is calculated first. Then all scores are sorted according to absolute value of PositionScore . Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting. Backtester successively enters the trades starting from highest ranked security until the number of positions open reaches " max. open positions " or there are no more funds available. The score has the following meaning:

  • higher positive score means better candidate for entering long trade
  • lower negative score means better candidate for entering short trade
  • the score of zero means no trade (exit the trade if there is already open position on given symbol)
  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered
  • the score equal to scoreExitAll constant causes rotational mode backtester to exit all positions regardless of HoldMinBars. Note that this is global flag and it is enough to set it for just any single symbol to exit all currently open positions, no matter on which symbol you use scoreExitAll (it may be even on symbol that is not currently held). By setting PositionScore to scoreExitAll you exit all positions immediatelly regardless of HoldMinBars setting
5 Likes

Thank you fxshrat. Your example makes the reading more clear to me. I now see the flexibility inherent in the Rotational backtest is greater than I assumed.

PostionScore works ALWAYS, in ANY mode, not only in rotational mode.

PositionScore together with Buy/Sell signals is just REGULAR backtest mode.
Which means you get the RANKING via PositionScore and ability to act upon regular Buy/Sell signals.

Everything is described in the manual:
http://www.amibroker.com/guide/h_portfolio.html

1 Like