Would you like to learn how to do this? Check back over the next few days and I will show you to do it. Warning: its not straightforward as it seems – some tricks are involved. Remember to sign up for email notifications of new posts.
A good portion of my readers use MultiCharts and the similarities between their PowerLanguage and EasyLanguage is almost indistinguishable. However, I came across a situation where one my clients was getting different values between an indicator function call and the actual plotted indicator when using Multi-Data.
Here is the code that didn’t seem to work, even though it was programmed correctly in TradeStation.
Pretty simple – so what is the problem. Data aliasing was utilized in the Vars: section – this keeps the indicator from being calculated on the time frame of data1. Its only calculated on the data2 time frame – think of data1 being a 5 min. chart and data2 a 30 min. chart. I discovered that you have to also add data aliasing to not just the variables used in the indicator function but also to the function call itself. This line of code fixed the problem:
See that! Just add Data2 to the end of the function call. This verifies in TradeStation and compiles in MC with no problems.
I had a request recently to publish the EasyLanguage code for my Dynamic Moving Average system. This system tries to solve the problem of using an appropriate length moving average that will keep you out of the chop. The adaptive engine utilizes market volatility and increases moving average lengths as volatility increases and decreases moving average lengths as volatility decreases. Its had some success, but the adaptive engine has not truly solved the problem. The logic is pretty straightforward and can be modified to use different types of adaptive engines.
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.
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|
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.
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.
Or this one?
Obviously the first one. Even though it had a substantial draw down late in the test. What if I told you that the exact same system logic generated both curves? Here is the EasyLanguage code for this simple system.
This algorithm relies heavily on needing to know which occurred first: the high or the low of the day. The second chart tells the true story because it looks inside the daily bar to see what really happened. The first chart uses an algorithm to try to determine which happened first and applies this to the trades. In some instances, the market looks like it opens then has a slight pull back and then goes up all day. As a result the system buys and holds the trade through the close and onto the next day, but in reality the market opens, goes up and triggers a long entry, then retraces and you get stopped out. What was a nice winner turns into a bad loss. Here is an example of what might have happened during a few trades:
Nice flow – sold, bought, sold, bought, sold again and finally a nice profit. But this is what really happened:
Sold, bought, reversed short on same day and stopped out on same day. Then sold and reversed long on same day and finally sold and took profit. TradeStation’s Look Inside Bar feature helps out when your system needs to know the exact path the market made during the day. In many cases, simply clicking this feature to on will take care of most of your testing needs. However, this simple algorithm needs to place or replace orders based on what happens during the course of the day. With daily bars you are sitting on the close of the prior day spouting off orders. So once the new day starts all of your orders are set. You can’t see this initially on the surface, because it seems the algorithm is so simple. Here is another consequence of day bar testing when the intra-day market movement is paramount:
Here the computer is doing exactly what you told it! Sell short and then take a profit and sell short 25% of the ATR below the open. Well once the system exited the short it realized it was well below the sell entry point so it immediately goes short at the exact same price (remember TS doesn’t allow stop limit orders). You told the computer that you wanted to be short if the market moves a certain amount below the open. These were the orders that were place on yesterday’s close This may not be exactly what you wanted, right? You probably wanted to take the profit and then wait for the next day to enter a new trade. Even if you did want to still be short after the profit level was obtained you wouldn’t want to exit and then reenter at the same price (practically impossible) and be levied a round-turn slip and commission. You could fiddle around with the code and try to make it work, but I guarantee you that a system like this can only be tested properly on intra-day data. Let’s drop down to a lower time frame, program the system and see what the real results look like:
Looks very similar to the daily bar chart with Look Inside Bar turned on. However, it is different. If you wan’t to gauge a systems potential with a quick program, then go ahead and test on daily bars with LIB turned on. If it shows promise, then invest the time and program the intra-day version just to validate your results. What do you mean spend the time? Can’t you simply turn your chart from daily bars to five minute bars and be done with it. Unfortunately no! You have to switch paradigms and this requires quite a bit more programming. Here is our simple system now in EasyLanguage:
Here is a validation that Look Inside Bar does work:
This is the trade from June 1st. Scroll back up to the second chart where LIB is turned on.
Camarilla – A group of confidential, often scheming advisers; a cabal.
I wanted to elaborate on the original version of Camarilla. The one that users have been downloading from this website is pure reversion version. The Camarilla Equation was by created by Nick Scott, a bond day trader, in 1989. The equation uses just yesterday’s price action to project eight support/resistance price levels onto today’s trading action. These levels, or advisers, as the name of the equation suggests provides the necessary overlay to help predict turning points as well as break outs. Going through many charts with the Camarilla indicator overlay it is surprising how many times the market does in fact turn at one of these eight price levels. The equations that generate the support/resistance levels are mathematically simple:
Resistance #4 = Close + Range * 1.1 / 2;
Resistance #3 = Close + Range * 1.1/4;
Resistance #2 = Close + Range * 1.1/6;
Resistance #1 = Close + Range * 1.1/12;
Support #1 = Close – Range * 1.1/12;
Support #2 = Close – Range * 1.1/6;
Support #3 = Close – Range * 1.1/4;
Support #4 = Close – Range * 1.1/2;
The core theory behind the equation and levels is that prices have a tendency to revert to the mean. Day trading the stock indices would be easy if price broke out and continued in that direction throughout the rest of the day. We all know that “trend days” occur very infrequently on a day trade basis; most of the time the indices just chop around without any general direction. This is where the Camarilla can be effective. Take a look at the following chart [ ES 5-minute day session] where the indicator is overlaid. and how the strategy was able to take advantage of the market’s indecisiveness. This particular example shows the counter-trend nature of the Camarilla. The original Camarilla looked at where the market opened to make a trading decision. The chart below is an adapted version of the one I send out when one registers on for the download. I thought it would be a good idea to show the original that incorporates a break out along with the counter trend mechanism. I will go over the code in the next post. You can copy the code below and paste directly into you EasyLanguage editor.
Original Camarilla rules:
- If market opens between R3 and R4 go with the break out of R4. This is the long break out part of the strategy.
- If market opens between R3 and S3 then counter trend trade at the R3 level. In other words, sell short at R3. If the market moves down, then buy S3. As you can see this is the mean reversion portion of the strategy.
- If market open between S3 and S4 go with the break out of S4 – the short break out method.
- Stops are placed in the following manner:
- If long from a R4 break-out, then place stop at R3.
- If short from a S4 break-out, then place stop at S3.
- If long from a R3 countertrend, then place stop at R4.
- If short from a S3 countertrend, then place stop at S4.
- Profit objectives can be placed at opposite resistance/support levels:
- If short from a R3 countertrend, then take profits at S1, S2, or S3.
- If long from a S3 countertrend, then take profits at R1, R2, or R3.
Profit objectives for all trades can be a dollar, percent of price of ATR multiple.
Well it happened again. I had four analysis techniques open in my TradeStation Development Environment and my TS crashed and when I went back to work after restarting they were all garbage. The easiest way to back up your code is to have NotePad open and while working on your EasyLanguage copy and past your code into a NotePad file. Do this in stages and if you like you can keep different revisions. If you don’t want to keep revisions just select all and replace with the code you are copying from the TDE. Make sure you copy your final changes so that you can archive this in a separate folder and store it on your computer, flash drive or the cloud. Remember TradeStation stores your analysis techniques in one big library glob. If that glob gets corrupted and you don’t back up your TradeStation periodically, then you run a chance of losing all of your hard work. So definitely back up TradeStation automatically using their software and also back up your coding in a NotePad file.
For all of you who want to open multiple text files from within VBA here is the code that will do just that. Here I open a Open File Dialog and enable multiple file selection. I then parse the return string to gather the path and individual file names. The data is then processed – I am looking for a file with a date and a value separated by a comma. You can basically do anything you want with VBA and you also have the EXCEL functions right at your fingertips. Plus output to a worksheet is ultra simple.
One metric that seems to be missing from TradeStation is a Day Of Week analysis. It would be nice, but I don’t know how helpful, to know the $P/L breakdown on a weekday basis; does your system make all of its money on Mondays and Fridays? I created the code that will print out to the print log using an EasyLanguage method. A method is a subroutine that can be included in the main code (strategy, indicator, etc.,.) The global parameters to the main program can be seen inside the method. You can localize variable scope to the method by declaring the variable within the method’s body. A method is a great way to modularize your programming, but it is not the best way to reuse software; the method is accessible only to the main program. This EasyLanguage also utilizes an array and shows a neat piece of code to access the array elements and align them with the day of the week. The dayOfWeek() function returns [1..5] depending on what day of the week the trading day falls on. Monday = 1 and Friday = 5. The array has five elements and each element accumulates the $P/L for each of the five days based on when the trade was initiated.
Here is an example of the print out created by running this simple EasyLanguage strategy. Maybe don’t trade on Monday? Or is that curve fitting. This is an interesting tool and might carry more weight if applied to day trade algorithm. Maybe!
- Monday -8612.50
- Tuesday 6350.00
- Wednesday 2612.50
- Thursday -937.50
- Friday -1987.50