Category Archives: Free EasyLanguage

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

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

 

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

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.

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.

Updated Pattern Smasher in EasyLanguage

Update To Original Pattern Smasher

What will you learn : string manipulation, for-loops, optimization

Before proceeding I would suggest reading my original post on this subject.    If you believe the relationship of the last few bars of data can help determine future market direction, then this post will be in you wheel house.  Another added benefit is that you will also learn some cool EasyLanguage.

Original post was limited to four day patterns!

This version is limitless (well not really, but pretty close).  Let’s stick with the original string pattern nomenclature (+ + – – : two up closes followed by two down closes.)  Let’s also stick with our binary pattern representation:

Pattern # 2^3 2^2 2^1 1
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 1

Remember a 0 represents a down close and a 1 represents an up close.  We will deviate from the original post by doing away with the array and stick with only strings (which are really just arrays of characters.)  This way we won’t have to worry about array manipulation.

How to create a dynamic length string pattern

This was the difficult part of the programming.  I wanted to be able to optimize 3, 4 and 5 day patterns and I wanted to control this with using just inputs.  I discovered that pattern three is different in a three day pattern than it is in a four day pattern: in a three day pattern it is 011 or – + + and in a four day pattern it is 0011 or – – + +.  Since I am counting 0’s as down closes, pattern #3 depends on the ultimate size of the pattern string.  No worries I will have eventually have another version where I utilize a different value for down closes and we can then have holes in our string patterns.  But I digress – so to differentiate the patterns based on the pattern length I included a maxPatternLen input.  So if maxPatternLen is three and we are trying to match pattern #3 then we will be looking for 011 and not 0011.  That was an easy fix.  But then I wanted to build a string pattern based on this input and the pattern number dynamically.  Here is some psuedo code on how I figured it out.


{Psuedo code to translate pattern number into binary number}
patternNumber = 3
maxPatternLen = 3

numBits = 0    						// stick with binary representation
testValue = 0						// temporary test value
numBits = maxPatternLen-1  			// how many bits will it take to get to the
									// center of - or numBits to represent max
									// number of patterns or 2^numBits
currentBit =numBits					// start wit current bit as total numBits

value1 = patternOptTest				// value1 represents current pattern number
testString = ""  					// build test string from ground up


for icnt = numBits downto 0			//building string from left to right
begin       						// notice keyword downto
	if power(2,currentBit) > value1 then  // must use power function in EL
	begin							// if the very far left bit value > 
		testString = testString + "-"	  // patten number then plug in a "-"
	end
	else
	begin							// else plug in a "+" and deccrement by
		testString = testString + "+"	 // that bits value - if its the 3rd bit
	value1 = value1 - power(2,currentBit)// then decrement by 8
	end;
	currentBit = currentBit - 1		// move onto the next bit to the right
end;
Pseudocode for Binary Representation of Pattern #

Now if you want to optimize then you must make sure your pattern number search space or range can be contained within maxPatternLen.  For example, if you want to test all the different combinations of a four day pattern, then your maxPatternLen would naturally be four and you would optimize the pattern number from 0 to 15.  Don’t use 1-16 as I use zero as the base.  A five day pattern would include the search space from 0 – 31.  The rest of the code was basically hacked from my original post.   Here is the rest of the code to do optimizations on different length pattern strings.  Notice how I use strings, for-loops and comparisons.

input: buyPattern("+++-"),sellPattern("---+"),patternOptimize(True),patternOptTest(7),maxPatternLen(3),patternOptBuySell(1),
	   stopLoss$(2000),profitTarg$(2000),holdDays(5);
vars: buyPatternString(""),sellPatternString(""),buyPatternMatch(""),sellPatternMatch(""),numBits(0),testValue(0),currentBit(0),
      remainder(0),value(0),icnt(0),testString(""),numCharsInBuyPattern(0),numCharsInSellPattern(0);
vars:okToBuy(false),okToSell(false);

buyPatternMatch = buyPattern;
sellPatternMatch = sellPattern;
numCharsInBuyPattern = strLen(buyPatternMatch);
numCharsInSellPattern = strLen(sellPatternMatch);

