All posts by George Pruitt

George Pruitt - Author - Blogger - Programmer - Technician Bachelor of Science in Computer Science from UNC-Asheville Levo Oculos Meos In Montes

Vertical Horizontal Filter for Congestion – EasyLanguage Code

Trend following might be making a come back. It looks like we have had some very good trends in the currencies and grains. The financial have been trendy as well but with several phases of congestion. In this issue of George’s Corner in Futures Truth Magazine I am writing an article and providing the code for a simple moving average system that incorporates the VHF to help weed out congestive phases. Here is the code of the filter that was created by Tuschar Chande.

value1 = (highest(c,13) - lowest(c,13));
value2 = 0;
for iCnt = 0 to 13
begin
value2 = value2 + absValue(c[iCnt] - c[iCnt+1]);
end;
VHFValue = value1/value2;

Bollinger %B function for TradeStation

This function isn’t built into TradeStation so I decided to create it. I am doing some testing with it and will reveal any worthwhile information.

inputs:
	BollingerPrice( NumericSeries ), { price to be used in calculation of the moving 
	 average;  this is also the price of which the standard deviation will be taken
	 for calculation of the upper and lower bands }
	Length( numericSimple ), { number of bars to be used in the moving average and standard
	 deviation calculations }
	NumDevsUp( numericSimple ), { number of standard deviations to be added to the moving
	 average to calculate the upper Bollinger band }
	NumDevsDn( numericSimple ); { number of standard deviations to be added to the
	 moving average to calculate the lower Bollinger band;  this input should be
	 negative if it is desired for the lower band to be at a price that is lower
	 than the moving average }
	

variables: Avg(0),SDev( 0 ),LowerBand( 0 ),UpperBand( 0 ),PercentB( 0 ),ScaledPercentB( 0 ) ;
	

Avg = AverageFC( BollingerPrice, Length ) ;
SDev = StandardDev( BollingerPrice, Length, 1 ) ;
UpperBand = Avg + NumDevsUp * SDev ;
LowerBand = Avg + NumDevsDn * SDev ;

if UpperBand <> LowerBand then
	BollingerB = ( BollingerPrice - LowerBand ) / ( UpperBand - LowerBand )
else
	BollingerB = 0;

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;

Using TradeStations Object Oriented Code

This little code snippet uses EasyLanguage objects to get inside of the program and pull out some pretty cool information.  What it’s doing is using elsystem.drawingobjects to count the number of “text elements” on the chart and then report where the objects are located.  Our cartesian plane utilizes time as the x and price as the y.  So with price and time we can locate exactly where the points are located and feed this system into a system or an indicator.  Over the summer I plan on delving into the OO aspect of EL and reporting back the things that may enhance a charting analysis.  I have already created a pretty neat strategy that will allow a user to place “B” for buy “S” for sell and “X” for exit and have TradeStation calculate the P/L from the hand placed signals.  You can buy this from www.futurestruth.com for a small price.

using elsystem ;
using elsystem.drawingobjects ;

method void PrintTextObjectContents()
variables: int MyTextObjects, TextLabel MyTextObj, DTPoint MyTextLoc,
int Counter ;
begin
     {  limit drawing object collection to manually drawn text objects }
     MyTextObjects = ObjectCategory.textlabelcreatedbydrawingobject ;
     print (DrawingObjects.Items[MyTextObjects].Count);
     { loop through the manually drawn text objects to try and locate "A" and "B" }
     bCnt = 0;sCnt = 0;
     for Counter = 0 to DrawingObjects.Items[MyTextObjects].Count - 1
     begin
          MyTextObj = DrawingObjects.Items[MyTextObjects][Counter] astype TextLabel ;

Backing Up Source With AutoIt

I have never liked how TradeStation bundles all of “our” source code in a library.  I have always copied my source code into a notepad text file as a back up.  Not only does this back up your source you can actually look at it without launching the EL editor.  The only drawback is having to copy to notepad and then save it.  I recently came across a very powerful window’s automation software called AutoIt.  With this FREE software you can automate a ton of mundane daily tasks.  I have created a script that runs in the background and when you compose a new Analysis Technique and hit the F7 function key it will copy the contents of the current window to a notepad text file and then all  you do is save it.  With this script you should never say I wished I had back up my source outside of the TradeStation realm.

