Don’t Fool Yourself – Limitations of Back Testing with Daily Data [EasyLanguage]

Which equity curve do you like best? (created with EasyLanguage script) This one…

Or this one?

Obviously the first one.  Even though it had a substantial draw down late in the test.  What if I told you that the exact same system logic generated both curves?  Here is the EasyLanguage code for this simple system.

Buy next bar at open of next bar + .25 *avgTrueRange(10) stop;
Sellshort next bar at open of next bar - .25*avgTrueRange(10) stop;

setStopLoss(500);
setProfitTarget(1000);
Open Range Break Out with Profit and Loss Objective

This algorithm relies heavily on needing to know which occurred first: the high or the low of the day.   The second chart tells the true story because it looks inside the daily bar to see what really happened.  The first chart uses an algorithm to try to determine which happened first and applies this to the trades.  In some instances,  the market looks like it opens then has a slight pull back and then goes up all day.  As a result the system buys and holds the trade through the close and onto the next day, but in reality the market opens, goes up and triggers a long entry, then retraces and you get stopped out.  What was a nice winner turns into a bad loss.  Here is an example of what might have happened during a few trades:

Nice flow – sold, bought, sold, bought, sold again and finally a nice profit.  But this is what really happened:

Sold, bought, reversed short on same day and stopped out on same day.  Then sold and reversed long on same day and finally sold and took profit.   TradeStation’s Look Inside Bar feature helps out when your system needs to know the exact path the market made during the day.  In many cases, simply clicking this feature to on will take care of most of your testing needs.  However, this simple algorithm needs to place or replace orders based on what happens during the course of the day.  With daily bars you are sitting on the close of the prior day spouting off orders.  So once the new day starts all of your orders are set.  You can’t see this initially on the surface, because it seems the algorithm is so simple.   Here is another consequence of day bar testing when the intra-day market movement is paramount:

Here the computer is doing exactly what you told it!  Sell short and then take a profit and sell short 25% of the ATR below the open.  Well once the system exited the short it realized it was well below the sell entry point so it immediately goes short at the exact same price (remember TS doesn’t allow stop limit orders).  You told the computer that you wanted to be short if the market moves a certain amount below the open.  These were the orders that were place on yesterday’s close  This may not be exactly what you wanted, right?  You probably wanted to take the profit and then wait for the next day to enter a new trade.  Even if you did want to still be short after the profit level was obtained you wouldn’t want to exit and then reenter at the same price (practically impossible) and be levied a round-turn slip and commission.   You could fiddle around with the code and try to make it work, but I guarantee you that a system like this can only be tested properly on intra-day data.  Let’s drop down to a lower time frame, program the system and see what the real results look like:

Looks very similar to the daily bar chart with Look Inside Bar turned on.  However, it is different.  If you wan’t to gauge a systems potential with a quick program, then go ahead and test on daily bars with LIB turned on.  If it shows promise, then invest the time and program the intra-day version just to validate your results.  What do you mean spend the time?  Can’t you simply turn your chart from daily bars to five minute bars and be done with it.  Unfortunately no!  You have to switch paradigms and this requires quite a bit more programming.  Here is our simple system now in EasyLanguage:

Vars:stb(0),sts(0),atr(0),icnt(0);
Vars:buysToday(0),sellsToday(0),mp(0);

{Use highD() and XXXXD(0)  functions to capture the highs, lows, and closes for the past 10 days.  
 I could have just used a daily bar as data2.  
 I am looking at five minute bars so we know how the market flows through the day.
}

{This loop kicks out a warning message, but seems to work
 Just do this once at the beginning of the day - faster}

{remember true range is either the higher of todays high
 Or yesterdays close minus the lower of todays low or 
 yesterdays close}

{ tradeStation time stamps at the close of the bar so
 we capture the opening of the open time plus the bar interval - 
 in this case 5 minute - so at 1800 + 5 (1805) I capture the open
 of the day}

if time = sess1StartTime + barInterval then  
begin
	Value1 = 0.0;
	for icnt = 1 to 10
	begin
		Value1 = value1 + maxList(closeD(icnt-1),highD(icnt)) - minList(closeD(icnt-1),lowD(icnt));
	end;
	atr = value1/10.0;
	stb = open + .25* atr;
	sts = open - .25* atr;
	buysToday = 0;
	sellsToday = 0;
