Posted on February 1st, 2007
Written by Richard


A couple articles in the February 2007 TAS&C (http://www.traders.com/S&C_homepg.html) caught me eye. One of them talked about a method to attempt to predict a simple moving average crossover the bar before it happens. Basically, they figure out what the price would have to be in the next bar, in order to make the MAs cross. Then, they compare the needed price to the current price. If the MAs would cross even if the stock remained unchanged in the next bar, they predict that it will happen.
I liked the idea, and made a few enhancements while playing with it:
  1. I extended the technique to exponential moving averages.
  2. I changed the indicator to a +1/-1 oscillator, so it’s easy to read (the indicator in the magazine had a huge range, and you had to watch for it to cross the stock price)
  3. I added a way to make the indicator tunably optimistic, if you wanted earlier warning at the cost of more false alerts

If you want to know how it all works, and was derived, you can read the Mathematica notebook I used (http://www.movethemarkets.com/mathem...rediction.html). I tried to explain things as I went so it’s easy enough to follow. I’ll put some of the graphs from that file below.
Here’s some (obviously extremely fake) price data, with two moving averages. The fast MA is the red line, and the slow MA is the blue line.

I zoomed in on a couple of the crossings, and superimposed the oscillator in green on top of it. You can see that it tends to go from positive to negative (or vice versa) the bar before the crossing actually happens:


The Formulas

For EMAs, the formula for tomorrow’s needed price for crossover is:

.. where ‘f’ and ’s’ are the ‘fast’ and ’slow’ ema periods, respectively. If you plug in 8 and 20, you’ll get (57ema[20] – 49ema[8]) / 8.
For SMAs, the formula is:

Note that for SMAs, you wind up doing the computation with faster MAs than the ones you are predicting. For instance, the formula for 8 and 20 SMAs is: (38*MA[19] – 35*MA[7]) / 3. So, for 8 and 20, you end up using 7 and 19. The reasons are explained in the Mathematica file.
MA Crossover Prediction on Real Stock Data

I turned my version of the indicator into a QuoteTracker PaintBar (http://www.movethemarkets.com/blog/2...bars-tutorial/) setting. The one I made looks for 8EMA vs. 20EMA crossings. The formulas above tell you how to make settings for other MAs.
Here’s an example from the daily chart for SLAB:

The 2 ema’s are on this chart in red and blue. You can see that the crossover was correctly predicted, and that going long the day of the trigger would have worked pretty well (although in this case there really wasn’t much advantage in getting in early). That’s not to say I recommend jumping in front of MA crossovers as your complete trading system! If you turn on the paintbars, and look at lots of charts, you’ll see that it is a pretty standard mix for a trend-following system… lots of little losses and occasional huge wins. Using other indicators and chart patterns to inform the trades would probably increase the win rate substantially.
The other thing you’ll notice when you look at lots of charts, is that it often predicts a big move, but in the wrong direction. I haven’t done any thorough analysis, but I think this is more likely when the price is very close to the MAs. So, instead of pushing through and causing the crossover, the price is repelled and runs the other way. Here’s an example of this:

The indicator predicts a down crossover on 8/9 and 8/11, but the crossover doesn’t happen, and instead there’s an explosive move the other direction. Again, other indicators or chart patterns would help make more informed trading decisions. Or, it seems just taking an early loss on the short trade and going long would be profitable in most situations like this.
Here’s an example of the indicator applied to a 15-minute chart:

You can see two triggers on this one (the red and green box at the bottom). The first you’d likely scratch or take a loss, and the second would make some money. Sorry, the 2 EMAs aren’t on that intraday chart (I don’t like messy intraday charts), but I did confirm that both crossover predictions were correct in this case.
The Paintbar Code

Here is the code for the “conservative” version of the paintbar code, which matches the TAS&C article’s approach:
Rule 1:
if (EMA(8) < EMA(20)) AND ((57*EMA(20) – 49*EMA(8))/8 <= Bar Close) AND ((57*EMA(20)[1] – 49*EMA(8)[1])/8 > Bar Close[1]) set color to Lime and stop
Rule 2:
if (EMA(8) > EMA(20)) AND ((57*EMA(20) – 49*EMA(8))/8 >= Bar Close) AND ((57*EMA(20)[1] – 49*EMA(8)[1])/8 < Bar Close[1]) set color to $2B2BFF
…. and here is the code for the “eager” version, that is more optimistic about possible crossings, but also cries wolf more often:
Rule 1:
if (EMA(8) < EMA(20)) AND ((57*EMA(20) – 49*EMA(8))/8 <= (Bar Close + 0.5*Average True Range(14,3).Signal)) set color to Lime and stop
Rule 2:
if (EMA(8) > EMA(20)) AND ((57*EMA(20) – 49*EMA(8))/8 >= (Bar Close – 0.5*Average True Range(14,3).Signal)) set color to $2B2BFF
You can tune the optimism by adjusting that 0.5 factor on the Average True Range. This basically says, assume it will cross if the crossing point is within half an ATR of the current price. The “conservative” version assumes a cross will happen if the crossing point is at the current price, or better.
source:
Code:
http://www.movethemarkets.com/blog/2007/02/01/ma-and-ema-crossover-prediction/