Goto http://www.autoitscript.com and download the program.  Here is the code for the macro/script:



#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

 

HotKeySet("{F7}", "copyELD")
HotKeySet("{F8}", "terminate")

 

Func copyELD()
$window_title = WinGetTitle("[active]")
WinActivate($window_title)
Send("{CTRLDOWN}a{CTRLUP}{CTRLDOWN}c{CTRLUP}{down}")
$variable_fromclipboard = ClipGet()
Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("//")
Send($window_title)
; Send($variable_fromclipboard)
Send("{CTRLDOWN}v{CTRLUP}{down}")
MsgBox(0, "The text was pasted ok", "Yep can see it")

Local Const $sMessage = "Choose a filename."
; Local $sFileSaveDialog = FileSaveDialog($sMessage, "::{450D8FBA-AD25-11D0-98A8-0800361B1103}", "ELD Text (*.txt)", $FD_PATHMUSTEXIST)
; MsgBox($MB_SYSTEMMODAL, "", "You saved the following file:" & @CRLF & $sFileSaveDialog)
Send("{CTRLDOWN}s{CTRLUP}{down}")
Send("{CTRLDOWN}x{CTRLUP}{down}")
endFunc
Func terminate()
Exit
endFunc
While 1
Sleep(10)
Wend 

Welles Wilder’s smoothing = XAVG(PeriodLen*2-1)

I was referred to a very excellent blog http://etfhq.com/ and wanted to confirm that Welles Wilder’s trick (ease of calculating) smoothing algorithm was indeed equivalent to an exponential moving average but twice the length – 1.  This blog by Derry Brown is worth taking a look at.

Here is my TS code for verify the length difference between WellesSmooth and Exp. Smooth:

{Test for equivelance}

if currentBar = 1 then
begin
value1 = average(c,14);
end;

if currentBar > 1 then
begin
value1 = (13*value1+c)/14; //Wells Wilder Smoothing
end;

plot1(value1,"WSMA",red);
plot2(xaverage(c,14),"XMA",blue);
plot3(xaverage(c,27),"2X-1XMA",green);

Volatility Break Out Day Trader

Here is a nice template to use when testing a volatility break out system to daytade. Note I haven’t yet finished the filter coding.

{OPEN RANGE BREAK OUT with Trade Management}
{:data1 = 5 minbars
 :data2 = daily bars}
 
inputs: atrLookBack(10),brkOutAmt(.20),initProtStop$(500),profitThresh$(300),percentTrail(0.3),waitNumBars(3),endTradeEntryTime(1430);
inputs: tradeFilterNum(1);
{tradeFilterNum indicates how you want to 
 filter the trades:
 filter #1 : prior day was a narrow range
 filter #2 : prior day was a NR4
 filter #3 : buy/sell day only base on today's open
 filter #4 : combo of filter #1 and filter #3
 filter #5 : combo of filter #2 and filter #3 
 }


vars: buysToday(0),sellsToday(0),atrVal(0),todaysOpen(0),canBuy(false),canSell(false);
vars: trailLong(false),trailShort(false),trailLongStop(0),trailShortStop(999999);
vars: myBuysToday(0),mySellsToday(0);
if tradeFilterNum = 1 then
begin
	canBuy = false;
	canSell = false;
	if range of data2 < avgTrueRange(atrLookBack) of data2 and time < endTradeEntryTime then
	begin
		canBuy = true;
		canSell = true;
	end;
end; 

