Let’s say that you had some money that you wanted to invest and wanted to know if right now would be a good time to get in the market. So you go to a financial news website and pull up a chart of the S&P 500 as shown below.
You see that over the past 3 months the S&P 500 has gained 4.94% which is a very good return for this market over such a short period. You also see that although the market went up overall, it did not do so in a straight forward manner. It went up a little bit, then was flat for a while, then down for a bit and then up again. The million dollar question is : What is the market going to do next ?
Step 1 : Choosing a Technical Indicator
Since you don’t want to gamble your hard earned cash, you decide to take a quantitative approach to find an answer to your question. The first step in the quantitative process is to find some kind of measure or metric of the market behavior that you can feed into a computer so you can study the market behavior over time. Such measure is known as a technical indicator and describes some characteristic of the market price action.
It is important to note at this time that the technical indicator is derived from the market price action and not the other way around. If you follow financial literature, you will see a lot of examples where people talk about basing investment decisions on the behavior of such and such technical indicators as if this behavior was the absolute truth and the market is bound to follow these indicators. However these indicators are merely a simplistic description of the market and there is no information present in these indicators that’s not already present in the price action chart. If the market changes, these indicators will change accordingly.
For the purposes of this study we will use the simple moving average indicator or SMA for short. This indicator is widely used and simple to understand. The SMA indicator is the arithmetic average of the previous n day closes. So SMA (3) would be the average of today’s close, yesterday’s close and the day before yesterday’s close. Every day you can compute the average value and thus get a moving average where each value represents the average of the past n days. Note that since this indicator uses today’s value, you have to wait until the end of the trading day to know what today’s SMA value is going to be.
So let’s add a simple moving average to our chart and see what it looks like. Since there are usually 5 market days in a week we’ll use a length of 5.
One characteristic of the SMA indicator is that it is much smoother than the daily prices. Such smoothness increases as the length of the indicator increases as daily variations in price have less of an effect on the overall average value.
Step 2 : Rule Definition
The second step in the quantitative process is to define a rule that attempts to help you find an answer to your question. In this case we will use the following rule: Buy or invest in the market when the market price at today’s close is above the SMA (5) value and sell off any open positions if the market price at today’s close is below the SMA (5) value.
When you define a rule it is important to understand what it means and what you are trying to do. In the rule defined above we are essentially saying that we are going to be in the market as long as the closing price is above the 5 day average price and exit if it falls below. It also says that you are going to ignore smaller price variations as long as you stay above the 5 day average. So you can have one or more losing days but you will stay in the market as long as you are still above the 5 day average.
Step 3 : Testing on Historical Data
Now we can use the power of computers to back test our idea. This will tell us, had we followed this rule in the past how would we have done? We run this test using S&P 500 data starting on January 3rd, 1950 and ending on December 31st, 2016 for a total of 67 years. This gives us the following result:
By contrast, if we had bought the S&P 500 on January 3rd 1950 and held it until December 31st 2016, we would have had the following result:
This tells us that our approach would have done about 1% better on a year to year basis being in the market only about 56% of the time (9,421 days in compared to 16,859). However, our approach had a bigger drawdown than the buy and hold approach and thus the buy and hold approach is a better strategy in terms of Risk Reward or RR. The RR is simply the ratio of the CAGR to the maximum drawdown.
Step 4 : Parameter Optimization
The next step in the quantitative process is to modify our entry and exit parameters in an attempt to improve our results. This is known as the optimization process. There is nothing special about a simple moving average length of 5. Thus why not try 10 or 20? For the purpose of this example we will test every length from 5 to 500 in intervals of 5. This is where the power of computing comes in handy as it would take an insanely long amount of time to attempt to do this by hand.
We run this test and find out that although a length of 5 still gives us the best CAGR of 8.59%, using a length of 375 gives us an RR of 0.879 with a maximum drawdown of only -7.97% as summarized below. Since there are about 250 trading days in a calendar year, a length of 375 corresponds to about one year and a half. So we are essentially comparing the last market price to the average value of the past year and a half. To download a full list of trades and drawdowns please click here.
If we update our initial market chart with the SMA (375) instead of the SMA(5) we get the picture shown below. Notice how the market is at $2553.17 while the SMA (375) is at $2,282.69. This is a difference of about 10.59% of the market value. This is slightly more than the maximum historical drawdown of -7.97% in our backtesting.
It is worth noting at this point that there is nothing special about the SMA (50) and SMA (200), the two most commonly used parameter lengths that are most widely included in financial discussions. Thus any seemingly predictive property of these two parameter lengths is more of a self fulfilling prophecy than anything else as traders collectively follow them.
Using the RR as a comparison metric, we see that using an SMA (375) filter or buying rule to the market provided us with an approach that is about 6.6 times better than a buy and hold approach. The RR metric considers both the returns of the investment approach as represented by the CAGR as well as the risk involved as represented by the maximum historical drawdown. The investor can then make an informed decision of how much risk they are willing to take or how much returns they are willing to give up.
Step 5 : Parameter Selection
The figures below illustrate the variation in the Total Return and the variation in the RR ratio as we change the length of the SMA. To download a spreadsheet with the optimization results and an illustration of how all the parameters change as we change the SMA length please click here.
Looking at the above two images, one can see that although there are some values of the SMA that give a better Return or RR value, moving away from this optimal value also gives reasonable good Returns or RR values. Thus the approach is stable with respect to changes in the underlying parameter. One would be ill advised to invest in a strategy where this was not the case.
It is important to note at this point that for the purposes of this example we selected a very simple system that uses just one parameter, the SMA length. This simplicity is a good thing. One of the dangers of the optimization process is that given enough parameters, if one tries enough variations and combinations of these parameters, one can most likely get some great looking results that are however meaningless. This can be visualized as trying to find a shape that fits in a square box. If I have a single big ball and I put it in the square box, I will see that there is still a lot of empty space as the box is not spherical like the ball. However, if I have a million tiny balls or if I fill the box with sand, I won’t have as much empty space any more since the sand will fill the box well. However the sand is not going to tell me if the box is square or not.
Through the optimization process we are trying to better understand the underlying market behavior and see if we can identify a characteristic that we can exploit to make money. It is this inherent characteristic of the market that allows us to make money.
Step 6 : Sticking with the Plan
As you can see from this example, the investment approach is very well defined and clear. There is no guessing involved and no subjectivity. All you have to do is stick to the plan. In doing so there will be times when you go in the market and it goes against you and there will be other times when you sit out waiting for your strategy to give you a signal and the market rallies leaving you behind. This is when you need to put on your statistician’s hat and remember that this is a statistics game.
According to your experience and shaped by the backtesting you’ve done, although you don’t know if this trade is going to be a winner or a loser, you do know that over time you have an edge that will translate into profits. One way to assimilate this thinking is to study the historical trades one by one and get a first hand impression of your strategy in action by looking at historical trades on the chart.
What about shorting the market?
In the above discussion we only looked at getting in the market when the market price closed above the moving average and staying in cash when the market price closed below the moving average. However, we can very easily change our rule so that we buy or go long the market when the price is above the moving average and we sell or go short the market when the price falls below the moving average. This way we are constantly in the market with either a long or a short position. The motivation behind this approach is to try and exploit the market’s movements in both directions.
When we run the optimization process for the short side we see that there are no variations that make money as listed here. No matter what the filter length, we always lose money. Thus it is actually better to sit out in cash and wait for the market to start rallying again so that we can go long. The failure to find any short variation that makes money is a reflection of the upward bias of the market. It also tells us that the market behaves differently on the long side than it does on the short side. In particular, stock indexes such as the S&P 500 tend to go up slowly and come down fast. Hence a different strategy is required if we want to exploit the market on the short side.
The Quantitative Process – Part II
In the next post we will have a look at some more details of the quantitative process outlined above and discuss some of it’s limitations. To continue reading click here.