Category Archives: MultiCharts

Volatility, Volatility, Volatility – A Building Block for Day Trading the ES.D – Free System

Did that Title get your Attention?

I didn’t say a very good Free System!  This code is really cool so I thought I would share with you.  Take a look at this rather cool picture.

Six Bar Break Out with Volatility Buffer and Volatility Trailing Stop

Thanks to a reader of this blog (AG), I got this idea and programmed a very simple day trading system that incorporated a volatility trailing stop.  I wanted to make sure that I had it programmed correctly and always wanted to draw a box on the chart – thanks to (TJ) from MC forums for getting me going on the graphic aspect of the project.

Since I have run out of time for today – need to get a haircut.  I will have to wait till tomorrow to explain the code.  But real quickly the system.

Buy x% above first y bar high and then set up a trailing stop z% of y bar average range – move to break-even when profits exceed  $w.  Opposite goes for the short side.  One long and one short only allowed during the day and exit all at the close.

What the heck here is the code for the Strategy.

inputs: startTradeTime(930),startTradeBars(6),endTradeTime(1530),
		breakOutVolPer(0.5),trailVolPer(.25),breakEven$(500);
		

vars:	longsToday(0),shortsToday(0),
		longStop(0),shortStop(0),
		longTrail(0),shortTrail(0),
		trailVolAmt(0),
		barCount(0),highToday(0),lowToday(0),
		volAmt(0),mp(0);
		
if t = startTradeTime + barinterval then
begin
	longsToday = 0;
	shortsToday = 0;
	longStop = 0;
	shortStop = 0;
	longTrail = 0;
	shortTrail = 99999999;
	barCount = 0;
	highToday = 0;
	lowToday = 999999999;
end;

highToday = maxList(h,highToday);
lowToday = minList(l,lowToday);

mp = marketPosition;

barCount +=1;

if barCount >= startTradeBars then
begin
	volAmt = average(range,startTradeBars);
	if barCount = startTradeBars then
	begin
		longStop = highToday + breakOutVolPer * volAmt;
		shortStop = lowToday - breakOutVolPer * volAmt;
	end;
	if t < endTradeTime then
		begin
		if longsToday = 0 then buy("volOrboL") next bar at longStop stop;
		if shortsToday = 0 then sellShort("volOrboS") next bar shortStop stop;
	end;

	trailVolAmt = volAmt * trailVolPer;
	if mp = 1 then
	begin
		longsToday +=1;
		if c > entryPrice + breakEven$/bigPointValue then
			longTrail = maxList(entryPrice,longTrail);
		longTrail = maxList(c - trailVolAmt,longTrail);
		sell("L-TrlX") next bar at longTrail stop;
	end;
	if mp = -1 then
	begin
		shortsToday +=1;
		if c < entryPrice - breakEven$/bigPointValue then
			shortTrail = minList(entryPrice,shortTrail);
		shortTrail = minList(c + trailVolAmt,shortTrail);
		buyToCover("S-TrlX") next bar at shortTrail stop;
	end;
end;
setExitOnClose;
I will comment in a later post!

And the code for the Strategy Tracking Indicator.

inputs: startTradeTime(930),startTradeBars(6),endTradeTime(1530),
		breakOutVolPer(0.5),trailVolPer(.25),breakEven$(500);
		

vars:	longsToday(0),shortsToday(0),
		longStop(0),shortStop(0),
		longTrail(0),shortTrail(0),
		trailVolAmt(0),
		barCount(0),highToday(0),lowToday(0),
		volAmt(0),mp(0);
		
if t = startTradeTime + barinterval then
begin
	longsToday = 0;
	shortsToday = 0;
	longStop = 0;
	shortStop = 0;
	longTrail = 0;
	shortTrail = 99999999;
	barCount = 0;
	highToday = 0;
	lowToday = 999999999;
	mp = 0;
end;

highToday = maxList(h,highToday);
lowToday = minList(l,lowToday);
		
barCount +=1;

vars: iCnt(0),mEntryPrice(0),myColor(0);

if barCount >= startTradeBars  then
begin
	volAmt = average(range,startTradeBars);
	if barCount = startTradeBars then
	begin
		longStop = highToday + breakOutVolPer * volAmt;
		shortStop = lowToday - breakOutVolPer * volAmt;
		for iCnt = 0 to startTradeBars-1
		begin
			plot1[iCnt](longStop,"BuyBO",default,default,default);
			plot2[iCnt](shortStop,"ShrtBo",default,default,default);
		end;

	end;
	if t < endTradeTime then
	begin
		if longsToday = 0 and h >= longStop then 
		begin
			mp = 1;
			mEntryPrice = maxList(o,longStop);
			longsToday += 1;
		end;
		if shortsToday = 0 and l <= shortStop then
		begin
			mp = -1;
			mEntryPrice = minList(o,shortStop);
			shortsToday +=1;
		end;	
		plot3(longStop,"BuyBOXTND",default,default,default);
		plot4(shortStop,"ShrtBOXTND",default,default,default);
	end;
	
	trailVolAmt = volAmt * trailVolPer;
	
	if mp = 1 then
	begin
		if c > mEntryPrice + breakEven$/bigPointValue then
			longTrail = maxList(mEntryPrice,longTrail);

		longTrail = maxList(c - trailVolAmt,longTrail);
		plot5(longTrail,"LongTrail",default,default,default);
	end;
	if mp = -1 then
	begin
		if c < mEntryPrice - breakEven$/bigPointValue then
			shortTrail = minList(mEntryPrice,shortTrail);
		shortTrail = minList(c + trailVolAmt,shortTrail);
		plot6(shortTrail,"ShortTrail",default,default,default);
	end;
end;
Cool code for the indicator

Very Important To Set Indicator Defaults Like This

For the BO Box use these settings – its the first 4 plots:

Use these colors and bar high and bar low and set opacity

The box is created by drawing thick semi-transparent lines from the BuyBo and BuyBOXTND down to ShrtBo and ShrtBOXTND.   So the Buy components of the 4 first plots should be Bar High and the Shrt components should be Bar Low.  I didn’t specify this the first time I posted.  Thanks to one of my readers for point this out!

Use bar low for ShrtBo and ShrtBOXTND plots

Also I used different colors for the BuyBo/ShrtBo and the BuyBOXTND/ShrtBOXTND.  Here is that setting:

The darker colored line on the last bar of the break out is caused by the overlap of the two sets of plots.

Here is how you set up the trailing stop plots:

Make Dots and Make Then Large – I have Red and Blue Set

 

EasyLanguage Programming Workshop Part 1: 2D Array, Print Format, and Loops

Storing Trades for Later Use in a 2D Array

Since this is part 1 we are just going to go over a very simple system:  SAR (stop and reverse) at highest/lowest high/low for past 20 days.

A 2D Array in EasyLanguage is Immutable

Meaning that once you create an array all of the data types must be the same.  In a Python list you can have integers, strings, objects whatever.   In C and its derivatives you also have a a data structure (a thing that stores related data) know as a Structure or Struct.  We can mimic a structure in EL by using a 2 dimensional array.  An array is just a list of values that can be referenced by an index.

array[1] = 3.14

array[2] = 42

array[3] = 2.71828

A 2 day array is similar but it looks like a table

array[1,1], array[1,2], array[1,3]

array[2,1], array[2,2], array[2,3]

The first number in the pair is the row and the second is the column.  So a 2D array can be very large table with many rows and columns.  The column can also be referred to as a field in the table.  To help use a table you can actually give your fields names.  Here is a table structure that I created to store trade information.

  1. trdEntryPrice (0) – column zero – yes we can have a 0 col. and row
  2. trdEntryDate(1)
  3. trdExitPrice (2)
  4. trdExitDate(3)
  5. trdID(4)
  6. trdPos(5)
  7. trdProfit(6)
  8. trdCumuProfit(7)

So when I refer to tradeStruct[0, trdEntryPrice] I am referring to the first column in the first row.

This how you define a 2D array and its associate fields.

arrays: tradeStruct[10000,7](0);

vars: trdEntryPrice (0),
      trdEntryDate(1), 
      trdExitPrice (2),
      trdExitDate(3),
      trdID(4),
      trdPos(5),
      trdProfit(6),
      trdCumuProfit(7);
2D array and its Fields

In EasyLanguage You are Poised at the Close of a Yesterday’s Bar