if date <> date[1] then
begin
	todaysOpen = open; {Capture Today's Open}
	trailLongStop = 0;
	trailShortStop = 9999999;
	myBuysToday = 0;
	mySellsToday = 0;
end;

if marketPosition = 1 then myBuysToday = 1;
if marketPosition = -1 then mySellsToday = -1;

atrVal = avgTrueRange(atrLookBack) of data2;

if  canBuy and myBuysToday = 0 and marketPosition <> 1 then buy("BBO") next bar at todaysOpen + atrVal * brkOutAmt stop;
if canSell and mySellsToday = 0 and marketPosition <>-1 then sellshort("SBO") next bar at todaysOpen - atrVal *brkOutAmt stop;

if marketPosition <> 1 then trailLong = false;
if marketPosition <> -1 then trailShort = false;

if marketPosition = 1 then
begin
	sell("LongExit") next bar at entryPrice - initProtStop$/bigPointValue stop;
	if h > entryPrice + profitThresh$/bigPointValue then trailLong = true;
	if trailLong then
	begin
		trailLongStop = maxList(trailLongStop,h - (h - entryPrice)*percentTrail);
		sell("LongTrail") next bar at trailLongStop stop;
	end;
end;

if marketPosition = -1 then
begin
	buyToCover("ShrtExit") next bar at entryPrice + initProtStop$/bigPointValue stop;
	if l < entryPrice - profitThresh$/bigPointValue then trailShort = true;
	if trailShort then
	begin
		trailShortStop = minList(trailShortStop,l + (entryPrice - l)*percentTrail);
		buyToCover("ShortTrail") next bar at trailShortStop stop;
	end;
end;

setExitOnClose;

{finished}

Zone Analysis System

I posted some research on zone analysis at www.futurestruth/wordpress.com.  Based on this research I created a very simple system framework.  Here is some trade examples and performance results for the past four years.

ZoneWinner

ZoneLoser

ZonePerformance

 

 

 

 

 

//Zone Program by George Pruitt 
// Buy when market opens in zone 1 and dips into zone 3
// Sell when market opens in zone 4 and rises into zone 2

vars: myZone1B(0),myZone2T(0),myZone2B(0),myZone3T(0),myZone3B(0),myzone4T(0);
vars:oz1cz1(0),oz1cz2(0),oz1cz3(0),oz1cz4(0);
vars:oz2cz1(0),oz2cz2(0),oz2cz3(0),oz2cz4(0);
vars:oz3cz1(0),oz3cz2(0),oz3cz3(0),oz3cz4(0);
vars:oz4cz1(0),oz4cz2(0),oz4cz3(0),oz4cz4(0);

vars:myBarCount(0),buysToday(0),sellsToday(0);

inputs: profitAmt$(1000),riskAmt$(500),stopEntryTime(1300);
if date <> date[1] then
begin
	buysToday = 0;
	sellsToday = 0;
	myBarCount = 1;
end;

if(marketPosition = 1) then buysToday = 1;
if(marketPosition =-1) then sellsToday = 1;
	
if date<> date[1] then myBarCount = myBarCount + 1;
myZone1B = highD(1) + minMove/PriceScale;
myZone2T = highD(1);
myZone2B = (highD(1) + lowD(1))/2 + minMove/PriceScale;
myZone3T = (highD(1) + lowD(1))/2;
myZone3B = lowD(1); 
myZone4T = lowD(1) - minMove/PriceScale;


//if openD(0) > myZone1B and low > myZone1B and myBarCount > 1 and sellsToday = 0 and time <> Sess1endtime then sellshort next bar at low stop;
//if openD(0) < myZone4T and high < myZone4T and myBarCount > 1 and buysToday = 0 and time <> Sess1endtime then buy next bar at high stop;

if openD(0) > myZone1B and low < myZone2B and myBarCount > 1 and buysToday = 0 and time < stopEntryTime then buy next bar at low limit;
if openD(0) < myZone4T and high > myZone3T and myBarCount > 1 and sellsToday = 0 and time < stopEntryTime then sellshort next bar at high limit;

myBarCount = myBarCount + 1;
//print(date," ",time," ",buysToday," ",openD(0) > myZone1B and low > myZone1B and myBarCount > 1 and buysToday = 0);
setProfitTarget(profitAmt$);
setStopLoss(riskAmt$);

setExitOnClose;