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.
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.
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
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:
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
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.
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.
It was recently brought to my attention that the Excel System Back-Tester(ESB) did not function properly on the MAC OS X. In other words it bombed when trying to open a comma delimited data file and also when one tried to run an algorithm. Thanks to a purchaser of the UATSTB I was able to fix the bugs without removing any functionality. I will post the Macintosh version here as well as have WILEY put it on the book’s website. Sorry for any inconvenience this may have caused. Here is the the link:
If you haven’t come across the great data resource Quandl.com I highly suggest in doing so. A portion of the data that I used in writing my latest book came from Quandl, specifically the wiki futures or CHRIS database. Here is the link:
There is a ton of free futures data. The different contracts are concatenated into large files. However, the rollover discount is not taken into consideration so the data “as-is” is somewhat un – testable. I am in the process of scrubbing the data as well as creating a “Panama” adjustment to the contracts in the large files. As soon as I complete this task I will provide the data on this website. Purchasers of my latest book will find 10+ plus years of history of continuous futures data that I pieced together from several different sources, including Quandl.
Here is a quick video that shows how to create a custom symbol list in TradeStation’s Portfolio Maestro. I describe this in my new book, “The Ultimate Algorithmic Trading System Toolbox.” The books should be released in May.
wAvg = wAverage(c of data2,19);
mySlope = wAvg – wAvg;
If you interrogate mySlope intra-week then it will always be equal to zero. The wAvg is by default tied to data1 which in this case is daily bars. So the value of wAvg is carried over from one day to the next. It only changes when the average of the weekly bar changes and that only occurs on Friday.
There are two possible solutions:
Without the use of data aliasing – inLine function calls
mySlope = wAverage(c of data2,19) – wAverage(c of data2,19) ;
With the use of data aliasing –
vars: wAvg(close of data2,0);
wAvg = wAverage(c of data2,19);
mySlop = wAvg – wAvg;
Either examples will work, but if you have several variables tied to a different data stream, then the code will be much cleaner looking using data aliasing – plus it cuts down on multiple function calls.
I have been working on a project where the strategy combined daily and weekly bars. Keeping track of the two time frames was, at one time, not that easy. However, with TradeStation’s Data Aliasing it is no problem at all. We all know that Data 1 is the highest resolution time frame and is the one used for trade execution. Data 2 can be a different market or a different time from of the same market. TradeStation allows for multiple data streams. Take a look at the following output in table 1. Wavg is a nine period moving average of weekly crude data. Wavg is the prior value of the moving average. If you wanted to make a trading decision on a daily bar basis by looking at the slope of the Wavg you couldn’t. The Wavg and Wavg only changes at the beginning of the next week. Most traders want to be able to make a trading decision intra-week by examining the current values of the Davg1, Davg2 and the slope of Wavg. During the week the slope of Wavg is ZERO.
Date Davg1 Davg2 Wavg Wavg
1151019 46.94 46.38 46.17 46.17
1151020 47.01 46.54 46.17 46.17
1151021 47.00 46.69 46.17 46.17
1151022 46.95 46.74 46.17 46.17
1151023 46.93 46.70 46.54 46.17<< changed here
1151026 46.83 46.55 46.54 46.54
1151027 46.71 46.47 46.54 46.54
1151028 46.74 46.44 46.54 46.54
1151029 46.74 46.40 46.54 46.54
1151030 46.73 46.39 46.60 46.54
1151102 46.57 46.37 46.60 46.60
1151103 46.55 46.45 46.60 46.60
1151104 46.36 46.44 46.60 46.60
Now look at table 2. The Wavg is not being updated on a daily basis but on a weekly basis. The current Wavg doesn’t become the prior Wavg on each daily bar. Wavg stays the same until a new weekly bar occurs. You can now make a trading decision intra-week by examining the slope of the Wavg. Each time frame update should only occur when a new bar of that same time frame is generated. This feature is really cool and is easy to implement.
Date Davg1 Davg2 Wavg Wavg
1151019 46.94 46.38 46.17 45.75 < notice how the Wavg and Wavg are always different
1151020 47.01 46.54 46.17 45.75
1151021 47.00 46.69 46.17 45.75
1151022 46.95 46.74 46.17 45.75
1151023 46.93 46.70 46.54 46.17
1151026 46.83 46.55 46.54 46.17
1151027 46.71 46.47 46.54 46.17
1151028 46.74 46.44 46.54 46.17
1151029 46.74 46.40 46.54 46.17
1151030 46.73 46.39 46.60 46.54
1151102 46.57 46.37 46.60 46.54
1151103 46.55 46.45 46.60 46.54
1151104 46.36 46.44 46.60 46.54
Here is the code that utilizes Data Aliasing. All I did was declare the weekly avg variable and tied it to data2.
vars: mavShortDaily(0),mavLongDaily(0); vars: mavWeekly(0,data2); mavShortDaily = average(c,19); mavLongDaily = average(c,39); mavWeekly = average(C of data2, 9); If mavShortDaily > mavLongDaily and mavWeekly > mavWeekly then buy this bar on close; If mavShortDaily < mavLongDaily and mavWeekly < mavWeekly then sellshort this bar on close; print(date," ",mavShortDaily," ",mavLongDaily," ",mavWeekly," ",mavWeekly);
Notice how the variable mavWeekly was tied to data2. When you delcare a variable that is tied to another data other than data1 you can put the data stream right in the variable delcaration : mavWeekly(0,data2).
The easiest way to program a multi step criteria entry/exit signal is to use a Finite State Machine (FSM). This is much easier than setting flags and then having to turn them on and off. Here is a diagram of a FSM: