DayofWeek issues in a Rotation strategy

Good morning all. I'm new to the forum and I must say this community looks like a wonderful place to solve those nagging coding issues.

I'm testing a rotation strategy and want to include rotation frequency as part of the analysis. Works fine when end of month is activated, but end of week (i.e Friday or DayofWeek == 5) gives no results at all. It seems to be in line with sample code from AB help, so I'm at a loss.

Any clues?


@Meme FIRST: Please use the forum properly and read this first! If you don't enter your code properly we can not help you.

It is of utmost importance as improperly formatted code results in syntax errors. Start code block with three back-ticks and end the code block with three back-ticks too,

copy and past your code in here

Back tick key is located at the upper left corner of any keyboard (just below ESC and left to 1key). Don't paste screen captures of code, it can't be copied into our AmiBroker editor for testing.

Second: welcome to the forum

Third: I tried your code and it works for me so i don't know what you are seeing.

Fourth: what do you intend to do on Friday's that are statutory holidays and the markets are closed? Skip rotation that week?

Thanks for your prompt reply and your comments. I had searched the forum and found plenty of valuable information regarding rotation, but not anything which helped with the issue I’m having. Also, originally entered the code using the backticks but it didn’t look right so used the screen capture instead. Tried to not look like a complete newbie but blew it on my first attempt!!

Glad it worked for you, but when I run the code as below, I still get no results for the weekly test.

SetBacktestMode( backtestRotational );
BuyPrice = Open;
SetTradeDelays( 1, 1, 1, 1);
Totalpositions = 10;
SetOption("WorstRankHeld", 12);
SetOption("MaxOpenPositions", Totalpositions );
PositionSize = -100 / Totalpositions ;
//Tradeday = Month() != Ref(Month(), 1);					//	Rotate only at monthly close	
Tradeday = DayofWeek() == 5;								//	Rotate only at Friday's close	
Score = ROC(EMA(Close,200), 10);
Score = IIf(Score < 0, 0, Score ); 							// Long only

PositionScore = IIf(Tradeday, Score, scoreNoRotate);

Regarding non-trading Fridays, I’m really looking at the general efficacy of the strategy here and given that there are so few of them I’m working on the basis that the results, especially over a long testing period, are unlikely to be meaningfully different.

1 Like

Weekly? Change your periodicity to DAILY in the Settings. The only reason why you don’t get any trades with DayOfWeek() == 5 is that your bar timestamps do NOT include Friday. This can be because of data holes or because you are using interval longer than daily, so this condition is never met.

You really need to do read and implement this: How do I debug my formula? . Once you implement this you will quickly know why your formulas don’t work.

Hi Tomasz, thanks for your reply.

Periodicity is set to DAILY so doesn’t seem to be that and data (Norgate) is clean and I can see the data on the chart. Will implement the debug and see what I can find. Will post here if I can work it out.

Assumption that the “data is clean” and can be ignored is far from reality. Assumptions are not facts.

You need to understand what you wrote and you need to understand your data. With this:

Tradeday = DayofWeek() == 5;	
PositionScore = IIf(Tradeday, Score, scoreNoRotate);

You are telling the program to trade only if on ALL SYMBOLS under test you have FRIDAY data present.

In other words if ANY of your symbols has data hole on Friday, it won’t trade.
scoreNoRotate has top precedence. If if occurs on ANY symbol it will stop trading.

Typically you WILL have data holes.
First check your “Pad and align” setting and CHECK YOUR DATA using the Exploration (see my previous post). Typically you will need to turn on padding to some reference symbol (like index) to ensure that your data are hole-free.

As I explained Exploration is extremely powerful tool that you should use to understand your formula and your data. For example to count how many fridays you have in your data you can use this:

Filter = Status("lastbarinrange");
FridayQty = Cum( DayOfWeek() == 5 And Status("barinrange") );
AddColumn( FridayQty, "FridayQty" );

Ideally you should see same number in all symbols.

If you run this with “pad and align” you will always get same number. But if you run it without “pad and align” then you will see if there are any missing Fridays.


Thanks Tomasz,

I hadn’t realised the issue around the data holes and that it applied to ALL symbols instead of EACH symbol.

Pad and align was indeed unchecked. When checked and aligned to the local index the test ran just fine.

Can’t thank you enough. Don’t know how long that would have taken without your help.

1 Like