Hello, I'm finding some difficulties when coding a rotational portfolio. I've searched into the forum for some insight but I didn't find any useful solution (other topics like this: here and others). I hope you could help me.
The system is pretty simple: rotate 2 assets (that are part of a Norgate's watchlist). Position score based on the ROC values an a filter consisting on a SMA. Every rotation have to be done only on a particular day of week, but the system should cash out any asset also at any other day if the close price goes below the SMA.
So, to fix the idea, the rules have to be:
- buy next day on open if today is the right day for rotations AND if close is above SMA AND if ROC is the greatest or the second greatest.
- sell at the opening of the next day if close goes below SMA (only that particular stock, NOT all the stocks)
- doesn't rotate if close price is higher than SMA
My issue is that the exits don't respond correctly. The exits occur only at the same day of week setted for the entry. It ignores the condition C < SMA that could occur during the week
this is my code attempt:
// Rt_custEntryDay_condCashOut.afl
// PARAMETERS
Positions = 2;
RotationDay = RtD = 1; //0=Sun... 6=Sat
// BACKTESTER SETUP
SetBacktestMode(backtestRotational);
SetOption("MaxOpenPositions",Positions);
SetOption("WorstRankHeld",Positions);
SetOption("InitialEquity", 100000);
SetPositionSize(100/Positions, spsPercentOfEquity);
// INDICATORS AND CONDITIONS
SelInd = selectionIndicator = ROC(C, 5) + 1000;
trendFilter = TF = MA(C, 5);
DOW = DayOfWeek();
// ROTATION SELECTIONS
PositionScore = IIf(DOW == RtD , IIf( C > TF, SelInd, 0), IIf(C > TF, scoreNoRotate, 0));
To simulate the next day at open operations I've set the following options:
This is an example of the error: