Thanks @didrip,
With my testing I only had two of your dates misaligning, did you have Pad & Align turned on?
I have made some changes to suit your logic.
Try this updated version to see if it is as required.
/*
Daily periodicity, Monthly rotation, Pad & Align checked, run 'Explore'.
Rotates on the last Friday of every month, if Friday is a non-trading day then previous trading day is the rotation day for that week.
When back testing
* if the current months (last month in database) rotation day has NOT occured, select 'Back Testing: (Last Month - Do NOT Rotate [month too young])'
* if the current months (last month in database) rotation day HAS occured, select 'Back Testing: (Last Month - Rotate [rotation day has occured])'
When live trading
* when the current bar (last bar in database) is a rotation day, select 'Do Live Rotation (current bar is rotation day)'
*/
// Params
p_backtesting = ParamList("Trading Mode:", "Do Live Rotation (current bar is rotation day)" + "|" +
"Back Testing: (Last Month - Do NOT Rotate [month too young])" + "|" +
"Back Testing: (Last Month - Rotate [rotation day has occured])", defaultVal = 0);
if (Status("ActionEx") == actionExplore)
{
bi = BarIndex();
dow = DayOfWeek();
dom = Day();
mth = Month();
lastBar_bi = BarCount - 1;
onLastBar = bi == lastBar_bi;
onCurrentMonth = Year() == LastValue(Year()) AND mth == LastValue(mth);
lastTradingDayOfWeek = Nz(TimeFrameExpand(True, inWeekly, expandPoint));
lastTradingDayOfMonth = Nz(TimeFrameExpand(True, inMonthly, expandPoint));
lastTradingDayOfWeek_bs = BarsSince(lastTradingDayOfWeek);
previousTradingDayOfWeek_bs = BarsSince(Ref(lastTradingDayOfWeek, -1));
monthlyDateDiff = DateTimeAdd(DateTime(), 1, inMonthly);
monthlyDateDiff = DateTimeDiff(monthlyDateDiff, DateTime()) / 60 / 60 / 24;
daysRemainingInMonth = monthlyDateDiff - dom;
backtesting = p_backtesting != "Do Live Rotation (current bar is rotation day)";
lastMonthRotate = p_backtesting == "Back Testing: (Last Month - Rotate [rotation day has occured])";
rotationBar_offset = 0;
rotationBar = False;
if (backtesting)
{
for (i = 7; i < BarCount; i++)
{
if (onCurrentMonth[i] AND NOT lastMonthRotate) break;
rotationBar_offset = i - lastTradingDayOfWeek_bs[i];
if (lastTradingDayOfMonth[i] AND mth[rotationBar_offset] == mth[i]) rotationBar[rotationBar_offset] = True;
rotationBar_offset = i - (previousTradingDayOfWeek_bs[i] + 1);
if (lastTradingDayOfMonth[i] AND lastTradingDayOfWeek[i] AND mth[rotationBar_offset] == mth[i] AND dow[i] != 5)
{
rotationBar[i] = False;
rotationBar[rotationBar_offset] = True;
}
}
}
else rotationBar = onLastBar;
Filter = IIf(backtesting, True, Status("lastBarInRange"));
AddColumn(backtesting, "backtesting", 1.0);
AddColumn(lastMonthRotate, "lastMonthRotate", 1.0);
AddColumn(daysRemainingInMonth, "Days Remaining in Month", 1.0);
AddColumn(dow, "Day of Week", 1.0, colorDefault, IIf(dow == 5, colorOrange, colorDefault));
AddColumn(lastTradingDayOfMonth, "Last Trading Day of Month", 1.0, colorDefault, IIf(lastTradingDayOfMonth, colorYellow, colorDefault));
AddColumn(lastTradingDayOfWeek, "Last Trading Day of Week", 1.0, colorDefault, IIf(lastTradingDayOfWeek, colorOrange, colorDefault));
AddColumn(lastTradingDayOfWeek_bs, "lastTradingDayOfWeek_bs", 1.0);
AddColumn(previousTradingDayOfWeek_bs, "previousTradingDayOfWeek_bs", 1.0);
AddColumn(rotationBar, "Rotation Bar", 1.0, colorDefault, IIf(rotationBar, colorGreen, colorDefault));
AddColumn(bi, "Bar Index", 1.0);
AddColumn(BarCount, "Bar Count", 1.0);
AddColumn(onCurrentMonth, "onCurrentMonth", 1.0);
AddColumn(onLastBar, "onLastBar", 1.0);
AddTextColumn(Interval(2), "Periodicity");
}