Thursday, February 26, 2009

Information Theory and TA Indicators

Information theory is a branch of math that deals with determining how random something is. As such, it is useful to think about information theory when trying to find patterns in mostly random data (like stock prices).

In finance, the basic problem is determining what the next value in a time series is. With technical analysis, this problem is solved by looking for historical patterns that are similar to the current time series and seeing what the next value was after the match. In other words, TA is basically the practice of putting each day into a bin of quantitatively similar days and then prediciting that the next return will be close to the mean value of returns for that bin.

An important question to consider is how many bins to sort your data into. If you take the RSI indicator, it produces a number from 0-1. You can clculate that number to a fairly large number of decimal places. But realistically it won't matter past at most 2 (which is one reason it makes sense to scale it to 0-100). And it mostly won't matter beyond high, low, and neutral. You might be able to add very high and very low. But you don't actually have enough reliable information to split it into more than maybe 5 or 6 bins.

In information theory, a common way to express the amount of information you have is in bits. A bit basically represents a 50% chance of guessing what the information is when you haven't actually received it yet. More bits of information represent a lower probability of randomly guessing correctly. So with 2 bits of information, you would have about a 25% chance of guessing correctly. Note that in information theory, bits are not discrete, so it is perfectly valid to say you have 2.5 bits of information.

My feeling is that most technical analysis indicators give you at most 2-3 bits of information. That is equivalent to saying that you could divide the numbers into 4-8 bins where the different bins actually mean something, but that if you try to go further than that you aren't actually improving your sorting.

Furthermore, most TA indicators are fairly highly correlated, which means you can't improve your predictions much by adding more indicators. So if you have 3 oscillators operating on the same time scale which all provide 2 bits of information, you will probably get about 2.2-3 bits by using 2 of them and 2.5-3.5 bits by using all three of them. The reason for this is that if one of your oscillators is oversold, it is likely that the other two are as well (and so on). I would guess that the most information you can get from technical analysis is probably on the order of 4-5 bits. With 4 bits of information, you would expect to get the market direction right at least 80% of the time, which is more than enough to make money.

One problem that many system developers run into is that they pretend to have more information than they really have. One example is curve-fitting. Let's say you have an oscillator and you are choosing the period, overbought, and oversold levels. You are introducing about 5-10 bits of information based on what levels you choose. That makes you indicator appear to produce substantially more information than it really does. The best setting will produce phenomenal results, but they aren't likely to continue out of sample.

Another way to produce imagined information is by using overly complicated indicators. For example, it has been widely reported that you can make a decent trading system based on looking at whether today was an up day or a down day. This produces almost 1 bit of information (slightly less because it isn't a 50/50 split). If you look at 2 day patterns of up or down, you get about 1.8 bits, and you can make a better prediction. But if you keep adding more days, eventually you aren't adding useful information any more. If you go out to 20 days, you have about 16-19 bits of information, but most of it won't help you predict what will happen the next day. In fact I would expect the returns from the strategy to start dropping as you add more days past a certain point (probably 3-5 days).

One way of knowing when you have overestimated your information level is by looking at your sample sizes. 5 bits of information gives you 32 bins. You probably want at least 50 samples per bin, so you need about 1600 days to reliably get 5 bits of predictive information (and that's under ideal conditions). If you look at 20 days of up/down data, you have just over 1 million bins, so you need about 50 million days of data to expect your information to be reliable. When a system is curve-fit, varying the parameters just a little bit will move only a small number of observations from one bin to another. In terms of information, the two systems are different by only a small fraction of a bit. If that produces large changes in your system's performance, then you are using unreliable information to produce the extra performance. Larger sample sizes would increase the number of observations sorted differently, which would increase the information difference and increase your confidence that the difference is real.

Labels: ,

Wednesday, December 03, 2008

Oscillator Time Scales

Most technical analysis indicators are some form of oscillator. An oscillator can be roughly defined as a numerical indicator that goes up and down in a somewhat fixed range. Non-oscillating indicators (like moving averages) are harder to use because they don't have many repeated values. But most non-oscillating indicators can be turned into oscillators by comparing relative values (for example, the ratio of two moving averages is an oscillator). So for the most part, numerical technical analysis is the art of finding oscillators with predictive value. Charting is an entirely separate practice.

For the most part, oscillators are either positively or negatively correlated with price: if the price goes up, the oscillator goes up/down, and if prices go down, the oscillator goes down/up. There are a few exceptions (mostly breadth-based indicators), but the vast majority of indicators follow this pattern. Inversion of an oscillator allows every oscillator to be turned into a positively correlated oscillator. So a simple analysis of postively correlated oscillators will cover most things that can be practically achieved with TA. Also, you can use just about any oscillator you want to, since they all tell you the same thing.

The main thing that will change from one oscillator to another is how the indicator accounts for past price changes. A fast indicator will mostly just use the last few bars of prices, while a slow indicator will use a larger number of bars. Different indicators will have varying weightings over the period they look at, so some indicators will do a better job of picking up net price movements in a choppy market than others will. But the speed ends up being the most important factor.

There are also two ways to trade the market: momentum and mean-reversion. Momentum trading is when you buy when prices have risen and sell when prices have fallen. Mean-reversion trading is when you sell when prices have risen and buy when prices have fallen. Both styles are profitable most of the time at some time scale. The key trick to trading profitably is to determine what time scale works best for mean-reversion and what time scale works best for momentum.

As an example, consider the RSI_ema indicator. The Matlab code for this indicator is
function [x] = rsi(price, period)

u(2:length(price))=max(0,diff(price));
d(2:length(price))=max(0,-diff(price));
us=ema(u,period);
ds=ema(d,period);
x=us./(us+ds);
x(1)=.5;

The u variable is the up movement in the price each day (0 if the price dropped). The d is the down movement (0 if price went up). us and ds are the ema's of the variables. Then you compute the fraction of the total price movement that is up movement. The standard RSI indicator uses sma's instead of ema's. Most people also multiply the value by 100 to get a range of 0-100 instead of 0-1.

The RSI_ema indicator is a simple, positively correlated oscillator. To trade in momentum mode, you buy when the indicator crosses above some threshold (somewhere between .7 and .95 normally) and sell when the indicator crosses below some other threshold (somewhere between .05 and .3 normally). you can stay in until the reverse signal is generated, or you can set a threshold closer to .5 for closing your trades. So you might go long when the indicator crosses above .9 and then close your position when it drops below .5 and go short when it crosses below .1 and then cover when it crosses above .5, or whatever values you have chosen. You can even use asymmetric values if you want to go long or short more easily. To trade in mean reversion mode, you take the opposite trades.

The test I am performing determines how RSI_ema responds to different oscillation rates in the market. For this test, I am using a sine wave plus a constant to generate the price signal. The market never moves in perfect patterns like sine waves, but this will demonstrate how indicator performance will vary based on market conditions.

The first test is to see how different values for the RSI_ema period perform at different market frequencies. I ran a sine wave with a period from 10 to 200 bars through the momentum trading strategy. For each RSI_ema period, the sell threshold was set to .1 plus the 20th percentile value of the indicator (to adjust the level somewhat based on what range the indicator actually moved in). The buy threshold was set to one minus the sell thrshold. The system is always in once it makes its first trade.

Here is the result of a very choppy market (sine wave period=10):


As you can see, the choppy market is moving too fast for most values of the RSI_ema indicator to even try trading it. The ones that do manage to trade it lose money. They would make money if they had been traded in mean reversion instead.

Slowing down the choppiness a bit:

The faster settings of RSI_ema start making money. The crossover seems to be around 1/3 of the sine wave period.

And slowing it down even more:

Here, the crossover can be seen better. Again, trying to trade a slow moving market with a fast momentum indicator works well. Trying to trade a slow market with a slow momentum indicator does not work. The slower settings should be used for mean reversion.

Now for one more graph. This one shows how the profitability changes with a fixed RSI_ema period of 4 as the sine wave period is varied:

Once again, for very fast markets, the indicator does not provide any signals. As the market slows down a little bit, the indicator makes money when used in mean-reversion mode (remember that mean-reversion trades opposite to momentum, so a momentum loss is a mean-reversion profit). As the market slows down even more, the indicator makes money in momentum mode. The odd spikes occur when the sine wave period is a multiple of the RSI_ema period and should be ignored.

Of course, no security is ever going to trade with a perfect sine wave (and if it does, you don't need an indicator to trade it). But, knowing how oscillators vary based on the market speed is useful for building an adaptive trading system. There is no single setting for any indicator that will always work. Sometimes the market is very choppy and you want a fast mean-reversion system to maximize your profits. Sometimes the market is moving in long trends and you want a momentum strategy. Looking at the recent performance of trading strategies based on a range of indicator speed settings will provide some guidance as to how fast the market has been changing directions recently. If you assume that this fundamental period will change slowly, then you can measure the recent value and use that to choose optimal indicator settings.

For example, RSI_ema crosses over from making money to losing money when the setting is about 1/3 of the sine wave period. If you plot the profitability of RSI_ema strategies over the revent past, you should see a graph moderately similar to the ones above. That will show you roughly where the crossover point is from mean-reversion to momentum. You can use this information to determine whether you should be trying to catch trends or watching for reversals. You could also do this with any other oscillator.

Note that in real markets, you won't get a nice clean drop-off like you do when trading a sine wave. Real markets have a balance between mean-reversion and momentum going on at all sorts of time scales. A graph of the profitability based on the time setting of the indicator is likely to cross zero several times as you pick up on various trends and reversals at different time scales. The trick is finding regions that are relatively stable on one side or the other (momentum or mean reversion).

This can also be used to determine the underlying characteristics of the market. The time scale-profit graph is one of the signatures of who is trading and how. Interpreting it correctly can provide an edge in trading.

Labels: ,