A Cool and Easy Way to Analyze Chart Patterns with TS

Have you every tested different combinations of closing or opening relationships to uncover the following days probability of either closing up or down.  I am sure you have tested selling after two down closes or just the opposite:  buying after two up closes.  Testing the relationship between two days is easy but when you go beyond just two then the programming can become very difficult.

 
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 like

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. Now all we need is a way for TradeStation, based on the run number, to change the pattern or pattern number.

The following 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 its 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 ” + – – – “.

Oh yeah this was tested in the ES over the past five years so we are probably witnessing a bullish bias. Let’s see if we sell using the same patterns.
Pattern P/L
2 -14150
9 -13850
1 -12187.5
13 -11025
10 -10225
8 -9762.5
12 -7700
3 -7687.5
7 -7500
6 -6887.5
4 -5425
14 -4975
5 -2412.5
11 12.5
16 1000 “+ + + +”
15 1312.5 “+ + + -”

Well the bullish bias is definitely evident. The best 4 day pattern to sell the next bar is to wait for 4 consecutive up closes or 3 consecutive up closes followed by 1 down close.

Is there anything to this pattern recognition? I think it can be used to filter trades and that’s about it. We tested a four day pattern for illustration purposes only but that might be too many. A two day pattern might work better. Please check this out and let me know.

Here is the program in its entirety:

input: patternTests(14),orbAmount(0.20),LorS(1),holdDays(0),atrAvgLen(10);

var: patternTest(""),patternString(""),tempString("");
var: iCnt(0),jCnt(0);
array: patternBitChanger[4](0);

{written by George Pruitt -- copyright 2006 by George Pruitt
This will test a 4 day pattern based on the open to close
relationship. A plus represents a close greater than its
open, whereas a minus represents a close less than its open.
The default pattern is set to pattern 14 +++- (1110 binary).
You can optimize the different patterns by optimizing the
patternTests input from 1 to 16 and the orbAmount from .01 to
whatever you like. Same goes for the hold days, but in this
case you optimize start at zero. The LorS input can be
optimized from 1 to 2 with 1 being buy and 2 being sellshort.}

patternString = "";
patternTest = "";

patternBitChanger[0] = 0;
patternBitChanger[1] = 0;
patternBitChanger[2] = 0;
patternBitChanger[3] = 0;

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;

patternString = "";

for iCnt = 3 downto 0 begin
if(patternBitChanger[iCnt] = 1) then
begin
patternTest = patternTest + "+";
end
else
begin
patternTest = patternTest + "-";
end;
end;

for iCnt = 3 downto 0
begin
if(close[iCnt]> close[iCnt+1]) then
begin
patternString = patternString + "+";
end
else
begin
patternString = patternString + "-";

end;
end;

if(barNumber = 1) then print(elDateToString(date)," pattern ",patternTest," ",patternTests-1);
if(patternString = patternTest) then begin

// print(date," ",patternString," ",patternTest); //uncomment this and you can print out the pattern
// if(LorS = 2) then SellShort("PatternSell") next bar at open of tomorrow - avgTrueRange(atrAvgLen) * orbAmount stop;
// if(LorS = 1) then buy("PatternBuy") next bar at open of tomorrow + avgTrueRange(atrAvgLen) * orbAmount stop;

if(LorS = 2) then SellShort("PatternSell") next bar at open;
if(LorS = 1) then buy("PatternBuy") next bar at open;

end;

if(holdDays = 0 ) then setExitonClose;
if(holdDays > 0) then
begin
if(barsSinceEntry = holdDays and LorS = 2) then BuyToCover("xbarLExit") next bar at open;
if(barsSinceEntry = holdDays and LorS = 1) then Sell("xbarSExit") next bar at open;
end;