# Detecting Moving Average Crossover Before It Happens

Have you ever wish for the moving averages to crossover earlier? I am going to show you a way to detect moving average crossovers before they actually happen.

**The Advantage**

Being able to know that the moving averages are going to crossover gives you an edge over the others who simply follow the signals after the fact. You can take advantage of the extra time you have to cross check other confirmation signals, price your orders better, etc.

**Understanding the Mathematics**

*(Lawrence – This part is not necessary for the utilization of the concept presented. It is useful, however, for those who are interested in applying the same concept to indicators other than simple moving average.)*

If we define S as a simple moving average, where

`S (n) = (P (0) + P (1) + ... + P (n - 1)) / n`

and P (n) is the price of n bars ago.

Lets also define S’ (n) as the next value that S (n) will hold. Then,

`S' (n) = (n S (n) - P (n - 1) + P') / n`

and P’ is the next price value.

For a crossover between a n-period and m-period moving averages to happen, we have

`S (n) < S (m)`

`S' (n) >= S' (m)`

By expanding the inequalities above and then reducing the parts, we will get the following,

`P' >= n m / (m - n) . n m ( S (m) - S (n) - P (m - 1) / m + P (n - 1) / n)`

Why are we interested in knowing P’ before it happens?

That’s because we are interested to know if we can predict a crossover before it actually happens.

**A Picture is Better than a Thousand Words**

Here is a chart with 20-period and 50-period moving averages.

The *PriceToCross* indicator is a *fml *indicator with the following formula that calculates P’.

`$period1 := 20;`

$period2 := 50;

ma1 := average (data1, $period1);

ma2 := average (data1, $period2);

$period1 * $period2 / ($period2 - $period1) *

(ma2 - ma1 - data1 ($period2 - 1) / $period2 + data1 ($period1 - 1) / $period1);

*PriceToCrossFromClose *is the *subtraction *indicator taking the difference between the *PriceToCross *indicator and the data series *close *values.

*SMACross *indicator is the *Crossed *indicator that shows the crossover signals generated by SMA20 and SMA50. It is colored in Red.

*PriceToCrossFromCloseCrossZero *is the *Crossed *indicator that shows the crossover signals generated by PriceToCrossFromClose and the Zero line. It is colored in Green.

Notice that the Green signals happen before the Red ones. Our Green signals *predict *the happening of the Red signals.

In general, using the predictive method you get your signal one bar ahead. For some situation, you can get prediction 2 to 3 bars ahead. For steep turning points, the predictive method will happen at the same time as the actual signal. The predictive method never lags the actual one.

**There Is Always a Catch**

Using the predictive method, where P’ crossover zero, sometimes generate false signals as the actual crossovers between the moving averages do not materialize. Here is a chart with 2 false signals.

In the morning of March 3, the moving averages are so close together for multiple bars already, thus the P’ that we have also jitter around the zero line, producing the false signals.

**Summary**

The technique shown here is a general one that can be utilized with all moving averages, not just the ones applied onto price. For example, if you trade stochastics signals, then if you use two (2) simple moving averages to smooth your indicator, you can then utilize the same concept presented here for early crossover detection.

Your wish of having the moving averages cross sooner may not be filfilled yet, but you are definitely one step closer.