Algorithmic Solutions to Market Timing

by: Emmanuel Marot

Choosing when to invest is almost as important as deciding what to invest in. Unfortunately, save for a few mavericks, very few people seem to have the capacity to forecast the market's direction. As a matter of fact, most investors take the worst possible decisions as to when to enter or to exit the stock market. In a famous example, the best-performing fund of the last decade returned a yearly 18%, but because of poor market timing, its investors lost on average 11% per year (Morningstar Fund Spy, August 2009).

Recent researches even show there are neurological reasons for this; because it rewards our herd-like behavior, oxytocin may well be the most costly hormone in the world.

If one doesn't trust himself, there are only two other options. The simplest one is to avoid trying to time the market, and invest only for the long run. This buy and hold philosophy has its merits, the biggest being that is works reasonable well. Nevertheless, its practicality is questionable, since investors may always crack at one point.

The second option is to rely upon a quantitative method. Such an algorithm takes the human factor out of the decision process while still complying with the tendency to seek an escape plan. Some of the methods below revise published academic papers while others are, as far as we know, original.

In all cases, the purpose of this article is not to describe sure-winning formulas, but to give examples of ideas and methods.

Information sources

What information can we use to try to time the market?

  • The market trend itself: In a previous article, we mentioned relying upon the momentum of various ETFs to select an industry to invest in. If no industries had an positive momentum, then we'd simply stay out of the market. It increased returns significantly, but could the same principle apply to the market in general?
  • The real price of stocks: The more expensive they are, the more likely a correction is to occur. Obviously, the trick is to get out at the peak, but not before.
  • Economic indicators: After all, the market is heavily influenced by the economic situation. If we can forecast the economic trends, we'll be able to better time our market positions. Then, by another token, if we can forecast the economy, we could also win a Nobel prize.
  • Market perception: What is the dominant mood? A crisis can be seen as essentially a vicious subjective circle, pessimism driving prices down, in turn increasing pessimism. Therefore, being able to gauge the zeitgeist beforehand could give an edge. We'll seek two different perception sources: The professionals trader, and the man in the street, or at least in front of his computer.

Experimentation parameters

We start by downloading from Yahoo Finance the S&P 500 values for approximately the last 20 years (starting January 2, 1991). To keep in sync with the simulations of our previous article, we trade once every 28 days. At the end, we'll measure the overall return and risks. Those are theoretical results, and do not take transaction costs into account. On the other hand, cash should at least be replaced by a low-risk investment, such as short term treasury bonds, that will slightly increase the timed returns.

Following Trends

Using simple moving averages

The 50-day and 200-day simple moving averages are very popular indicators of the market trends, but do they work? At each trading period, we simply invest in the market (or stay invested) if the 50-day SMA is above the 200-day, and get out of the market (or stay out) otherwise.

During bullish periods like 1991-2000, this market timing method significantly lowers returns. On the other hand, it worked relatively well to exit the market in 2001 and 2008. Although at the end it theoretically beats the market, with an annualized return of 7.81% (end money is $465K) versus 6.95% for the S&P 500. However, the difference is only 0.86%, and has therefore no practical value. It's also worth noting that save for the crack of 2008, it would have stayed systematically under the market. Signal changes occurring every 497 days on average makes it a very longer-term indicator.

Using Momentum

We calculate the momentum, as the ratio between two exponential moving averages of the S&P500 prices, one short (30 trading days), one long (90 days). If the S&P 500 is going up, the short EMA will be greater than the long EMA, and the momentum will be greater than 1. If the momentum is greater than 1, we invest in the market; if it's not, we stay with cash.

This method gives significantly better results than the previous one. In 2000, the algorithm was still underperforming the market by 25%, but it successfully caught up mid-2002 and managed to avoid the crisis of 2008.

At the end, a $100K investor following this method would end up with $532K instead of $394K for the market in general, an annualized return of 8.52% vs 6.95%. Maybe more spectacular, the maximum drawdown is only 15.08% instead of 53.75% for the market (although one has to question the psychological pressure of staying out of the market when everybody's around is doing well). Signal changes every 497 days on average, which means very few back-and-forth transactions.

Economic Indicators

The real prices of stocks

Because the market is inherently riskier than debt instruments, its yield shall be higher. If it's not sufficiently higher, then the market is too expensive and ripe for a correction. Such an idea was explored by Campbell and Shiller (1998) or Shen (2002) with promising results. One can download both the historical S&P 500 P/E ratios and the 10-year Treasury yields here. From there, it's only a matter of calculating the difference between the P/E inverse (the yield of stocks) and the long-term yield. Every time this difference is lower than the tenth percentile (here -3.15), we stay out of the market, considering stocks too expensive.

Whereas this principle seems to have given good results in the far past, here it's clearly not working; the return is under the market at 6.24%, and it did not help lowering the risks, since the maximum drawdown is still at 53.75%. The beige dotted line shows the spread itself. As one can notice, the low spread triggered an exit from the market in 1999, but 18 months too early. As for the crash of 2008, it went totally unfiltered by this method, since the S&P500 yield spread was quite high.

Economic Forecast

The main difficulty is to find true leading indicators, since we need statistics that allow to foresee a depression, not ones that are consequences. The first indicator we try to use is the OECD composite leading indicator, for which data can be easily obtained here. We calculate a momentum for this indicator by comparing the latest value with an EMA of period 2. Note that we don't try to smooth the data more, since this indicator is already a composite. At last, because this indicator is published with a delay of at least a month, our action is based on the momentum of the previous month.

The composite leading indicator may be an indicator for the economy, but it's not for the stock market. Returning only an annualized 5.65%, this momentum indicator should clearly be avoided, even if it reduced the maximum drawback to 23.79%.

Maybe it's worth trying another data source; something between production and consumption. We'll use freight as a tentative leading economic indicator, a favorite of Warren Buffett. We can obtain a graph of the freight-ton-kilometers scheduled for air cargo on the IATA site. Transcribing this graph back to raw data (we use the seasonally adjusted figures), we can again calculate a momentum, hopefully indicating the health of the worldwide economy.

Although the simulation is unfortunately on a shorter period, it shows promising results: It beat the market with an annualized return of 3.69% instead of 0.25% on the period, managed to stay above the S&P 500 at all times, and limited the losses during the crash of 2008, with a maximum drawback to 30.32%.

Market Perception

From Experts

When calculating the prices of options, professionals have to make hypothesis about the volatility. One can view ex-ante volatilities as a measure of nervousness; therefore, if we compare them with the real market volatility, we can determine how serene are the experts at any time.

Luckily, this comparison is very easy to do. The VIX index is an average of those hypothetical volatilities on a 30-day period for the S&P 500 stocks. We first calculate a real volatility for the S&P 500, as an annualized monthly standard deviation. We then divide it by the VIX value to obtain a serenity indicator; the higher this indicator, the lower the subjective perception of the market volatility by professionals. At each cycle, we only trade if this indicator is above a certain level, taking the arbitrary value of the tenth percentile (0.00157 in this simulation).

As you can see, in two words the results are "not working." Not only is the return under the market, at 5.89% annualized, but the maximum drawdown is not improved.

From Everybody

Several academic papers describe how taking into account the general feeling can improve market timing; see Gilbert and Karahalios (2009) and Bollen, Mao and Zeng (2010). But those rely upon data that are somewhat complex to obtain or aggregate. By using search statistics instead, we may try to measure the people's worries in a relatively simple, straightforward way. Such a system is used by Google (NASDAQ:GOOG) itself to predict flu pandemics.

Here we download queries statistics from Google Insight, then calculate the same kind of momentum we did with prices. When this momentum is positive, we assume people tend to see more negative events around them and stay out of the stock market, and vice-versa. The word we chose to monitor is bankrupt, since it's non-equivocal, well understood, and shall be an excellent marker of business difficulties (personal economic woes are harder to identify, since words like job or crisis are equivocal). However, Google Insight data are only available from January 2004; therefore, this simulation is much shorter than the previous ones.

The interesting thing is that this method both managed to cut losses during the 2008 crash, and to enter the market early in 2009, something impossible with market-following strategies. Over those six years, it gives an excellent annualized return of 7.74% against a meager 1.46% for the S&P 500 over the same period, and spectacularly reduced the maximum drawdown from 53.75% to 10.36%. At last, with signal changes every 157 days on average, it gives a usable horizon for mid and long-term traders.


It goes without saying, but no market timing can beat a totally bullish market. Therefore, any market timing strategy must be seen as an insurance: Costly when everything goes right, but dearly needed when the times get rough. One may consider the attractiveness of such a strategy to vary according to the economic circumstances, and given the current concerns about the economy at large and sovereign debts in particular, now maybe a good time. You may or may not want to keep that arrow in your quiver.

Disclosure: I have no positions in any stocks mentioned, and no plans to initiate any positions within the next 72 hours.