# Another Way to Follow the Trend. Hidden Divergences in Trading.

## New Trading Ideas using Python.

Naturally, when prices are rising and making new tops while a price-based indicator is making lower tops, a weakening is occurring and a possibility to change the bias from long to short can present itself. That is what we call a normal divergence. The figure below shows a normal divergence occurring with respect to the RSI’s levels. Notice how prices corrected afterwards which evidently does not always happen. It may also take a long time to materialize.

But, as financial traders or analysts, we are all aware of the famous divergence. We know that:

- When prices are making
**higher highs**while the indicator is making**lower highs**, it is called a**bearish divergence**, and the market might stall. - When prices are making
**lower lows**while the indicator is making**higher lows**, it is called a**bullish divergence**, and the market might show some upside potential.

A hidden divergence can be seen as the mirror view of a normal divergence. Here’s how:

- When prices are making
**lower highs**while the indicator is making**higher highs**, it is called a**bearish hidden divergence**, and the market might stall. - When prices are making
**higher lows**while the indicator is making**lower lows**, it is called a**bullish hidden divergence**, and the market might show some upside potential.

The hidden divergence technique is a local trend-confirmation strategy, as opposed to the normal divergence which is a contrarian strategy. They more or less confirm the recent trend (not the overall direction). Hidden divergences tend to occur with the Stochastic oscillator more than others and that is why we will focus on it for the remainder of the article.

**The Stochastic oscillator** seeks to find oversold and overbought zones by incorporating the highs and lows as well in its formula. It uses the below formula to calculate its values:

We will create the below function that calculates the Stochastic on an OHLC data:

`def stochastic(Data, lookback, onwhat, where):`

for i in range(len(Data)):

try:

Data[i, where] = (Data[i, onwhat] - min(Data[i - lookback:i + 1, 2])) / (max(Data[i - lookback:i + 1, 1]) - min(Data[i - lookback:i + 1, 2]))

except ValueError:

pass

Data[:, where] = Data[:, where] * 100return Data

Although visually, divergences seem to predict the direction, **they are not to be used in isolation as trading strategies**.

In a previous article, I have discussed a back-test on normal divergences on the RSI:

In this study we will try to code a strategy that uses hidden divergences to trigger trades. Therefore, we will not go too far from the code we have used in the other article, but we will be back-testing hidden divegences on the Stochastic oscillator rather than normal divergences on the RSI.

# Back-testing the Hidden Divergence Strategy.

The hidden divergence function that we will be using is the following which is really simple and recursive. It is self-explanatory in the sense that reading it should make sense to the subjective eye. It should be applied to an OHLC data structure with the fifth column being the Stochastic indicator. Hence, the below hidden divergence function will loop and find the trading triggers and populate the seventh and eight columns with long orders [1] or short orders [-1].

`def hidden_divergence(Data, lower_barrier, upper_barrier, width):`

for i in range(len(Data)):

try:

if Data[i, 4] < lower_barrier:

for a in range(i + 1, i + width):

if Data[a, 4] > lower_barrier:

for r in range(a + 1, a + width):

if Data[r, 4] < lower_barrier and \

Data[r, 4] < Data[i, 4] and Data[r, 3] > Data[i, 3]:

for s in range(r + 1, r + width):

if Data[s, 4] > lower_barrier:

Data[s, 6] = 1

break

else:

break

else:

break

else:

break

else:

break

except IndexError:

pass

for i in range(len(Data)):

try:

if Data[i, 4] > upper_barrier:

for a in range(i + 1, i + width):

if Data[a, 4] < upper_barrier:

for r in range(a + 1, a + width):

if Data[r, 4] > upper_barrier and \

Data[r, 4] > Data[i, 4] and Data[r, 3] < Data[i, 3]:

for s in range(r + 1, r + width):

if Data[s, 4] < upper_barrier:

Data[s, 7] = -1

break

else:

break

else:

break

else:

break

else:

break

except IndexError:

pass

return Data

The **width **variable refers to the window which we’ll calculate the divergence on. For example, on the chart you may see a big divergence spanning over multiple bars (big width) and you can also see small divergence that happen fast over a few bars (small width). Enlarging the width will make the algorithm take into account those big ones. It is up to the coder to tweak it.

The below are some examples of the signal charts. Notice, that the quality of the signals is respectable, trading them however may require more subjective reasoning.

**Stochastic lookback = 5.****Upper barrier = 90.****Lower barrier = 10.****Width = 20.**

The below is the performance on 10 major currency pairs through the 3H time frame compared to its sister strategy of a normal divergence (same conditions and same pairs). It is clear that these two strategies deserve a chance within the trading framework as they can be optimized and used as a confirmation signal to strengthen your conviction. I know I use them all the time.

For more information on the normal divergence strategy, refer to the article provided above.

# Conclusion

It is clear that there is much that can be done with the optimization parameters in order to get more signals. For example, tightening the barriers to 20 and 80 will definitely trigger more divergences and thus, more trades. Increasing the Stochastic lookback period will show the usual divergences that can be easily spotted. Combining the divergence system with other methods will yield stronger results. Finally, the touch of risk management should stabilize them more.