This paradigm allows you to sneak a peek at tomorrow’s open tick but that is it.  You can’t really cheat, but it also limits your creativity and makes things more difficult to program when all you want is an accurate backtest.   I will go into detail, if I haven’t already in an earlier post, the difference of sitting on Yesterday’s close verus sitting on Today’s close with retroactive trading powers.  Since we are only storing trade information when can use hindsight to gather the information we need.

Buy tomorrow at highest(h,20) stop;

SellShort tomorrow at lowest(l,20) stop;

These are the order directives that we will be using to execute our strategy.  We can also run a Shadow System, with the benefit of hindsight, to see where we entered long/short and at what prices. I call it a Shadow because its all the trades reflected back one bar.   All we need to do is offset the highest and lowest calculations by 1 and compare the values to today’s highs and lows to determine trade entry.  We must also test the open if a gap occurred and we would have been filled at the open.  Now this code gets a bit hairy, but stick with it.

Shadow System

stb = highest(h,20);
sts = lowest(l,20);
stb1 = highest(h[1],20);
sts1 = lowest(l[1],20);

buy("Sys-L") 1 contract next bar at stb stop;
sellShort("Sys-S") 1 contract next bar at sts stop;

mp = marketPosition*currentContracts;
totTrds = totalTrades;

if mPos <> 1 then
begin
	if h >= stb1 then
	begin
		if mPos < 0 then // close existing short position
		begin
			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];
			mExitPrice = maxList(o,stb1);
			tradeStruct[numTrades,trdExitPrice] = mExitPrice;
			tradeStruct[numTrades,trdExitDate] = date;
			mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp;
			cumuProfit += mProfit;
			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;
			tradeStruct[numTrades,trdProfit] = mProfit;
			print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);
		print("-------------------------------------------------------------------------");	
		end;	
		numTrades +=1;
		mEntryPrice = maxList(o,stb1);
		tradeStruct[numTrades,trdID] = 1;
		tradeStruct[numTrades,trdPOS] = 1;
		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;
		tradeStruct[numTrades,trdEntryDate] = date;
		mPos = 1;
		print(d+19000000:8:0," longEntry ",mEntryPrice:4:5);
	end;
end;
if mPos <>-1 then
begin
	if l <= sts1 then
	begin
		if mPos > 0 then // close existing long position
		begin
			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];
			mExitPrice = minList(o,sts1);
			tradeStruct[numTrades,trdExitPrice] = mExitPrice;
			tradeStruct[numTrades,trdExitDate] = date;
			mProfit = (mExitPrice - mEntryPrice ) * bigPointValue - mCommSlipp;
			cumuProfit += mProfit;
			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;
			tradeStruct[numTrades,trdProfit] = mProfit;
			print(d+19000000:8:0," longExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);
			print("---------------------------------------------------------------------");
		end;	
		numTrades +=1;
		mEntryPrice =minList(o,sts1);
		tradeStruct[numTrades,trdID] = 2;
		tradeStruct[numTrades,trdPOS] =-1;
		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;
		tradeStruct[numTrades,trdEntryDate] = date;
		mPos = -1;
		print(d+19000000:8:0," ShortEntry ",mEntryPrice:4:5);
	end;
end;
Shadow System - Generic forany SAR System

Notice I have stb and stb1.  The only difference between the two calculations is one is displaced a day.  I use the stb and sts in the EL trade directives.  I use stb1 and sts1 in the Shadow System code.  I guarantee this snippet of code is in every backtesting platform out there.

All the variables that start with the letter m, such as mEntryPrice, mExitPrice deal with the Shadow System.  Theyare not derived from TradeStation’s back testing engine only our logic.  Lets look at the first part of just one side of the Shadow System:

if mPos <> 1 then
begin
	if h >= stb1 then
	begin
		if mPos < 0 then // close existing short position
		begin
			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];
			mExitPrice = maxList(o,stb1);
			tradeStruct[numTrades,trdExitPrice] = mExitPrice;
			tradeStruct[numTrades,trdExitDate] = date;
			mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp;
			cumuProfit += mProfit;
			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;
			tradeStruct[numTrades,trdProfit] = mProfit;
			print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);
		print("-------------------------------------------------------------------------");	
		end;	

mPos and mEntryPrice and mExitPrice belong to the Shadow System

if mPos <> 1 then the Shadow Systems [SS] is not long.  So we test today’s high against stb1 and if its greater then we know a long position was put on.  But what if mPos = -1 [short], then we need to calculate the exit and the trade profit and the cumulative trade profit.  If mPos = -1 then we know a short position is on and we can access its particulars from the tradeStruct 2D arraymEntryPrice = tradeStruct[numTrades,trdEntryPrice].  We can gather the other necessary information from the tradeStruct [remember this is just a table with fields spelled out for us.]  Once we get the information we need we then need to stuff our calculations back into the Structure or table so we can regurgitate later.  We stuff date in to the following fields trdExitPrice, trdExitDate, trdProfit and trdCumuProfit in the table.

Formatted Print: mEntryPrice:4:5

Notice in the code how I follow the print out of variables with :8:0 or :4:5?  I am telling TradeStation to use either 0 or 5 decimal places.  The date doesn’t need decimals but prices do.  So I format that so that they will line up really pretty like.

Now that I take care of liquidating an existing position all I need to do is increment the number of trades and stuff the new trade information into the Structure.

		numTrades +=1;
		mEntryPrice = maxList(o,stb1);
		tradeStruct[numTrades,trdID] = 1;
		tradeStruct[numTrades,trdPOS] = 1;
		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;
		tradeStruct[numTrades,trdEntryDate] = date;
		mPos = 1;
		print(d+19000000:8:0," longEntry ",mEntryPrice:4:5);

The same goes for the short entry and long exit side of things.  Just review the code.  I print out the trades as we go along through the history of crude.  All the while stuffing the table.

If LastBarOnChart -> Regurgitate

On the last bar of the chart we know exactly how many trades have been executed because we were keeping track of them in the Shadow System.  So it is very easy to loop from 0 to numTrades.

if lastBarOnChart then
begin
	print("Trade History");
	for arrIndx = 1 to numTrades
	begin
		value20 = tradeStruct[arrIndx,trdEntryDate];
		value21 = tradeStruct[arrIndx,trdEntryPrice];
		value22 = tradeStruct[arrIndx,trdExitDate];
		value23 = tradeStruct[arrIndx,trdExitPrice];
		value24 = tradeStruct[arrIndx,trdID];
		value25 = tradeStruct[arrIndx,trdProfit];
		value26 = tradeStruct[arrIndx,trdCumuProfit];
		
		print("---------------------------------------------------------------------");
		if value24 = 1 then
		begin
			string1 = buyStr;
			string2 = sellStr;
		end;
		if value24 = 2 then
		begin
			string1 = shortStr;
			string2 = coverStr;
		end;	
		print(value20+19000000:8:0,string1,value21:4:5," ",value22+19000000:8:0,string2,
			  value23:4:5," ",value25:6:0," ",value26:7:0);
	end;
end;

Add 19000000 to Dates for easy Translation

Since all trade information is stored in the Structure or Table then pulling the information out using our Field Descriptors is very easy.  Notice I used EL built-in valueXX to store table information.  I did this to make the print statements a lot shorter.  I could have just used tradeStruct[arrIndx, trdEntry] or whatever was needed to provide the right information, but the lines would be hard to read.  To translate EL date to a normal looking data just add 19,000,000 [without commas].

If you format your PrintLog to a monospaced font your out put should look like this.

 

PrintLog OutPut

Why Would We Want to Save Trade Information?

The answer to this question will be answered in Part 2.  Email me with any other questions…..

Why Do I Need to Test with Intraday Data

Why Can’t I Just Test with Daily Bars and Use Look-Inside Bar?

Good question.  You can’t because it doesn’t work accurately all of the time.   I just default to using 5 minute or less bars whenever I need to.  A large portion of short term, including day trade, systems need to know the intra day market movements to know which orders were filled accurately.  It would be great if you could just flip a switch and convert a daily bar system to an intraday system and Look Inside Bar(LIB) is theoretically that switch.  Here I will prove that switch doesn’t always work.

Daily Bar System

  • Buy next bar at open of the day plus 20% of the 5 day average range
  • SellShort next at open of the day minus 20% of the 5 day average range
  • If long take a profit at one 5 day average range above entryPrice
  • If short take a profit at one 5 day average range below entryPrice
  • If long get out at a loss at 1/2 a 5 day average range below entryPrice
  • If short get out at a loss at 1/2 a 5 day average range above entry price
  • Allow only 1 long and 1 short entry per day
  • Get out at the end of the day