If patternOptimize then
begin
	numBits = 0;
    testValue = 0;
    value = maxPatternLen;
    numBits = maxPatternLen-1;  
    currentBit =numBits;
    remainder = patternOptTest;
    testString = "";
    for icnt = numBits downto 0
    begin       
        if power(2,currentBit) > value1 then
        begin
            testString = testString + "-";
        end
        else
        begin
            testString = testString + "+";
            remainder = remainder - power(2,currentBit);
        end;
        currentBit = currentBit - 1;
	end;
	numCharsInBuyPattern = maxPatternLen;
	numCharsInSellPattern = maxPatternLen;
	if patternOptBuySell = 1 then
	Begin
		buyPatternMatch = testString;
		sellPatternMatch = "0";
	end;
	If patternOptBuySell = 2 then
	Begin
		buyPatternMatch = "0";
		sellPatternMatch = testString;
	end;
end;
	

buyPatternString = "";
sellPatternString = "";

For icnt = numCharsInBuyPattern-1 downto 0
Begin
	If close[icnt] >= close[icnt+1] then buyPatternString = buyPatternString + "+";
	If close[icnt] < close[icnt+1] then buyPatternString = buyPatternString + "-";
end;
For icnt = numCharsInSellPattern-1 downto 0
Begin
	If close[icnt] >= close[icnt+1] then sellPatternString = sellPatternString + "+";
	If close[icnt] < close[icnt+1] then sellPatternString = sellPatternString + "-";
end;


okToBuy = false;
okToSell = false;

if buyPatternMatch <> "" then
	If buyPatternString = buyPatternMatch then okToBuy = true;
If buyPatternMatch = "" then
	okToBuy = true;
If sellPattern <> "" then
	If sellPatternString = sellPatternMatch then okToSell = true;
If sellPatternMatch = "" then
	okToSell = true;
	
If okToBuy then buy next bar at open;
If okToSell then sellshort next bar at open;

If marketPosition = 1 and barsSinceEntry > holdDays then sell next bar at open;
If marketPosition = -1 and barsSinceEntry > holdDays then buytocover next bar at open;

setStopLoss(stopLoss$);
setProfitTarget(profitTarg$);

If lastBarOnChart then print(d," ",buyPatternMatch);
Final Version of New Pattern Smasher

Also see how I incorporate a profit target and protective stop.  I use the built in BarsSinceEntry function to count the number of days I am in a trade so I can utilize a time based exit.  Here is an interesting equity curve I developed using a two day pattern ( – –) to go long.

Register on the website and I will email you an ELD of the improved Pattern Smasher.  Or just shoot me an email.

 

 

Making Trading Decisions on Current Month’s Profit/Loss

Keeping track of intra-month profit or loss

In real time trading I have noticed that once you reach a certain loss for the month its best, sometimes, to circle the wagons and quit trading until the beginning of the next month.  This concept works best for very short term or day trade algorithms, as its very easy to get started back up.  You can do this with Trend Following, but you must build a logical and replicable process for re-entering existing positions.  Let’s assume a trading algorithm whose averaging losing month is $1500 and you are currently down $2000 – what are the chances that you will revert to the mean or draw down further?  Probably 50/50.  Who knows you might turn around and actually make money by month’s end.  If you review a track record of a hedge fund manager, trader, or algorithm and they show a bar chart of monthly returns and there sticking out like a sore thumb is a big down bar, that kind of makes you think that could happen again.  If you can control the monthly downside without sacrificing the existing Profit:DrawDown ratio, then why not do it.

Sample Code To Monitor IntraMonth $P/L

if month(date) <> month(date[1]) then
Begin
	begMonthProf = netProfit; 
	print(d," ",t," ",begMonthProf);
	canTrade = true;
end;
Capture Beginning Of Month Net Profit

Here I am comparing the month of the current bar against the month of the prior bar.  If they are not equal, then we have a new month.  Store the netProfit in the variable begMonthProf.  All you have to do is compare the current bar’s netProfit to begMonthProf and make a decision.  Here is some code:

