The first concept we need to know to calculate the MAWI is the width between the two chosen moving averages, which is simply the difference between the short-term moving average and the long-term moving average.
The width is actually one of the two lines on the MAWI. With this being said, I should mention that this indicator is composed of two lines. The first is the MAWI which is calculated using the width formula above (mostly for discretionary trading), and the second is the normalized MAWI (mostly for systematic trading) which is used to find extremes in the width. Therefore, the structure of the MAWI is as such:
- The MAWI line is used to find simple crossovers. As we can see from the above, the width variable is the MAWI line. Hence, whenever it crosses the zero line, a crossover between the two moving averages has just occurred. It is mostly used to detect the structural changes in the trend and therefore is suited to a discretionary (non-algorithmic) trading system. In other words, the MAWI line just facilitates the detection of crossovers by simply looking at the zero line.
- The MAWI normalized is used to find extremes in the MAWI line (width). It is a contrarian line for short-term reactions and quick trades as opposed to the one above. It is more suited to a systematic (algorithmic) trading approach due to its frequent signals and requirement of a solid risk management system.
The way to normalize the MAWI so that we get an idea of when the distance becomes “too much” distance is by using the normalization formula below.
This means that we are looking for when the width between the two moving averages moves away from its usual range. When this happens, we want to initiate a contrarian trade. For example, in a bullish trend, when the difference between the short-term moving average and the long-term moving average increases due to the lag, then we can say that there might be an extreme. The question is, how to know whether there is an extreme?
We are doing that through the normalization function. The latter function is the one we have been seeing together in the previous articles. It traps values between 0 and 1 so that we understand extremes better.
def normalizer(Data, lookback, what, where):for i in range(len(Data)):
try:
Data[i, where] = (Data[i, what] - min(Data[i - lookback + 1:i + 1, what])) / (max(Data[i - lookback + 1:i + 1, what]) - min(Data[i - lookback + 1:i + 1, what]))except ValueError:
return Data
pass
We can code the MAWI in Python using the following function:
def mawi(Data, short_ma, long_ma, normalization_lookback, what, where):Data = ma(Data, short_ma, what, where)
# MAWI line (Width)
Data = ma(Data, long_ma, what, where + 1)
Data[:, where + 2] = Data[:, where] - Data[:, where + 1] # MAWI normalized
Data = normalizer(Data, normalization_lookback, where + 2, where + 3)
Data[:, where + 3] = Data[:, where + 3]return Data