Simple Code for the System

value1 = .2 * average(Range,5);
value2 = value1 * 5;

Buy next bar at open of next bar + value1 stop;
sellShort next bar at open of next bar - value1 stop;

setProfitTarget(value2*bigPointValue);
setStopLoss(value2/2*bigPointValue);
setExitOnClose;
Simplified Daily Bar DayTrade System using ES.D Daily
Daily Bar Using 5 min Look Inside Bar

Looks great with just the one hiccup:  Bot @ 3846.75 and the Shorted @ 3834.75 and then took nearly 30 handles of profit.

Now let’s see what really happened.

What Really Happened – Bot – Shorted – Stopped Out

Intraday Code to Control Entry Time and Number of Longs and Shorts

Not an accurate representation so let’s take this really simple system and apply it to intraday data.  Approaching this from a logical perspective with limited knowledge about TradeStation you might come up with this seemingly valid solution.  Working on the long side first.

//First Attempt


if d <> d[1] then value1 = .2 * average(Range of data2,5);
value2 = value1 * 5;
if t > sess1startTime then buy next bar at opend(0) + value1 stop;
setProfitTarget(value2*bigPointValue);
setStopLoss(value2/2*bigPointValue);
setExitOnClose;
First Simple Attempt

This looks very similar to the daily bar system.  I cheated a little by using

if d <> d[1] then value1 = .2 * average(Range of data2,5);

Here I am only calculating the average once a day instead of on each 5 minute bar.  Makes things quicker.  Also I used

if t > sess1StartTime then buy next bar at openD(0) + value1 stop;

I did that because if you did this:

buy next bar at open of next bar + value1 stop;

You would get this:

Cannot Sneak a Peek with Data2

That should do it for the long side, right?

Didn’t work quite right!

So now we have to monitor when we can place a trade and monitor the number of long and short entries.

How does this look!

Correct Execution!

So here is the code.  You will notice the added complexity.  The important things to know is how to control when an entry is allowed and how to count the number of long and short entries.  I use the built-in keyword/function totalTrades to keep track of entries/exits and marketPosition to keep track of the type of entry.

Take a look at the code and you can see how the daily bar system is somewhat embedded in the code.  But remember you have to take into account that you are stepping through every 5 minute bar and things change from one bar to the next.

vars: buysToday(0),shortsToday(0),curTotTrades(0),mp(0),tradeZoneTime(False);


if d <> d[1] then 
begin
	curTotTrades = totalTrades;
 	value1 = .2 * average(Range of data2,5);
	value2 = value1 * 5;	
	buysToday = 0;
	shortsToday = 0;
	tradeZoneTime = False;
end;

mp = marketPosition;

if totalTrades > curTotTrades then
begin
	if mp <> mp[1] then 
	begin
		if mp[1] = 1 then buysToday = buysToday + 1;
		if mp[1] = -1 then shortsToday = shortsToday + 1;
	end;
	if mp[1] = -1 then print(d," ",t," ",mp," ",mp[1]," ",shortsToday);
	curTotTrades = totalTrades;
end;
if t > sess1StartTime and t < sess1EndTime then tradeZoneTime = True;

if tradeZoneTime and buysToday = 0 and mp <> 1 then 
	buy next bar at opend(0) + value1 stop;
	
if tradeZoneTime and  shortsToday = 0 and mp <> -1 then 
	sellShort next bar at opend(0) - value1 stop;

setProfitTarget(value2*bigPointValue);
setStopLoss(value2/2*bigPointValue);
setExitOnClose;
Proper Code to Replicate the Daily Bar System with Accuracy

Here’s a few trade examples to prove our code works.

Looks Right!

Okay the code worked but did the system?

Uh? NO!

Conclusion

If you need to know what occurred first – a high or a low in a move then you must use intraday data.  If you want to have multiple entries then of course your only alternative is intraday data.   This little bit of code can get you started converting your daily bar systems to intraday data and can be a framework to develop your own day trading/or swing systems.

Can I Prototype A Short Term System with Daily Data?

You can of course use Daily Bars for fast system prototyping.  When the daily bar system was tested with LIB turned on, it came close to the same results as the more accurately programmed intraday system.  So you can prototype to determine if a system has a chance.  Our core concept buyt a break out, short a break out, take profits and losses and have no overnight exposure sounds good theoretically.  And if you only allow 2 entries in opposite directions on a daily bar you can determine if there is something there.

A Dr. Jekyll and Mr. Hyde Scenario

While playing around with this I did some prototyping of a daily bar system and created this equity curve.  I mistakenly did not allow any losses – only took profits and re-entered long.

Wow! Awesome! Holy Grail Uncovered. Venalicius Cave!

Venalicius Cave!  Don’t take a loser you and will reap the benefits.  The chart says so – so its got to be true – I know right?

The same chart from a different perspective.

You Start and End at the Same Place. But What A Ride. Yikes!

Moral of the Story – always look at your detailed Equity Curve.  This curve is very close to a simple buy and hold strategy.   Maybe a little better.

Daily Bar Ratcheting Stop and Conditional Optimization

Happy New Year!  My First Post of 2021!

In this post I simply wanted to convert the intraday ratcheting stop mechanism that I previously posted into a daily bar mechanism.  Well that got me thinking of how many different values could be used as the amount to ratchet.  I came up with three:

I have had requests for the EasyLanguage in an ELD – so here it is – just click on the link and unZip.

RATCHETINGSTOPWSWITCH

Ratcheting Schemes

  • ATR of N days
  • Fixed $ Amount
  • Percentage of Standard Deviation of 20 Days

So this was going to be a great start to a post, because I was going to incorporate one of my favorite programming constructs : Switch-Case.  After doing the program I thought wouldn’t it be really cool to be able to optimize over each scheme the ratchet and trail multiplier as well as the values that might go into each scheme.

In scheme one I wanted to optimize the N days for the ATR calculation.  In scheme two I wanted to optimize the $ amount and the scheme three the percentage of a 20 day standard deviation.  I could do a stepwise optimization and run three independent optimizations – one for each scheme.  Why not just do one global optimization you might ask?  You could but it would be a waste of computer time and then you would have to sift through the results.  Huh?  Why?  Here is a typical optimization loop:

Scheme Ratchet Mult Trigger Mult Parameter 1
1 : ATR 1 1 ATR (2)
2 : $ Amt 1 1 ATR (2)
3 : % of Dev. Amt 1 1 ATR (2)
1 : ATR 2 1 ATR (2)
2 : $ Amt 2 1 ATR (2)

Notice when we switch schemes the Parameter 1 doesn’t make sense.  When we switch to $ Amt we want to use a $ Value as Parameter 1 and not ATR.  So we could do a bunch of optimizations across non sensical values, but that wouldn’t really make a lot of sense.  Why not do a conditional optimization?  In other words, optimize only across a certain parameter range based on which scheme is currently being used.  I knew there wasn’t an overlay available to use using standard EasyLanguage but I thought maybe OOP,  and there is an optimization API that is quite powerful.  The only problem is that it was very complicated and I don’t know if I could get it to work exactly the way I wanted.

EasyLanguage is almost a full blown programming language.  So should I not be able to distill this conditional optimization down to something that I could do with such a powerful programming language?  And the answer is yes and its not that complicated.  Well at least for me it wasn’t but for beginners probably.  But to become a successful programmer you have to step outside your comfort zones, so I am going to not only explain the Switch/Case construct (I have done this in earlier posts)  but incorporate some array stuff.

When performing conditional optimization there are really just a few things you have to predefine:

  1. Scheme Based Optimization Parameters
  2. Exact Same Number of Iterations for each Scheme [starting point and increment value]
  3. Complete Search Space
  4. Total Number of Iterations
  5. Staying inside the bounds of your Search Space

Here are the optimization range per scheme:

  • Scheme #1 – optimize number of days in ATR calculation – starting at 10 days and incrementing by 2 days
  • Scheme #2 – optimize $ amounts – starting at $250 and incrementing by $100
  • Scheme #3 – optimize percent of 20 Bar standard deviation – starting at 0,25 and incrementing by 0.25

I also wanted to optimize the ratchet and target multiplier.  Here is the base code for the daily bar ratcheting system with three different schemes.  Entries are based on penetration of 20 bar highest/lowest close.

inputs: 
ratchetMult(2),trailMult(2),
volBase(True),volCalcLen(20),
dollarBase(False),dollarAmt(250),
devBase(False),devAmt(0.25);