Making a Trading Decision Based on Monthly $P/L

		If dayOfMonth(date) > 15 and begMonthProf - netProfit >= intraMonthMaxLoss then canTrade = false;
If Down MaxLoss for Month and Past Mid-Month - Quit Trading

If the day of the month is greater than 15 (month half over) and the difference between the current netProfit and begMonthProfit is greater than a negative intraMonthMaxLoss then quit trading for the month.  Only turn it back on the first bar of the next month.  See how this works for your algos.

How to Create a Dominant Cycle Class in Python

John Ehlers used the following EasyLanguage code to calculate the Dominant Cycle in a small sample of data.  If you are interested in cycles and noise reduction, definitely check out the books by John Ehlers – “Rocket Science for Traders” or “Cybernetic Analysis for Stocks and Futures.”  I am doing some research in this area and wanted to share how I programmed the indicator/function in Python.  I refer you to his books or online resources for an explanation of the code.  I can tell you it involves an elegantly simplified approach using the Hilbert Transform.

 

Inputs:	Price((H+L)/2);

Vars:	Imult(.635),
		Qmult (.338),
		InPhase(0),
		Quadrature(0),
		count(0),
		Re(0),
		Im(0),
		DeltaPhase(0),
		InstPeriod(0),
		Period(0);

If CurrentBar > 8 then begin
	Value1 = Price - Price[7];
 	Inphase = 1.25*(Value1[4]  - Imult*Value1[2]) + Imult*InPhase[3];
	 	
//    print(price," ",price[7]," ",value1," ",inPhase," ",Quadrature," ",self.im[-1]," ",self.re[-1])	
//	print(d," ",h," ",l," ",c," ",Value1[4]," ",Imult*Value1[2]," ", Imult*InPhase[3]," ",Inphase);
	Quadrature = Value1[2] - Qmult*Value1 + Qmult*Quadrature[2];
	Re = .2*(InPhase*InPhase[1] + Quadrature*Quadrature[1]) + .8*Re[1];
	Im = .2*(InPhase*Quadrature[1] - InPhase[1]*Quadrature)   + .8*Im[1];
	print(d," ",o," ",h," ",l," ",c," ",value1," ",inPhase," ",Quadrature," ",Re," ",Im);
	If Re <> 0 then DeltaPhase = ArcTangent(Im/Re);

	{Sum DeltaPhases to reach 360 degrees.  The sum is the instantaneous period.}
	InstPeriod = 0;
	Value4 = 0;
	For count = 0 to 50 begin
		Value4 = Value4 + DeltaPhase[count];
		If Value4 > 360 and InstPeriod = 0 then begin
			InstPeriod = count;
		end;
	end;

	{Resolve Instantaneous Period errors and smooth}
	If InstPeriod = 0 then InstPeriod = InstPeriod[1];
	Period = .25*InstPeriod + .75*Period[1];

	Plot1(Period, "DC");
EasyLanguage Code For Calculating Dominant Cycle

In my Python based back tester an indicator of this type is best programmed by using a class.  A class is really a simple construct, especially in Python, once you familiarize yourself with the syntax.   This indicator requires you to refer to historical values to calculate the next value in the equation:  Value1[4], inPhase[1], re[2], etc.,.  In EasyLanguage these values are readily accessible as every variable is defined as a BarArray – the complete history of a variable is accessible by using indexing.  In my PSB I used lists to store values for those variables most often used such as Open, High, Low, Close.  When you need to store the values of let’s say the last five bars its best to just create a list on the fly or build them into a class structure.  A Class stores data and data structures and includes the methods (functions) that the data will be pumped into.  The follow code describes the class in two sections:  1) data declaration and instantiation and 2) the function to calculate the Dominant Cycle.  First off I create the variables that will hold the constant values: imult and qmult.  By using the word self I make these variables class members and can access them using “.” notation.  I will show you later what this means.  I also make the rest of the variables class members, but this time I make them lists and instantiate the first five values to zero.  I use list comprehension to create the lists and zero out the first five elements – all in one line of code.  This is really just a neat short cut, but can be used for much more powerful applications.  Once you create a dominantCycleClass object the object is constructed and all of the data is connected to this particular object.  You can create many dominantCycleClass objects and each one would maintain its own data.  Remember a class is just a template that is used to create an object.

