Category Archives: Free EasyLanguage

Testing Keith Fitschen’s Bar Scoring with Pattern Smasher

Keith’s Book

Thanks to MJ for planting the seed for this post.  If you were one of the lucky ones to get Keith’s “Building Reliable Trading SystemsTradable Strategies that Perform as They Backtest and Meet Your Risk-Reward Goals”  book by John Wiley 2013 at the list price of $75 count yourself lucky.  The book sells for a multiple of that on Amazon.com.  Is there anything earth shattering in the book you might ask?  I wouldn’t necessarily say that, but there are some very well thought out and researched topics that most traders would find of interest.

Bar Scoring

In his book Keith discusses the concept of bar-scoring.  In Keith’s words, “Bar-scoring is an objective way to classify an instrument’s movement potential every bar.  The two parts of the bar-scoring are the criterion and the resultant profit X days hence.”  Keith provides several bar scoring techniques, but I highlight just one.

Keith broke these patterns down into the relationship of the close to the open, and close in the upper half of the range; close greater than the open and close in the lower half of the range.  He extended the total number of types to 8 by adding the relationship of the close of the bar to yesterdays bar.

The PatternSmasher code can run through a binary representation

for each pattern and test holding the position for an optimizable number of days.  It can also check for long and short positions.  The original Pattern Smasher code used a for-loop to create patterns that were then compared to the real life facsimile.  In this code it was easier to just manually define the patterns and assign them the binary string.

if c[0]> c[1] and c[0] > o[0] and c[0] > (h[0] + l[0])/2  then patternString = "----";
if c[0]> c[1] and c[0] > o[0] and c[0] < (h[0] + l[0])/2  then patternString = "---+";
if c[0]> c[1] and c[0] < o[0] and c[0] > (h[0] + l[0])/2  then patternString = "--+-";
if c[0]> c[1] and c[0] < o[0] and c[0] < (h[0] + l[0])/2  then patternString = "--++";
if c[0]< c[1] and c[0] > o[0] and c[0] > (h[0] + l[0])/2  then patternString = "-+--";
if c[0]< c[1] and c[0] > o[0] and c[0] < (h[0] + l[0])/2  then patternString = "-+-+";
if c[0]< c[1] and c[0] < o[0] and c[0] > (h[0] + l[0])/2  then patternString = "-++-";
if c[0]< c[1] and c[0] < o[0] and c[0] < (h[0] + l[0])/2  then patternString = "-+++";
Manual Pattern Designations

Please check my code for any errors.  Here I go through the 8 different relationships and assign them to a Patter String.  “-+++”  represents pattern number (7 ) or type (7 + 1 = 8 – my strings start out at 0).  You can then optimize the test pattern and if the test pattern matches the actual pattern, then the Pattern Smasher takes the trade  on the opening of the next bar and holds it for the number of days you specify.  You an also designate long and short positions in the code.  Here I optimized the 8 patterns going long and short and holding from 1-4 days.

Here is the equity curve!  Remember these are Hypothetical Results with $0 commission/slippage and historic performance is not necessarily indicative of future results.  Educational purposes only!  This is tested on ES.D

Play around with the code and let me know if you find any errors or any improvements.

input: patternTests(8),orbAmount(0.20),LorS(1),holdDays(0),atrAvgLen(10),enterNextBarAtOpen(true);
  
var: patternTest(""),patternString(""),tempString("");
var: iCnt(0),jCnt(0);
array: patternBitChanger[4](0);
   
