Using The Hash Table
Now that we have created an empty Hash Table and the Hash Index it is now time to start filling the table up with the appropriate information. As I pointed out in my last post, every day of any given year can be represented by a nine character string. If January 1st lands on a Tuesday, you can express this day with the following string, “1stTueJan.” That is if you want to ignore the year and in this case, we do.
Mapping Into the Hash Table
The table has already been prepared as well as the index. All we have to do is map the current day into the index. The location of the index value in the Hash Index array will then be used to locate the day’s location in the Hash Table. We will use a function to convert the current day of the year into a value our Hash Index can interpret.
Here is the code to the function. Don’t fret too much at the number of lines of code!
Here is where using an integer representation of the date would reduce the number of lines of code tremendously. Well, I made my bed I might as well sleep in it. You will see some duplication between this code and the Hash Table creator function. I have to store names for the week rank, day of the week, and month in arrays. There isn’t a simple function that will pull the week rank from any given date. So I simply take the date and work my way back to the beginning of the month counting each weekday as I go along.
Getting The Hash Index
The number that is stored in the individual counters (monCnt, tueCnt, etc.) determines which week of the month the current day is located. I build the string through concatenation. First I get the week rank (“1st”, “2nd”, “3rd”, “4th”, “5th”), add the name of the day and then add the month. The end result looks like “1stMonJan”. From here I cross-reference the Hash Index and pull out the location of the of the string (aka index.) Here is the function GetHashIndex.
As you can see it is a linear search that returns the Hash Index’s Index. Check out how I prematurely exit the loop by using the keyword Break. This keyword knocks you out of any loop where it is located. If you have a nested loop, the break only gets you out of that current loop where it is located.
Hast Table Indicator
Now how can we pull all this together to create a useful trading tool. I used these tools to create an indicator that plots the average daily change from one day to the next. So, if today is the “3rdMonJune” and the indicator reads 0.52, this represents that over the last X years the average percentage change is a plus .5%. Would it make sense to buy the “2ndFriJun” and exit on the close of the “3rdMonJune?” Maybe.
Here is the code for the Hash Table indicator.
Results of Using the Hash Table
Here is a simple output of the results from the indicator for the year of 2016. I sorted the data based on highest average daily change and number of years collected.
It looks like the buying the close “4thThuJul” is the way to go! But since there are only seven observations I think would think twice. But, buying the close on the day prior to “2ndFriJan” might offer that technical advantage you’re looking for.