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

Using Strings in EL for multi-step System

This little strategy uses EasyLanguage’s string manipulation to keep track of a multi-step, mutli-criteria, multi-state trade entry. You don’t buy until the buyString is equal to “BUY”. The sell side is just the opposite. When you program a multi-step entry you also need to build a reset situation. In the case of this system you reset the string to null(“”) when the price dips back down below the 9 day moving average. After resetting the process starts over again.


{Use curly brackets for mult-line
 comments
 
 This system needs three criteria to be met
 before a trade is initiated
 Buy Criteria 1:  C > 9 day movAvg - trend Up
 Buy Criteria 2:  H = HighestHigh 10 days - break Out
 Buy Criteria 3:  C < C[2] - retracement }
 
 vars:buyString(""),sellString("");
 
 
 if marketPosition = 0 then {If flat then reset strings}
 begin
 	buyString = "";
 	sellString = "";
 end;
 
 if c >= average(c,9) then buyString = "B";  //First criteria met
 if c < average(c,9) then buyString = "";
 
 if c > average(c,9) then sellString = "";
 if c <= average(c,9) then sellString = "S"; 
 
 if buyString = "B" then
 begin
 	if h > highest(h,10)[1] then buyString = buyString + "U"; //Second Criteria met
 end;
 
 if buyString = "BU" then
 begin
 	if c < c[2] then buyString = buyString + "Y"; //Third criteria met
 end;
 
 if buyString = "BUY" then buy ("BuyString") next bar at open; //Read BUY
 
 if sellString = "S" then
 begin
 	if l > lowest(l,10)[1] then sellString = sellString + "E";
 end;
 
 if sellString = "SE" then
 begin
 	if c > c[2] then sellString = sellString + "LL";
 end;
 
 if sellSTring = "SELL" then sellShort ("sellString") next bar at open; 
 setStopLoss(1000);

 SetPercentTrailing(1000, 30);  

Triple Moving Average

The Triple Moving Average Crossover System

Trend following is on a comeback. Most of the more popular trend following algorithms have shown positive performance for the past three months. I wanted to take a look at the TMA because I like how it trades in the direction of the overall long term trend but can get out quick if the market has a reversal. The Double Crossover system is usually a stop and reversal.

Here’s how the system works: Go long if the short term average crosses from below the mid term average if and only if the mid term average is greater than the long term average. In some cases the short term will cross the mid term but both will be below the long term average. When this happens you will stay neutral until the mid term average crosses from below the long term average. Even when this happens the short term must still be greater than the mid term (this is usually the case though). Selling short is simply the opposite.

You exit long positions when the short term averages crosses from above the mid term average. You don’t need to look at the long term average as it plays not part in the liquidation signal. Shorts are liquidated when the short term averages crosses from below the mid term.

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);