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:
- I extended the technique to exponential moving averages.
- 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)
- 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.
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 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:
Here’s an example of the indicator applied to a 15-minute chart:
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/
Interesting post
Thanks