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:

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].

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.


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.

Written by

Institutional FOREX Strategist | Trader | Data Science Enthusiast. Author of the Book of Back-tests:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store