{written by George Pruitt -- copyright 2019 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;
 
 
//example patternTests = 0 -- > 0000
//example patternTests = 1 -- > 0001
//example patternTests = 2 -- > 0010
//example patternTests = 3 -- > 0011
//example patternTests = 4 -- > 0100
//example patternTests = 5 -- > 0101
//example patternTests = 6 -- > 0110
//example patternTests = 7 -- > 0111

if(value1 >= 0) then
begin
 
    if(mod(value1,2) = 1) or value1 = 1 then patternBitChanger[0] = 1;
    value2 = value1 - patternBitChanger[0] * 1;
  
    if(value2 >= 7) 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;

for iCnt = 3 downto 0  begin
    if(patternBitChanger[iCnt] = 1) then
    begin
        patternTest = patternTest + "+";
    end
    else
    begin
        patternTest = patternTest + "-";    
    end;
end;
 
 patternString = "";
  
if c[0]> c[1] and c[0] > o[0] and c[0] > (h[0] + l[0])/2  then patternString = "----";
if c[0]> c[1] and c[0] > o[0] and c[0] < (h[0] + l[0])/2  then patternString = "---+";
if c[0]> c[1] and c[0] < o[0] and c[0] > (h[0] + l[0])/2  then patternString = "--+-";
if c[0]> c[1] and c[0] < o[0] and c[0] < (h[0] + l[0])/2  then patternString = "--++";
if c[0]< c[1] and c[0] > o[0] and c[0] > (h[0] + l[0])/2  then patternString = "-+--";
if c[0]< c[1] and c[0] > o[0] and c[0] < (h[0] + l[0])/2  then patternString = "-+-+";
if c[0]< c[1] and c[0] < o[0] and c[0] > (h[0] + l[0])/2  then patternString = "-++-";
if c[0]< c[1] and c[0] < o[0] and c[0] < (h[0] + l[0])/2  then patternString = "-+++";

 
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 (enterNextBarAtOpen) then
	begin
		if(LorS = 2) then SellShort("PatternSell") next bar on open;
		if(LorS = 1) then buy("PatternBuy") next bar at open;
	end
	else
	begin
		if(LorS = 2) then SellShort("PatternSellBO") next bar at open of tomorrow - avgTrueRange(atrAvgLen) * orbAmount stop;
    	if(LorS = 1) then buy("PatternBuyBO") next bar at open of tomorrow + avgTrueRange(atrAvgLen) * orbAmount stop;
    end;
	

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;
Bar Scoring Testing Template
Please follow and like us:
error

MULTI-TIME FRAME – KEEPING TRACK OF DISCRETE TIME FRAMES

Just a quick post here.  I was asked how to keep track of the opening price for each time frame from our original Multi-Time Frame indicator and I was answering the question when I thought about modifying the indicator.  This version keeps track of each discrete time frame.  The original simply looked back a multiple of the base chart to gather the highest highs and lowest lows and then would do a simple calculation to determine the trend.  So let’s say its 1430 on a five-minute bar and you are looking back at time frame 2.  All I did was get the highest high and lowest low two bars back and stored that information as the high and low of time frame 2.  Time frame 3 simply looked back three bars to gather that information.  However if you tried to compare these values to a 10-minute or 15-minute chart they would not match.

In this version, I use the modulus function to determine the demarcation of each time frame.  If I hit the border of the time frame I reset the open, high, low and carry that value over until I hit the next demarcation.  All the while collecting the highest highs and lowest lows.  In this model, I am working my way from left to right instead of right to left.  And in doing so each time frame is discrete.

Let me know which version you like best.

 

Inputs:tf1Mult(2),tf2Mult(3),tf3Mult(4),tf4Mult(5);



vars: mtf1h(0),mtf1l(0),mtf1o(0),mtf1c(0),mtf1pvt(0),diff1(0),
      mtf2h(0),mtf2l(0),mtf2o(0),mtf2c(0),mtf2pvt(0),diff2(0),
      mtf3h(0),mtf3l(0),mtf3o(0),mtf3c(0),mtf3pvt(0),diff3(0),
      mtf4h(0),mtf4l(0),mtf4o(0),mtf4c(0),mtf4pvt(0),diff4(0),
      mtf0pvt(0),diff0(0);

If barNumber = 1 then
Begin
	mtf1o = o;
	mtf2o = o;
	mtf3o = o;
	mtf4o = o;
end;


If barNumber > 1 then
Begin

	Condition1 =  mod((barNumber+1),tf1Mult) = 0;
	Condition2 =  mod((barNumber+1),tf2Mult) = 0;
	Condition3 =  mod((barNumber+1),tf3Mult) = 0;
	Condition4 =  mod((barNumber+1),tf4Mult) = 0;
	
	mtf1h = iff(not(condition1[1]),maxList(high,mtf1h[1]),high);
	mtf1l = iff(not(condition1[1]),minList(low,mtf1l[1]),low);
	mtf1o = iff(condition1[1],open,mtf1o[1]);
	mtf1c = close;

	
	mtf0pvt = (close + high + low) / 3;
	diff0 = close - mtf0pvt;
	
	mtf2h = iff(not(condition2[1]),maxList(high,mtf2h[1]),high);
	mtf2l = iff(not(condition2[1]),minList(low,mtf2l[1]),low);
	mtf2o = iff(condition2[1],open,mtf2o[1]);
	mtf2c = close;
	
	
	mtf1pvt = (mtf1h+mtf1l+mtf1c) / 3;
	diff1 = mtf1c - mtf1pvt;
		
	mtf2pvt = (mtf2h+mtf2l+mtf2c) / 3;
	diff2 = mtf2c - mtf2pvt;
		
	mtf3h = iff(not(condition3[1]),maxList(high,mtf3h[1]),high);
	mtf3l = iff(not(condition3[1]),minList(low,mtf3l[1]),low);
	mtf3o = iff(condition3[1],open,mtf3o[1]);
	mtf3c = close;
	
	mtf3pvt = (mtf3h+mtf3l+mtf3c) / 3;
	diff3 = mtf3c - mtf3pvt;
	
	mtf4h = iff(not(condition4[1]),maxList(high,mtf4h[1]),high);
	mtf4l = iff(not(condition4[1]),minList(low,mtf4l[1]),low);
	mtf4o = iff(condition4[1],open,mtf4o[1]);
	mtf4c = close;

	mtf4pvt = (mtf4h+mtf4l+mtf4c) / 3;
	diff4 = mtf4c - mtf4pvt;
	
	
	Condition10 = diff0 > 0;
	Condition11 = diff1 > 0;
	Condition12 = diff2 > 0;
	Condition13 = diff3 > 0;
	Condition14 = diff4 > 0;
	 
	If condition10 then setPlotColor(1,Green) else SetPlotColor(1,Red);
	If condition11 then setPlotColor(2,Green) else SetPlotColor(2,Red);
	If condition12 then setPlotColor(3,Green) else SetPlotColor(3,Red);
	If condition13 then setPlotColor(4,Green) else SetPlotColor(4,Red);
	If condition14 then setPlotColor(5,Green) else SetPlotColor(5,Red);
	
	condition6 = condition10 and condition11 and condition12 and condition13 and condition14;
	Condition7 = not(condition10) and not(condition11) and not(condition12) and not(condition13) and not(condition14);

	If condition6 then setPlotColor(7,Green);
	If condition7 then setPlotColor(7,Red);
	
	If condition6 or condition7 then plot7(7,"trend");

	Plot6(5,"line");	
	Plot1(4,"t1");
	Plot2(3,"t2");
	Plot3(2,"t3");
	Plot4(1,"t4");
	Plot5(0,"t5"); 

end;
Multi-Time Frame with Discrete Time Frames
Please follow and like us:
error

Using a Dictionary to Create a Trading System

Dictionary Recap

Last month’s post on using the elcollections dictionary was a little thin so I wanted to elaborate on it and also develop a trading system around the best patterns that are stored in the dictionary.  The concept of the dictionary exists in most programming languages and almost all the time uses the (key)–>value model.  Just like a regular dictionary a word or a key has a unique definition or value.  In the last post, we stored the cumulative 3-day rate of return in keys that looked like “+ + – –” or “+ – – +“.  We will build off this and create a trading system that finds the best pattern historically based on average return.  Since its rather difficult to store serial data in a Dictionary I chose to use Wilder’s smoothing average function.

Ideally, I Would Have Liked to Use a Nested Dictionary

Initially, I played around with the idea of the pattern key pointing to another dictionary that contained not only the cumulative return but also the frequency that each pattern hit up.  A dictionary is designed to have  unique key–> to one value paradigm.  Remember the keys are strings.  I wanted to have unique key–> to multiple values. And you can do this but it’s rather complicated.  If someone wants to do this and share, that would be great.  AndroidMarvin has written an excellent manual on OOEL and it can be found on the TradeStation forums.  

Ended Up Using A Dictionary With 2*Keys Plus an Array

So I didn’t want to take the time to figure out the nested dictionary approach or a vector of dictionaries – it gets deep quick.  So following the dictionary paradigm I came up with the idea that words have synonyms and those definitions are related to the original word.  So in addition to having keys like “+ + – -” or “- – + -” I added keys like “0”, “1” or “15”.  For every  + or – key string there exists a parallel key like “0” or “15”.  Here is what it looks like:

–  –  –  –  = “0”
– – – + = “1”
– – + – = “2”

You can probably see the pattern here.  Every “+” represents a 1 and every “0” represent 0 in a binary-based numbering system.  In the + or – key I store the last value of Wilders average and in the numeric string equivalent, I store the frequency of the pattern.

Converting String Keys to Numbers [Back and Forth]

To use this pattern mapping I had to be able to convert the “++–” to a number and then to a string.  I used the numeric string representation as a dictionary key and the number as an index into an array that store the pattern frequency.  Here is the method I used for this conversion.  Remember a method is just a function local to the analysis technique it is written.

//Lets convert the string to unique number
method int convertPatternString2Num(string pattString) 
Vars: int pattLen, int idx, int pattNumber;
begin
	pattLen = strLen(pattString);
	pattNumber = 0;
	For idx = pattLen-1 downto 0 
	Begin
		If MidStr(pattString,pattLen-idx,1) = "+" then pattNumber = pattNumber + power(2,idx);
	end;
	Return (pattNumber);
end;
String Pattern to Number

This is a simple method that parses the string from left to right and if there is a “+” it is raised to the power(2,idx) where idx is the location of “+” in the string.  So “+  +  –  –  ” turns out to be 8 + 4 + 0 + 0 or 12.

Once I retrieve the number I used it to index into my array and increment the frequency count by one.  And then store the frequency count in the correct slot in the dictionary.

patternNumber = convertPatternString2Num(patternString); 
//Keep track of pattern hits
patternCountArray[patternNumber] = patternCountArray[patternNumber] + 1;
//Convert pattern number to a string do use as a Dictionary Key
patternStringNum = numToStr(patternNumber,2);
//Populate the pattern number string key with the number of hits
patternDict[patternStringNum] = patternCountArray[patternNumber] astype double;
Store Value In Array and Dictionary

Calculating Wilder’s Average Return and Storing in Dictionary

Once I have stored an instance of each pattern [16] and the frequency of each pattern[16] I calculate the average return of each pattern and store that in the dictionary as well.

//Calculate the percentage change after the displaced pattern hits
Value1 =  (c - c[2])/c[2]*100;
//Populate the dictionary with 4 ("++--") day pattern and the percent change
if patternDict.Contains(patternString) then
Begin
	patternDict[patternString] = (patternDict[patternString] astype double * 
	(patternDict[patternStringNum] astype double - 1.00) + Value1) / patternDict[patternStringNum] astype double;
end
Else
begin
	patternDict[patternString] = value1;
//	print("Initiating: ",patternDict[patternString] astype double);
end;
(pAvg * (N-1) + return) / N

When you extract a value from a collection you must us an identifier to expresses its data type or you will get an error message : patternDict[patternString] holds a double value {a real number}  as well as patternDict[patternStringNum] – so I have to use the keyword asType.  Once I do my calculation I ram the new value right back into the dictionary in the exact same slot.  If the pattern string is not in the dictionary (first time), then the Else statement inserts the initial three-day rate of return.

Sort Through All of The Patterns and Find the Best!

The values in a dictionary are stored in alphabetic order and the string patterns are arranged in the first 16 keys.  So I loop through those first sixteen keys and extract the highest return value as the “best pattern.”

//  get the best pattern that produces the best average 3 bar return
vars: hiPattRet(0),bestPattString("");
If patternDict.Count > 29 then
Begin
	index = patternDict.Keys;
	values = patternDict.Values; 
	hiPattRet = 0;
	For iCnt = 0 to 15
	Begin
		If values[iCnt] astype double > hiPattRet then
		Begin
			hiPattRet = values[iCnt] astype double ;
			bestPattString = index[iCnt] astype string;
		end;
	end;
//	print(Date," BestPattString ",bestPattString," ",hiPattRet:8:4," CurrPattString ",currPattString);
end;
Extract Best Pattern From All History

If Today’s Pattern Matches the Best Then Take the Trade

// if the current pattern matches the best pattern then bar next bar at open
If currPattString = BestPattString then buy next bar at open;
// cover in three days
If barsSinceEntry > 2 then sell next bar at open;
Does Today Match the Best Pattern?

If today matches the best pattern then buy and cover after the second day.

Conclusion

I didn’t know if this code was worth proffering up but I decided to posit it because it contained a plethora of programming concepts: dictionary, method, string manipulation, and array.  I am sure there is a much better way to write this code but at least this gets the point across.

Contents of Dictionary at End of Run

++++    0.06
+++-   -0.08
++-+    0.12
++--   -0.18
+-++    0.08
+-+-    0.40
+--+   -0.46
+---    0.34
-+++    0.20
-++-    0.10
-+-+    0.23
-+--    0.31
--++    0.02
--+-    0.07
---+    0.22
----    0.46
0.00  103.00
1.00  128.00
10.00  167.00
11.00  182.00
12.00  146.00
13.00  168.00
14.00  163.00
15.00  212.00
2.00  157.00
3.00  133.00
4.00  143.00
5.00  181.00
6.00  151.00
7.00  163.00
8.00  128.00
9.00  161.00
Contents of Dictionary

Example of Trades

Pattern Dictionary System

 

Code in Universum

//Dictionary based trading sytem
//Store pattern return
//Store pattern frequency
// by George Pruitt
Using elsystem.collections; 

vars: string keystring("");
vars: dictionary patternDict(NULL),vector index(null), vector values(null);
array: patternCountArray[100](0);

input: patternTests(8);
  
var: patternTest(""),tempString(""),patternString(""),patternStringNum("");
var: patternNumber(0);
var: iCnt(0),jCnt(0);
//Lets convert the string to unique number
method int convertPatternString2Num(string pattString) 
Vars: int pattLen, int idx, int pattNumber;
begin
	pattLen = strLen(pattString);
	pattNumber = 0;
	For idx = pattLen-1 downto 0 
	Begin
		If MidStr(pattString,pattLen-idx,1) = "+" then pattNumber = pattNumber + power(2,idx);
	end;
	Return (pattNumber);
end;


once begin 
   clearprintlog; 
   patternDict = new dictionary; 
   index = new vector;
   values = new vector;
end;   

//Convert 4 day pattern displaced by 2 days
patternString = ""; 
for iCnt = 5 downto 2
begin
    if(close[iCnt]> close[iCnt+1]) then
    begin
        patternString = patternString + "+";
    end
    else
    begin
        patternString = patternString + "-";
    end;
end;

//What is the current 4 day pattern
vars: currPattString("");
currPattString = "";

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

//Get displaced pattern number
patternNumber = convertPatternString2Num(patternString); 
//Keep track of pattern hits
patternCountArray[patternNumber] = patternCountArray[patternNumber] + 1;
//Convert pattern number to a string do use as a Dictionary Key
patternStringNum = numToStr(patternNumber,2);
//Populate the pattern number string key with the number of hits
patternDict[patternStringNum] = patternCountArray[patternNumber] astype double;
//Calculate the percentage change after the displaced pattern hits
Value1 =  (c - c[2])/c[2]*100;
//Populate the dictionary with 4 ("++--") day pattern and the percent change
if patternDict.Contains(patternString) then
Begin
	patternDict[patternString] = (patternDict[patternString] astype double * 
	(patternDict[patternStringNum] astype double - 1.00) + Value1) / patternDict[patternStringNum] astype double;
end
Else
begin
	patternDict[patternString] = value1;
//	print("Initiating: ",patternDict[patternString] astype double);
end;
//  get the best pattern that produces the best average 3 bar return
vars: hiPattRet(0),bestPattString("");
If patternDict.Count > 29 then
Begin
	index = patternDict.Keys;
	values = patternDict.Values; 
	hiPattRet = 0;
	For iCnt = 0 to 15
	Begin
		If values[iCnt] astype double > hiPattRet then
		Begin
			hiPattRet = values[iCnt] astype double ;
			bestPattString = index[iCnt] astype string;
		end;
	end;
//	print(Date," BestPattString ",bestPattString," ",hiPattRet:8:4," CurrPattString ",currPattString);
end;
// if the current pattern matches the best pattern then bar next bar at open
If currPattString = BestPattString then buy next bar at open;
// cover in three days
If barsSinceEntry > 2 then sell next bar at open;
Pattern Dictionary Part II

 

 

 

Please follow and like us:
error

Calculating Position Size with Optimal F

I had a reader of the blog ask how to use Optimal F.  That was really a great question.  A few posts back I provided the OptimalFGeo function but didn’t demonstrate on how to use it for allocation purposes.  In this post, I will do just that.

I Have Optimal F – Now What?

From Ralph Vince’s book, “Portfolio Management Formulas”, he states: “Once the highest f is found, it can readily be turned into a dollar amount by dividing the biggest loss by the negative optimal f.  For example, if our biggest loss is $100 and our optimal f is 0.25, then -$100/ 0.25 = $400.  In other words, we should bet 1 unit for every $400 we have in our stake.”

Convert Optimal F to dollars and then to number of shares

In my example strategy, I start out with an initial capital of $50,000 and allow reinvestment of profit or loss.  The protective stop is set as 3 X ATR(10).  A fixed $2000 profit objective is also utilized.  The conversion form Optimal F to position size is illustrated by the following lines of code:

//keep track of biggest loss
biggestLoss = minList(positionProfit(1),biggestLoss);
//calculate the Optimal F with last 10 trades.
OptF = OptimalFGeo(10);
//reinvest profit or loss
risk$ = initCapital$ + netProfit;
//convert Optimal F to $$$
if OptF <> 0 then numShares = risk$ / (biggestLoss / (-1*OptF));
Code snippet - Optimal F to Position Size
  1. Keep track of biggest loss
  2. Calculate optimal F with OptimalFGeo function – minimum 10 trades
  3. Calculate Risk$ by adding InitCapital to current NetProfit (Easylanguage keyword)
  4. Calculate position size by dividing Risk$  by the quotient of biggest loss and (-1) Optimal F

I applied the Optimal F position sizing to a simple mean reversion algorithm where you buy on a break out in the direction of the 50-day moving average after a lower low occurs.

Code listing:

vars: numShares(0),initCapital$(50000),biggestLoss(0),OptF(0),risk$(0);


//keep track of biggest loss
biggestLoss = minList(positionProfit(1),biggestLoss);
//calculate the Optimal F with last 10 trades.
OptF = OptimalFGeo(10);
//reinvest profit or loss
risk$ = initCapital$ + netProfit;
//convert Optimal F to $$$
if OptF <> 0 then numShares = risk$ / (biggestLoss / (-1*OptF));
numShares =  maxList(1,numShares);
//if Optf <> 0 then print(d," ",t," ",risk$ / (biggestLoss / (-1*OptF))," ",biggestLoss," ",optF);

if c > average(c,50) and low < low[1] then Buy numShares shares next bar at open + .25* range stop;

setStopPosition;
setProfitTarget(2000);

setStopLoss(3*avgTrueRange(10)*bigPointValue);
Strategy Using Optimal F

I have included the results below.  At one time during the testing the number of contracts jumped up to 23.  That is 23 mini Nasdaq futures ($20 * 7,300) * 23.  That’s a lot of leverage and risk.  Optimal doesn’t  always mean the best risk mitigation.  Please let me know if you find any errors in the code or in the logic.

 

Here is the ELD that incorporates the Strategy and the Function.USINGOPTIMALF

 

Please follow and like us:
error

A Bar Scoring System – inspired by Keith Fitschen

In Keith’s wonderful book, “Building Reliable Trading Sytems”, he reveals several algorithms that classify an instruments’ movement potential.  In the part of the book that is titled Scoring by a Bar Type Criterion, he describes eight different two-day patterns that involve 3 different criteriaEight different Bar-Types

He looks at the relationship between today’s open and today’s close, today’s close and yesterday’s close, and today’s close in terms of the day’s range.  Bar-Types 1 to 4 all have the close of today >= close of yesterday.  Bar-Types 5 to 8 have close of today < close of yesterday.

I wanted to program this into my TradeStation and do some research to see if the concept is valid.  In his book, Keith tested a lot of different stocks and commodities.  In this post, I just test the ES, US, and Beans.  This form of research can be used to enhance an existing entry technique.

Here is how I defined the eight different bar types:

array : barTypeArray[8](false); 

midRange = (h + l)/2;

barTypeArray[0] = c >= c[1] and c > o and c >= midRange;
barTypeArray[1] = c >= c[1] and c > o and c <  midRange;
barTypeArray[2] = c >= c[1] and c < o and c >= midRange;
barTypeArray[3] = c >= c[1] and c < o and c <  midRange;
barTypeArray[4] = c <  c[1] and c > o and c >= midRange;
barTypeArray[5] = c <  c[1] and c > o and c <  midRange;
barTypeArray[6] = c <  c[1] and c < o and c >= midRange;
barTypeArray[7] = c <  c[1] and c < o and c <= midRange;
Defining Eight Different Bar Types

I used a brute force approach by creating an 8-element array of boolean values.  Remember EasyLanguage uses a 0 index.  If the two -day pattern matches one of the eight criteria I assign the element a true value.  If it doesn’t match then a false value is assigned.  I use an input value to tell the computer which pattern I am looking for.  If I choose Bar-Type[0] and there is a true value in that array element then I take a trade.   By providing this input I can optimize over all the different Bar-Types.

Input : 
	BarTypeNumber(0), // which bar type
	buyOrSell(1),   //1 to buy 2 to sell
	numDaysToHold(2); //how many days to hold position




For cnt = 0 to 7 //remember to start at 0
Begin
	If barTypeArray[cnt] = true then whichBarType = cnt;
end;

If whichBarType = BarTypeNumber then 
begin
 	if buyOrSell = 1 then buy this bar on close;
	if buyOrSell = 2 then sellshort this bar on close;
end;
Loop Thru Array to find Bar Type

Here are some results of looping through all eight Bar-Types, Buy and Sell, and holding from 1 to 5 days.

ES – ten – year results – remember these are hypothetical results with no commission or slippage.

Here’s what the equity curve looks like.   Wild swings lately!!

Beans:

Bonds

Keith was right – look at the Bar Category that bubbled to the top every time – the most counter-trend pattern.  My Bar-Type Number 7  is the same as Keith’s 8.  Here is the code in its entirety.

{Bar Scoring by Keith Fitschen
 from his book "Building Reliable Trading Systems" 2013 Wiley}
 
Input : BarTypeNumber(0),
	 buyOrSell(1),
	 numDaysToHold(2);
	 
vars: midRange(0);
array : barTypeArray[8](false); 

midRange = (h + l)/2;

barTypeArray[0] = c >= c[1] and c > o and c >= midRange;
barTypeArray[1] = c >= c[1] and c > o and c <  midRange;
barTypeArray[2] = c >= c[1] and c < o and c >= midRange;
barTypeArray[3] = c >= c[1] and c < o and c <  midRange;
barTypeArray[4] = c <  c[1] and c > o and c >= midRange;
barTypeArray[5] = c <  c[1] and c > o and c <  midRange;
barTypeArray[6] = c <  c[1] and c < o and c >= midRange;
barTypeArray[7] = c <  c[1] and c < o and c <= midRange;

vars: whichBarType(0),cnt(0);

For cnt = 0 to 7
Begin
	If barTypeArray[cnt] = true then whichBarType = cnt;
end;

If whichBarType = BarTypeNumber then 
begin
 	if buyOrSell = 1 then buy this bar on close;
	if buyOrSell = 2 then sellshort this bar on close;
end;

If barsSinceEntry = numDaysToHold then
begin
	If marketPosition = 1 then sell this bar on close;
	If marketPosition =-1 then buytocover this bar on close;
end;
Bar Scoring Example

Keith’s book is very well researched and written.  Pick one up if you can find one under $500.  I am not kidding.  Check out Amazon.

 

 

Please follow and like us:
error

An ES Day Trading Model Explained – Part 2

This is a continuation post or Part 2 of the development of the ES day trading system with EasyLanguage.

If you can understand this model you can basically program any of your day trading ideas.

Inputs Again:

First I want to revisit our list of inputs and make a couple of changes before proceeding.

inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2); 
inputs: volStopPer(.7),Stop$(500); 
inputs: volThreshPer(.3),ProfThresh$(250);
inputs: volTrailPer(.2),Trail$(200);
inputs: endTradingTime(1500);
Modification to our inputs

