Rotational Trading with Trend Filter

My goal here is to own the top 10 ranked stocks in the S&P 500 using the classic cast out method when the stock falls out of the top 50. In addition I want to block the addition of new positions when $spx is below it's 50-day moving average, but not sell existing positions if they are still in the top 50. Does the code below accomplish those goals? is there a better way?

As usual, thanks for your consideration!

SetBacktestMode( backtestRotational );
SetOption("WorstRankHeld", 50);

positions = 10;
SetOption("MaxOpenPositions", positions);
SetPositionSize( 100 / positions, spsPercentOfEquity );

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = Open;

momentum = ROC( Close, 50 );
Score = 1000 + momentum;

index = Foreign( "$spx", "Close" );
Bull = index > MA( index, 50 );

PositionScore = IIf( Bull, Score, scoreNoRotate );

Probably not quite what you're after. Here are a couple of excerpts from the documentation:

The rotational trading mode uses "buy price" and "buy delay" from the Settings | Trade page as trade price and delay for both entries and exits (long and short)

So your BuyPrice assignment isn't going to have any effect.

  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered

I don't often use Rotational Mode myself, but I'm pretty sure this means that when Bull is false, you will neither Buy nor Sell any positions even if they are below the worst rank held setting.

Oh, good catch Matt! Does anyone know if BOTH opens and closes are impacted by scoreNoRotate. Intuition suggests no new opens, but logic suggests no trades?

Document reference Enable Rotational Trading.

  • the score equal to scoreNoRotate constant means that already open trades should be kept and no new trades entered

The docs are opaque on this matter. What about exits? If a security falls below the minimum rank is it held or sold when scoreNoRotate is in effect? If it is held that I suggest a new feature, scoreNoAdd, which would prevent additions but allow liquidations.



Rotation means that TWO things happen at the same time:
a position on symbol that falls below "worstrankheld" is exited and
in the released place a position in top-ranked symbol that is not currently held is opened.

These two are bond together ("adding" may only occur when there is a released place).

scoreNoRotate does NOT prevent exits by stops. So, if you use scoreNoRotate, NO NEW positions will be open but existing positions may be exited (but not because they fell out of worst rank held). During scoreNoRotate period exits may be exited because of STOPS TRIGGERED.

1 Like