class dominantCycleClass(object):
    def __init__(self):
        self.imult = 0.635
        self.qmult = 0.338
        self.value1 = [0 for i in range(5)]
        self.inPhase = [0 for i in range(5)]
        self.quadrature = [0 for i in range(5)]
        self.re = [0 for i in range(5)]
        self.im = [0 for i in range(5)]
        self.deltaPhase = [0 for i in range(5)]
        self.instPeriod = [0 for i in range(5)]
        self.period = [0 for i in range(5)]
Data Portion of Class

 

The second part of the class template contains the method or function for calculating the Dominant Cycle.  Notice how I index into the lists to extract prior values.  You will also see the word self. preceding the variable names used in the calculations Initially I felt like this redundancy hurt the readability of the code and in this case it might.  But by using self. I know I am dealing with a class member.  This is an example of the ” . ” notation I referred to earlier.  Basically this ties the variable to the class.

def calcDomCycle(self,dates,hPrices,lPrices,cPrices,curBar,offset):
        tempVal1 = (hPrices[curBar - offset] + lPrices[curBar-offset])/2
        tempVal2 = (hPrices[curBar - offset - 7] + lPrices[curBar-offset - 7])/2
        self.value1.append(tempVal1 - tempVal2)
        self.inPhase.append(1.25*(self.value1[-5] - self.imult*self.value1[-3]) + self.imult*self.inPhase[-3])        
        self.quadrature.append(self.value1[-3] - self.qmult*self.value1[-1] + self.qmult*self.quadrature[-2])
        self.re.append(.2*(self.inPhase[-1]*self.inPhase[-2]+self.quadrature[-1]*self.quadrature[-2])+ 0.8*self.re[-1])
        self.im.append(.2*(self.inPhase[-1]*self.quadrature[-2] - self.inPhase[-2]*self.quadrature[-1]) +.8*self.im[-1])
        if self.re[-1] != 0.0: self.deltaPhase.append(degrees(atan(self.im[-1]/self.re[-1])))
        if len(self.deltaPhase) > 51:
            self.instPeriod.append(0)
            value4 = 0
            for count in range(1,51):
                value4 += self.deltaPhase[-count]
                if value4 > 360 and self.instPeriod[-1] == 0:
                    self.instPeriod.append(count)
            if self.instPeriod[-1] == 0: self.instPeriod.append(self.instPeriod[-1])
            self.period.append(.25*self.instPeriod[-1]+.75*self.period[-1])
            return(self.period[-1])
Dominant Cycle Method

Okay we now have the class template to calculate the Dominant Cycle but how do we us it?

#---------------------------------------------------------------------------------
#Instantiate Indicator Classes if you need them
#---------------------------------------------------------------------------------
#    rsiStudy = rsiClass()
#    stochStudy = stochClass()
    domCycle = dominantCycleClass()
#---------------------------------------------------------------------------------
#Call the dominantCycleClass method using " . " notation.
	tempVal1 = domCycle.calcDomCycle(myDate,myHigh,myLow,myClose,i,0)
#Notice how I can access class members by using " . " notation as well!
	tempVal2 = domCycle.imult
Dominant Cycle Object Creation

Here I assign domCycle the object created by calling the dominantCycleClass constructor.  TempVal1 is assigned the Dominant Cycle when the function or method is called using the objects name (domCycle) and the now familiar ” . ” notation.  See how you can also access the imult variable using the same notation.

Here is the code in its entirety.  I put this in the indicator module of the PSB.