If we want to optimize these values then we can’t use the keyword bigPointValue in the input variable default value.  So I removed them – also I added an input endTradingTime(1500).  I wanted to cut off our trading at a given time – no use entering a trade five minutes prior to the closing.

Disengage the Vol or $Dollar Trade Management:

I may have muddied the waters a little with having volatility and $ values simultaneously.  You can use either for the initial protective stop, profit threshold, and trailing stop amount.  You can disengage them by using large values.  If you want to ignore all the $ inputs just add a couple of 00 to each of the values:

Stop$(50000), ProfThres$(25000),Trail$(50000)

If you want to ignore the volatility trade management stops just put a large number in front of the decimal.

volStopPer(9.7), volThreshPer(9.3), volTrailPer(9.2)

If you make either set large then the algorithm will use the values closest to the current market price.

Computations:

Let’s now take a look at the computations that are done on the first bar of the day:

If d <> d[1] then
Begin
	rangeSum = 0.0;  // range calculation for entry
	For iCnt = 1 to volCalcLen
	Begin
		rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
	end;
	vol = rangeSum/volCalcLen;
	buyPoint =  openD(0) + vol*orboBuyPer; 
	sellPoint = openD(0) - vol*orboSellPer; 
	
	longStopAmt = vol * volStopPer; 
	longStopAmt = minList(longStopAmt,Stop$/bigPointValue);
	
	shortStopAmt = vol *volStopPer; 
	shortStopAmt = minList(shortStopAmt,Stop$/bigPointValue);

	longThreshAmt = vol  * volThreshPer;
	longThreshAmt = minList(longThreshAmt,ProfThresh$/bigPointValue);
	
	shortThreshAmt = vol * volThreshPer;
	shortThreshAmt = minList(shortThreshAmt,ProfThresh$/bigPointValue);
	
	longTrailAmt = vol * volTrailPer;
	longTrailAmt = minList(longTrailAmt,Trail$/bigPointValue);
	
	shortTrailAmt = vol * volTrailPer;
	shortTrailAmt = minList(shortTrailAmt,Trail$/bigPointValue);
	
	longTrailLevel = 0;
	shortTrailLevel = 999999;
	buysToday = 0;
	shortsToday = 0;
