Week Of Month
I could be wrong, but I couldn’t find this functionality in EasyLanguage. I was testing a strategy that didn’t want to trade a specific week of the month. If it’s not built-in, then it must be created. I coded this functionality in Sheldon Knight’s Seasonality indicator but wanted to create a simplified universal version. I may have run into one little snag. Before discussing the snag, here is a Series function that returns my theoretical week of the month.
This function is of type Series because it has to remember the prior output of the function call – weekCnt. This function is simple as it will not provide the week count accurately (at the very beginning of a test) until a new month is encountered in your data. It needs a ramp up (at least 30 days of daily or intraday data) to get to that new month. I could have used a while loop the first time the function was called and worked my way back in time to the beginning of the month and along the way calculate the week of the month. I decided against that, because if you are using tick data then that would be a bunch of loops and TradeStation can get caught in an infinite loop very easily.
This code is rather straightforward. If the today’s month is not equal to yesterday’s month, then weekCnt is set to one. Makes sense, right. The first day of the month should be the first week of the month. I then compare the day of week of today against the day of week of yesterday. If today’s day of week is less than the prior day’s day of week, then it must be a Sunday (futures markets open Sunday night to start the week) or Monday (Sunday = 0 and Monday = 1 and Friday = 5.) If this occurs and today’s month is the same as yesterday’s month, weekCnt is incremented. Why did I check for the month? What if the first trading day was a Sunday or Monday? Without the month comparison I would immediately increment weekCnt and that would be wrong. That is all there is to it? Or is it? Notice I put some debug code in the function code.
Is There a Snag?
April 2023 started the month on Saturday which is the last day of the week, but it falls in the first week of the month. The sun rises on Sunday and it falls in the second week of the month. If you think about my code, this is the first trading day of the month for futures:
month(April 2nd) = April or 4
month(March 31st) = March or 3
They do not equal so weekCnt is set to 1. The first trading day of the month is Sunday or DOW=0 and the prior trading day is Friday or DOW=5. WeekCnt is NOT incremented because month(D) doesn’t equal month(D[1]). Should WeekCnt be incremented? On a calendar basis it should, but on a trading calendar maybe not. If you are searching for the first occurrence of a certain day of week, then my code will work for you. On April 2nd, 2023, it is the second week of the month, but it is the first Sunday of April.
Thoughts??? Here are a couple of screenshots of interest.
This function is an example of where we let the data tell us what to do. I am sure there is calendar functions, in other languages, that can extract information from just the date. However, I like to extract from what is actually going to be traded.
Email me if you have any questions, concerns, criticism, or a better mouse trap.