One of the most famous quotations in world literature comes from William Shakespeare’s Hamlet- “To be or not to be: that is the question”. When it comes to investing/trading, perhaps the most fundamental question is “Trend or counter-trend: that is the question”. Trend analysis is mainly used for technical analysis, where you look at asset prices, but it can also apply to fundamental analysis, where you would look at earnings or economic indicators. Trend analysis can be applied to the market as a whole, individual sectors, mutual funds and individual securities.

In a trending market, higher prices are generally followed by still higher prices. Falling prices are generally followed by still lower prices. A trend reversal is signaled by several declines following a long period of rising prices, or vice versa.

In a counter-trend or trading range market, higher prices are generally followed by lower prices and lower prices are generally followed by rising prices. A series of several declines that followed a rising market would signal a buying opportunity. Several advances following a falling market signal a sale.

A trading system that is profitable in a trending market will stay with the trend as long as possible and usually uses something similar to a trailing stop loss as an exit strategy because it signals that the trend is changing. A system that is profitable in a counter-trend market or trading range market does the opposite- it takes positions against the trend. Something similar to an RSI indicator is often used to measure when a market is “overbought” or “oversold”.

There are many technical indicators that have been developed that try to measure trend or counter-trend. But I have found that one of the most useful indicators comes from statistics- the runs test (also called Wald-Wolfowitz). It is a statistical test that checks the hypothesis that elements of a two-valued sequence are mutually independent. In non-mathematical terms, it can be used to categorize a time series as trend, counter-trend or random.

What I like about the runs test is that it is non-parametric. This means it is distribution free and does not rely on assumptions that the data is drawn from a given probability distribution. Some hedge funds have blown up because they assumed a normal distribution, which did not take into account the “fat tails” in financial data.

Given any time series, you can assign each data value a plus sign when it is higher than its predecessor. Assign a minus sign if it is lower or unchanged. A run is a sequence of data points with the same sign. The number of increasing or decreasing values is the length of the run. For example, the sequence “+++++—+++++++–“ contains four runs, two of which consist of +’s and the others of –‘s.

The runs test can be used on time series of any data frequency- intra-day, daily, weekly, and monthly. A Z-Statistic can be computed which measures how far a given sequence departs from the number of runs expected in a random sequence. A higher negative number means there are fewer runs than expected or that a sequence is trending. A higher positive number means that there are more runs than expected and a sequence is in a trading range or is oscillating.

This formula may be used to compute the Z Statistic for the runs test:

N*(R – 0.5) – X

Z= ———————————

SQRT{ X*(X-N)/ (N-1) }

Where N= # of data points; R=# of runs;

X= 2*W*L where W=number of up days and L= number of down days.

Let’s compute an example. We have data for 252 trading days. There are 140 up days and 112 down days, and we there 110 runs.

X= 2*140*110= 30,800 W=140 L=112

N= 252 R=110

252(110 – 0.5) – 30,800 – 3206

Z= ————————————- = ———- = -1.66

SQRT{(30,800*30,548)/251} 1936.1

Since Z is negative, there are fewer runs than by chance, which imply trending behavior with fewer but longer streaks. A positive Z score would have indicated countertrend or trading range behavior.

The absolute value of Z can be used as a measure of how far from normal the data series is. If we accept the sign of Z as correct and want to know how significant it is, we would use a one-tailed statistical test. The absolute value of Z must be greater than 1.65 to be significant at the 5% level, and greater than 2.33 to be significant at the 1% level. For this example, the data series exhibits trending behavior with significance at the 5% level.

I have written some software to compute the Z-statistic for a time series using any look back period and data frequency. Usually you will find that individual equities and equity indices tend to be normal most of the time with absolute Z-statistics oscillating above and below zero. But sector funds tend to trend more often with negative Z-statistics. The best trenders seem to be open end fixed income mutual funds which often have very negative Z-statistics and trend at the 1% level or less.

It is important to keep in mind that the same data series can be trending or counter trending based on the time frame or data frequency used. For example, consider a hypothetical mutual fund that has regular streaks of 20 up days followed by 20 down days. If you look at daily frequencies, the Z-statistic will be very negative implying strong trending behavior. But if you looked at month-end data only, the data would be very choppy and the Z-statistic would be very high, implying a trading range.

Pingback: Exit Strategies for Trend Following Systems « Quant Investor’s Blog