class dominantCycleClass(object):
    def __init__(self):
        self.imult = 0.635
        self.qmult = 0.338
        self.value1 = [0 for i in range(5)]
        self.inPhase = [0 for i in range(5)]
        self.quadrature = [0 for i in range(5)]
        self.re = [0 for i in range(5)]
        self.im = [0 for i in range(5)]
        self.deltaPhase = [0 for i in range(5)]
        self.instPeriod = [0 for i in range(5)]
        self.period = [0 for i in range(5)]

    def calcDomCycle(self,dates,hPrices,lPrices,cPrices,curBar,offset):
        tempVal1 = (hPrices[curBar - offset] + lPrices[curBar-offset])/2
        tempVal2 = (hPrices[curBar - offset - 7] + lPrices[curBar-offset - 7])/2
        self.value1.append(tempVal1 - tempVal2)
        self.inPhase.append(1.25*(self.value1[-5] - self.imult*self.value1[-3]) + self.imult*self.inPhase[-3])        
        self.quadrature.append(self.value1[-3] - self.qmult*self.value1[-1] + self.qmult*self.quadrature[-2])
        self.re.append(.2*(self.inPhase[-1]*self.inPhase[-2]+self.quadrature[-1]*self.quadrature[-2])+ 0.8*self.re[-1])
        self.im.append(.2*(self.inPhase[-1]*self.quadrature[-2] - self.inPhase[-2]*self.quadrature[-1]) +.8*self.im[-1])
        if self.re[-1] != 0.0: self.deltaPhase.append(degrees(atan(self.im[-1]/self.re[-1])))
        if len(self.deltaPhase) > 51:
            self.instPeriod.append(0)
            value4 = 0
            for count in range(1,51):
                value4 += self.deltaPhase[-count]
                if value4 > 360 and self.instPeriod[-1] == 0:
                    self.instPeriod.append(count)
            if self.instPeriod[-1] == 0: self.instPeriod.append(self.instPeriod[-1])
            self.period.append(.25*self.instPeriod[-1]+.75*self.period[-1])
            return(self.period[-1])
Dominant Cycle Class - Python

 

How to Keep Track of BuysToday and SellsToday

The Useful MP

We all know how to use the reserved word/function MarketPosition – right?  Brief summary if not – use MarketPosition to see what your current position is: -1 for short, +1 for long and 0 for flat.  MarketPosition acts like a function because you can index it to see what you position was prior to the current position – all you need to do is pass a parameter for the number of positions ago.  If you pass it a one (MarketPosition(1)) then it will return the your prior position.  If you define a variable such as MP you can store each bars MarketPosition and this can come in handy.

mp = marketPosition;

If mp[1] <> 1 and mp = 1 then buysToday = buysToday + 1;
If mp[1] <> -1 and mp = -1 then sellsToday = sellsToday + 1;
Keeping Track of Buy and Sell Entries on Daily Basis

The code compares prior bar’s MP value with the current bar’s.   If there is a change in the value, then the current market position has changed.   Going from not 1 to 1 indicates a new long position.  Going from not -1 to -1 implies a new short.  If the criteria is met, then the buysToday or sellsToday counters are incremented.  If you want to keep the number of buys or sells to a certain level, let’s say once or twice,  you can incorporate this into your code.

If  time >= startTradeTime and t < endTradeTime and 
	buysToday < 1 and 
	rsi(c,rsiLen) crosses above rsiBuyVal then buy this bar on close;
If  time >= startTradeTime and t < endTradeTime and 
	sellsToday < 1 and 
	rsi(c,rsiLen) crosses below rsiShortVal then sellShort this bar on close;
Using MP to Keep Track of BuysToday and SellsToday

This logic will work most of the time, but it depends on the robustness of the builtin MarketPosition function Look how this logic fails in the following chart:

I didn't want entries in the same direction per day!
I only wanted 1 short entry per day!

MarketPosition Failure

Failure in the sense that the algorithm shorted twice in the same day.  Notice on the first trade how the profit objective was hit on the very next bar.  The problem with MarketPosition is that it only updates at the end of the bar one bar after the entry.  So MarketPosition stays 0 during the duration of this trade.  If MarketPosition doesn’t change then my counter won’t work.  TradeStation should update MarketPosition at the end of the entry bar.  Alas it doesn’t work this way.  I figured a way around it though.  I will push the code out and explain it later in more detail.