end;
Once a day computations

I determine it is the first bar of the day by comparing the current 5-minute bar’s date stamp to the prior 5-minute bar’s date stamp.  If they are not the same then you have the first bar of the day.  The first thing I do is calculate the volatility of the current market by using a for-loop to accumulate the day ranges for the past volCalcLen days.  I start the iterative process using the iCnt index and going from 1 back in time to volCalcLen.  I use iCnt to index into the function calls HighD and LowD.  Indexing is not really the right word here – that is more appropriate when working with arrays.  HighD and LowD are functions and we are passing the values 1 to volCalcLen into the functions and summing their output.  When you do this you will get a warning “A series function should not be called more than once with a given set of parameters.”  Sounds scary but it seems to work just fine.  If you don’t do this then you have to include a daily bar on the chart.  I like to keep things as simple as possible.   Once I sum up the daily ranges I then divide by volCalcLen to get the average range over the few days.  All of the vol based variables will use this value.

Entries:

Entry is based off a move away from the opening in terms of volatility.  If we use 0.2 (twenty percent) as orboBuyPer then the algorithm will buy on a stop 20% of the average range above the open tick.  Sell short is just the opposite.   We further calculate the longStopAmt as a function of vol and a pure $ amount.  I am using the minList function to determine the smaller of the two values  .This is how I disengage either the vol value or the $ value.  The other variables are also calculated just once a day: shortStopAmt, longThreshAmt, shortThreshAmt, longTrailAmt, shortTrailAmt. You could calculate every bar but that would be inefficient. I am also resetting four values at the beginning of the day:  longTrailLevel, shortTrailLevel, buysToday and shortsToday.

 

The Mighty MP:

mp = marketPosition;

If mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;
If mp = -1 and mp[1] <> -1 then shortsToday = shortsToday + 1;
MarketPosition monitoring and determining Buys/Shorts Today

I like using a variable to store each bar’s marketPosition.  In this case I am using MP.  By aliasing the marketPosition function call to a variable allows us to do this:

if mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;

