Momentum can be faded in many ways when we have enough elements to suggest that exhaustion may happen. This can be done through divergence analysis, oversold/overbought levels, and timing indicators. In this article, we will create an indicator that can be used both for discretionary and systematic trading, the Swing Indicator which takes into account highs and lows to determine empirical mean-reversion levels.
I have released a new book called "Contrarian Trading Strategies in Python". It features a lot of advanced contrarian indicators and strategies with a GitHub page dedicated to the continuously updated code. If you are interested, you could buy the PDF version directly through a PayPal payment of 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.
Creating the Indicator
The Swing Indicator is based on finding patterns where the trend starts to weaken when approaching a certain threshold. This can be measured by the Momentum Gauge calculation seen below. The first calculation has the following score that will be later summed up:

- 0 if the current high is less than the previous high and the current close is less than the current open price.
- 1 if either the current high is greater than the previous high or the current close is greater than the current open price.
- 2 if both current high is greater than the previous high and the current close is greater than the current open price.

- 0 if the current low is greater than the previous low and the current close is greater than the current open price.
- 1 if either the current low is lower than the previous low or the current close is lower than the current open price.
- 2 if both current low is lower than the previous low and the current close is lower than the current open price.
Then, we calculate the difference between the Upside Momentum Gauge and the Downside Momentum Gauge to find the net momentum effect.

Finally, we have to sum up the latest values of the Raw Swing. By default, the lookback period should be 8.


We need to define the primal manipulation functions before writing the code of the Swing Indicator:
# The function to add a certain number of columns
def adder(Data, times):
for i in range(1, times + 1):
z = np.zeros((len(Data), 1), dtype = float)
Data = np.append(Data, z, axis = 1)
return Data
# The function to deleter a certain number of columns
def deleter(Data, index, times):
for i in range(1, times + 1):
Data = np.delete(Data, index, axis = 1)
return Data
# The function to delete a certain number of rows from the beginning
def jump(Data, jump):
Data = Data[jump:, ]
return DataAnd now for the code needed to output the indicator:
def swing_indicator(Data, lookback, opening, high, low, close, where):
# Adding Columns
Data = adder(Data, 8)
# Upside Momentum Gauge
for i in range(len(Data)):
if Data[i, high] > Data[i - 1, high]:
Data[i, where] = 1
else:
Data[i, where] = 0
for i in range(len(Data)):
if Data[i, close] > Data[i, opening]:
Data[i, where + 1] = 1
else:
Data[i, where + 1] = 0
Data[:, where + 2] = Data[:, where] + Data[:, where + 1]
# Downside Momentum Gauge
for i in range(len(Data)):
if Data[i, low] < Data[i - 1, low]:
Data[i, where + 3] = 1
else:
Data[i, where + 3] = 0
for i in range(len(Data)):
if Data[i, close] < Data[i, opening]:
Data[i, where + 4] = 1
else:
Data[i, where + 4] = 0
Data[:, where + 5] = Data[:, where + 3] + Data[:, where + 4]
# Raw Swing
Data[:, where + 6] = Data[:, where + 2] - Data[:, where + 5]
# Swing Indicator
for i in range(len(Data)):
Data[i, where + 7] = Data[i - lookback + 1:i + 1, where + 6].sum()
# Cleaning
Data = deleter(Data, where, 7)
return Data
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:
Using the Indicator
The levels for the 8-period Swing Indicator are by default 10 for bearish signals and -10 for bullish signals. The conditions are therefore:
- Go long (Buy) whenever the Swing Indicator reaches or breaks -10 with the previous four readings above -10.
- Go short (Sell) whenever the Swing Indicator reaches or surpasses 10 with the previous four readings below 10.

def signal(Data, what, buy, sell):
Data = adder(Data, 10)
Data = rounding(Data, 5)
for i in range(len(Data)):
if Data[i, what] < lower_barrier and Data[i - 1, buy] == 0 and Data[i - 2, buy] == 0 and Data[i - 3, buy] == 0 and Data[i - 4, buy] == 0:
Data[i, buy] = 1
elif Data[i, what] > upper_barrier and Data[i - 1, sell] == 0 and Data[i - 2, sell] == 0 and Data[i - 3, sell] == 0 and Data[i - 4, sell] == 0:
Data[i, sell] = -1
return Data
Depending on the asset analyzed, the indicator may give out either frequent or rare contrarian signals. The way to use the indicator is to understant that it gives out consolidation or correction signals and is not really used for extreme reversals.

It is worth-mentioning that the indicator has a correlation of 0.60–0.70 with a same period Relative Strength Index, meaning that it offers limited diversification of signals but the added value is still there.
I have recently partnered with Lumiwealth, and if you want to see how to create all sorts of algorithms yourself, feel free to check out the below link. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Summary
To sum 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 subjective and scientifically unfounded.
Medium is a hub to interesting reads. I read a lot of articles before I decided to start writing. Consider joining Medium using my referral link (at NO additional cost to you).
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 paperback link of the book, you may use the following link: