Coding automated strategies is iterative

Programming an automated strategy is an iterative process, like any other kind of programming. The first attempt in writing code that trades your strategy will likely not be successful. Ideally, the goal of your first pass at writing your strategy is to create the absolute simplest possible version of your strategy. If you usually trade with 3 indicators, try to code it using only the main one. If you have 4 criteria for entering a trade manually, try to code it using only one or two.

In your next version, you can add in your additional filters, trade criteria, and special cases, but on this first iteration, keep it really simple. You might discover that your strategy is pretty good, even without all the additional indicators and special cases. If you can get this simple version to be profitable in back-testing, the additional indicators and entry or exit criteria will just make it that much better. Be patient. Get the simple version really, really excellent before you make it more complex.

Each iteration should become closer and closer to your goal. And after every version of the software, do crazy amounts of back testing–optimize different parameters, test on long time periods, short time periods, recent months, and time periods from several years ago. Then try your best set of parameters on data you didn’t optimize with (for those in the know, we’re doing walk-forward testing…)

 

Trading Systems Book Review

Trading Systems by Urban Jaekle and Emilio Tomasini

Trading Systems has changed my life. That’s a stretch, but it’s true. I have been working on writing automated trading systems for a some time now, and this book has given me direction and focus to that task. Previous systems that I have written were done by finding all the details and rules of a manual trading system, and coding each one into the automated system, trying to figure out which rule might take precedence, and whether one rule of the trading system might be more important than another. I’ve come up with what I thought were moderately profitable trading systems, only to see them under-perform in the real world. Enter Trading Systems, which clearly showed me I was doing things backwards, and that there were much better and more reliable ways to test a system’s viability.

The most important thing I learned from this book is that the trading system itself can be be very simple; actually, it must be very simple. The example the authors use throughout the book is the LUXOR system, which is the simplest of moving-average cross-over systems, probably the first trading system anyone learns. Tomasini shows that the system tested against historical data does well; it is profitable with very simple trading rules. Even better, the LUXOR system is free; he tells you where you can download it. The code is presented in EasyLanguage, but it is simple enough that you can translate it into Metatrader or Ninjascript, or whatever your language of choice.

The next important thing I learned is that once you have run some tests to show that the system is basically profitable, there is a correct order of what to do next. In my own previous work, I threw in indicators, trading rules, stops and profit targets, Money Management rules, and everything all at once; but Tomasini is clear that the very first thing you should do is get the trading algorithm to work well on its own. Then, add a filter to two to weed out some bad trades; in the book he adds a time-of-day filter to limit when the system can trade.

Only then do you start thinking about stops, trailing stops and profit targets. In the world of discretionary trading, that’s crazy talk. In my manual trading, I have always been taught that you determine your stop at the same time you determine where you will place the trade. That idea gets drilled into us at every trading class and lecture — Use stops! But in automated trading, the algorithm must be profitable even without stops. In fact, after adding stops to the program, the total profit doesn’t change very much. What does change is the drawdown; using stops, especially trailing stops, drastically reduces drawdown on the account.

The last thing that gets added into the system is money management (or the trademarked term “Position Sizing”); once a good money management system is coded in, the system goes from profitable to uber-profitable. A simple money management rule is that the system will always trade only 2% of the account’s equity. As the equity increases, so does each trade size.

Tomasini spends the majority of this first section of the book on back testing and optimization; dozens of interesting graphs show the testing results after each change is made to the code. His graphing software is making me a bit jealous, and I’ve got it on my list to figure out how he does those graphs so that I can do my own.

One thing that is interesting about optimization is that you have to be clear about what you are optimizing: profits, or percentage of profitable trades, or the average trade, or minimizing drawdown. These various goals need to be clear your mind before you begin optimizing. It’s not enough to say that you want the system to make as much money as quick as possible. If you optimize only for net profit, and ignore drawdown numbers, your system may well wipe you out before you get to profit.

The important lesson of back testing is that over-optimization will make a trading system look good with historical data but perform poorly in the real world. Tomasini spends a lot of time explaining how to not over-optimize. One of those methods is to use walk-forward analysis. What that means is that you use a portion of your historical data to optimize your algorithm’s parameters, and then another portion of the data to test those parameters. In other words, you use half of your data for training the system, and the other half (the “unseen” half) for testing. This is known as “in sample” data and “out of sample data”. These are really important concepts, and make all the difference in the world on creating a profitable system. Walk-forward analysis keeps moving the window of in-sample and out-of sample data forward through time, and each time you get parameter results that are little better than before; by “better”, I mean more robust, and more likely to work well long-term.

Two more concepts Tomasini describes for testing the robustness of a system are timescale changes and Monte Carlo analysis. Timescale changes just means that if you have tested and optimized your system using 1-hour chart data, then you run the tests against 30 minute or 4-hour chart data, and see how it performs. If your system still does moderately well, you likely have a good system.

Monte Carlo analysis sounds so cool that I just like saying the words: “Monte Carlo Analysis”. Monte Carlo Analysis is a tool that checks for the worst case drawdown scenarios. The analysis re-organizes the order of your trades randomly, and shows you various scenarios. Your beginning equity and ending equity are still the same, but the order in which the trades are taken is randomized a number of times. Some of the equity curves will be better, and some will be worse. The analysis shows the worst case scenario on the drawdown on your account for the trading system you are testing.

