Category Archives: EasyLanguage equivalent to cool code

Hash Table in EasyLanguage [Part 1]

 

This concept may be considered advanced and only used by pure programmers, but that is not the case at all.  A Hash Table is simply a table that is indexed by a function.  The function acts like the post office – it sends the data to the correct slot in the table.  I utilized this data structure because  I wanted to know the closing prices for the past fifteen years for the “1stThuJan” (first Thursday of January.)  This, of course, would require some programming and I could simply store the values in an array.  However, what if I wanted to know the closing prices for the “3rdFriMar?”  I would have to spend more time and re-code, right?   What if I changed my mind again.  Instead, as we programmers often do, I wanted to be able to pull the data for any instance of “Week, Day Of Week, Month.”  This is where a table structure comes in handy.  With this table, I can query it and find out the average yearly closing prices for the “1stMonSep” or the “4thFriJuly” or the “3rdWedApr ” on a rolling year by year basis.  Why would you want this you might ask?  Would it be helpful to know the price  change from the “2ndMonMar” to the subsequent “2ndMonMar” on a rolling basis?  What if the average price change is 10%.  You could use this information to make sure you always buy on this particular day.  That is if you believe in this form of analysis.

Here’s how I created a table that stores the closing prices for the past 15 years for each entry in the table.  Remember each row value only comes up once a year.  You only have one “1stMonJan” in a calendar year.  So the first part of the problem was simple, create a table that can store the closing prices with all the different combinations like the “1stTueJan” for the past fifteen years.  The second part of creating the post office like function that places the correct closing price in the right row was a little more difficult, but not much.  Here’s how I did it.

As I said earlier, a Hash Table is a very simple concept and very useful as well!  For some of you out there, I just want to make sure that you know that I am not talking about a device to keep your cannabis off of the floor;-) All kidding aside, go ahead and take a look at the table below.  Notice how it stores the closing prices of all the possible occurrences of Week, Day Of Week, Month.   Column 1 is the key or Hash Index value.  You will need this key to unlock the data for that particular row.  Column 2 shows the number of years that the data was collected.  Column 3 and on are the closing prices for that particular day across the years.  Once you have the data collected you can do anything your heart desires with it.

Table Index Num. Years Close 1 Close 2 Close 3 Close 4 Close 5 Close 6
1stWedJan 6 603 496 450.25 589 612.75 684.5
1stThuJan 6 606.5 486.25 446 571.75 597.75 683
1stFriJan 6 607.25 491.75 451.5 564.75 597.75 674
1stMonJan 6 606.75 490.75 447 590.25 606.25 679.25
1stTueJan 6 619.25 507 447.25 578.25 612.75 682.5
2ndWedJan 6 617.75 446 412.5 600.75 605.75 688
2ndThuJan 6 615.5 444.75 409.5 612.25 565.75 692.5
2ndFriJan 6 635.5 490.25 400 618.5 553.75 702.5
2ndMonJan 6 652.5 460.25 451 576.75 574.25 717.75

Sounds cool – so let’s do it!

Step 1:  Calculate the size of the table.

Each month consists of 4.25 weeks (52/12).  Because of this, you can have up to five occurrences of any given day of the week inside of a month – five Mondays, Tuesdays, etc.,  So we must build the table big enough to handle five complete weeks for each month.  Since there are 5 days in a week and 5 weeks in a month (not really but plan on it)  and 12 months in a year, the table must contain at least 300 rows ( 5 X 5 X 12.)  Since we don’t know how many years of data that we might want to collect we could make the arrays dynamic, but I want to keep things simple so I will reserve space for 100 years.  Overkill?

Step 2:  Use measurements from Step 1 to construct the container and create an addressing function.

The container is easy just dimension a 2-d array.  A 2-d array is a table whereas a 1-d array is a list.  A spreadsheet is an example of a 2-d array.  Just make the table big enough to hold the data.  Remember the key component to the Hash Table is not what it can hold, but the ability to quickly reference the data.  Just like your home, we need to create a unique address for each of the three hundred rows so the right mail, er data can be delivered or stored.  This is really quite simple –  we know we need a distinctive address and we know we need 300 of them.  Like the table above we can create a unique address in the form of “1stMonJan.”  This is a nine character string.  This  string can easily represent the 300 different addresses.  We start with “1stMonJan” and end with “5thFriDec.”  These values most consist of only nine characters.  I could have done the same thing using an integer value to represent each address.  “1stMonJan” could also be represented with 10101.  The “3rdFriDec” would be 30512.  I liked the string approach because the addresses are instantly recognizable with little or no translation.  So we need to get to typing, right?  Always remember if you are doing something redundant a computer can do the chore and do it quicker.  Just a quick note here.  I  designed the table ahead of time with the values in column 1 already filled in.  I could have done it more dynamically, but creating a data structure and filling in as much information before can save time on the programming side.