vars:longMult(0),shortMult(0);
vars:ratchetAmt(0),trailAmt(0);
vars:stb(0),sts(0),mp(0);
vars:lep(0),sep(0);



if volBase then 
begin
	ratchetAmt = avgTrueRange(volCalcLen) * ratchetMult;
	trailAmt = avgTrueRange(volCalcLen) * trailMult;
end;
if dollarBase then 
begin
	ratchetAmt =dollarAmt/bigPointValue * ratchetMult;
	trailAmt = dollarAmt/bigPointValue * trailMult;
end;
if devBase then 
begin
	ratchetAmt = stddev(c,20) * devAmt * ratchetMult;
	trailAmt = stddev(c,20) * devAmt * trailMult;
end;


if c crosses over highest(c[1],20) then buy next bar at open;
if c crosses under lowest(c[1],20) then sellshort next bar at open;

mp = marketPosition;
if mp <> 0 and mp[1] <> mp then
begin
	longMult = 0;
	shortMult = 0;
end;


If mp = 1 then lep = entryPrice;
If mp =-1 then sep = entryPrice;


// Okay initially you want a X point stop and then pull the stop up
// or down once price exceeds a multiple of Y points
// longMult keeps track of the number of Y point multiples of profit
// always key off of lep(LONG ENTRY POINT)
// notice how I used + 1 to determine profit
// and -  1 to determine stop level

If mp = 1 then 
Begin
	If h >= lep + (longMult + 1) * ratchetAmt then	longMult = longMult + 1;
	Sell("LongTrail") next bar at (lep + (longMult - 1) *  trailAmt) stop;
end;

If mp = -1 then 
Begin
	If l <= sep - (shortMult + 1) * ratchetAmt then	shortMult = shortMult + 1;
	buyToCover("ShortTrail") next bar (sep - (shortMult - 1) *  trailAmt) stop;
end;
Daily Bar Ratchet System

This code is fairly simple.  The intriguing inputs are:

  • volBase [True of False] and  volCalcLen [numeric Value]
  • dollarBase [True of False] and  dollarAmt [numeric Value]
  • devBase [True of False] and devAmt [numeric Value]

If volBase is true then you use the parameters that go along with that scheme.  The same goes for the other schemes.  So when you run this you would turn one scheme on at a time and set the parameters accordingly.  if I wanted to use dollarBase(True) then I would set the dollarAmt to a $ value.  The ratcheting mechanism is the same as it was in the prior post so I refer you back to that one for further explanation.

So this was a pretty straightforward strategy.  Let us plan out our optimization search space based on the different ranges for each scheme.  Since each scheme uses a different calculation we can’t simply optimize across all of the different ranges – one is days, and the other two are dollars and percentages.

Enumerate

