index filter for rotation system

In a rotational system i want to implement a market regime filter, that when the market filter turns off, the positions exit only when they drop below the worst rank.

I have used backtest rotational mode and Position score and worst rank held.

With scoreNoRotate, when the market filter turns off, it will continue to hold the same existing positions until the market filter turns back on.

With scoreExitAll, it will exit all existing positions when the market filter turns off.

What I would like it to do, is when the market filter turns off, to only exit each of the existing positions when they fall below the WorstRankHeld set at 20. It shouldn’t enter any new positions until the market filter turns back on.

Please help in coding this!

exit each of the existing positions when they fall below the WorstRankHeld set at 20

Think about it for a while, what you are asking is to have rotation without having rotation. You can't have a cake and eat it too. For given symbol to "fall below WorstRankHeld" there must be NEW symbols coming with scores higher than given symbol and they must "push" given symbol out of ranking. This means rotation. Only with rotation ranks change. So you can only have rotation turned on or off. There is no third option.

What you CAN do however, is to use STOP (ApplyStop) and scoreNoRotate. This will EXIT existing position should any of stop conditions occur. With dynamic stops you can have similar effect to "dropping score".

Alternatively you can keep rotation on-going, but simply set position size to ZERO

PositionSize = IIF( MarketFilter, 0, OriginalPositionSize );

Having zero position size should prevent from opening new positions at all even if they are ranked higher.

2 Likes

Could you please refer some sample AFL with rotation, index filter and Stops as suggested by you implemented.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.