Input: rsiLen(14),rsiBuyVal(30),rsiShortVal(70),profitObj$(250),protStop$(300),startTradeTime(940),endTradeTime(1430);

Vars: mp(0),buysToday(0),sellsToday(0),startOfDayNetProfit(0);

If d <> d[1] then
Begin
	buysToday = 0;
	sellsToday = 0;
	startOfDayNetProfit = netProfit;
end;

{mp = marketPosition;

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

If entriesToday(date) > buysToday + sellsToday then 
Begin
	If marketPosition = 1 then buysToday = buysToday + 1;
	If marketPosition =-1 then sellsToday = sellsToday + 1;
	If marketPosition = 0 then
	Begin
		if netProfit > startOfDayNetProfit then
		begin
			if exitPrice(1) > entryPrice(1) then buysToday = buysToday + 1;
			If exitPrice(1) < entryPrice(1) then sellsToday = sellsToday + 1;
		end;;
		if netProfit < startOfDayNetProfit then
		Begin
			if exitPrice(1) < entryPrice(1) then buysToday = buysToday + 1;
			If exitPrice(1) > entryPrice(1) then sellsToday = sellsToday + 1;
		end;
	end;
	print(d," ",t," ",buysToday," ",sellsToday);
end;

If  time >= startTradeTime and t < endTradeTime and 
	buysToday < 1 and 
	rsi(c,rsiLen) crosses above rsiBuyVal then buy this bar on close;
If  time >= startTradeTime and t < endTradeTime and 
	sellsToday < 1 and 
	rsi(c,rsiLen) crosses below rsiShortVal then sellShort this bar on close;

SetProfittarget(profitObj$);
SetStopLoss(protStop$);

SetExitOnClose;
A Better Buy and Short Entries Counter

TradeStation does update EntriesToday at the end of the bar so you can use this keyword/function to help keep count of the different type of entries.  If MP is 0 and EntriesToday increments then you know an entry and an exit has occurred (takes care of the MarketPosition snafu) – all you need to do is determine if the entry was a buy or a sell.  NetProfit is also updated when a trade is closed.   I establish the StartOfDayNetProfit on the first bar of the day (line 9 in the code) and then examine EntriesToday and if NetProfit increased or decreased.  EntryPrice and ExitPrice are also updated at the end of the bar so I can also use them to extract the information I need.   Since MarketPosition is 0  I have to pass 1 to the EntryPrice and ExitPrice functions – prior position’s prices.  From there I can determine if a Long/Short entry occurred.  This seems like a lot of work for what you get out of it, but if you are controlling risk by limiting the number of trades (exposure) then an accurate count is so very important.

An alternative is to test on a higher resolution of data – say 1 minute bars.  In doing this you give a buffer to the MarketPosition function – more bars to catch up.

 

Pyramiding and then Scaling Out at Different Price Levels – EasyLanguage

TOTAL, TOTAL, TOTAL – an important keyword

I just learned something new!  I guess I never programmed a strategy that pyramided at different price levels and scaled out at different price levels.

Initially I thought no problem.  But I couldn’t get it to work – I tried everything and then I came across the keyword Total and then I remembered.  If you don’t specify Total in you exit directives then the entire position is liquidated.  Unless you are putting all your positions on at one time – like I did in my last post.   So remember if you are scaling out of a pyramid position use Total in your logic.

vars: maxPosSize(2);

If currentContracts < maxPosSize - 1 and c > average(c,50) and c = lowest(c,3) then buy("L3Close") 1 contract this bar on close;
If currentContracts < maxPosSize and c > average(c,50) and c = lowest(c,4) then buy("L4Close") 1 contract this bar on close;


If currentContracts = 2 and c = highest(c,5) then sell 1 contract total this bar on close;
If currentContracts = 1 and c = highest(c,10) then sell 1 contract total this bar on close;
Scaling Out Of Pyramid

Why you have to use the Total I don’t know.  You specify the number of contracts in the directive and that is sufficient if you aren’t pyramiding.  The pyramiding throws a “monkey wrench” in to the works.