We know how to make TradeStation loop based on the range of a value.  If you want to optimize from $250 to $1000 in steps of $250, you know this involves [$1000 – $250] / $250 + 1 or 3 + 1 or 4 interations.   Four loops will cover this entire search space.  Let’s examine the search space for each scheme:

  • ATR Scheme: start at 10 bars and end at 40 by steps of 2 or [40-10]/2 + 1 = 16
  • $ Amount Scheme: start at $250 and since we have to have 16 iterations [remember # of iterations have to be the same for each scheme] what can we do to use this information?  Well if we start $250 and step by $100 we cover the search space $250, $350, $450, $550…$1,750.  $250 + 15 x 250.  15 because $250 is iteration 1.
  • Percentage StdDev Scheme:  start at 0.25 and end at 0.25 + 15 x 0.25  = 4

So we enumerate 16 iterations to a different value.  The easiest way to do this is to create a map.  I know this seems to be getting hairy but it really isn’t.  The map will be defined as an array with 16 elements.  The array will be filled with the search space based on which scheme is currently being tested.  Take a look at this code where I show how to define an array of 16 elements and introduce my Switch/Case construct.

array: optVals[16](0);

switch(switchMode)
begin
	case 1:
		startPoint = 10; // vol based
		increment = 2;
	case 2:
		startPoint = 250/bigPointValue; // $ based
		increment = 100/bigPointValue;
	case 3:
		startPoint = 0.25; //standard dev
		increment = 0.25*minMove/priceScale;
	default:
		startPoint = 1;
		increment = 1;
end;

vars: cnt(0),loopCnt(0);
once 
begin
	for cnt = 1 to 16 
	begin
		optVals[cnt] = startPoint + (cnt-1) * increment;
	end;
end
Set Up Complete Search Space for all Three Schemes

This code creates a 16 element array, optVals, and assigns 0 to each element.  SwitchMode goes from 1 to 3.

  • if switchMode is 1: ATR scheme [case: 1] the startPoint is set to 10 and increment is set to 2
  • if switchMode is 2: $ Amt scheme [case: 2] the startPoint is set to $250 and increment is set to $100
  • if switchMode is 3: Percentage of StdDev [case: 3] the startPoint is set to 0.25 and the increment is set to 0.25

Once these two values are set the following 15 values can be spawned by the these two.  A for loop is great for populating our search space.  Notice I wrap this code with ONCE – remember ONCE  is only executed at the very beginning of each iteration or run.

once
begin
   for cnt = 1 to 16
   begin
     optVals[cnt] = startPoint + (cnt-1) * increment;
   end;
end

Based on startPoint and increment the entire search space is filled out.  Now all you have to do is extract this information stored in the array based on the iteration number.

Switch(switchMode) 
Begin
	Case 1:
		ratchetAmt = avgTrueRange(optVals[optLoops]) * ratchetMult;
		trailAmt = avgTrueRange(optVals[optLoops]) * trailMult;	
	Case 2:
		ratchetAmt =optVals[optLoops] * ratchetMult;
		trailAmt = optVals[optLoops] * trailMult;
	Case 3: 
		ratchetAmt =stddev(c,20) * optVals[optLoops] * ratchetMult;
		trailAmt = stddev(c,20) * optVals[optLoops] * trailMult;
	Default:
		ratchetAmt = avgTrueRange(optVals[optLoops]) * ratchetMult;
		trailAmt = avgTrueRange(optVals[optLoops]) * trailMult;
end;


if c crosses over highest(c[1],20) then buy next bar at open;
if c crosses under lowest(c[1],20) then sellshort next bar at open;

mp = marketPosition;
if mp <> 0 and mp[1] <> mp then
begin
	longMult = 0;
	shortMult = 0;
end;


If mp = 1 then lep = entryPrice;
If mp =-1 then sep = entryPrice;


// Okay initially you want a X point stop and then pull the stop up
// or down once price exceeds a multiple of Y points
// longMult keeps track of the number of Y point multiples of profit
// always key off of lep(LONG ENTRY POINT)
// notice how I used + 1 to determine profit
// and -  1 to determine stop level

If mp = 1 then 
Begin
	If h >= lep + (longMult + 1) * ratchetAmt then longMult = longMult + 1;
	Sell("LongTrail") next bar at (lep + (longMult - 1) *  trailAmt) stop;
end;

If mp = -1 then 
Begin
	If l <= sep - (shortMult + 1) * ratchetAmt then	shortMult = shortMult + 1;
	buyToCover("ShortTrail") next bar (sep - (shortMult - 1) *  trailAmt) stop;
end;
Extract Search Space Values and Rest of Code

Switch(switchMode)
Begin
Case 1:
  ratchetAmt = avgTrueRange(optVals[optLoops])ratchetMult;
  trailAmt = avgTrueRange(optVals[optLoops]) trailMult;
Case 2:
  ratchetAmt =optVals[optLoops] * ratchetMult;
  trailAmt = optVals[optLoops] * trailMult;
Case 3:
  ratchetAmt =stddev(c,20)optVals[optLoops]
ratchetMult;
  trailAmt = stddev(c,20) * optVals[optLoops] * trailMult;

Notice how the optVals are indexed by optLoops.  So the only variable that is optimized is the optLoops and it spans 1 through 16.  This is the power of enumerations – each number represents a different thing and this is how you can control which variables are optimized in terms of another optimized variable.   Here is my optimization specifications:

Opimization space

And here are the results:

Optimization Results

The best combination was scheme 1 [N-day ATR Calculation] using a 2 Mult Ratchet and 1 Mult Trail Trigger.  The best N-day was optVals[2] for this scheme.  What in the world is this value?  Well you will need to back engineer a little bit here.  The starting point for this scheme was 10 and the increment was 2 so if optVals[1] =10 then optVals[2] = 12 or ATR(12).    You can also print out a map of the search spaces.

vars: cnt(0),loopCnt(0);
once 
begin
	loopCnt = loopCnt + 1;
//	print(switchMode," : ",d," ",startPoint);
//	print("  ",loopCnt:2:0,"  --------------------");
	for cnt = 1 to 16 
	begin
		optVals[cnt] = startPoint + (cnt-1) * increment;
//		print(cnt," ",optVals[cnt]," ",cnt-1);
	end;
end;	
  Scheme 1
  --------------------
   1.00   10.00    0.00 10 days
   2.00   12.00    1.00
   3.00   14.00    2.00
   4.00   16.00    3.00
   5.00   18.00    4.00
   6.00   20.00    5.00
   7.00   22.00    6.00
   8.00   24.00    7.00
   9.00   26.00    8.00
  10.00   28.00    9.00
  11.00   30.00   10.00
  12.00   32.00   11.00
  13.00   34.00   12.00
  14.00   36.00   13.00
  15.00   38.00   14.00
  16.00   40.00   15.00
 
  Scheme2
  --------------------
   1.00    5.00    0.00 $ 250
   2.00    7.00    1.00 $ 350
   3.00    9.00    2.00 $ 400
   4.00   11.00    3.00 $ ---
   5.00   13.00    4.00
   6.00   15.00    5.00
   7.00   17.00    6.00
   8.00   19.00    7.00
   9.00   21.00    8.00
  10.00   23.00    9.00
  11.00   25.00   10.00
  12.00   27.00   11.00
  13.00   29.00   12.00
  14.00   31.00   13.00
  15.00   33.00   14.00
  16.00   35.00   15.00 $1750

  Scheme 3
  --------------------
   1.00    0.25    0.00 25 % stdDev
   2.00    0.50    1.00
   3.00    0.75    2.00
   4.00    1.00    3.00
   5.00    1.25    4.00
   6.00    1.50    5.00
   7.00    1.75    6.00
   8.00    2.00    7.00
   9.00    2.25    8.00
  10.00    2.50    9.00
  11.00    2.75   10.00
  12.00    3.00   11.00
  13.00    3.25   12.00
  14.00    3.50   13.00
  15.00    3.75   14.00
  16.00    4.00   15.00

This was a elaborate post so please email me with questions.  I wanted to demonstrate that we can accomplish very sophisticated things with just the pure and raw EasyLanguage which is a programming language itself.

ES.D Strategy with Ratcheting Trailing Stop [Intra-Day] – EasyLanguage

ES.D Strategy Utilizing Ratchet Trailing Stop

A reader of this blog wanted a conversion from my Ratchet Trailing Stop indicator into a Strategy.  You will notice a very close similarity with the indicator code as the code for this strategy.  This is a simple N-Bar [Hi/Lo] break out with inputs for the RatchetAmt and TrailAmt.  Remember RatchetAmt is how far the market must move in your favor before the stop is pulldown the TrailAmt.  So if the RatchetAmt is 12 and the TrailAmt is 6, the market would need to move 12 handles in your favor and the Trail Stop would move to break even.  If it moves another 12 handles then the stop would be moved up/down by 6 handles.  Let me know if you have any questions – this system is similar to the one I just posted.

Notice how the RED line Ratchets Up by the Fixed Amount [8/6]
inputs: ratchetAmt(6),trailAmt(6);
vars:longMult(0),shortMult(0),myBarCount(0);
vars:stb(0),sts(0),buysToday(0),shortsToday(0),mp(0);
vars:lep(0),sep(0);

If d <> d[1] then
Begin
	longMult = 0;
	shortMult = 0;
	myBarCount = 0;
	mp = 0;
	lep = 0;
	sep = 0;
	buysToday = 0;
	shortsToday = 0;
end;

myBarCount = myBarCount + 1;

If myBarCount = 6 then  // six 5 min bars = 30 minutes
Begin
	stb = highD(0);  //get the high of the day
	sts = lowD(0);   //get low of the day
end;

If myBarCount >=6 and t < calcTime(sess1Endtime,-3*barInterval) then
Begin
	if buysToday = 0  then buy("NBar-Range-B") next bar stb stop;
	if shortsToday = 0 then sellShort("NBar-Range-S") next bar sts stop;
end;

mp = marketPosition;
If mp = 1 then 
begin
	lep = entryPrice;
	buysToday = 1;
end;
If mp =-1 then 
begin
	sep = entryPrice;
	shortsToday = 1;
end;


// Okay initially you want a X point stop and then pull the stop up
// or down once price exceeds a multiple of Y points
// longMult keeps track of the number of Y point multipes of profit
// always key off of lep(LONG ENTRY POINT)
// notice how I used + 1 to determine profit
// and -  1 to determine stop level

If mp = 1 then 
Begin
	If h >= lep + (longMult + 1) * ratchetAmt then	longMult = longMult + 1;
	Sell("LongTrail") next bar at (lep + (longMult - 1) *  trailAmt) stop;
end;

If mp = -1 then 
Begin
	If l <= sep - (shortMult + 1) * ratchetAmt then	shortMult = shortMult + 1;
	buyToCover("ShortTrail") next bar (sep - (shortMult - 1) *  trailAmt) stop;
end;

setExitOnClose;
I Used my Ratchet Indicator for the Basis of this Strategy

A Simple Break Out Algorithm Demonstrating Time Optimization

What is Better:  30, 60, or 120 Minute Break-Out on ES.D

Here is a simple tutorial you can use as a foundation to build a potentially profitable day trading system.  Here we wait N minutes after the open and then buy the high of the day or short the low of the day and apply a protective stop and profit objective.  The time increment can be optimized to see what time frame is best to use.  You can also optimize the stop loss and profit objective – this system gets out at the end of the day.  This system can be applied to any .D data stream in TradeStation or Multicharts.

Logic Description

  1. get open time
  2. get close time
  3. get N time increment
    1. 15 – first 15 minute of day
    2. 30 – first 30 minute of day
    3. 60 – first hour of day
  4. get High and Low of day
  5. place stop orders at high and low of day – no entries late in day
  6. calculate buy and short entries – only allow one each*
  7. apply stop loss
  8. apply profit objective
  9. get out at end of day if not exits have occurred

Optimization Results [From 15 to 120 by 5 minutes] on @ES.D 5 Minute Chart – Over Last Two Years

Optimization of Time: Look How the # Trades Decrease as the Time Increment Increases

Simple Orbo EasyLanguage

I threw this together rather quickly in a response to a reader’s question.  Let me know if you see a bug or two.  Remember once you gather your stops you must allow the order to be issued on every subsequent bar of the trading day.  The trading day is defined to be the time between timeIncrement and endTradeMinB4Close.  Notice how I used the EL function calcTime to calculate time using either a +positive or -negative input.  I want to sample the high/low of the day at timeIncrement and want to trade up until endTradeMinB4Close time.  I use the HighD and LowD functions to extract the high and low of the day up to that point.  Since I am using a tight stop relative to today’s volatility you will see more than 1 buy or 1 short occurring.  This happens when entry/exit occurs on the same bar and MP is not updated accordingly.  Somewhere  hidden in this tome of a blog you will see a solution for this.  If you don’t want to search I will repost it tomorrow.


//Optimizing Time to determine a simple break out
//Only works on .D data streams
Inputs: timeIncrement(15),endTradeMinB4Close(-15),stopLoss$(500),profTarg$(1000);

vars: firstBarTime(0),lastBarTime(0),buyStop(0),shortStop(0),
calcStopTime(0),quitTradeTime(0),buysToday(0),shortsToday(0),mp(0);

firstBarTime = sessionStartTime(0,1);
lastBarTime = sessionEndTime(0,1);

calcStopTime = calcTime(firstBarTime,timeIncrement);
quitTradeTime = calcTime(lastBarTime,endTradeMinB4Close);



If time = calcStopTime then 
begin
       buyStop = HighD(0);
       shortStop = LowD(0);
       buysToday = 0;
       shortsToday = 0;
End;

if time >= calcStopTime and time < quitTradeTime then 
begin
	if buysToday = 0 then Buy next bar at buyStop stop;
	if shortsToday = 0 then Sell short next bar at shortStop stop;
end;

mp = marketPosition;

If mp = 1 then buysToday = 1;
If mp = -1 then shortsToday = 1;

SetStopLoss(stopLoss$);
setProfitTarget(profTarg$);
setExitOnClose;
Orbo EasyLanguage Code

 

 

 

Highly Illogical – Best Guess Doesn’t Match Reality

An ES Break-Out System with Unexpected Parameters

I was recently testing the idea of a short term VBO strategy on the ES utilizing very tight stops.  I wanted to see if using a tight ATR stop in concert with the entry day’s low (for buys) would cut down on losses after a break out.  In other words, if the break out doesn’t go as anticipated get out and wait for the next signal.  With the benefit of hindsight in writing this post, I certainly felt like my exit mechanism was what was going to make or break this system.  In turns out that all pre conceived notions should be thrown out when volatility enters the picture.

System Description

  • If 14 ADX < 20 get ready to trade
  • Buy 1 ATR above the midPoint of the past 4 closing prices
  • Place an initial stop at 1 ATR and a Profit Objective of 1 ATR
  • Trail the stop up to the prior day’s low if it is greater than entryPrice – 1 ATR initially, and then trail if a higher low is established
  • Wait 3 bars to Re-Enter after going flat – Reversals allowed

That’s it.  Basically wait for a trendless period and buy on the bulge and then get it out if it doesn’t materialize.  I knew I could improve the system by optimizing the parameters but I felt I was in the ball park.  My hypothesis was that the system would fail because of the tight stops.  I felt the ADX trigger was OK and the BO level would get in on a short burst.  Just from past experience I knew that using the prior day’s price extremes as a stop usually doesn’t fair that well.

Without commission the initial test was a loser: -$1K and -$20K draw down over the past ten years.  I thought I would test my hypothesis by optimizing a majority of the parameters:

  • ADX Len
  • ADX Trigger Value
  • ATR Len
  • ATR BO multiplier
  • ATR Multiplier for Trade Risk
  • ATR Multiplier for Profit Objective
  • Number of bars to trail the stop – used lowest lows for longs

Results

As you can probably figure, I  had to use the Genetic Optimizer to get the job done.  Over a billion different permutations.  In the end here is what the computer pushed out using the best set of parameters.

No Commission or Slippage – Genetic Optimized Parameter Selection

Optimization Report – The Best of the Best

Top Parameters – notice the Wide Stop Initially and the Trailing Stop Look-Back and also the Profit Multiplier – but what really sticks out is the ADX inputs

ADX – Does it Really Matter?

Take a look at the chart – the ADX is mostly in Trigger territory – does it really matter?

A Chart is Worth a 1000 Words

What does this chart tell us?

70% of Profit was made in last 40 trades

Was the parameter selection biased by the heightened level of volatility?  The system has performed on the parameter set very well over the past two or three years.  But should you use this parameter set going into the future – volatility will eventually settle down.

Now using my experience in trading I would have selected a different parameter set.   Here are my biased results going into the initial programming.  I would use a wider stop for sure, but I would have used the generic ADX values.

George’s More Common Sense Parameter Selection – wow big difference

I would have used 14 ADX Len with a 20 trigger and risk 1 to make 3 and use a wider trailing stop.  With trend neutral break out algorithms, it seems you have to be in the game all of the time.  The ADX was supposed to capture zones that predicated break out moves, but the ADX didn’t help out at all.  Wider stops helped but it was the ADX values that really changed the complexion of the system.  Also the number of bars to wait after going flat had a large impact as well.  During low volatility you can be somewhat picky with trades but when volatility increases you gots to be in the game. – no ADX filtering and no delay in re-Entry.  Surprise, surprise!

Alogorithm Code

Here is the code – some neat stuff here if you are just learning EL.  Notice how I anchor some of the indicator based variables by indexing them by barsSinceEntry.  Drop me a note if you see something wrong or want a little further explanation.

Inputs: adxLen(14),adxTrig(25),atrLen(10),atrBOMult(1),atrRiskMult(1),atrProfMult(2),midPtNumBar(3),posMovTrailNumBars(2),reEntryDelay(3);
vars: mp(0),trailLongStop(0),trailShortStop(0),BSE(999),entryBar(0),tradeRisk(0),tradeProf(0);
vars: BBO(0),SBO(0),ATR(0),totTrades(0);

mp = marketPosition;
totTrades = totalTrades;
BSE = barsSinceExit(1);
If totTrades <> totTrades[1] then BSE = 0;
If totalTrades = 0 then BSE = 99;


ATR = avgTrueRange(atrLen);

SBO = midPoint(c,midPtNumBar) - ATR * atrBOMult;
BBO = midPoint(c,midPtNumBar) + ATR * atrBOMult;

tradeRisk = ATR * atrRiskMult;
tradeProf = ATR * atrProfMult;

If mp <> 1 and adx(adxLen) < adxTrig and BSE > reEntryDelay and open of next bar < BBO then buy next bar at BBO stop;
If mp <>-1 and adx(adxLen) < adxTrig AND BSE > reEntryDelay AND open of next bar > SBO then sellshort next bar at SBO stop;

If mp = 1 and mp[1] <> 1 then
Begin
	trailLongStop = entryPrice - tradeRisk;
end;

If mp = -1 and mp[1] <> -1 then
Begin
	trailShortStop = entryPrice + tradeRisk;
end;
	
if mp = 1 then sell("L-init-loss") next bar at entryPrice - tradeRisk[barsSinceEntry] stop;
if mp = -1 then buyToCover("S-init-loss") next bar at entryPrice + tradeRisk[barsSinceEntry] stop;


if mp = 1 then 
begin
	sell("L-ATR-prof") next bar at entryPrice + tradeProf[barsSinceEntry] limit;
	trailLongStop = maxList(trailLongStop,lowest(l,posMovTrailNumBars));
	sell("L-TL-Stop") next bar at trailLongStop stop;
end; 
if mp =-1 then 
begin
	buyToCover("S-ATR-prof") next bar at entryPrice -tradeProf[barsSinceEntry] limit;
	trailShortStop = minList(trailShortStop,highest(h,posMovTrailNumBars));
//	print(d, " Short and trailStop is : ",trailShortStop);
	buyToCover("S-TL-Stop") next bar at trailShortStop stop;
end;

Converting A String Date To A Number – String Manipulation in EasyLanguage

EasyLanguage Includes a Powerful String Manipulation Library

I thought I would share this function.  I needed to convert a date string (not a number per se) like “20010115” or “2001/01/15” or “01/15/2001” or “2001-01-15” into a date that TradeStation would understand.  The function had to be flexible enough to accept the four different formats listed above.

String Functions

Most programming languages have functions that operate strictly on strings and so does EasyLanguage.  The most popular are:

  • Right String (rightStr) – returns N characters from the right side of the string.
  • Left String (leftStr) – returns N character starting from the left side of the string
  • Mid String (midStr) – returns the middle portion of a string starting at a specific place in the string and advance N characters
  • String Length (strLen) – returns the number of characters in the string
  • String To Number (strToNum) – converts the string to a numeric representation.  If the string has a character, this function will return 0
  • In String (inStr) – returns location of a sub string inside a larger string ( a substring can be just one character long)

Unpack the String

If the format is YYYYMMDD format then all you need to do is remove the dashes or slashes (if there are any) and then convert what is left over to a number.   But if the format is MM/DD/YYYY format then we are talking about a different animal.  So how can you determine if the date string is in this format?  First off you need to find out if the month/day/year separator is a slash or a dash.  This is how you do this:

whereIsAslash = inStr(dateString,”/”);
whereIsAdash = inStr(dateString,”-“);

If either is a non zero then you know there is a separator.  The next thing to do is locate the first “dash or slash” (the search character or string).  If it is located within the first four characters of the date string then you know its not a four digit year.  But, lets pretend the format is “12/14/2001” so if the first dash/slash is the 3rd character you can extract the month string by doing this:

firstSrchStrLoc = inStr(dateString,srchStr);
mnStr= leftStr(dateString,firstSrchStrLoc-1);

So if firstSrchStrLoc = 3 then we want to leftStr the date string and extract the first two characters and store them in mnStr.  We then store what’s left of the date string in tempStr by using rightStr:

strLength = strLen(dateString);

tempStr = rightStr(dateString,strLength-firstSrchStrLoc);

Here I pass dateString and the strLength-firstSrchStrLoc – so if the dateString is 10 characters long and the firstSrchStrLoc is 3, then we can create a tempstring by taking [10 -3  = 7 ] characters from right side of the string:

“12/14/2001” becomes “14/2001” – once that is done we can pull the first two characters from the tempStr and store those into the dyStr [day string.]  I do this by searching for the “/” and storing its location in srchStrLoc.  Once I have that location I can use that information and leftStr to get the value I need.   All that is left now is to use the srchStrLoc and the rightStr function.

srchStrLoc = inStr(tempStr,srchStr);
dyStr = leftStr(tempStr,srchStrLoc-1);
yrStr = rightStr(tempStr,strLen(tempStr)-srchStrLoc);

Now convert the strings to numbers and multiply their values accordingly.

DateSTrToYYYMMDD = strToNum(yrStr) X 10000-19000000 + strToNum(mnStr) X 100 + strToNum(dyStr)

To get the date into TS format I have to subtract 19000000 from the year.  Remember TS represents the date in YYYMMDD  format.

Now what do  you do if the date is in the right format but simply includes the dash or slash separators.  All you need to do here is loop through the string and copy all non dash or slash characters to a new string and then convert to a number.  Here is the loop:

        tempStr = "";
        iCnt = 1;
        While iCnt <= strLength
        Begin
            If midStr(dateString,iCnt,1) <> srchStr then
            	tempStr += midStr(dateString,iCnt,1);
            	iCnt+=1;
        end;
        tempDate = strToNum(tempStr);
        DateStrToYYYMMDD = tempDate-19000000;

Here I use midStr to step through each character in the string.  MidStr requires a string and the starting point and how many characters you want returned from the string.  Notice I step through the string with iCnt and only ask for 1 character at a time.  If the character is not a dash or slash I concatenate tempStr with the non dash/slash character.  At the end of the While loop I simply strToNum the string and subtract 19000000.  That’s it!  Remember EasyLanguage is basically a full blown programming language with a unique set of functions that relate directly to trading.

Here is the function and testFunc caller.

STRINGFUNCANDFUNCCALLER

 

The Complete Turtle EasyLanguage [Well About as Close as You Can Get]

The Complete Turtle EasyLanguage – Almost!

I have seen a plethora of posts on the Turtle trading strategies where the rules and code are provided.  The codes range from a mere Donchian breakout to a fairly close representation.  Without dynamic portfolio feedback its rather impossible to program the portfolio constraints as explained by Curtis Faith in his well received “Way Of The Turtle.”  But the other components can be programmed rather closely to Curtis’ descriptions.   I wanted to provide this code in a concise manner to illustrate some of EasyLanguage’s esoteric constructs and some neat shortcuts.  First of all let’s take a look at how the system has performed on Crude for the past 15 years.

Turtle Performance on Crude past 15 years

If a market trends, the Turtle will catch it.  Look how the market rallied in 2007 and immediately snapped back in 2008, and look at how the Turtle caught the moves – impressive.  But see how the system stops working in congestion.  It did take a small portion of the 2014 move down and has done a great job of catching the pandemic collapse and bounce.  In my last post, I programmed the LTL (Last Trader Loser) function to determine the success/failure of the Turtle System 1 entry.  I modified it slightly for this post and used it in concert with Turtle System 2 Entry and the 1/2N AddOn pyramid trade to get as close as possible to the core of the Turtle Entry/Exit logic.

Can Your Program This – sure you CAN!

Can You Program This?

I will provide the ELD so you can review at your leisure, but here are the important pieces of the code that you might not be able to derive without a lot of programming experience.

If mp[1] <> mp and mp <> 0 then 
begin
	if mp = 1 then
	begin	
		origEntry = entryPrice;
		origEntryName = "Sys1Long";
		If ltl = False and h >= lep1[1] then origEntryName = "Sys2Long";
	end;
	if mp =-1 then
	begin	
		origEntry = entryPrice;
		origEntryName = "Sys1Short";
		If ltl = False and l <= sep1[1] then origEntryName = "Sys2Short";
	end;
end;
Keeping Track Of Last Entry Signal Price and Name

This code determines if the current market position is not flat and is different than the prior bar’s market position.  If this is the case then a new trade has been executed.  This information is needed so that you know which exit to apply without having to forcibly tie them together using EasyLanguage’s from Entry keywords.  Here I just need to know the name of the entry.  The entryPrice is the entryPrice.  Here I know if the LTL is false, and the entryPrice is equal to or greater/less  than (based on current market position) than System 2 entry levels, then I know that System 2 got us into the trade.

If mp = 1 and origEntryName = "Sys1Long" then Sell currentShares shares next bar at lxp stop;
If mp =-1 and origEntryName = "Sys1Short" then buyToCover currentShares shares next bar at sxp stop;

//55 bar component - no contingency here
If mp = 0 and ltl = False then buy("55BBO") next bar at lep1 stop;
If mp = 1 and origEntryName = "Sys2Long" then sell("55BBO-Lx") currentShares shares next bar at lxp1 stop;

If mp = 0 and ltl = False then sellShort("55SBO") next bar at sep1 stop;
If mp =-1 and origEntryName = "Sys2Short" then buyToCover("55SBO-Sx") currentShares shares next bar at sxp1 stop;
Entries and Exits

The key to this logic is the keywords currentShares shares.  This code tells TradeStation to liquidate all the current shares or contracts at the stop levels.  You could use currentContracts contracts if you are more comfortable with futures vernacular.

AddOn Pyramiding Signal Logic

Before you can pyramid you must turn it on in the Strategy Properties.

Turn Pyramiding ON
If mp = 1 and currentShares < 4 then buy("AddonBuy") next bar at entryPrice + (currentShares * .5*NValue) stop;
If mp =-1 and currentShares < 4 then sellShort("AddonShort") next bar at entryPrice - (currentShares * .5*NValue) stop;

This logic adds positions on from the original entryPrice in increments of 1/2N.  The description for this logic is a little fuzzy.  Is the N value the ATR reading when the first contract was put on or is it dynamically recalculated?  I erred on the side of caution and used the N when the first contract was put on.  So to calculate the AddOn long entries you simply take the original entryPrice and add the currentShares * .5N.  So if currentShares is 1, then the next pyramid level would be entryPrice + 1* .5N.  If currentShares is 2 ,then entryPrice + 2* .5N and so on an so forth.  The 2N stop trails from the latest entryPrice.  So if you put on 4 contracts (specified in Curtis’ book), then the trailing exit would be 2N from where you added the 4th contract.  Here is the code for that.

Liquidate All Contracts at Last Entry –  2N

vars: lastEntryPrice(0);
If cs <= 1 then lastEntryPrice = entryPrice;
If cs > 1 and cs > cs[1] and mp = 1 then lastEntryPrice = entryPrice + ((currentShares-1) * .5*NValue);
If cs > 1 and cs > cs[1] and mp =-1 then lastEntryPrice = entryPrice - ((currentShares-1) * .5*NValue);

//If mp = -1 then print(d," ",lastEntryPrice," ",NValue);

If mp = 1 then sell("2NLongLoss") currentShares shares next bar at lastEntryPrice-2*NValue stop;
If mp =-1 then buyToCover("2NShrtLoss") currentShares shares next bar at lastEntryPrice+2*NValue Stop;
Calculate Last EntryPrice and Go From There

I introduce a new variable here: cs.  CS stands for currentShares and I keep track of it from bar to bar.  If currentShares or cs is less than or equal to1 I know that the last entryPrice was the original entryPrice.  Things get a little more complicated when you start adding positions – initially I couldn’t remember if EasyLanguage’s entryPrice contained the last entryPrice or the original – turns out it is the original – good to know.  So, if currentShares is greater than one and the current bar’s currentShares is greater than the prior bar’s currentShares, then I know I added on another contract and therefore must update lastEntryPrice.  LastEntryPrice is calculated by taking the original entryPrice and adding (currentShares-1) * .5N.  Now this is the theoretical entryPrice, because I don’t take into consideration slippage on entry.  You could make this adjustment.  So, once I know the lastEntryPrice I can determine 2N from that price.

Getting Out At 2N Trailing Stop

If mp = 1 then sell("2NLongLoss") currentShares shares next bar at lastEntryPrice-2*NValue stop;
If mp =-1 then buyToCover("2NShrtLoss") currentShares shares next bar at lastEntryPrice+2*NValue Stop;
Get Out At LastEntryPrice +/-2N

That’s all of the nifty code.  Below is the function and ELD for my implementation of the Turtle dual entry system.   You will see some rather sophisticated code when it comes to System 1 Entry and this is because of these scenarios:

  • What if you are theoretically short and are theoretically stopped out for a true loser and you can enter on the same bar into a long trade.
  • What if you are theoretically short and the reversal point would result in a losing trade.  You wouldn’t  record the loser in time to enter the long position at the reversal point.
  • What if you are really short and the reversal point would results in a true loser, then you would want to allow the reversal at that point

There are probably some other scenarios, but I think I covered all bases.  Just let me know if that’s not the case.  What I did to validate the entries was I programmed a 20/10 day breakout/failure with a 2N stop and then went through the list and deleted the trades that followed a non 2N loss (10 bar exit for a loss or a win.)  Then I made sure those trades were not in the output of the complete system.  There was quite a bit of trial and error.  If you see a mistake, like I said, just let me know.

Remember I published the results of different permutations of this strategy incorporating dynamic portfolio feedback at my other site www.trendfollowingsystems.com.  These results reflect the a fairly close portfolio that Curtis suggests in his book.

TURTLELTLFUNCTEST

 

Implementing Finite State Machine Functionality with EasyLanguage (Last Trade Was Loser Filter a la Turtle)

Last Trade Was a Loser Filter – To Use or Not To Use

Premise

A major component of the Turtle algorithm was to skip the subsequent 20-day break out if the prior was a winner.  I guess Dennis believed the success/failure of a trade had an impact on the outcome of the subsequent trade.  I have written on how you can implement this in EasyLanguage in prior posts, but I have been getting some questions on implementing FSM in trading and thought this post could kill two birds with one stone: 1) provide a template that can be adapted to any LTL mechanism and 2) provide the code/structure of setting up a FSM using EasyLanguage’s Switch/Case structure.