end;

mp = marketPosition; {The ole mp trick}

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

if buysToday = 0  and time < sess1EndTime and close <= stb then buy next bar at stb stop;
if sellsToday = 0 and time < sess1EndTime and close >= sts then sellshort next bar at sts stop;

setStopLoss(500);
setProfitTarget(1000);
Open Range Break Out Utilizing Five Minute Bars

Here is a validation that Look Inside Bar does work:

This is the trade from June 1st.  Scroll back up to the second chart where LIB is turned on.

Original Camarilla EasyLanguage Code [Correction]

Camarilla – A group of confidential, often scheming advisers; a cabal.

An attentive reader of this blog, Walter Baker,  found some typos in my code.  I have corrected them in the code section – if you have used this code make sure you copy and paste the code in its entirety into your EasyLanguage editor and replace your prior version.

I wanted to elaborate on the original version of Camarilla.  The one that users have been downloading from this website is pure reversion version.  The Camarilla Equation was by created by Nick Scott, a bond day trader, in 1989.  The equation uses just yesterday’s price action to project eight support/resistance price levels onto today’s trading action.  These levels, or advisers, as the name of the equation suggests provides the necessary overlay to help predict turning points as well as break outs.  Going through many charts with the Camarilla indicator overlay it is surprising how many times the market does in fact turn at one of these eight price levels.  The equations that generate the support/resistance levels are mathematically simple:

 

Resistance #4 = Close + Range * 1.1 / 2;

Resistance #3 = Close + Range * 1.1/4;

Resistance #2 = Close + Range * 1.1/6;

Resistance #1 = Close + Range * 1.1/12;

 

Support #1 = Close – Range * 1.1/12;

Support #2 = Close – Range * 1.1/6;

Support #3 = Close – Range * 1.1/4;

Support #4 = Close – Range * 1.1/2;

 

The core theory behind the equation and levels is that prices have a tendency to revert to the mean.  Day trading the stock indices would be easy if price broke out and continued in that direction throughout the rest of the day.  We all know that “trend days” occur very infrequently on a day trade basis; most of the time the indices just chop around without any general direction.  This is where the Camarilla can be effective.  Take a look at the following chart [ ES 5-minute day session] where the indicator is overlaid. and how the strategy was able to take advantage of the market’s indecisiveness.  This particular example shows the counter-trend nature of the Camarilla.  The original Camarilla looked at where the market opened to make a trading decision.  The chart below is an adapted version of the one I send out when one registers on for the download.   I thought it would be a good idea to show the original that incorporates a break out along with the counter trend mechanism.  I will go over the code in the next post.  You can copy the code below and paste directly into you EasyLanguage editor.

Camarilla at its Best!
Carmarilla in Reversion Mode

Original Camarilla rules:

  • If market opens between R3 and R4 go with the break out of R4.  This is the long break out part of the strategy.
  • If market opens between R3 and S3 then counter trend trade at the R3 level.  In other words, sell short at R3.  If the market moves down, then buy S3.  As you can see this is the mean reversion portion of the strategy.
  • If market open between S3 and S4 go with the break out of S4 – the short break out method.
  • Stops are placed in the following manner:
    • If long from a R4 break-out, then place stop at R3.
    • If short from a S4 break-out, then place stop at S3.
    • If long from a R3 countertrend, then place stop at R4.
    • If short from a S3 countertrend, then place stop at S4.
  • Profit objectives can be placed at opposite resistance/support levels:
    • If short from a R3 countertrend, then take profits at S1, S2, or S3.
    • If long from a S3 countertrend, then take profits at R1, R2, or R3.

Profit objectives for all trades can be a dollar, percent of price of ATR multiple.

Example of Camarilla Break Out Trade from S4
Camarilla Break Out
inputs: endTradetime(1530);
vars:	R1(0),R2(0),R3(0),R4(0),S1(0),S2(0),S3(0),S4(0),pivotPoint(0),myAvg(0);
vars: buyTrig(0),sellTrig(0),waitBar(0),s3Pen(0),r3Pen(0),s4Pen(0),r4Pen(0),s2Pen(0),r2Pen(0);
vars: buysToday(0),sellsToday(0);
vars: s3_s4(0),s2_s3(0),s1_s2(0),s4_s5(0);
vars: r1_r2(0),r2_r3(0),r3_r4(0),r4_r5(0);
vars: r1_s1(0),r3_s3(0),r4_s4(0),r5_s5(0);