Finally, Tomasini recommends periodically re-optimizing your system. Markets change over time, so if you re-optimize and re-analyze your system periodically, you can keep it generating good profits.

This is probably one of the best books I have read on my particular niche of trading systems; if you have any thoughts of developing a profitable automated trading system, you need to ready and apply Trading Systems.

(Note: The second part of the book is on portfolio optimization, which is a topic I will cover in a later entry.)

Trading Systems: A New Approach to System Development and Portfolio Optimisation

Has Your Automated System Failed You

Michael Gutmann’s Futures magazine article: Minimizing loss: Run-length trade statistics

I read the article in Futures Magazine last month by Michael Gutmann; he goes into a lot of detail on how you can know if your automated trading system is doing what is expected, or if it has broken down. How can you know?  When you run an automated system, you will have (or you should have) statistics from back testing that show the percentage of wins versus losses and the maximum number of consecutive losing trades. That second number, the number of consecutive losing trades, is a pain threshold for traders watching their automated system trade and lose. How many trades can you watch your system lose before you pull the plug? Is there a way to determine this number for a particular trading system?

As it turns out, there is. Using some statistics math, Gutmann shows how you can determine the most probable number of losing trades you should have to endure, providing an objective measure to determine if you should stay with your system, or bail. (Or at least, re-tune the parameters.)

In the article, Gutmann shows a nice table of trade statistics, based on 250 trades; the rows are runs of length N and the columns are the probability of that number of consecutive runs for a 30%, a 50% and a 70% win ratio system. For example, with a system that wins 30% of the time, you should expect to see 9 losses in a row 60% of the time; with a system that wins 70% of the time, expect to see 5 losses in a row 25% of the time. Gutmann uses some heavy math to show how he arrived at those numbers, but the table captures much of the data in an easy to read format.

So, if your automated system presents you with a 9-loss losing streak, you can take a look at the chart and see that that should only happen 5% of the time; it might be time to toss that particular strategy.

The article also states that you can write Mr. Gutmann and request the C++ code for running the numbers on your own trades. I did write him, and I received a nice reply from him with the C++ code. I have yet to run it on my trading data, but I plan to do so soon, and will provide an update here when I do so.

See also Michael Guttman’s book: The Very Latest E-Mini Trading, 2nd Edition: Using Market Anticipation to Trade Electronic Futures

Forex Trade Log

Forex Trade Log, by Forex Smart Tools

How it will help your trading: You’ll have a record of all of your trades, for review and to help correct mistakes in trading

Forex Trade Log is awesome. If you do any manual back-testing at all, you need this program. Almost every course or book I have read on trading mentions the need to practice the strategies and keep a record of your trades. Some suggest using an Excel template, or a paper notebook, or some online tool. But I swear by Forex Trade Log.

Forex Smart Tools has a couple different versions of this tool — Lite, Standard and Pro. I first bought Lite, and was so impressed, I upgraded to Pro very shortly afterward. And, as an automated systems developer, you need Pro. The features most helpful in the Pro for us are the the ability to import statements from various brokers into the trade log, and also sort them by strategy. But I’m getting ahead of myself here. First things first.

So, what is the trade log? It’s a place to keep track of your trades. Whenever you place a trade, record it in the trade log. It’s that simple. What do you record? Well, the obvious entry and exit time and price, stops, trade size, buy/sell and profit or loss. But how about Stop Needed, and Ideal Stop, and Reason for Loss; maybe Entry trigger and Exit Trigger? And the best one? Screen shot. I use this all the time. When I am back-testing a strategy manually, every trade goes into the trade log, and I take a screen shot of every one. When the trade results in a loss, I record why it lost: Dumb mistake (I get a lot of those), Strategy Failed, Market Anomaly and Barely stopped out. Then, later, I can go back and look at the reasons for the losses. If there are a lot of dumb mistakes, I need to read up on the strategy again. If there are a lot of places where the strategy failed, then maybe it’s not such a good strategy. Other things I go back and look at are Notes and Lessons Learned. Both provide invaluable information that helps me later.

Recently, I needed to find a file of real trading information–of real trades taken–as input to an analysis program. I realized I could export my data out of Forex Trade Log into a csv file, and was able to use that file for my analysis. That was the only place I had that information; the actual trade information had been long gone. Because I had stored all of that information in a the trade log, I was able to easily analyze my trading and look at certain metrics.

Forex Trade Log is also useful for the automated system trader. I like to import trades from Metatrader 4’s Strategy Tester results into the trade log; there are some metrics on the summary and analysis tabs that show me things about the trades that are very helpful, such as equity graphs, and the ability to create my own time periods in which to see the trade results. Additionally, it’s just a good place to keep records of a strategy’s individual trades, that otherwise would be erased by the next run of strategy tester.

Here’s the best part though: the creators of Forex Trade Log are documentation fanatics. There is so much good documentation out there, I haven’t even seen it all. There are FAQ’s, help files, and videos, videos, videos! Every feature has an associated video that shows you how to use it. They have a newsletter that has amazing information in it, and the developers use this tool themselves.

For both the discretionary trader and the automated systems trader, the Forex Trade Log is invaluable.

Forex Trade Log, by Forex Smart Tools
Pricing: Lite $149, Standard $297, Pro $389