Turtle Specific LTL Logic

The Turtle LTL logic states that a trade is a loser if a 2N loss occurs after entry.  N is basically an exponential-like moving average of TrueRange.  So if the market moves 2N against a long or short position and stops you out, you have a losing trade.  What makes the Turtle algorithm a little more difficult is that you can also exit on a new 10-day low/high depending on your position.  The 10-day trailing exit does not signify a loss.  Well at least in this post it doesn’t.  I have code that says any loss is a loss, but for this explanation let’s just stick to a 2N loss to determine a trade’s failure.

How To Monitor Trades When Skipping Some Of Them

This is another added layer of complexity.  You have to do your own trade accounting behind the scenes to determine if a losing trade occurs.  Because if you have a winning trade you skip the next trade and if you skip it how do you know if it would have been a winner or a loser.  You have to run a theoretical system in parallel with the actual system code.

Okay let’s start out assuming the last trade was a winner.  So we turn real trading off.  As the bars go by we look for a 20-Day high or low penetration.  Assume a new 20-Day high is put in and a long position is established at the prior 20-Day high.  At this point you calculate a 2N amount and subtract if from the theoretical entry price to obtain the theoretical exit price.  So you have a theoMP (marketPosition) and a theoEX (exit price.)  This task seems pretty simple, so you mov on and start looking for a day that either puts in a new 10-Day low or crosses below your theoEX price.  If a new 10-Day low is put in then you continue on looking for a new entry and a subsequent 2N loss.  If a 2N loss occurs, then you turn trading back on and continue monitoring the trades – turning trading off and then back on when necessary.  In the following code I use these variables:

  • state – 0: looking for an entry or 1: looking for an exit
  • lep – long entry price
  • sep– short entry price
  • seekLong – I am seeking a long position
  • seekShort – I am seeking a short position
  • theoMP – theoretical market position
  • theoEX – theoretical exit price
  • lxp – long exit price
  • sxp – short exit price