if date <> date[1] then
begin
	buyTrig = 0;
	sellTrig = 0;
	waitBar = 0;
	
	s3Pen = 0;
	r3Pen = 0;
	s4Pen = 0;
	r4Pen = 0;
	s2Pen = 0;
	r2Pen = 0;
	
	buysToday = 0;
	sellsToday = 0;
	
	r4_r5 = 0;
	r3_r4 = 0;
	r2_r3 = 0;
	r1_r2 = 0;
	
	r1_s1 = 0;
	r3_s3 = 0;
	r4_s4 = 0;
	r5_s5 = 0;
	
	s1_s2 = 0;
	s2_s3 = 0;
	s3_s4 = 0;
	s4_s5 = 0;

end;

waitBar = waitBar + 1;

R4 = CloseD(1)+(HighD(1)-LowD(1)) * 1.1 / 2;
R3 = CloseD(1)+(HighD(1)-LowD(1)) * 1.1/4;
R2 = CloseD(1)+(HighD(1)-LowD(1)) * 1.1/6;
R1 = CloseD(1)+(HighD(1)-LowD(1)) * 1.1/12;
S1 = CloseD(1)-(HighD(1)-LowD(1)) * 1.1/12;
S2 = CloseD(1)-(HighD(1)-LowD(1)) * 1.1/6;
S3 = CloseD(1)-(HighD(1)-LowD(1)) * 1.1/4;
S4 = CloseD(1)-(HighD(1)-LowD(1)) * 1.1/2;

if openD(0)<= s4 then s4_s5 = 1;

If openD(0)> s4 and openD(0) <= s3 then s3_s4 = 1;
If openD(0)> s3 and openD(0) <= s2 then s2_s3 = 1;
If openD(0)> s2 and openD(0) <= s1 then s1_s2 = 1;

If openD(0)> s1 and openD(0) <= r1 then r1_s1 = 1;

If openD(0)> r1 and openD(0) <= r2 then r1_r2 = 1;
If openD(0)> r2 and openD(0) <= r3 then r2_r3 = 1;
If openD(0)> r3 and openD(0) <= r4 then r3_r4 = 1;

If openD(0)> r4 then r4_r5 = 1;

if openD(0) < r3 and openD(0) > s3 then r3_s3 = 1;
If openD(0) < r4 and openD(0) > s4 then r4_s4 = 1;


if time < endTradeTime and time > 930 then
begin
	
	
	if r3_r4 = 1 and entriesToday(date) < 3 and  c < r4 then buy("R4-BrkOut") next bar at r4 stop;
	if s3_s4 = 1 and entriesToday(date) < 3 and  c > s4 then sellShort("S4-BrkOut") next bar at s4 stop;
	
	if c > r2 then r2Pen = 1;
	if c > r3 then r3Pen = 1;
	if c > r4 then r4Pen = 1;
	
	if r3_s3 = 1 and r3Pen = 1 and c > r3 and entriesToday(date) < 3 
		 then sellShort("R3Sell") next bar at r3 stop;
		 
	if r4Pen = 1 and c < r4 then r4Pen = 0;	
	if r3Pen = 1 and c < r3 then r3Pen = 0;	
	if r2Pen = 1 and c < r2 then r2Pen = 0;

	if c < r1 then 
	begin
		r2Pen = 0;
		r3Pen = 0;
		r4Pen = 0;
	end;
	if c > s1 then
	begin
		s2Pen = 0;
		s3Pen = 0;
		s4Pen = 0;
	end;

	if c < s2 then s2Pen = 1;
	if c < s3 then s3Pen = 1;
	if c < s4 then s4Pen = 1;

	if r3_s3 = 1 and s3Pen = 1 and c < s3 and entriesToday(date) < 3 then 
		buy("S3Buy") next bar at s3 stop;
		
	if s4Pen = 1 and c > s4 then s4Pen = 0;	
	if s3Pen = 1 and c > s3 then s3Pen = 0;
	if s2Pen = 1 and c > s2 then s2Pen = 0;
		
	if marketPosition = 1 then
	begin
		sell from entry("S3Buy") next bar at s4 stop;
		sell from entry("R4-BrkOut") next bar at r3 stop;
	end;
	
	if marketPosition = -1 then
	begin
		buyToCover from entry("R3Sell") next bar at r4 stop;
		buyToCover from entry("S4-BrkOut") next bar at s3 stop;
	end;


end;
 
setExitOnClose;
Camarilla Strategy EasyLanguage Source

Don’t Forget To Make Copies of your EasyLanguage in Text/ASCII

Well it happened again.  I had four analysis techniques open in my TradeStation Development Environment and my TS crashed and when I went back to work after restarting they were all garbage.  The easiest way to back up your code is to have NotePad open and while working on your EasyLanguage copy and past your code into a NotePad file.  Do this in stages and if you like you can keep different revisions.   If you don’t want to keep revisions just select all and replace with the code you are copying from the TDE.  Make sure you copy your final changes so that you can archive this in a separate folder and store it on your computer, flash drive or the cloud.  Remember TradeStation stores your analysis techniques in one big library glob.  If that glob gets corrupted and you don’t back up your TradeStation periodically, then you run  a chance of losing all of your hard work.  So definitely back up TradeStation automatically using their software and also back up your coding in a NotePad file.

VBA for Excel Code to Open Multiple Text Files

For all of you who want to open multiple text files from within VBA here is the code that will do just that. Here I open a Open File Dialog and enable multiple file selection. I then parse the return string to gather the path and individual file names. The data is then processed – I am looking for a file with a date and a value separated by a comma.  You can basically do anything you want with VBA and you also have the EXCEL functions right at your fingertips.  Plus output to a worksheet is ultra simple.

 Set fd = Application.FileDialog(msoFileDialogOpen)
    With fd
        Set ffs = .Filters
        With ffs
            .Clear
            .Add "CSV Data", "*.csv; *.txt"
        End With
        .AllowMultiSelect = True
        If .Show = False Then Exit Sub
        fcnt = 0
        maxDate = DateValue("01/01/1900")
        minDate = DateValue("12/31/2200")
        prevSymbol = "**"
        dateNewestToOldest = False
        For Each it In fd.SelectedItems
			fcnt = fcnt + 1
            filePath(fcnt) = it
            fileName(fcnt) = Right(filePath(fcnt), Len(filePath(fcnt)) - InStrRev(filePath(fcnt), "\"))
            symbol(fcnt) = Left(fileName(fcnt), 2)
            Set DataHolder = New DataClass
                        
            If symbol(fcnt) <> prevSymbol Then symbolHolder.Add symbol(fcnt)
            prevSymbol = symbol(fcnt)
            dataCnt = 0
            Open filePath(fcnt) For Input As #1
            rowNumber = 1
			Do Until EOF(1)
				Line Input #1, lineFromFile
                dataCnt = dataCnt + 1
                If dataCnt > skipLines Then
					lineFromFile = Replace(lineFromFile, " ", "")
                    lineItems = Split(lineFromFile, ",")
                    numItems = UBound(lineItems) - LBound(lineItems) + 1
                    isDash1 = InStr(lineItems(0), "-")
                    isSlash1 = InStr(lineItems(0), "/")
                    If isDash1 > 0 Or isSlash1 > 0 Then
                        inputDate = CDate(lineItems(0))
                    Else
                        myYear = Left(lineItems(0), 4)
                        myMonth = Mid(lineItems(0), 5, 2)
                        myDay = Right(lineItems(0), 2)
                        inputDate = DateSerial(Left(lineItems(0), 4), Mid(lineItems(0), 5, 2), Right(lineItems(0), 2))
                    End If
                	If dataCnt > 1 Then
                    	If inputDate < tempDate Then dateNewestToOldest = True
                	End If
                	If inputDate > maxDate Then maxDate = inputDate
                	If inputDate < minDate Then minDate = inputDate
                	Set DataHolder = New DataClass
                	DataHolder.SymbolName = symbol(fcnt)
                	DataHolder.SymbolNum = fcnt
                	DataHolder.EquDate = inputDate
                	DataHolder.EquVal = lineItems(1)
                	myEquityCollection.Add DataHolder
                	tempDate = inputDate              
				End If
            Loop
        Close #1
        Next it
    End With
VBA code to open multiple text files

Day Of Week Analysis using a Method In EasyLanguage

One metric that seems to be missing from TradeStation is a Day Of Week analysis.  It would be nice, but I don’t know how helpful, to know the $P/L breakdown on a weekday basis; does your system make all of its money on Mondays and Fridays?  I created the code that will print out to the print log using an EasyLanguage method.  A method is a subroutine that can be included in the main code (strategy, indicator, etc.,.)  The global parameters to the main program can be seen inside the method.  You can localize variable scope to the method by declaring the variable within the method’s body.  A method is a great way to modularize your programming, but it is not the best way to reuse software; the method is accessible only to the main program.  This EasyLanguage also utilizes an array and shows a neat piece of code to access the array elements and align them with the day of the week.  The dayOfWeek() function returns [1..5] depending on what day of the week the trading day falls on.  Monday = 1 and Friday = 5.  The array has five elements and each element accumulates the $P/L for each of the five days based on when the trade was initiated.

vars: mp(0);
array: weekArray[5](0);

method void dayOfWeekAnalysis()   {method definition}
var: double tradeProfit;
begin
	If mp = 1 and mp[1] = -1 then tradeProfit = (entryPrice(1) - entryPrice(0))*bigPointValue;
	If mp = -1 and mp[1] = 1 then tradeProfit = (entryPrice(0) - entryPrice(1))*bigPointValue;
	weekArray[dayOfWeek(entryDate(1))] = weekArray[dayOfWeek(entryDate(1))] + tradeProfit;
end; 
Buy next bar at highest(high,9)[1] stop;
Sellshort next bar at lowest(low,9)[1] stop;

mp = marketPosition;

if mp <> mp[1] then dayOfWeekAnalysis();

If lastBarOnChart then
Begin
	print("Monday ",weekArray[1]);
	print("Tuesday ",weekArray[2]);
	print("Wednesday ",weekArray[3]);
	print("Thursday ",weekArray[4]);
	print("Friday ",weekArray[5]);
end;
Code using METHOD and ARRAY manipulation

Here is an example of the print out created by running this simple EasyLanguage strategy.  Maybe don’t trade on Monday?  Or is that curve fitting.  This is an interesting tool and might carry more weight if applied to day trade algorithm.  Maybe!

  • Monday -8612.50
  • Tuesday 6350.00
  • Wednesday 2612.50
  • Thursday -937.50
  • Friday -1987.50

Connors and Raschke (Momentum Pinball)™ EasyLanguage

In the Connors Raschke book, “Street Smarts – High Probability Short-Term Trading Strategies” published by M. Gordon Publishing Group 1995, the authors describe a strategy that incorporates several ideas and functions that can be difficult to program into TradeStation.  I highly suggest purchasing the book as it has some very interesting ideas and comes from the minds of two very accomplished traders.

The strategy is conceptually simple and uses the Taylor Buy/Sell day concept.   On buy days, buy on the penetration of the first hour high and use the extreme of the first hour as the stop.  If you get stopped out the system will allow you to re-enter once more on a re-penetration of the first hour high.  If the trade is profitable at the close of the day session then exit on the open of the following day.  Selling short is just the opposite.  The Buy Day is defined when the three day RSI of the one day ROC (close[0] – close[1]) is below 30 and the Sell Day  is defined when this indicator rises above 70.  As you can see, pretty simply.

The first problem when programming this strategy is the mixing of the 5 minute bars (for trade execution) and daily bars (calculating the indicator.)  How do you calculate an RSI(c[0] – c[1],3) indicator value when looking at a 5 minute bar chart.   This turns out to be pretty simple since TradeStation allows the mixing of different time frames on the same chart.  First plot a 5 minute bar of the @ES.D as data 1 and then insert a daily bar of the @ES.D as data 2.   The first step in programming this strategy is setting up the indicator.

//EasyLanguage

Vars: rsiVal(0,data2);

rsiVal = rsi(close of data2 - close[1] of data2,3);
Setting up the nested indicator.

Notice how I declared the variable rsiVal?  I initially set it to zero and tie it to data2.  This tying or aliasing limits the variable from being updated on each five minute bar.  Remember we want the RSI calculation done on the daily bars.  The RSI function allows the embedding of calculations as well as different prices.  Here we are telling the computer to look at the momentum of today’s close versus yesterday’s  and apply the RSI  3 – period calculation.

In their book Connors and Raschke determined that it would be better to buy after an RSI reading of below 30 and sell after an RSI above 70.  Once we have this set, then the fun really begins with this type of programming.  If you can do this type of programming, there’s very little you can’t do.  This is because we are solving a few limitations of EasyLanguage by logically addressing the what we need from language. In this example, we have to capture the range of the first hour.  There really isn’t a built-in way to do this or at least not an easy one.  When testing intra-day, I always like using five minute bars.  This time frame is small enough that not much usually happens during this t time and it isn’t much of a resource hog – like say minute or smaller bars. Since there are 12 five minute bars in an hour, I wait until the 12th bar of the day is completed to determine the first hour’s range and its extremes.  I set the dayBarCount to zero on the first bar of the day and then increment the variable on each bar.

If date <> date[1] then
begin
	dayBarCount = 0;
	buysToday = 0;
	sellsToday = 0;
end;

dayBarCount = dayBarCount + 1;

If dayBarCount = 12 then
Begin
	buyLevel = highD(0) + minMove/priceScale;
	sellLevel = lowD(0) - minMove/priceScale;
end;
Tracking the number of bars and capturing first hour.

TradeStation has made this process simpler by providing the highD and lowD functions.  When you call these functions the current daily bar extremes are returned at the point in the day.  So the functions are called on the 12th bar and they return the high and low of the first hour.   The functions are not called again until the next day.  Oh yeah – passing a zero as the function argument informs EasyLanguage to return today’s values.  A 1 would return yesterday’s values.  These are nifty functions if you aren’t incorporating a secondary daily data stream.  Even if you are, like in our example, they still come in handy.

mp = marketPosition;

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

If dayBarCount > 12 and time < 1430 then
Begin
	if rsiVal < 30 and mp <> 1 and buysToday < 2 then buy next bar at buyLevel Stop;
	If rsiVal > 70 and mp <> -1 and sellsToday < 2 then sellShort next bar at sellLevel stop;
end;
Order placement directives and the use of mp.

I also keep track of the number of buys and shorts for the day.  The books states that a re-entry is possible, so I allow up to two trade entries.  I look and the current MP value and its prior value to see if  the system went from one state to another.  If current MP is long and the prior reading is not long, then a long entry was undoubtedly entered.  You can use this logic (I did for illustrative purposes) or use the built-in functions entriesShortToday/entriesLongToday to determine the number of trades for the day.  If the rsiValue < 30 and less than two long entries have occurred, then you can buy on a breakout of the first hourly bar.  Entering short is the opposite, but with the RSI reading of greater than 70.

If mp = 1 then
Begin
	Sell next bar at sellLevel stop;
end;

If mp= -1 then
Begin
	Buytocover next bar at buyLevel stop;
end;

If time = 1510 and openPositionProfit < 0 then setExitOnClose;
If time = 1515 then
Begin
	If mp = 1 then sell next bar at open;
	If mp =-1 then buyToCover next bar at open;
end;
Initial stop and if winning trade exit next morning else get out on close.

The buy/sell levels are the first hourly bar’s extreme +/- min. tick.  These levels are used as protective stops as well as entry levels.  If long then the first hourly bar’s low is the protective stop.  Notice how I check at 1510 if the trade is in a profit by examining the current openPositionProfit.  I am checking five minutes prior to the close because I need to execute setExitOnClose if in a loss.  I don’t think I can check at 15:15 (closing time) and then exit, but I will check and let you know.

The last little trick is to execute on the open tick the next morning.  I have explained in my books the different trading paradigms where you are either sitting on the prior bar when you place orders or sitting on the open of the current bar.  EasyLanguage assumes the former so you always have to place an order for the next bar.  If you wait until the open, then the earliest you can exit is 8:35.  So you have to issue the order on the last bar of the prior day to get the open tick, hence the test to see if the time stamp is 15:15.

This sample of EasyLanguage is a good example of some easy fixes to things that usually leave beginning EasyLanguage coders scratching their heads and groaning out loud.

Trade Entry/Exit Functions in PSB

Since Python doesn’t  allow for a GOTO program flow structure I changed the inline trade entry/exit logic into function calls.  This allows for the call of these functions to be non-sequential.  The original PSB order placement was sequential and would examine the entry/exit logic in TOP-DOWN fashion.  Meaning that if you put the Long Entry Logic first, the program would evaluate that logic first on every bar of data.  This was fine for the majority of trading systems out there.  However, systems that could enter multiple signals on the same bar require the orders to be placed in order of whichever was closest to the current market price.  Let’s say you have a trading algorithm that issues a stop to exit at a loss and a stop to reverse your current position.  If you examine the reversal logic prior to the stop loss and the stop loss is actually closer, then you will execute the wrong signal first.   By encapsulating the entry/exit logic into functions you can use decision constructs to flow through the correct logic in the correct order.  Here are the trade signals programmed as function calls:

entryexitfuncs

As you can see there are six modules – Long Entry, Long Loss, Long Profit, Short Entry, Short Loss, and Short Profit (note non-necessary code was collapsed).  I used the Sublime text editor to collapse the unnecessary lines of code.  You can download a trail version from their website.  I am no longer using the IDLE as my go to IDE – I have fallen deeply in love with PyScripter.  I will delve into this subject later.

Now that you have all your trade signals programmed as functions you can utilize if-thens to determine what order they are called.  Here is the code that calls these functions

functionflow

If you are flat and you can buy or sell on the same day then you call both functions.  This back tester will allow you to buy and sell on the same day – the only problem is it doesn’t know which occurred first: the buy or the sell.  This can cause a problem because you need to know the correct position by the end of the day.  Since we can peak into the future (be very, very careful) we can look at the close of the day and compare it with the long and short entry prices.  Here is some code that might make your entries more accurate on those occasions where both orders could be filled.

if myHigh[D0] >= buyLevel and myLow[D0] <= sellLevel:
	closeToBuyDiff = myClose[D0] -  buyLevel
	closeToSellDiff = sellLevel - myClose[D0]
    	if closeToBuyDiff < closeToSellDiff:
    		se = sellEntry(sellLevel)
	    	le = longEntry(buyLevel)
      	else:
    		le = longEntry(buyLevel)
    		se = sellEntry(sellLevel)
					
Increasing Trade Entry/Exit Accuracy - Maybe?

This code will execute the long entry logic after the short entry logic whenever the close is closer to the long entry price than the short entry price.  If both orders are filled on the same bar and the close happens to fall closer to the long entry price, then the software assumes the short entry was entered earlier in the day and the long entry later in the day making the position at end of day long.  Is this 100% accurate?  No but it logically stands to reason that a close near the high would indicate they high was made last.  Without intraday data we simply do not know what happened first.

Using Jupyter Notebook and Plot.ly To Create Candle Stick Chart

In today’s post I show how you can plot a very nice looking Candlestick chart inside a Jupyter (IPython) notebook. This chore is
made much easier by using  Plotly. So first thing you sholud do is sign up for a free account at Plotly and then download Jupyter Interactive Python notebooks.  I did this in an interactive notebook for demonstration purposes only.  After installing Plotly I was able to import the libraries into my notebook and then call the various functions to graph the data.  I imported numpy, but it wasn’t necessary.  I simply copied some data (CL.CSV) to the subdirectory that held my notebooks and then used the CSV Reader to pull the data into the various lists that the Plotly functions required.  All of the plotting is done in a browser and its interactive.  After creating the PSB I wanted to provide a tool for plotting the data that was being tested.  Jupyter and Plotly are free for non-commercial users.

import numpy as np
import datetime
import csv
import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import *

d = list()
dt = list()
o = list()
h = list()
l = list()
c = list()
v = list()
oi = list()
cnt = 0

with open("CL.CSV") as f:
    f_csv = csv.reader(f)
    for row in f_csv:
        numCols = len(row)
        cnt += 1
        d.append(int(row[0]))
        dt.append(datetime.datetime.strptime(row[0],'%Y%m%d'))
        o.append(float(row[1]))
        h.append(float(row[2]))
        l.append(float(row[3]))
        c.append(float(row[4]))
        v.append(float(row[5]))
        oi.append(float(row[6]))
        
xDate = list()
yVal = list()
indicCnt = 0
for i in range(len(c)-40,len(c)):
    xDate.append(dt[i])
    sum = 0.0
    for j in range(i-9,i):
        sum += c[j]
    yVal.append(sum/10)
                      
fig = FF.create_candlestick(o, h,l, c, dt)

add_line = Scatter(
    x=xDate, 
    y=yVal, 
    name= 'movingAverage', 
    line=Line(color='blue')
    )

fig['data'].extend([add_line])

py.iplot(fig, filename='simple-candlestick', validate=False)
Candlesticks with Plot.ly
CandleStick of Crude Oil with Moving Average Overlay
CandleStick of Crude Oil with Moving Average Overlay

Restructuring Trade Entry with PSB

It has been brought to my attention by a very astute reader of the book that the ordering of the buy/sell/longliq/shortliq directives creates a potential error by giving preference to entries.  The code in the book and my examples thus far test for a true condition first in the entry logic and then in the exit logic.  Let’s say your long exit stop in the Euros is set at 11020, but your reversal is set at 11000.  The python back tester will skip the exit at 11020 and reverse at 11000.  This only happens if both stops are hit on the same day.  If this happens you will incur a 20 point additional loss.  You can prevent this by using logic similar to:

if ((mp == 0 or (mp == -1 and stb < stopb)) and myHigh[D0] >= stb) :
Eliminate Reversal Bias

Notice bow I compare the price level of stb and stopb [stb – reversal and stopb – liquidation].  I added this to the long entry logic – the code is only executed if the entry is less than the exit (closer to the current market).  I am in the process of restructuring the flow so that all orders will be examined on a bar by bar basis and the ones that should take place (chronologically speaking) will do so and be reflected in the performance metrics.  This can cause multiple trades on a single bar.  This is what happens in real trading and should be reflected in the PSB.  This is where the lack of a GOTO creates a small headache in Python.  The ESB already takes this into consieration.  I will post when I finalize the code.

Version 2.0 of Python System Back-tester Available

I have just wrapped up the latest version of the Python System Back-tester (PSB).

I have added some more portfolio performance metrics and you will see these in the performance reports.  The most useful addition is the concept of the .POR file when you run a system.  Instead of having to select your data files each time you run a system, you can build a .POR file with a list of files/markets you want to batch run.

Here is an example of a Portfolio file:

TY.CSV

CU.CSV

SB.CSV

S2.CSV

QG.CSV

QM.CSV

C2.CSV

Just make sure you put the .POR file inside the same folder that contains your testing data.  I have included a TestPortfolio.por file in version 2.0.  I would treat the different versions as completely separate applications.  Your existing .py algorithm files will need to be slightly modified to work with version 2.0.

This line of code needs to be modified from this:

systemMarket.setSysMarkInfo(sysName,myComName,listOfTrades,equity)

to this:

systemMarket.setSysMarkInfo(sysName,myComName,listOfTrades,equity,initCapital)

This line is near the bottom of the overall loop.  I added the initCapital variable so you could do position sizing and the performance metrics would reflect this initial value.

And set initCapital to a pertinent value.  I put it right below the sysName variable:

sysName = ‘BollingerBandSys’ #System Name here

initCapital = 100000 #starting account balance

Also I corrected a small bug in the main loop.  You should change this:

for i in range(len(myDate) – numBarsToGoBack,len(myDate)):

to

for i in range(len(myDate) – (numBarsToGoBack-rampUp),len(myDate)):

In another post I will show how the portfolio performance metrics have changed.  I hope you like the new version.  I will be adding a library of trading systems utilizing this new version in a few days.

If you want to download Version 2.0 – just go the the following link

PSBVersion2.0

Backtesting with [Trade Station,Python,AmiBroker, Excel]

Follow

Get every new post delivered to your Inbox

Join other followers: