Tuesday, August 20, 2013


50 MA is the line to watch today. After early morning pop, the $SPX is confined in a small range from 1655 to 1659. Maybe it will just stay in the range for the rest of the day. We'll see.

Thursday, August 1, 2013

POMO Update (08/01/2013): Sight Tilt To Short Term

In the past two weeks the Fed bought $14B 7-year and 5-year notes, and $11B 10-year notes and 30-year bonds. It appears that the Fed slightly tilted towards short duration Treasuries. According to my two SeekingAlpha articles (here and here), overweighing short duration Treasuries may indicate the Fed in itself is preparing for tapering.

I'll post another update two weeks later.

Friday, July 26, 2013

Decode the Myths of Moving Averages

Moving averages play an import role in trading and investment. Trend following strategies use MAs to determine whether a trend is ongoing. MAs are also employed in risk management. When the price breaches a preset MA, the position will be reduced or closed.

Traders (and investors) believe in a couple of myths of moving averages: MAs provide support and resistance to stock prices, and a handful of MAs are far more important than others. They are the 5-day, the 20-day, the 50-day, and the 200-day MAs.

It is confusing, especially to beginners. A student driver will be scared away if the teacher asks him to drive a car with only one pedal being simultaneously the gas and the brake. Yet traders are fearless. When the price falls back to a MA, traders expect it to bounce up – be supported by the MA. But if the price penetrates the MA, traders will make an about face and anticipate that, from now on, the MA will push the price down.

Theses and Counterarguments

Various theses exist to explain the phenomenon. But it doesn’t appear they did a good job to make things clear. Sometimes, they raised more questions.

MAs are the average prices over a period. A 20-day MA is the average cost investors have paid to buy a stock during a 20-day period. If the current price is above the 20-day MA, it is higher than the average cost. An average investor is supposed to be in profit. When the price falls back to the MA, the investor, being excited by the profit, sees a new opportunity to buy more for less. Should he rush in to buy more, his buying would have supported the price. On the other hand, if the price falls below the MA – the average cost – the investor would be in loss. To prevent further loss, he would have hit the panic button to sell, which pushes the price down.

Another thesis says it’s the calendar effect. One week has 5 trading days. At the end of a week, traders tend to close positions to mitigate headline risks during the weekend, when the market is closed and they have no way to manage their portfolio actively. The periodical opening and closing of positions create a tidal flow at a cycle of 5 days. The 5-day MA effectively averages out the ebbs and flows and reveals the true direction of the market.

Likewise, the 20-day MA is attributed to the monthly paychecks of 401K investors. The 50-day MA is related to the quarterly earnings reports when long term investors reset their expectations and adjust positions accordingly. And the 200-day MA is able to iron out any seasonal swings of business activities over a year. It is also related to the year-end portfolio adjustments for tax purposes.

Yet another thesis is based on the theory of behavioral finance. Those numbers – 5, 20, 50, and 200 – are key integers that have special meanings to us who use decimals. If we used binaries, it would have been a different set of numbers. They are important simply because everyone else treats them specially – a typical herd behavior.

One counterargument is that the first thesis assumes investors are all chasing the momentum, which is not the case. A value investor will keep buying even if the price falls as long as he sees value, no matter it is above or below a certain MA.

Another problem is that not all investors share the same trading timeframe. In fact, investors have all different timeframes. There are day traders, whose timeframes are up to a few hours. There are short term traders, who hold positions for a few days or a few weeks. There are 401K investors, who put cash to work whenever a new paycheck comes in. There are long term investors, who plan to hold a position for the foreseeable future. It’s not likely they all agree upon one or a few trading periods.

If it is the calendar effect, the problem is a quarter has 60 trading days and a year, 250. True 50 and 200 are good approximations. But if 60 and 250 are optimal, trading strategies based on them shall yield better returns. In financial markets, excessive returns will always be arbitraged. The arbitrage shall pull things back to their optimal values, and away from 50 and 200.

There is also a counterargument based on the herd behavior. A crowded trade is always vulnerable. If everyone expects the price to bounce up at the 5-day MA, they will all buy at the 5-day MA. Because no buyers are left to support the price, evil traders will try to push the price below the 5-day MA, force those who have bought to liquidate at loss, and profit from it.

In fact, if any particular MA is an optimal indicator, contrarian strategies will be developed to attack it and eventually take its power away. In the end, nothing is left to be special.

Quantitative Evidences

Trading is a complicated activity involving many people subscribing to different theses. All the theses and counterarguments have their merits. Collectively they explain the power, or lack thereof, of MAs. But qualitative arguments can only go so far. Quantitative evidences are needed to portray their combined effects.

I carried out a numerical exercise and the data shows, strikingly, the myths are at best half true. Key findings are:
  • Yes, some MAs are more important than other
  • And those MAs provide support and resistance to stock prices
  • But 20, 50, and 200 are not optimal
  • Different stocks have different optimal MAs
  • Some optimal MAs are transitory over time
  • A pure MA-based strategy performed poorly

Start from the myth that MAs are supposed to provide support and resistance. This is to say that a MA tends to push the price away from both directions. One way to quantify this is to count the number of times the price crosses the MA. The most powerful MA shall have minimal number of crosses.

If all the stocks have the same behavior, then the behavior shall appear on the S&P 500 Index. My first step is to count, on the S&P 500 Index, the number of crosses of MAs ranging from 2 days to 200 days, over the past 20 years. Chart 1 shows the numbers.
Chart 1. No. Crosses on S&P 500

The result is expected. The price often hovers around short period MAs, such as the 5-day, and away from long period MAs, such as the 50-day. Chances are it crosses more often the 5-day MA than the 50-day MA. From the chart, it appears that the relationship follows the so called power law. A power law is frequently observed in social sciences. For example, the number of cities having a certain population size follows a power law. The number of companies having a certain market cap is the same.

The beauty of the power law is the function is a straight line on a log-log scale chart. And a straight line is easy to analyze. Chart 2 shows the least-squares fit of the straight line.
 Chart 2. No. Crosses on S&P 500, Log-Log Scale

According to the theory of the least squares method, the “error” – the deviation from the straight line – is the behavior that couldn’t be explained by the straight line. The deviation is what I’m interested in. A negative deviation means the MA has less than usual number of crosses, that is, higher tendency to push the price away.
Chart 3. Errors on No. Crosses

On Chart 3, I marked the minimal values of the deviations, which are the optimal MAs that provide the strongest support and resistance.

The 5-day MA has the smallest (largest negative) deviation and is the most powerful. This matches well with that a week has 5 trading days.

The 31-day MA shall be attributed to the monthly effect of paychecks. The difference (a month has roughly 20 trading days) can be explained by the fact that some companies have a bi-weekly payment schedule, where a year consists of 26 payments, equivalent to 13 monthly payments. This blurs with the regular 12 monthly payments and the result is the 31-day period.

The number 56 is associated to the quarterly earnings report effect. The difference between 56 and 60 – the number of trading days in a quarter – can be explained by contrarian strategies playing tricks around the 60-day MA and eventually reduces its power.

The interesting number is 105, clearly linked to the number 100. Although the number 100 has no significance on the calendar, it is a special integer to all of us. This justifies that the herd behavior is evident in trading and investment.

The same for the number 194, which is closer to another special number 200 than to 250, the number of trading days in a year. The difference between 194 and 200 is the result of contrarian strategies.

The numbers show that, all the theses and counterarguments have their merits, though each of them explains only a piece of the puzzles.

A theory is valuable if works in practice. Chart 4 shows how the 56-day MA works in practice. 

Chart 4. 50-day and 56-day MA on S&P 500

On Apr. 18th (the blue arrow), the S&P 500 breached the 50-day MA, but closed above the 56-day MA. The very next day, the price bounced up. This shows that the 56-day MA is more effective than the 50-day MA. Another instance is around the end of June and the beginning of July. When the price was above the 50-day and the 56-day MA, it bounced up twice. Once broke below the two MAs, it dived hard. Then the price was held off by both MAs. On Jul. 5th, once the price broke above, a rally followed.

Let’s revisit the reasoning behind the MAs. The 5-day and the 31-day MAs are linked to the calendar. The 56-MA is linked to the calendar as well, but underneath it is linked to the earnings reports, that is, the fundamentals of the stocks. The 105-day MA is linked to the herd behavior. Later on I’ll show that only the 56-day and the 105-day MAs are shared by other stock indices. Hence the fundamentals and the herd behavior are more important in trading and investment, at least if MAs are considered.

I’ll also show that the 5-day and the 31-day MAs are transitory overtime. They were evident during the period from 1993 to 2003 but disappeared in past 10 years (2003 to 2013). The long-term MAs – the 56-day, the 105-day, and the 194-day MAs – are consistent in both periods. This suggests that long term investment strategies are stable than short term ones, again, at least when MAs are considered.

Not the Holy Grail

But the MAs are not the Holy Grail. The bad news is different stocks have different optimal MAs.  Chart 5 and Chart 6 show the same analysis on the Russell 2000 Index and the NASDAQ 100 Index. 

Chart 5. Russell 2000

Chart 6. NASDAQ 100

Small cap stocks are inherently volatile. NASDAQ 100 stocks are basically driven by their growth prospective. Their behaviors are different from those in the S&P 500 Index. Still, the quarterly effect is evident on both the Russell 2000 and the NASDAQ 100, shown by the 59-day MA on the former and the 61-day MA on the latter. And the number 100 works on both of them, shown by the 100-day MA on the former and the 90-day MA on the latter.

Another problem is, even on the same index, some optimal MAs are transitory over time. Chart 7 and 8 show the same analysis on the S&P 500 over two periods, from 1993 to 2003 and from 2003 to 2013. The 5-day and 31-day MAs were evident during the period from 1993 to 2003. But they disappeared in past 10 years. The long-term MAs are consistent over both periods.

Chart 7. S&P 500, 1993 - 2003

Chart 8. S&P 500, 2003 - 2013

Yet another issue is that a trading strategy based purely on optimal MAs performed poorly in the past 20 years. The trading strategy buys the S&P 500 Index when the price crosses above an optimal MA, and liquidates the position when the price crosses below it. As shown in Table 1, only the 194-day MA barely beat the S&P 500 Index. All the rest performed poorly. The 5-day MA even produced a negative return. This is another piece of evidence that long term MAs are better than short term ones.

Table 1. Performance of the MA Crossover Strategy
 Chart 9. Performance of the MA Crossover Strategy

The result is somehow expected. If a simple MA crossover strategy has outstanding performance, contrarian strategies will be developed to attack it and finally reduce its power. MAs with certain periods are good at providing support and resistance to the price. But the power alone is not enough to make someone rich. Still MAs are good building blocks for investment strategies. Just it’s better to use them in conjunction with other techniques.

Share the Program

I would like to share my Python script that calculates the number of crosses of MAs. Click on the link and you can view the code on the Cloud9 website. Sign up for free and then chat with me so I can grant you write permission. After that you can edit the code and run it. Everything is online and hassle-free.

Line 33 and 34 control the time period for which the script will download historical price data. Line 91 controls the ticker of the stock or index the script will calculate the number of crosses.

Saturday, July 6, 2013

Python Script to Monitor Fed's POMOs

This Python script is used to generate all the revealing charts for my latest SeekingAlpha article: "Tapering: The Fed Means It". Using it, I was able to show that the Fed is shorting duration of its Treasury portfolio and that suggested the Fed in itself is anticipating the "tapering".

The code is shared on Cloud9 website, which is an on-line Python IDE. Register a free account and you can view the code by following this link. On the website you can chat with me and I can grant you write permission and you can tweak the code and run it. All online and hassle-free.

For first time users, I'd suggest you scroll to the bottom of the code, where you can find the entry to the script. Putting all technical details aside, at the bottom of the code you will find two dates are used as the parameters --- the start date and the end date. This tells the script the time window you are interested in and the script will download data from Fed's website and run the numbers for you. I've already put in some dates that I was interested for the sake of my article. Maybe as a first step, you can tweak the dates and see whether it produces different results, meaning whether there is a change of Fed's behavior over a different time window.

For experienced users, it's easy to modify the code if you have better ways to slice and dice the POMO data. I'd like to ask you sharing your ideas and findings with me. It will help me and others better understand the financial market. And that's the purpose of sharing my code.