D of W Analysis
How important is a day of week analysis? Many pundits would of course state that it is very important, especially when dealing with a day trading algorithm. Others would disagree. With the increase in market efficiency maybe this study is not as important as it once was, but it is another peformance metric that can be used with others.
I am currently working on the second book in the Easing into EasyLanguage trilogy (Hi-Res Edition) and I am including this in one of the tutorials on developing a day trading template. The book, like this post, will focus on intraday data such as 5 or less minute bars. I hope to have the book finalized in late November. If you haven’t purchased the Foundation Edition and like this presentation, I would suggest picking a copy up – especially if you are new to EasyLanguage. The code for this analysis is quite simple, but it is pretty cool and can be re-used.
Day Trading Algorithms Make Things Much More Simple
When you enter and exit on the same day and you don’t need to wrap around a 00:00 (midnight) time stamp, things such as this simple snippet of code are very easy to create. The EasyLanguage built-in functions work as you would expect as well. And obtaining the first bar of the day is ultra simple. The idea here is to have five variables, one for each day of the week, and accumulate the profit that is made on each day, and at the end of the run print out the results. Three things must be known on the first bar of the new trading day to accomplish this task:
- were trades taken yesterday?
- how much profit was made or lost?
- what was yesterday – M, T, W, R, or F?
Two Reserved Words and One Function Are Used: Total Trades, NetProfit and the DayOfWeek function.
The reserved word TotalTrades keeps track of when a trade is closed out. The second reserved word, NetProfit keeps track of total profit everytime a trade is closed out. Along with the DayOfWeek(D) function you can capture all the information you need for this analysis. Here is the code. I will show it first and then explain it afterwards.
Code Explanation – Switch and Case
I have used the Switch – Case construct in some of my prior posts and I can’t emphasize enough how awesome it is, and how you can cut down on the use of if – thens. This snippet only takes place on the first bar of the trading day. Since we are using day sessions we can simply compare today’s date to the prior bar’s date, and if they are different then you know you are sitting on the first intraday bar of the day. After some initial housekeeping, the first if – then checks to see if trade(s) were closed out yesterday. If totalTrades is greater than my user defined totTrades, then something happened yesterday. My totTrades is updated to totalTrades after I am done with my calculations. The switch keys off of the DayOfWeek function. Remember you should account for every possible outcome of the variable inside the switch expression. In the case of the DayOfWeek function when know:
Notice I am passing Date into the function, because I want to know the day of the week of yesterday. After the Switch and its associated expression you have a Begin statement. Each outcome of the expression is preceded withthe keyword Case followed by a colon (:). Any code associated with each distinct result of the expression is sandwiched between Case keywords. So if the day of week of yesterday is 1 or Monday then MProf accumulates the change in the current NetProfit and the begDayEquity (beginning of the yesterday’s NetProfit) variable. So, if the equity at the beginning of yesterday was $10,000 and there was a closed out trade and the current NetProfit is $10,500 then $500 was made by the end of the day yesterday. This exact calculation is used for each day of the week and stored in the appropriate day of the week variable:
- MProf – Monday
- TProf – Tuesday
- WProf – Wednesday
- RProf – Thursday
- FProf – Friday
You might ask why RProf for Thursday? Well, we have already used TProf for Tuesday and Thursday contains an “R”. This is just my way of doing it, but you will find this often in code dealing with days of the week. Every Switch should account for every possible outcome of the expression its keying off of. Many times you can’t always know ahead of time all the possible outcomes, so a Default case should be used as an exception. It is not necessary and it will not kick an error message if its not there. However, its just good programming to account for everything. Once the Switch is concluded begDayEquity and totTrades are updated for use the following day.
Here is the code that prints out the results of the DayOfWeek Analysis
The printout occurs on October 27, 2021 at 11 AM. Here is my analysis of a day trading algorithm I am working on, tested over the last two years on 5 minute bars of the @ES.D
Does This Agree with Strategy Performance Report?
Looks like it does. These results were derived from one of the Tutorials in The Hi-Res edition of EZ-NG-N2-EZ-LANG trilogy. I should have it availabe at Amazon some time in late November. Of course if you have any questions just email me @ email@example.com.