Let’s jump into the Switch/Case structure when state = 0:

	Switch(state)
	Begin
		Case 0:
			lep = highest(h[1],20) + minMove/priceScale;
			sep = lowest(l[1],20) - minMove/priceScale;
			If seekLong and h >= lep then 
			begin
				theoMP = 1;
				theoEX = maxList(lep,o) - 2 * atr; 
//				print(d," entered long >> exit at ",theoEX," ",atr);
			end;
			If seekShort and l <= sep then 
			begin
				theoMP = -1;
				theoEX = minList(sep,o) + 2 * atr;
			end;
			If theoMP <> 0 then 
			begin
				state = 1;
				cantExitToday = True;
			end;
State 0 (Finite State Set Up)

The Switch/Case is a must have structure in any programming language.  What really blows my mind is that Python doesn’t have it.  They claim its redundant to an if-then structure and it is but its so much easier to read and implement.  Basically you use the Switch statement and a variable name and based on the value of the variable it will flow to whatever case the variable equates to.  Here we are looking at state 0.  In the CASE: 0  structure the computer calculates the lep and sep values – long and short entry levels.  If you are flat then you are seeking a long or a short position.  If the high or low of the bar penetrates it respective trigger levels then theoMP is set to 1 for long or -1 for short.  TheoEX is then calculated based on the atr value on the day of entry.  If theoMP is set to either a 1 or -1, then we know a trade has just been triggered.  The Finite State Machine then switches gears to State 1.  Since State = 1 the next Case statement is immediately evaluated.  I don’t want to exit on the same bar as I entered (wide bars can enter and exit during volatile times) I use a variable cantExitToday.  This variable delays the Case 1: evaluation by one bar.

