Building & Testing a Trading System
by Wayne A. Thorp, CFA
To some, “trading system” is an intimidating term. However, simply put, a trading system is one or more indicators or criteria used in a systematic fashion to generate buy and sell signals for a given security. A trading system can be as simple as buying when the price of a security goes above a moving average line and selling when the price dips below the moving average. The complexity of a trading system is bounded only by the user’s imagination.
Beyond indicating when to buy and sell a security based on a set of indicators, trading systems play perhaps an even larger role in investing—they take emotion out of the equation. Well-defined trading systems make the buying and selling process automatic, without the need for subjective decision making on the part of the trader. However, the emotional separation is only possible when you have complete faith in the system and trade it unflinchingly.
This article walks you through the steps involved in creating a sound trading system. All of the basic elements are covered, from deciding which type of market you should trade and selecting entry points to adding elements of reality such as commissions and slippage. We will also discuss how you can go about optimizing your trading system and analyzing the financial strength of the system.
Intraday or End-of-Day?
Before you can begin constructing your trading system, there are a few important questions you should ask yourself that will influence the process. The most important question is what type of trader you wish to be. Will you be monitoring the market during the course of the trading day and entering and exiting trades intraday? Alternatively, will you take more of a “buy-and-hold” approach and monitor your portfolio on an end-of day basis? Intraday trading, as one might expect, requires a greater amount of dedication and resources on the part of the investor. Someone attempting “day trading” should ideally pursue it as their sole activity. The markets move too quickly and, sometimes, violently to allow distractions during trading hours. Intraday trading will also require a real-time data feed and generally a real-time analysis software package. Real-time data and software carry with them higher prices compared to their end-of-day cousins. For this article, the systems will be based on end-of-day results. For more information on intraday trading, see our Day Trader’s Reference Guide within the Editor’s Choice segment of the Computerized Investing Web site (www.aaii.com/cimember).
Selecting the Software
Elements that dictate appropriate software include your trading horizon (intraday vs. end-of-day), securities that you wish to trade, types and number of indicators, as well as the ability to create, test and edit trading systems. The May/June 2000 issue of Computerized Investing included a detailed comparison article on a wide range of both intraday and end-of-day technical analysis programs (available at our Web site). For this article, Metastock 7.0 is used to illustrate the process of creating and optimizing a trading system.
Choosing an Indicator
With software in hand, we can now focus our attention on the trading system itself. A trading system can be as simple or complex as the user wishes. Sometimes you may feel that you must create a system with the most obscure and complex indicators in order for it to be profitable. However, profitable systems can be constructed from the simplest of ideas and indicators.
Technical analysis is the pursuit of identifying trends to predict future price movements. Therefore, most trading systems are created using trend-following technical indicators. Trend-following indicators highlight a security when it is in a strong uptrend or downtrend. Trending indicators typically are subject to some lag in reporting a market reversal and may produce false signals in a sideways-trending environment. Some indicators such as oscillators tend to be leading indicators and provide better signals during changing markets.
The simplest trend-following indicators are moving averages, and the system we work with here will involve moving averages. Our system uses two moving averages—one shorter-term and one longer-term. A basic moving average system will generate a buy signal when the shorter-term average crosses above the longer-term average. Likewise, sell (short) signals arise when the short-term average crosses below the longer-term moving average. Such a system is termed a moving average crossover system.
Indicator Parameters
Most indicators have adjustable parameters that influence their sensitivity and usefulness for a given security or market environment. We are sticking with simple moving averages, so our next step is to decide on the period lengths of our moving averages. The shorter the time periods, the more responsive the moving averages tend to be to price movements and reversals. However, shorter time periods tend to generate more “false” signals due to momentary price reversals, but will also warn you more quickly of a major change in trend. Our system will start out with a 50-day and 200-day simple moving average. Experience and/or preference may lead you to use different time periods. Our focus at the moment is on the initial creation of the system. Later, we will try to optimize the system to find those period lengths that generate the largest profits or the smallest losses for the tested securities.
Trading Parameters
Using MetaStock’s System Editor, we establish our criteria for entering and exiting both long and short trades (Figure 1). Programs such as MetaStock and TradeStation from Omega Research have their own proprietary programming language that you can use to create your trading systems.
One critical issue involves how to deal with sell orders. When a sell is triggered, you could sell your long position and go to cash or you can elect to be more aggressive and “double down.” This involves selling your long position and establishing a short position in which you profit if the security decreases in value and lose money if the security goes up in value.
For our system, we are going to be more aggressive and establish a short position on a sell signal. The system will cover an open short position and execute a buy when the 50-day moving average crosses above the 200-day moving average. Conversely, a long position will be closed out and a short position established when the 50-day moving average crosses (closes below) the 200-day moving average.
Testing Your Strategy
Before you begin trading any system for real, you should run a variety of tests to determine its potential profitability across a range of market environments. As part of the testing process, you need to identify and inject some “real world” conditions such as initial equity amount, commissions, slippage, stops, and interest on idle accounts.
The initial equity amount is the amount of money you have in your account before you begin trading. By beginning with a sizable amount of equity, you gain greater flexibility in the form of entering a larger position, which, in turn, can generate larger total dollar gains (or losses). Typically, by entering with more money, you can stay in the game longer. This is especially true if you plan to short stocks. When a stock is sold short your potential loss extends well beyond your initial investment. Depending on whom you ask, you will probably receive different answers regarding the “ideal” equity balance. Ultimately it is up to you, just be sure you can afford to lose it! For our purposes here, we will start with an equity account of $10,000.
Take a peek
at all the member benefits AAII has to offer.
AAII is a nonprofit association dedicated to investment education.
Commissions
People tend to forget what a dramatic impact commissions, the fees paid for buying and selling securities through a broker, can have on the overall success of a trading system.
To get a more accurate picture of a system’s profitability, it is important to figure in the commission costs. This is especially important for a system that generates numerous buy and sell signals, which will dramatically lower the profits or increase the losses of a system. Commissions can vary greatly depending on the type of security you are trading and whether you are using a deep-discount broker or a full-service one.
For this example, we will assume that it costs $25 to both buy and sell securities. Therefore, a round-trip trade, one buy and sell, will cost $50.
Slippage
Another element that many traders lose sight of is that you will rarely be able to enter or exit a trade at exactly the same price at which the signal was generated.
Our example uses daily end-of-day data. A buy or sell signal will be generated after the market close. Realistically, your first opportunity to act on the signal is at the open the next day. The difference between the price at which the signal was generated and the price at which your order is actually filled is called slippage.
When testing your trading system, it is important to account for slippage, otherwise the trading results are overstated. Some programs allow you to specify slippage in dollar or percentage terms, while others allow you to build in a time delay between the signal and order execution.
With MetaStock, you are able to specify the delay (in periods) from the time both sell signals and buy signals are generated by the system and when orders are filled. In addition, you can specify the price at which the order is filled—open, high, low, or close.
Learning something new?
AAII is a nonprofit association dedicated to investment education. Take a peek at all the member benefits AAII has to offer.
For our system, we have specified that there be a one-period delay between signal and execution for both exits and entries of a position and that the price used in the calculation of profits/losses be the opening price. Therefore, when the moving average system signals a buy, the program uses the next day’s opening price as the price at which the security is purchased.
Stops
Perhaps the most useful tool in developing a trading system is a stop. Stops are predefined points where a position is closed out. When a stop is triggered, the position is closed regardless of the current status of your trading rules. Stops allow you to limit your losses should a trade go against you. There are several types of stops, the most popular being breakeven, inactivity, maximum loss, profit target, and trailing.
Breakeven stops close open positions when the closed-out value of the position would fall below the amount at which the current trade was opened. The stop is placed at the price where the trade could be closed and the proceeds generated would equal the current equity amount.
Inactivity stops will close an open position when the security’s price does not generate a minimum positive percent or price change within a specified time period. If you specify 1% as the minimum change and 20 as the number of periods, MetaStock will automatically close any long (short) positions where the security’s price has not increased (decreased) by at least 1% within any 20-period time frame.
Maximum loss (max loss) stops are useful as a risk management strategy, because you can specify the exact percentage of your total equity you wish to risk on a given position. These stops close an open position when the losses resulting from the trade exceed the specified maximum loss amount. A common mistake made by new traders is risking too much on a single trade. If we decided to risk 10% on every trade (a position would be “stopped out” once the trade loses 10%), our $10,000 opening equity balance would fall to just over $3,400 after 10 trades, losing 10% of the available balance. For this reason, we will only risk 2% on each trade we make using this system.
Profit target stops exit a trade once it reaches a predetermined profit level. Therefore, if you specify 10% as the profit target, open positions will be closed when they generate a 10% profit (after commissions).
Take a peek
at all the member benefits AAII has to offer.
AAII is a nonprofit association dedicated to investment education.
Lastly, trailing stops close open positions when a specified amount of the current open position’s profits is lost. Each time a position’s profits reach a new high, the trailing stop is placed at a level that allows a specified portion of the position’s profits to be lost.
With programs such as MetaStock, you are also able to specify the number of periods to ignore in trailing stops. For example, if you instruct the program to ignore three periods, the trailing stop will lag by three periods. Therefore, the last three periods’ profits or losses will be ignored when determining the current stop level. Such lags are useful in filtering out price swings. However, you need to exercise caution when using trailing stops. They are not designed to limit losses—they are used to lock in profits. For our system, we will also implement a trailing stop of 20%, meaning we are willing to give up 20% of our profits before exiting a trade. We also wish to ignore the last three periods so that we are not stopped out by any “momentary” price fluctuations (see Figure 2).
Testing the System
Having selected our trading criteria, the 50- and 200-day simple moving average crossover, and having specified our preferences for commissions, slippage, and stops, we are ready to test our system on a security. For this article, we have chosen General Motors (GM) as our test security.
When backtesting technical trading systems, as here, we use historical data. The key is to backtest your system over a “significant” time period. While this term is subjective, you ideally want to have enough data that prices have exhibited upward, downward, and sideways behavior. Figure 3 shows 30 years of daily price data for GM, from January 1970 through the end of 1999. As you can see from the trendlines drawn on the chart, GM has exhibited significant uptrends, extended downtrends, as well as periods of sideways trading—all of the traits we are looking for. Beyond having enough data to capture varying price behavior, you also want to have enough data to capture one or more business cycles, especially when tracking a highly cyclical firm such as GM. There have been five recessions over the last 30 years—December 1969 through October 1970, November 1973 through February 1975, January 1980 through June 1980, July 1981 through October 1982, and July 1990 through February 1991. With 30 years of data, we are able to capture such events as the October 1987 crash as well as the recent bull market.
For the purpose of testing and optimizing our moving average crossover system, we divide our 30 years of daily data from GM into three groups. We will use the first 10 years of data to run our system as is, so we have a reference point as to performance and number of trades, among other elements. We will use the middle portion of the data to optimize our system. Optimization is the process of testing different values for the variables in a system to come up with the combination that yields the highest gain or lowest loss. Our last 10 years of data will serve as a hold-out period to test our optimized system.
The first test we run uses price data from January 1970 through December of 1979. Figure 4 shows the equity line in the upper window and the buy, sell, and stop signals generated by the system in the lower window. The equity line is a visual representation of our equity balance over the course of the testing period. One item of note is that the line is sloping upward, which means the system made money. However, further investigation is needed to gauge the true merits of the system.
In MetaStock, and most technical analysis programs that allow you to backtest trading system, you can generate reports that summarize various statistics from a trading system test. Figure 5 shows the system report for our initial system test.
Learning something new?
AAII is a nonprofit association dedicated to investment education. Take a peek at all the member benefits AAII has to offer.
Beginning at the top, we see that the total net profit for the system is $2,710.62. The report notes a 2.71% annual percent gain, but this is simple arithmetic annual return; the compound annual return is 2.42% over the 10-year testing period. While this is certainly not encouraging (investing our $10,000 of initial equity over 10 years in cash at 4% nominal rate would have netted us over $4,918 with daily compounding), the report reveals that the system did beat out a simple buy-and-hold strategy, which would have lost just over $3,200. Some other useful statistics are the total number of trades, in this case 13. Over 10 years, this is a very small number of trades. Of these 13 trades, four were winning or profitable trades, indicating that just over 30% of the trades were profitable. On our winning trades, we made $621 while our losing trades gave up $2,280 for a net loss of $1,659 from trading. Where did the overall profit come from? We earned $4,370 from the 4% paid on our idle balance. Moreover, this system was idle a good deal of the time—2,462 days of 3,652 in the testing period were spent on the sidelines. The longest “out” period the system had was 528, which means the system was idle for a year and a half straight! Lastly, our largest equity “dip,” or drawdown over the testing period was $327. We would like to see the drawdown be small relative to our equity, the smaller the amount, the more tolerable the drop in equity.
Optimization
After examining this test, it’s clear that our system must be improved in order to trade in the real world. Otherwise, we would be better off investing our money in a savings account. Our next step then is to optimize the system to find those moving average lengths that generate the largest gains for General Motors. In MetaStock, you are able to substitute initial values, such as the 50- and 200-day moving averages, for optimized values, which the program will then test for you.
Figure 6 again shows the System Editor from MetaStock. However, as compared to Figure 1, we have replaced the 50 and 200 values with opt1 and opt2, respectively. These elements are what the program will then test over ranges you specify. In the lower right corner of Figure 6, we have specified the ranges that we want each optimized variable to be tested over. We want opt1, the short-term moving average, to be longer than 10 days, but no longer than 50. We also set the program to test this range in increments or steps of five—meaning that the periods used will be 10, 15, 20, etc. For the long-term average, we want to test the range between 40 and 200 days, also at five-day increments. Having set up our optimization criteria, we are ready to run the optimization test.
Figure 7 shows both the equity line for the optimized system and the summary report created by MetaStock. The summary report lists all of the different combinations (297 in all) as well as the profit, number of trades, number of winners and loser per test, and the optimization values used. The top test, which netted a profit of $10,170, used 30 days for the shorter moving average and 55 for the longer average. Also, take note of the equity line. This is more like the kind of shape we would like to see for all equity lines. For the most part, it has a steady upward slope to it. Such behavior is easier to trade from an emotional level because you are not seeing wild swings in your equity level.
Before moving on to test our optimized values over the last 10 years of data, we should stress that while optimizing is beneficial in attempting to find a better-performing system there are dangers involved as well. You must keep in mind that you are optimizing the system over a set of historical data. The results you generate are those that work best for that particular time period. However, there is no guarantee that the results will carry over to other periods. This is why you should always set aside data to test the strategy over a non-optimized period.
Now it is time to see how well our optimized system holds up over a different time period. Ideally, you will see similar results from one data set to another. Unfortunately, our system’s performance weakened during our hold-out period. The system generated a profit of over $4,678 for the period. There were 15 winning trades that generated $9,573 in profits offset with 31 losing trades that lost $9,939. Interest earned was $5,044 for the 2,312 days that the system was out of market. The equity line in Figure 8 is not as smooth as the one on Figure 7, meaning that our system is more volatile over this time period than in the 10 years prior.
Having optimized our system and tested it over another period to see how well it would hold up, we can now apply the system over the entire 30 years of data to see how it would have fared. Figure 9 shows the system report for the 30-year period. The new system generated a net profit of just under $21,000, which translates into roughly a 7% annual gain. If you had bought GM at the beginning of the period and held it until the end of December 1999, you would have made less than if you had traded the system ($14,000 versus $21,000). The system generated 132 trades over 30 years—less than five trades a year. Forty-five out of the total 132 trades made a profit. Lastly, the largest drawdown for the 30-year period was $1,296.
So how do we tell if we are better off with our optimized system? We compare it to our original system and how it would have fared over the entire testing period. The optimized system outgained the original by almost $14,000, or by more than 4% per year. On a percentage basis, the optimized system also had more profitable trades—35% to 17%. Overall, we appear to be better off with the optimized system.
Take a peek
at all the member benefits AAII has to offer.
AAII is a nonprofit association dedicated to investment education.
Conclusion
This article has served as a primer to computer-aided system development and implementation. The examples provided are meant to serve only as a foundation on which you can build more elaborate trading systems that match your trading style and preferences. No matter what system you use, be certain you test it thoroughly over varying market conditions. The more you test a system, the more comfortable you will be when actually trading.
Above all, if you plan to trade your system in the real world, follow it religiously. The reason for developing a systematic trading strategy is to eliminate the need for arbitrary decisions. Following these principles will put you on your way to developing a sound trading system.
Discussion
No comments have been added yet. Add your thoughts to the discussion!
