#### Discover more from All About Trading!

Momentum is an interesting concept in financial time series. Most strategies are either trend-following or mean-reverting. Momentum is the strength of the acceleration to the upside or to the downside, and if we can measure precisely when momentum has gone too far, we can anticipate reactions and profit from these short-term reversal points. One way to measure momentum is by the momentum indicator.

The idea of this article is to code the momentum indicator and then create volatility bands around it to make it bounded. This should give trading ideas whenever the indicator touches its upper or lower volatility band.

I have released a new book after the success of my previous one “*Trend Following Strategies in Python*”. It features advanced contrarian indicators and strategies with a **GitHub **page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below Amazon link (which contains a sample), or if you prefer to buy the PDF version, you could check the link at the end of the article.

**Contrarian Trading Strategies in Python***Amazon.com: Contrarian Trading Strategies in Python: 9798434008075: Kaabar, Sofien: Books*www.amazon.com

### The Momentum Indicator

The momentum indicator’s formula is extremely simple and can be summed up in the below mathematical representation:

What the above says is that we can divide the latest (or current) closing price by the closing price of a previous selected period, then we multiply by 100. Hence, if we say we are going to use momentum(14), then, we will subtract the current values from the values 14 periods ago and then divide by 100.

The momentum indicator is not bounded as can be seen from the formula, which is why we need to form a strategy that can give us signals from its movements.

The above graph shows the EURUSD values versus the Momentum Indicator of 5 periods. This means we are simply dividing the current closing price by the price 5 periods ago and multiplying by 100. Below is the Python code to create a function that calculates the Momentum Indicator on an OHLC array.

```
def add_column(data, times):
for i in range(1, times + 1):
new = np.zeros((len(data), 1), dtype = float)
data = np.append(data, new, axis = 1)
```

` return data`

```
def delete_column(data, index, times):
for i in range(1, times + 1):
data = np.delete(data, index, axis = 1)
```

` return data`

```
def delete_row(data, number):
data = data[number:, ]
return data
```

```
def momentum_indicator(data, lookback, close, position):
data = add_column(data, 1)
for i in range(len(data)):
data[i, position] = data[i, close] / data[i - lookback, close] * 100
data = delete_row(data, lookback)
return data
```

Now, we will briefly present the second element of the strategy, the Volatility Bands.

Make sure to focus on the concepts and not the code. You can find the codes of most of my strategies in my books. The most important thing is to comprehend the techniques and strategies.

Check out my weekly market sentiment report to understand the current positioning and to estimate the future direction of several major markets through complex and simple models working side by side. Find out more about the report through this link:

**Coalescence***A Weekly Report Covering FX & Equities Market Positioning Using Complex Models. Click to read Coalescence, by Sofien, a…*coalescence.substack.com

### The Filtered Momentum Indicator

Volatility bands are composed of a moving average on the price with standard deviations around it to envelop the market. The upper band represents a dynamic resistance while the lower band represents a dynamic support.

```
def fmi(data, lookback, standard_deviation, close_column, position):
data = add_column(data, 1)
# Calculating momentum
for i in range(len(data)):
data[i, position] = data[i, close_column] / data[i - lookback, close_column]
# Calculating the mean of the momentum
data = ma(data, lookback, position, position + 1)
```

```
# Calculating the volatility of the momentum
data = volatility(data, lookback, position, position + 2)
data = add_column(data, 2)
```

```
# Calculating the FMI
data[:, position + 3] = data[:, position + 1] + (standard_deviation * data[:, position + 2])
data[:, position + 4] = data[:, position + 1] - (standard_deviation * data[:, position + 2])
```

```
data = delete_column(data, position + 1, 2)
return data
```

The trading conditions seen above are as follows:

**A long (buy) signal is generated whenever the momentum indicator surpasses its lower volatility band.****A short (sell) signal is generated whenever the momentum indicator breaks its upper volatility band.**

`def signal(data, momentum_column, upper_band_column, lower_band_column, buy_column, sell_column):`

```
data = add_column(data, 5)
for i in range(len(data)):
try:
# Bullish pattern
if data[i, momentum_column] > data[i, lower_band_column] and data[i - 1, momentum_column] < data[i - 1, lower_band_column]:
data[i + 1, buy_column] = 1
# Bearish pattern
elif data[i, momentum_column] < data[i, upper_band_column] and data[i - 1, momentum_column] > data[i - 1, upper_band_column]:
data[i + 1, sell_column] = -1
except IndexError:
pass
return data
```

### Summary

To summarize up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being a subjective and scientifically unfounded.

I recommend you always follow the the below steps whenever you come across a trading technique or strategy:

Have a critical mindset and get rid of any emotions.

Back-test it using real life simulation and conditions.

If you find potential, try optimizing it and running a forward test.

Always include transaction costs and any slippage simulation in your tests.

Always include risk management and position sizing in your tests.

Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.

For the PDF alternative, the price of the book is **9.99 EUR**. Please include your email in the note before paying so that you receive it on the right address. Also, once you receive it, make sure to download it through google drive.

**Pay Kaabar using PayPal.Me***If you accept cookies, we’ll use them to improve and customize your experience and enable our partners to show you…*www.paypal.com