State = 1 code:

		Case 1:
			If not(cantExitToday) then
			begin
				lxp = maxList(theoEX,lowest(l[1],10)-minMove/priceScale);
				sxp = minList(theoEX,highest(h[1],10)+minMove/priceScale);	
				If theoMP = 1 and l <= lxp then
				begin
					theoMP = 0;
					seekLong = False;
					if lxp <= theoEX then 
						ltl = True
					Else 
						ltl = False;
				end;
				If theoMP =-1 and h >= sxp then
				begin
					theoMP = 0;
					seekShort = False;
					if sxp >= theoEX then 
						ltl = True
					else 
						ltl = False;
				end;
				If theoMP = 0 then state = 0;
			end;
			cantExitToday = False;	
	end;
State = 1 (Switching Gears)

Once we have a theoretical position, then we only examine the code in the Case 1: module.  On the subsequent bar after entry, the lxp and sxp (long exit and short exit prices) are calculated.  Notice these values use maxList or minList to determine whichever is closer to the current market action – the 2N stop or the lowest/highest low/high for the past 10-daysLxp and sxp are assigned whichever is closer.  Each bar’s high or low is compared to these values.  If theoMP = 1 then the low is compared to lxp.  If the low crosses below lxp, then things are set into motion.  The theoMP is immediately set to  0 and seekLong is turned to False.  If lxp <= a 2N loss then ltl (last trade loser) is set to true.  If not, then ltl is set to False.   If theoMP = 0 then we assume a flat position and switch the FSM back to State 0 and start looking for a new trade.  The ltl variable is then used in the code to allow a real trade to occur.

Strategy Incorporates Our FSM Output

vars:N(0),mp(0),NLossAmt(0);
If barNumber = 1 then n = avgTrueRange(20);
if barNumber > 1 then n = (n*19 + trueRange)/20;

If useLTLFilter then
Begin
	if ltl then buy next bar at highest(h,20) + minMove/priceScale stop;
	if ltl then sellShort next bar at lowest(l,20) -minMove/priceScale stop;
end
Else
Begin
	buy next bar at highest(h,20) + minMove/priceScale stop;
	sellShort next bar at lowest(l,20) -minMove/priceScale stop;
end;

mp = marketPosition;

If mp <> 0 and mp[1] <> mp then NLossAmt = 2 * n;

If mp = 1 then
Begin
	Sell("LL10-LX") next bar at lowest(l,10) - minMove/priceScale stop;
	Sell("2NLS-LX") next bar at entryPrice - NLossAmt stop;
end;
If mp =-1 then
Begin
	buyToCover("HH10-SX") next bar at highest(h,10) + minMove/priceScale stop;
	buyToCover("2NLS-SX") next bar at entryPrice + NLossAmt stop;
end;
Strategy Code Using ltl filter

This code basically replicates what we did in the FSM, but places real orders based on the fact that the Last Trade Was A Loser (ltl.)

Does It Work – Only Trade After a 2N-Loss

Last Trade Loser In Action

Without Filter on the last 10-years in Crude Oil

With Filter on the last 10-years in Crude Oil

I have programmed this into my TradingSimula-18 software and will show a portfolio performance with this filter a little later at www.trendfollowingsystems.com.

I had to do some fancy footwork with some of the code due to the fact you can exit and then re-enter on the same bar.  In the next post on this blog I will so you those machinations .  With this template you should be able to recreate any last trade was a loser mechanism and see if it can help out with your own trading algorithms.  Shoot me an email with any questions.