Instead of typing each unique address into the table, let’s let the computer do it for us.  Remember, Easylanguage has some cool string manipulation tools and with a little bit of cleverness, you can create the 300 unique addresses in one fell swoop.  The following code creates an array (list) of all of the possible combinations of “Week, Day Of Week, Month.”  There are 100 lines of code here, don’t freak out!  It’s mostly redundant.  I used a Finite State Machine and Easylanguage’s Switch – Case programming structure.  So you are learning about Hash Tables, Hash Indices, Finite State Machines, and Switch-Case programming in one post.  And here, all you want is a winning trading system.  Well, they are hard to come by and you need as many tools at your disposal to unlock the Holy Grail.  This is just one way to come up with the address values.

{Developed and programmed by George Pruitt-copyright 2017 www.georgepruitt.com}
{Just provide credit if you reuse! Or buy my book ;-)}

Input: hashIndex[n](stringArrayRef);
Vars: done(false);
Vars: firstCount(0),secondCount(0),thirdCount(0),fourthCount(0),fifthCount(0);
Vars: state(1),arrCnt(0),tempStr(""),monthCnt(0),returnValString(""),iCnt(0),jCnt(0),numBucket(0);
array: dayString[5](""),monString[12]("");


dayString[1] = "Mon";
dayString[2] = "Tue";
dayString[3] = "Wed";
dayString[4] = "Thu";
dayString[5] = "Fri";

monString[1] = "Jan";
monString[2] = "Feb";
monString[3] = "Mar";
monString[4] = "Apr";
monString[5] = "May";
monString[6] = "Jun";
monString[7] = "Jul";
monString[8] = "Aug";
monString[9] = "Sep";
monString[10] = "Oct";
monString[11] = "Nov";
monString[12] = "Dec";


arrCnt = 0;
monthCnt = 1;
While not(done) and arrCnt<300
begin
if state < 6 then arrCnt = arrCnt + 1;
switch (state)
Begin
case 1:
firstCount = firstCount + 1;
tempStr = "1st";
tempStr = tempStr + dayString[firstCount] + monString[monthCnt];
hashIndex[arrCnt] = tempStr;
If firstCount = 5 then
begin
state = 2;
firstCount = 0;
end;
case 2:
secondCount = secondCount + 1;
tempStr = "2nd";
tempStr = tempStr + dayString[secondCount] + monString[monthCnt];
hashIndex[arrCnt] = tempStr;
If secondCount = 5 then
begin
state = 3;
secondCount = 0;
end;
case 3:
thirdCount = thirdCount + 1;
tempStr = "3rd";
tempStr = tempStr + dayString[thirdCount] + monString[monthCnt];
hashIndex[arrCnt] = tempStr;
If thirdCount = 5 then
begin
state = 4;
thirdCount = 0;
end;
case 4:
fourthCount = fourthCount + 1;
tempStr = "4th";
tempStr = tempStr + dayString[fourthCount] + monString[monthCnt];
hashIndex[arrCnt] = tempStr;
If fourthCount = 5 then
begin
state = 5;
fourthCount = 0;
end;
case 5:
fifthCount = fifthCount + 1;
tempStr = "5th";
tempStr = tempStr + dayString[fifthCount] + monString[monthCnt];
hashIndex[arrCnt] = tempStr;
If fifthCount = 5 then
begin
state = 6;
fifthCount = 0;
end;
case 6:
If monthCnt < 12 then
Begin
state = 1;
monthCnt = monthCnt + 1;
end
else
begin
done = true;
end;
end;
end;
HashIndexCreator = 1;
Hash Index Creator

Here is a brief overview of this code.  The switch statement requires matching case statements.  In this machine, there are 6 different states.  Based on whatever the current state happens to be, the computer executes that block of code.  If the state is 1, then the block of code encapsulated with case(1) is executed.  All other code is ignored.  I start building the array by executing all of the “1st”‘s in January – “1stMonJan, 1stTueJan, 1stWedJan, 1stThuJan, and 1stFriJan.”   The nine character strings are built using concatenation.  In Easylanguage and most other languages you can add strings together:  “Cat” + “Dog” = “CatDog.”  So I take the string “1st” + “Mon” +  “Jan” to form the string “1stMonJan.”  I store the three characters for the day of the week and the three characters for the month in simple arrays.  After the fifth “1st”, I transition to state 2 and start working on all the “2nd”‘s.  Eventually the machine switches into 6th gear, er uh I mean state.  If month count is less than twelve, we gear down all the way back down to state 1 and start the process again for the month of February.  The machine finally turns off after month counter exceeds 12.  The Hash Index is completed; we have a unique address for the 300 rows.  In Part 2 I will show how to map the Hash Index onto the Hash Table and how to store the necessary information.  Finally, we will create an indicator using the data pulled from the table.