This little line does a bunch of stuff.  If the current bar’s position is 1 and the prior bars position is not one then we know we have just entered a long position.  So every time this happens throughout the day the buysToday is incremented.  ShortsToday works just the same.  Pitfall warning:  If you strategy enters and exits on the same bar then this functionality will not work!  Neither will the call to the marketPosition function.  It will look like nothing happened.  If you need to keep track of the number of trades make sure you can only enter or exit on different bars.  If you stuff is so tight then drop down to a 1 minute or tick bar.

Controlling the Nmber of Buys/Shorts for the Day:

if time < endTradingTime and buysToday < 1 then Buy("ORBo-B") next bar at buyPoint stop;
if time < endTradingTime and shortsToday < 1 then Sellshort("ORBo-S") next bar at sellPoint stop;	  

	
If marketposition = 1 then
Begin 
	longExitPoint = entryPrice - longStopAmt;
	sell("L-Exit") next bar at longExitPoint stop;
end;

If marketposition = -1 then
Begin
	shortExitPoint = entryPrice + shortStopAmt;
	buyToCover("S-Exit") next bar at shortExitPoint stop;
end;

If marketPosition = 1 and maxContractProfit/bigPointValue >= longThreshAmt then
Begin
	longTrailLevel = maxList(highest(h,barsSinceEntry) - longTrailAmt,longTrailLevel);
	sell("TrailSell") next bar at longTrailLevel stop;
end;

If marketPosition = -1 and maxContractProfit/bigPointValue >= shortThreshAmt then
Begin
	shortTrailLevel = minList(lowest(l,barsSinceEntry) + shortTrailAmt,ShortTraillevel);
	buyToCover("TrailCover") next bar at shortTrailLevel stop;
end;


SetExitOnClose;
Controlled trade directives

Notice how I am controlling the trade directives using the if statements.  I only want to enter a long position when the time is right and I haven’t already entered a long position for the day.  If you don’t control the trade directives, then these orders are placed on every bar, in our case every 5-minutes.  If you have pyramiding turned off then once you are long the buy directive is ignored.  This is an important concept – let’s say you just want to buy and short only one time per day trade session.  If you don’t control this directive, then it will fire off an order every five minutes.    You don’t want this -at least I hope you don’t.

So controlling the time and number of entries is paramount.  If you don’t control the time of entry then the day can arrive at the last bar of the day and fire off an order for the opening of the next day.  A big no, no !

Put To Work:

Here are the inputs I used to generate the trades in the graphic that follows.

Not Doing Exactly What You Want:

Here is what most day traders are looking for.   I made a comment on the chart – make sure you read it – it is another pitfall.

The trailing stop had to wait for the bar to complete to determine if the profit reached the threshold.  A little slippage here.  You can overcome this if you use the BuiltIn Percent Trailing Strategy or by using the SetPercentTrailing function call.

However, you lose the ability to really customize your algorithms by using the builtin functionalility.  You could drop down to a one minute bar and probably get out nearer your trailing stop amount.

Download the ELD:

Here you go!

GEODAYTRADERV1.01

Please follow and like us:
error

An ES Day Trading Model Explained – Part 1

Open Range BreakOut with Trade Management

How difficult is it to program a day trading system using an open range break out, protective stop and a trailing stop?  Let’s see.  I started working on this and it got a little more detailed than I wanted so I have now split it up into two posts.  Part 2 will follow very soon.

What inputs do we need?  How about the number of days used in the volatility calculation?  What percentage of the volatility from the open do you want to buy or sell?  Should we have a different value for buys and sells?  Do we want to use a volatility based protective stop or a fixed dollar?  How about a trailing stop?  Should we wait to get to a certain profit level before engaging the trailing stop?  Should it also be based on volatility or fixed dollar amt?  How much should we trail – again an amount of volatility or fixed dollar?

Proposed inputs:

inputs: volCalcLen(10), orboBuyPer(.2), orboSellPer(.2), volStopPer(.7), $Stop(500), volProfThreshPer(.5), $ProfThresh(250),volTrailPer(.2),$Trail(200);

That should do it for the inputs – we can change later if necessary.

Possible pitfalls:

This is where I will save you some time.  If we use an open range break out entry we must limit the number of entries or TradeStation will continue to execute as long as the price is above our buy level.  You might ask, “That’s what we want -right?”  What if we use a tight stop and we get stopped out of our first position.  Do you want to buy again later in the day?  What if we use a trailing stop and we get out of the market above the buy level.  What will TradeStation do?  It will follow your exact instructions and buy again if you don’t control the number of allowed entries.  Do you want to carry the buy and sell stops overnight for execution on the open of the next day – probably not!  So we not only need to control the number of entries buy we also need to control the time period we can enter a trade.

Calculations:

We need to determine the volatility and a good way do this is calculating the average range over the past N days.   There are two ways to do this: 1) incorporate a daily chart as data2 and use a built-in function for the calculation or 2) use a for-loop and use the built-in functions HighD and LowD and just use one data feed.   Both have their drawbacks.  The first is you need to have a multi-data chart and the second you get a warning that you shouldn’t put a series function call inside the body of a for-loop.   I have done it both ways and I prefer to deal with the warning – so far it has worked out nicely – so let’s go with a single data chart.

Building the code:

Inputs:

Since we are combining a volatility and fixed $ amount in our trade management, you will need to set either the vol or dollar amounts to a high value to disable them.  You can use both but I am taking the smaller of the respective values.

inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2); 
inputs: volStopPer(.7),$Stop(500/bigPointValue); 
inputs: volThreshPer(.5),$ProfThresh(250/bigPointValue);
inputs: volTrailPer(.2),$Trail(200/bigPointValue);
Inputs We Will Need - Can Changer Later

Variables:

vars:vol(0),buyPoint(0),sellPoint(0),
	longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),
	longThreshAmt(0),shortThreshAmt(0),
	longTrailAmt(0),shortTrailAmt(0),
	longTrailLevel(0),shortTrailLevel(0),
	hiSinceLong(0),loSinceShort(0),mp(0),
	rangeSum(0),iCnt(0),
	buysToday(0),shortsToday(0)
Variables That We Might Need

Once A Day Calculations:

Since we will be working with five-minute bars we don’t want to do daily calculations on each bar.  If we do it will slow down the process.  So let’s do these calculation on the first bar of the day only.

If d <> d[1] then
Begin
	rangeSum = 0.0;  // range calculation for entry
	For iCnt = 1 to volCalcLen
	Begin
		rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
	end;
	vol = rangeSum/volCalcLen;
	buyPoint =  openD(0) + vol*orboBuyPer; 
	sellPoint = openD(0) - vol*orboSellPer; 
	
	longStopAmt = vol * volStopPer; 
	longStopAmt = minList(longStopAmt,Stop$);
	
	shortStopAmt = vol *volStopPer; 
	shortStopAmt = minList(shortStopAmt,Stop$);

	longThreshAmt = vol  * volThreshPer;
	longThreshAmt = minList(longThreshAmt,ProfThresh$);
	
	shortThreshAmt = vol * volThreshPer;
	shortThreshAmt = minList(shortThreshAmt,ProfThresh$);
	
	longTrailAmt = vol * volTrailPer;
	longTrailAmt = minList(longTrailAmt,Trail$);
	
	shortTrailAmt = vol * volTrailPer;
	shortTrailAmt = minList(shortTrailAmt,Trail$);
	
	longTrailLevel = 0;
	shortTrailLevel = 999999;
	buysToday = 0;
	shortsToday = 0;
end;
Do These Just Once A Day

 

For All of You Who Don’t Want To Wait – Beta Version Is Available Below:

In my next post, I will dissect the following code for a better understanding.  Sorry I just ran out of time.

inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2); 
inputs: volStopPer(.7),Stop$(500/bigPointValue); 
inputs: volThreshPer(.3),ProfThresh$(250/bigPointValue);
inputs: volTrailPer(.2),Trail$(200/bigPointValue);


vars:vol(0),buyPoint(0),sellPoint(0),
	longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),
	longThreshAmt(0),shortThreshAmt(0),
	longTrailAmt(0),shortTrailAmt(0),
	longTrailLevel(0),shortTrailLevel(0),
	hiSinceLong(0),loSinceShort(0),mp(0),
	rangeSum(0),iCnt(0),
	buysToday(0),shortsToday(0);

