The latest piece of free TradeStation code helps with price patterns. In Toby Crabel’s classic book “Day Trading with Short Term Patterns and opening range breakout”, he discussed these simple open to close price patterns. There has also been work on close to close price patterns done by several people including Larry Williams. Open to close patterns are not as important in commodities today because the open are often only a few hours after the close in today’s electronic markets.

In this article we will discuss developing a tool for TradeStation written in EasyLanguage which we can use to test these close to close patterns. Use the form on the right to get the free EasyLanguage code for this tool. It would be easy to test one pattern by hardcoding the logic but much more difficult if you want to test multiple patterns. Let’s say you want to buy tomorrow at some level when you have had three up closes.

The code for this idea is quite simple:

The inefficient way: if c > c[1] and c[1] > c[2] and c[2] > c[3] then buy somewhere tomorrow

What if you wanted to test different combination of up/down closes then you would need to go into the code and make the changes to the above logic. Here we are buying after two up closes followed by a down close.

if c < c[1] and c[1] > c[2] and c[2] > c[3] then buy somewhere tomorrow

You would then need to verify, run and keep track of results. This is really a pain. I have come up with an easy way to accomplish looking at many patterns and letting the computer keep track of the results.

What you need is a simple and dynamic way to change the patterns based on each individual run. We know we can use TradeStation’s optimizer to create multiple runs and record these results in a table. Now all we need is a way for TradeStation, based on the run number, to change the pattern or pattern number.

The following EasyLanguage code does this:

value1 = patternTests - 1; if(value1 > 0) then begin if(mod(value1,2) = 1) then patternBitChanger[0] = 1; value2 = value1 - patternBitChanger[0] * 1; if(value2 >= 8) then begin patternBitChanger[3] = 1; value2 = value2 - 8; end; if(value2 >= 4) then begin patternBitChanger[2] = 1; value2 = value2 - 4; end; if(value2 = 2) then patternBitChanger[1] = 1; end;

Do you remember binary based systems where 0 – off and 1 – on? If you don’t remember – it’s OK. Here is a quick review for everyone.

0 0 0 0 pattern = 0 0 0 0 1 pattern = 1 0 0 1 0 pattern = 2 0 1 0 0 pattern = 4 1 0 0 0 pattern = 8

Each 1 or 0 is a placeholder and its placeholder is a bit than can either be on or off. The leftmost bit is 1′s place. The next bit is the 2′s place. The next bit is the 4′s place. And finally the rightmost bit is the 8′s place. So if we plug in 1 1 1 1 we get (8 + 4 + 2 + 1) = 15. If we start counting at 0 [ 0 0 0 0 ] then we can represent 16 distinct patterns with our four bits. Following?

So using this bit scheme and TradeStation’s optimizer we can run 16 different patterns sequentially. Now how do we get the bit pattern scheme to relate to the last four day’s close to close relationships. This is where the eloquence continues [if I have to say so myself]. EasyLanguage has a library of powerful string functions. One of those is the ability to concatenate or add a string to a string.

Let’s pretend we are the computer and we are optimizing thru 16 different runs. Let’s call the first run [0 run]. In our tests 1′s will be represented by “+” and 0′s by “-”. Using our 4 bit scheme how to we represent the number 0? Zero means no bits are on so 0 = [0 0 0 0]. If we translate our bits to a string where 1′s are “+” and 0′s are “-” then we get “- – – -”]. Right? If a “+” represents an up close and “-” represents a down close then the string “- – – -” would indicate four straight down closes.

Run 0 = 0 0 0 0 = ” – - – - ” – four straight closes down Run 1 = 0 0 0 1 = ” – - – + ” – three straight closes down followed by 1 up close Run 2 = 0 0 1 0 = ” – - + – ” – two straight closes down, one up close, one down close Run 3 = 0 0 1 1 = ” – - + + ” – two straight closes down, two straight closes up … … … Run 15= 1 1 1 1 = ” + + + + ” – four straight closes up

Following this scheme we can easily check out 16 different 4 day patterns. Let’s see what pattern is the most prolific on the long side. What do you guess – maybe after 4 consecutive down closes?

Simply buying the next bar’s open and exiting two days later, let’s see which pattern is king. Here is the pattern and its profit/loss:

Pattern P/L Pattern String 1 12187.5 ” – - – - ” 2 14150 ” – - + – ” 3 7687.5 ” – - + + ” 4 5425 5 2412.5 6 6887.5 7 7500 8 9762.5 9 13850 10 10225 11 -12.5 12 7700 13 11025 14 4975 15 -1312.5 16 -1000

Well it seems they were all winners but you shouldn’t buy after ” + + + + “. Pattern ” – – + -” was the best followed by ” + – – – “.

We can also test our patterns on the short side where we sell a given pattern. In market’s like the ES mini with a upward bias we will see that it’s hard to find patterns which are profitable on the short side. Let’s now look at a real example.

We will look at the ES for the past 15 years and test our PatternSmasher code. This Strategy has the following inputs:

**patternTests:**ranges from 0-16**orbAmount:**the percentage of the range used for an opening range breakout**LorS:**Long or short, 1=long 2 is short**holdDays:**0 is exit at the close non zero represents days to hold**atrAveLen:**is bars to use in average truerange calculation**enterNextBarAtOpen:**decides if we enter at the open or if we use an opening range breakout

We will test this on ES continuous contract for the last 15 years ending 11/7/2014. We will not deduct slippage or commission because we want to see the bias.

Let’s look at our first experiment, we will test entering at the next open and not using an opening range breakout.

We are entering at the open for all 16 patterns and hold days from exit at close to a 10 day hold and testing the long side.

We see that some of these patterns perform very good. Patterns 9 and 1 dominate the performance in the ES on the long side. The holding period seem to have two clusters of good results at 3-5 days and then 9-10 days.

Let’s look at our 9,4 test in more detail. We had 205 cases during our 15 year period, with 114 winning trades which is a little over 55% with a win loss ratio of 1.19. The average trade is $248.00 so we can take 50.00 in slippage and commissions and still average almost $200.00 a trade. That makes this a very interesting pattern. The maximum drawdown was around 16K. Let’s now test using an opening range breakout.

We are going to optimize opening range breakout from 0 to 1 times the average true range in steps of .1. We will also optimize atrAvgLen from 3 to 10 steps of 1.

This is over 15,000 combinations in TradeStation. You can see the number of combination increases quickly. This is why it’s good that TradeStation now has a genetic optimizer because if we test any more complex patterns than this we will have hundred thousand combinations or more and we will need the genetic optimizer to run these quickly. In this case though we found that our orbAmount of zero produced the best results. An orbAmount of zero is the same as entering at the open so we get the same results as above.

Let’s now check the short side once again without slippage and commissions. We will optimize on the short side changing LorS to 2.

We are now optimize for the short side patterns see LorS =2, We are optimize across all 16 patterns and holding periods of 0 to 10 step 1.

We can see the short side does not do as well as the long side. Best combinations pattern 12, hold 5 days makes $31,637.00 and average about 148.00 per trade or a little less than 100.00 after slippage and commission. We have 2 good patterns number 12 and 10. Winning percentages are slightly less than 50%.

This is just a start. This tool in included with complete open source code so you can improve it and continue your research. For example change it to open/close and test on equities and ETFs or test on a portfolio of commodities. You can also add different filters which can be tested. It also teaches a little used capability of easylanguage’s string manipulation. I hope you enjoy it.