If d <> d[1] then
Begin
	rangeSum = 0.0;  // range calculation for entry
	For iCnt = 1 to volCalcLen
	Begin
		rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
	end;
	vol = rangeSum/volCalcLen;
	buyPoint =  openD(0) + vol*orboBuyPer; 
	sellPoint = openD(0) - vol*orboSellPer; 
	
	longStopAmt = vol * volStopPer; 
	longStopAmt = minList(longStopAmt,Stop$);
	
	shortStopAmt = vol *volStopPer; 
	shortStopAmt = minList(shortStopAmt,Stop$);

	longThreshAmt = vol  * volThreshPer;
	longThreshAmt = minList(longThreshAmt,ProfThresh$);
	
	shortThreshAmt = vol * volThreshPer;
	shortThreshAmt = minList(shortThreshAmt,ProfThresh$);
	
	longTrailAmt = vol * volTrailPer;
	longTrailAmt = minList(longTrailAmt,Trail$);
	
	shortTrailAmt = vol * volTrailPer;
	shortTrailAmt = minList(shortTrailAmt,Trail$);
	
	longTrailLevel = 0;
	shortTrailLevel = 999999;
	buysToday = 0;
	shortsToday = 0;
end;

mp = marketPosition;

If mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;
If mp = -1 and mp[1] <> -1 then shortsToday = shortsToday + 1;

if time < sessionendTime(0,1) and buysToday < 1 then Buy("ORBo-B") next bar at buyPoint stop;
if time < sessionendTime(0,1) and shortsToday < 1 then Sellshort("ORBo-S") next bar at sellPoint stop;	  

	
If marketposition = 1 then
Begin 
	longExitPoint = entryPrice - longStopAmt;
	sell("L-Exit") next bar at longExitPoint stop;
end;

If marketposition = -1 then
Begin
	shortExitPoint = entryPrice + shortStopAmt;
	buyToCover("S-Exit") next bar at shortExitPoint stop;
end;

If marketPosition = 1 and maxContractProfit/bigPointValue >= longThreshAmt then
Begin
	longTrailLevel = maxList(highest(h,barsSinceEntry) - longTrailAmt,longTrailLevel);
	sell("TrailSell") next bar at longTrailLevel stop;
end;

If marketPosition = -1 and maxContractProfit/bigPointValue >= shortThreshAmt then
Begin
	shortTrailLevel = minList(lowest(l,barsSinceEntry) + shortTrailAmt,ShortTraillevel);
	buyToCover("TrailCover") next bar at shortTrailLevel stop;
end;


SetExitOnClose;
Beta Version - I will clean up later and post it

 

Please follow and like us:
error

The Perfect System

WARNING CHEATING AHEAD 😉 There is no Holy Grail!

Having the results of a Perfect Profit System for any given market can be helpful when trying to determine the quality and viability  of one’s own algorithm (ala Bob PardoThe Evaluation and Optimization of Trading Strategies).  The level of correlation between a user defined algorithm and the PPS can indicate the quality of an algorithm – if its not been overly curve-fit.

Michael Bryant of AdapTrade.com has written an excellent article on how to use the correlation between a user defined algo and the PPS to optimize the algo to increase the correlation co-efficient between the two.  The premise being the closer you get to the PPS with your own strategy the better off you are.

Click here for Michael Bryant’s excellent article.

Bob Pardo’s perfect system simply accumulated the absolute differences between consecutive closes to create the PPS equity stream.  In Michael Bryant’s article he utilizes price swings based of either price points or fractions of ATR (average true range.)  Bryant’s version takes advantage of 20/20 hindsight and creates a more realistic trade count.  Bob’s version would trade every day.  Michael’s version trades much, much, less.

My version, with the benefit of hindsight, picks the highest close and lowest close of every month and uses the data to sellShort and buy at these levels respectively.  I wanted to actually show the trades on the charts as they maybe helpful as well.  So to do this you have to run two strategies : 1) one that picks and outputs the trades and 2) one that takes the output from one and executes the trades.

There were two ways to accomplish this : 1) calculate the monthly turning points and output the data to  a file and then insert the data as 3rd party index into another chart of the same market and program a strategy that interprets the index or 2) calculate the monthly turning points and output the information as pure EasyLanguage code snippet and paste that code into another strategy.  I chose the second method as it also helps show the reader how to do some really cool stuff.

Before I started to program this research tool I asked myself “How can you pick the monthly peaks and valleys as you run through the data?”  Well I knew I had to be at the end of the month and then look back enough bars to choose these points.  Since the number of trading days in a month are variable I knew I had to use a while loop.  While loops are really cool and easy to use, but you must be careful or you will land in Steve Jobs 1 infinite loop.  TradeStation is very patient and will let you know before crashing and burning though.  Here is the snippet of code that loops back in time to gather the monthly peaks and valleys:

If month(d) <> month(d[1]) then // are we in a new month?
Begin
	Value1 = 1;
	hiMonthPrice = 0;
	loMonthPrice = 999999;
	testMonth = month(date); 	
	While month(d[value1]) = month(d[value1+1]) //loop until the prior month
	Begin
 		If c[value1] > hiMonthPrice then
 		Begin
 			hiMonthPrice = c[value1]; //store the peak price
 			hiMonthBar = value1; // store the peak bar number
 		end;
 		If c[value1] < loMonthPrice then
 		Begin
 			loMonthPrice = c[value1]; //store the valley price
 			loMonthBar = value1; // store the valley bar number
 		end;
 		Value1 = value1 + 1;
	end;
//	print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
//	print(d," monthLo ",loMonthPrice," ",loMonthBar);
Looping Backward - Collecting Peaks 'n Valleys

Okay we now have the prior month’s peak and valley values.  So we need to store this information into a list.  EasyLanguage allow for user defined arrays, which are really just indexable  (I think that is a word) lists.  Here is a list of arrays that we will be using and how they are declared:


 Array: equPerf[5000](0),  { equity at each bar, perfect trades }
        trIdeal[2000](0),      { equity at end of perfect trades }
        datesPerf[2000](0),   { dates of perfect trades }
        BorSPerf[2000](0),  { buys or sells of perfect trades}
        pricePerf[2000](0); { prices of perfect trades}
I over dimensioned these Arrays - 2 per month right?

To declare an array you use the Keyword array and then give the array a name and then use square brackets to tell the computer the maximum number of elements in the array then instantiate each array element to a value that is surrounded by parenthesis.  Here we assigned each value in the arrays to zero.

Now that we have our lists and we are running through the chart and stopping at the end of each month and collecting peaks and valleys how do we turn this into an EasyLanguage strategy?  I had to think about this which I usually don’t do I just start programming.  I did just start programming but I didn’t get the results I wanted so then I had to think about it.  I knew this strategy needed to be in the market 100% of the time so it would be a stop and reverse system.  The first trade would be important so I needed to know which occurred first in the month – the peak or the valley.  If it was a peak then the first trade would be a sell and the second would be a buy, since we are simply selling peaks and buying valleys.  I then extrapolated this idea out to each month.  If I exited a month in a long position than I would want the following month’s valley to be first.  In a perfect would it would be peak, valley, peak, valley and so on.  That doesn’t always happen.  Sometimes you buy a valley late in a month and the market starts off the next month in a decline.  So you have to wait for the monthly peak to sell short and this could lead to a sizable loss – remember we are looking for an Almost Perfect System.  I could have programmed it so that the  buying late in the month and subsequent market decline scenario didn’t occur, but it would require eliminating some trades. Since there’s not that many trades to start with I decided to go a different route.  So what I did was program an exception when there was a flux in the peak-valley continuum.  If I was exiting the month in a long position and a valley preceded a peak in the following month I simply reversed my position on the first day of the month.  Easy fix right?  At the beginning of the month I wanted all my trades set up from the preceding  month – I did this to just make things simpler.  Here is the snippet of code that puts the data into the arrays we will use later to create the EasyLanguage code:

If tempBors = 1 then
	Begin
		If hiMonthBar > loMonthBar then
		Begin  // long and peak occurs first
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];	
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
		end
		else
		begin // long and valley occurs first
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[value1];
			pricePerf[trdCnt] = c[value1];
			print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;	
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;	
		end;
	end;
Decision Process When Coming Into Month Long

Once you go through the entire chart you should have all of you trade arrays set up.  So on the last bar of the chart you need to print this data out to a file.  But you want to print it out so you can simply copy and paste the output into another strategy – in other words you want to be syntactically correct.

Use the keyword LastBarOnChart to predicate the output process.  You can only print out strings to a file in EasyLanguage so all numbers have to be converted to strings – use NumToStr to accomplish this feat.  I think I have discussed formatted output somewhere in a post, but here is a quick reminder.  Let’s say you want to convert 1234.5678 to the string 1234.56, you would use NumToStr(value1 , 2).  The second argument in the function call informs the computer to truncate the decimal to two places. If you want to concatenate multiple strings together (i.e. connect strings together) you simply use the + sign.  Look at this line of code:

StrOut = “DateArr[“+NumtoStr(value1,0)+”] = ” + numToStr(19000000+datesPerf[value1], 0) + “;TradeArr[“+numtoStr(value1,0)+”] = ” +numToStr(BorSPerf[value1],0) + “;PriceArr[“+NumtoStr(value1,0)+”] = ” + numToStr(pricePerf[value1],2) + “;”+ Newline;

This is what it creates:

DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;

This is a neat line of code where I mix hard coded word strings with converted numerical values to strings to create syntactically correct EasyLanguage in one complete, albeit long, string.

Now all you need now is an interpreter strategy.  The output from the first pass program creates the arrays needed for the interpreter program and then outputs all of the trades as pre-filled arrays.  Here is the output program in its entirety:

 input:  FName       ("c:\PerfectSystem\output.txt");     { file name to write results to }
 
 Var:  HighBar(0), LowBar(0),ibar(0), StrOut(""), 
       BorS  (0),daysInEquity(0),equCnt(0),dataIndex(0),tempMP(0),trdCnt(0),trdNum(0),
       oteEqu(0),clsTrdEqu(0), hiMonthPrice(0),hiMonthBar(0),
       loMonthPrice(0),loMonthBar(0),testMonth(0),firstTime(true);   
 
 Array: equPerf[5000](0),trPerf[2000](0),datesPerf[2000](0),   
        BorSPerf[2000](0),pricePerf[2000](0);
 
If month(d) <> month(d[1]) then // are we in a new month
Begin
	Value1 = 1;
	hiMonthPrice = 0;
	loMonthPrice = 999999;
	testMonth = month(date); 	
	While month(d[value1]) = month(d[value1+1]) //loop until the prior month
	Begin
 		If c[value1] > hiMonthPrice then
 		Begin
 			hiMonthPrice = c[value1]; //store the peak price
 			hiMonthBar = value1; // store the peak bar number
 		end;
 		If c[value1] < loMonthPrice then
 		Begin
 			loMonthPrice = c[value1]; //store the valley price
 			loMonthBar = value1; // store the valley bar number
 		end;
 		Value1 = value1 + 1;
	end;
//	print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
//	print(d," monthLo ",loMonthPrice," ",loMonthBar);
	
	vars: tempBorS(0);
	If firstTime then
	begin
	    tempBorS = -1;
		If hiMonthBar > loMonthBar then tempBorS = 1;
		trdCnt = 1;
		firstTime = false;
	end
	else
	begin
		tempBorS = BorSPerf[trdCnt-1];
	end;
	
	If tempBors = 1 then
	Begin
		If hiMonthBar > loMonthBar then
		Begin  // long and peak occurs first
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];	
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
		end
		else
		begin // long and valley occurs first
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[value1];
			pricePerf[trdCnt] = c[value1];
			print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;	
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;	
		end;
	end;
	If tempBors = -1 then
	Begin
		If loMonthBar > hiMonthBar then
		Begin
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt+ 1;	
		end
		else
		begin
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[value1];
			pricePerf[trdCnt] = c[value1];
			print(datesPerf[trdCnt]," Buy Conflict ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = -1;
			datesPerf[trdCnt] = d[hiMonthBar];
			pricePerf[trdCnt] = c[hiMonthBar];
			print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
			trdCnt = trdCnt + 1;
			BorSPerf[trdCnt]  = 1;
			datesPerf[trdCnt] = d[loMonthBar];
			pricePerf[trdCnt] = c[loMonthBar];
			print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);	
			trdCnt = trdCnt + 1;		
		end;
	end;
	
end;
			
tempMP = 0;
vars:cntDown(0);
If lastBarOnChart then
Begin	
	FileAppend(FName, NewLine + "arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);" + NewLine);
	For value1 = 1 to trdCnt
	Begin
   		StrOut = "DateArr["+NumtoStr(value1,0)+"] = " + numToStr(19000000+datesPerf[value1], 0) + ";TradeArr["+numtoStr(value1,0)+"] = " +numToStr(BorSPerf[value1],0) + ";PriceArr["+NumtoStr(value1,0)+"] = " + numToStr(pricePerf[value1],2) + ";"+ Newline;
   		FileAppend(FName, StrOut);
	end;
end;
Almost Perfect System EasyLanguage Creator Code

 

Here’s the output that was created when this strategy as was added to a 19 year long ES daily bar chart:

arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;
DateArr[11] = 20010402;TradeArr[11] = -1;PriceArr[11] = 1100.75;
DateArr[12] = 20010403;TradeArr[12] = 1;PriceArr[12] = 1059.25;
DateArr[13] = 20010419;TradeArr[13] = -1;PriceArr[13] = 1209.75;
DateArr[14] = 20010501;TradeArr[14] = 1;PriceArr[14] = 1223.50;
DateArr[15] = 20010521;TradeArr[15] = -1;PriceArr[15] = 1265.50;
DateArr[16] = 20010530;TradeArr[16] = 1;PriceArr[16] = 1201.00;
Sample OutPut

Here is the interpreter strategy code.  I simply copied and pasted the output from the file as the header to this strategy.

arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);//
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;// All these lines
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;//  created by Perfect
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;// System Out Put
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;//
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;//
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;//
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;//
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;//
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;//
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;//
...
...
...
...
DateArr[539] = 20180920;TradeArr[539] = -1;PriceArr[539] = 2939.50;//


vars: cnt(1);

If date +19000000 = DateArr[cnt]  then
Begin 
	print("Found Date : ",date);
	If tradeArr[cnt] = 1 then
	begin
		buy this bar on close;
	end;
	If tradeArr[cnt] = 2 then
	begin
		sell this bar on close;
	end;
	If tradeArr[cnt] = -1 then
	begin
		sellShort this bar on close;
	end;
	If tradeArr[cnt] = -2 then
	begin
		buyToCover this bar on close;
	end;
	cnt = cnt + 1;
	If DateArr[cnt] = DateArr[cnt-1] then
	Begin
		print("two trades same day ",d," ",dateArr[cnt]);
		If tradeArr[cnt] = 1 then
		begin
			buy this bar on close ;
		end;
		If tradeArr[cnt] = 2 then
		begin
			sell this bar on close;
		end;
		If tradeArr[cnt] = -1 then
		begin
	    	print("looking to go short at ",PriceArr[cnt]);
			sellShort this bar on close;
		end;
		If tradeArr[cnt] = -2 then
		begin
			buyToCover this bar on close;
		end;
		cnt = cnt + 1;
	end;	
end;
Almost Perfect System Interpreter Code

I think I discussed how this code works in a prior post.  If I go back and see that I didn’t I will edit and update this post.  So how did it do?

Here is a zip file that contains both ELDs for the strategies : PERFECTSYSTEM

Please follow and like us:
error

Using TradeStation’s COT Indicator to Develop a Trading System

TradeStation’s COT (Commitment of Traders) Indicator:

TradeStation COT Indicator

TradeStation now includes the historic COT (Commitment of Traders) report in the form of an indicator.

If you can plot it then you can use it in a Strategy.  The following code listing takes the Indicator code and with very few modifications turns it into a trading system.

{
Net positions of various groups of traders from the CFTC's weekly Commitments of
Traders report.  "Net" positions are calculated by taking the number of contracts
that a group of traders is long and subtracting the number of contracts that that
group of traders is short.

The user input "FuturesOnly_Or_FuturesAndOptions_1_or_2" determines whether the
CFTC's "Futures Only" report is used, or the "Futures and Options" report is
used to determine the positions of the various groups of traders.  By default, the
"Futures Only" report is used.

Plot1:  Commercial traders' net position
Plot2:  Non-commercial traders' net position
Plot3:  Speculators' net positions, for speculators not of reportable size
Plot4:  Zero line

If an error occurs retrieving one of the values used by this study, or if the value
is not applicable or non-meaningful, a blank cell will be displayed in RadarScreen or
in the OptionStation assets pane.  In a chart, no value will be plotted until a value
is obtained without generating an error when retrieved.
}

input:  FuturesOnly_Or_FuturesAndOptions_1_or_2( 1 ) ; { set to 1 to use the CFTC's
 "Futures Only" report, set to 2 (or to any value other than 1) to use the "Futures
 and Options" report }

variables:
	Initialized( false ),
	FieldNamePrefix( "" ),
	CommLongFieldNme( "" ),
	CommShortFieldNme( "" ),
	NonCommLongFieldNme( "" ),
	NonCommShortFieldNme( "" ),
	SpecLongFieldNme( "" ),
 	SpecShortFieldNme( "" ),
    CommLong( 0 ),
	oCommLongErr( 0 ),
	CommShort( 0 ),
	oCommShortErr( 0 ),
	NonCommLong( 0 ),
	oNonCommLongErr( 0 ),
	NonCommShort( 0 ),
	oNonCommShortErr( 0 ),
	SpecLong( 0 ),
	oSpecLongErr( 0 ),
	SpecShort( 0 ),
	oSpecShortErr( 0 ),
	CommNet( 0 ),
	NonCommNet( 0 ),
	SpecNet( 0 ) ;

if Initialized = false then
	begin
	if Category > 0 then
		RaiseRuntimeError( "Commitments of Traders studies can be applied only to" +
		 " futures symbols." ) ;
	Initialized = true ;
	FieldNamePrefix = IffString( FuturesOnly_Or_FuturesAndOptions_1_or_2 = 1,
	 "COTF-", "COTC-" ) ;
	CommLongFieldNme = FieldNamePrefix + "12" ;
	CommShortFieldNme = FieldNamePrefix + "13" ;
	NonCommLongFieldNme = FieldNamePrefix + "9" ;
	NonCommShortFieldNme = FieldNamePrefix + "10" ;
	SpecLongFieldNme = FieldNamePrefix + "16" ;
 	SpecShortFieldNme = FieldNamePrefix + "17" ;	
	end ;

CommLong = FundValue( CommLongFieldNme, 0, oCommLongErr ) ;
CommShort = FundValue( CommShortFieldNme, 0, oCommShortErr) ;
NonCommLong = FundValue( NonCommLongFieldNme, 0, oNonCommLongErr ) ;
NonCommShort = FundValue( NonCommShortFieldNme, 0, oNonCommShortErr );
SpecLong = FundValue( SpecLongFieldNme, 0, oSpecLongErr ) ; 
SpecShort = FundValue( SpecShortFieldNme, 0, oSpecShortErr ) ;

if oCommLongErr = fdrOk and oCommShortErr = fdrOk then
	begin
	CommNet = CommLong - CommShort ;
	Print ("CommNet ",commNet);
	end ;

if oNonCommLongErr = fdrOk and oNonCommShortErr = fdrOk then
	begin
	NonCommNet = NonCommLong - NonCommShort ;
	end ;

if oSpecLongErr = fdrOk and oSpecShortErr = fdrOk then
	begin
	SpecNet = SpecLong - SpecShort ;
	end ;
If CommNet < 0  then sellShort tomorrow at open;
If CommNet > 0 then buy tomorrow at open;


{ ** Copyright (c) 2001 - 2010 TradeStation Technologies, Inc. All rights reserved. ** 
  ** TradeStation reserves the right to modify or overwrite this analysis technique 
     with each release. ** }
COT Indicator Converted To Strategy

Line numbers 90 and 91 informs TS to take a long position if the Net Commercial Interests are positive and a short position if the Commercials are negative.  I kept the original comments in place in  case you wanted to see how the indicator and its associated function calls work.  The linchpin of this code lies in the function call FundValue.  This function call pulls fundamental data from the data servers and provides it in an easy to use format.  Once you have the data you can play all sorts of games with it.  This is just a simple system to see if the commercial traders really do know which direction the market is heading.

if you test this strategy on the ES you will notice a downward sloping 45 degree equity curve.  This leads me to believe the commercials are trying their best to  use the ES futures to hedge other market positions.  If you go with the non Commercials you will see  a totally different picture.  To do this just substitute the following two lines:

If CommNet < 0 then sellShort tomorrow at open;
If CommNet > 0 then buy tomorrow at open;

With:

If NonCommNet < 0 then sellShort tomorrow at open;
If NonCommNet > 0 then buy tomorrow at open;

I said a totally different picture not a great one.  Check out if the speculators know better.

Please follow and like us:
error

Anatomy Of Mean Reversion in EasyLanguage

Look at this equity curve:

As long as you are in a bull market buying dips can be very consistent and profitable.  But you want to use some type of entry signal and trade management other than just buying a dip and selling a rally.  Here is the anatomy of a mean reversion trading algorithm that might introduce some code that you aren’t familiar.  Scroll through the code and I will  summarize below.

inputs: mavlen(200),rsiLen(2),rsiBuyVal(20),rsiSellVal(80),holdPeriod(5),stopLoss$(4500);
vars: iCnt(0),dontCatchFallingKnife(false),meanRevBuy(false),meanRevSell(false),consecUpClose(2),consecDnClose(2);

Condition1 = c > average(c,mavLen);

Condition2 = rsi(c,rsiLen) < rsiBuyVal;
Condition3 = rsi(c,rsiLen) > rsiSellVal;


Value1 = 0;
Value2 = 0;

For iCnt = 0 to consecUpClose - 1 
Begin
	value1 = value1 + iff(c[iCnt] > c[iCnt+1],1,0);
end;

For iCnt = 0 to consecDnClose - 1 
Begin
	Value2 = value2 + iff(c[iCnt] < c[iCnt+1],1,0);
end;

dontCatchFallingKnife = absValue(C - c[1]) < avgTrueRange(10)*2.0;

meanRevBuy = condition1 and condition2 and dontCatchFallingKnife;
meanRevSell =  not(condition1) and condition3 and dontCatchFallingKnife;

If meanRevBuy then buy this bar on close;
If marketPosition = 1 and condition1 and value1 >= consecUpClose then sell("ConsecUpCls") this bar on close;

If meanRevSell then sellShort this bar on close;
If marketPosition = -1 and not(condition1) and value2 >= consecDnClose then buyToCover this bar close;

setStopLoss(stopLoss$);


If barsSinceEntry = holdPeriod then
Begin
	if marketPosition = 1 and not(meanRevBuy) then sell this bar on close;
	if marketPosition =-1 and not(meanRevSell) then buytocover this bar on close;
end;
Mean Reversion System

I am using a very short term RSI indicator, a la Connors, to initiate long trades.  Basically when the 2 period RSI dips below 30 and the close is above the 200-day moving average I will buy only if I am not buying “a falling knife.”  In February several Mean Reversion algos kept buying as the market fell and eventually got stopped out with large losses.  Had they held on they probably would have been OK.  Here I don’t buy if the absolute price difference between today’s close and yesterday’s is greater than 2 X the ten day average true range.  Stay away from too much “VOL.”

Once a trade is put on I use the following logic to keep track of consecutive closing relationships:

For iCnt = 0 to consecUpClose - 1 
Begin
	value1 = value1 + iff(c[iCnt] > c[iCnt+1],1,0);
end;
Using the IFF function in EasyLanguage

Here I am using the IFF function to compare today’s close with the prior day’s.  iCnt is a loop counter that goes from 0 to 1. IFF checks the comparison and if it’s true it returns the first value after the comparison and if false it returns the last value.  Here if I have two consecutive up closes value1 accumulates to 2.  If I am long and I have two up closes I get out.  With this template you can easily change this by modifying the input:  consecUpClose.  Trade management also includes a protective stop and a time based exit.  If six days transpire without two up closes then the system gets out – if the market can’t muster two positive closes, then its probably not going to go anywhere.  The thing with mean reversion, more so with other types of systems, is the use or non use of a protective stop.  Wide stops are really best, because you are betting on the market to revert.  Look at the discrepancy of results using different stop levels on this system:

Here an $1,800 stop only cut the max draw down by $1,575.  But it came at a cost of $17K in profit.  Stops, in the case of Mean Reversion, are really used for the comfort of the trader.

This code has the major components necessary to create a complete trading system.  Play around with the code and see if you can come up with a better entry mechanism.

Please follow and like us:
error