What are Divergences and Do They Work in Systematic Trading?

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.


Among the most widely used tools in technical analysis are oscillators, the purpose of which is to help us assess the quality and characteristics of current trends.

There's a widespread approach that consists of using oscillators to trade on divergences, that is, those periods when the price trend diverges from that of an oscillator.

In this video, we’ll show you some tests designed to verify the validity and usefulness of this approach in systematic trading. 

As usual, we'll use our trading method to create a divergence-based system and test it on a portfolio of instruments so that we can objectively assess whether it can really give us an advantage over other strategies.

By watching the video, you’ll discover:

- What divergences are and how to use the MACD to trade on them

- How to code a simple strategy based on this approach (with open-source code)

- In which markets the strategy might work best 

- Our conclusions: is it worth it?



Hey everyone, welcome to this new video! One of the Unger Academy coaches here, and today we're going to be having a look at one of the most popular types of trading approaches, that is, the one based on divergences.

Many of you have asked us if divergences may also be used in systematic trading, and that's why we've decided to make this video about it.

Intro: What are oscillators and trading with divergences

Okay, so let's start at the beginning. What are divergences? As many of you may already know, in trading there are several tools in technical analysis, one of them being oscillators, which are commonly used to assess the quality of an ongoing trend. At least that's what the major technical analysis texts state.

Incidentally, these oscillators can also be used to trade on divergences.

So, let's first see what a divergence is. A divergence occurs when the price trend diverges from the trend of an oscillator.

As we can see in this image, for example, in some places you have prices forming descending lows. For example, here, or more precisely here. But at the same time, the oscillator, in that given period, starts growing.

This is a bullish divergence. Similarly, one can find examples of bearish divergences instead, such as in this case: we see that prices mark rising highs, while instead the oscillator, the MACD in this case, is falling.

In this case, I used the MACD histogram, which is nothing but the difference between the MACD line, which is one of the most popular oscillators, and the signal line.

Let's go and see how a function that allows for divergence detection is constructed. So, first of all, here we can see that if I activate the divergence search from this indicator, I'll be asked within how many bars to go and look for them.

Because if we see them at a glance, we can decide which ones to accept and which ones to not. If, on the other hand, we are looking for them with a function, we'll need to set specific parameters. For example, we can look for divergences in a range going from a minimum of 5 to a maximum of 50 bars.

In this case, we can see that the indicator will report to us all those situations in which the price trend diverges from the oscillator trend within the number of bars that we've established in the parameters.

Powerlanguage: The scripts of the function, indicator and strategy

So, let's go and see how the function and oscillator are constructed, in other words, let's go and take a look at the code.

So, first of all, as I’ve already said, the function will ask us to specify a minimum and a maximum number of bars where to look for the divergencies.

Then it will ask us for the main parameters of the MACD, in this case, so the period of the fast average, the period of the slow average, and the period of the "smoothing," namely, how much rounding, let's say, of the signal line is done.

And the last parameter called the "k-value" parameter that simply tells us within how many bars this divergence was found. At this point here I'll simply go to calculate the MACD and then the histogram.

As you can see, the histogram is simply calculated as the MACD line minus the signal line, which, in turn, is an exponential average in this case at 9 periods of the first line, so it's a difference that creates this histogram.

And at this point, we're going to look, in our range of bars and through a "for" cycle, for a situation where for the bullish divergence we expect: a negative MACD histogram, a Low that is lower than one of the bars included in the search range, and an increasing MACD from the point where the divergence would be found.

Then we'll also expect a MACD that was also negative at the time the divergence was found, and finally a reinforcing condition that tells us that the MACD histogram at the point at which the divergence was found must be less than or equal to the MACD in the number of bars over which the divergence would be found. So, it must be the lowest peak of the local MACD.

At this point, in this case, a bullish divergence has been found. The opposite conditions, of course, apply for the opposite case, so for bearish divergence, which involves the highs instead of the lows and expects a decreasing MACD that must have peaked at bar "K," so let's say at the past bar, the hypothetical one in which the divergence is found.

At this point, the moment a divergence is found, from the most recent bar to the oldest bar, we'll stop this loop, which was a "for" loop, to go and tell either the system or the indicator from which this function is called that the divergence has been found.

This, on the other hand, is the indicator code. It looks particularly complicated but once divergences are found, all it does is plot them then on the chart, both on the indicator chart and on the price chart, just to highlight with different colors the fact that a divergence has been found and then indicate to us which one has been found.

Lastly, let's go back to our chart for a moment and see. We can see that the divergences here are indeed many. It almost looks as if for a good portion of the market history there was a divergence situation.

So, at this point, I've coded a very simple strategy that allows you to test whether the divergences work or not.

So, again from the indicator, we recall our divergence function with the usual parameters we've already discussed.

At this point I'll tell it that if there's a bullish divergence, the system will buy at market, that is to say at the price available at that time.

Conversely, if there's a bearish divergence, we'll sell at market, so, at the first available price.

To avoid major backtest errors, I simply told the system to avoid buying on the first and last bars of each session.

Tests with Portfolio Trader

At this point, I loaded the strategy into the MultiCharts Portfolio Trader and ran the test on a portfolio of the main futures.

Since I did the test on a portfolio, I left the standard settings that I used, which are from a minimum of 5 bars up to a maximum of 50.

Look, it's up to you to modify these parameters to study each market in more detail.

In this case, since this is just an initial test, I didn't even include commissions and slippage, and in each of these markets, I used a 60-minute chart, which I think is pretty good for this kind of approach.

Let's say I wouldn't use a chart below 30 minutes, as for any indicator in general, and at the same time, 60 minutes in my opinion in this case is good enough.

So, let's look at the results of this basic test. What immediately caught my eye was that most of the products are negative, so they would give a negative result.

Here of course we're talking about tests done with standard parameters that then should perhaps be developed a little bit more on each market.

It must also be said that many of these markets are commodities that have a pronounced trend-following behavior that isn't particularly well suited to approaches such as divergences, which instead seek to exploit counter-trend situations.

This is particularly visible, for example, on Heating Oil, which is an energy future listed on the Nymex in New York, and on Coffee, which is listed on ICE and is a very trend-following market.

On Platinum, which is also a trend-following market and is listed in New York. On Gasoline, which is also a very volatile underlying asset and is very trend following.

And on Silver, which is a metal, and is also more trend following than mean reverting in general. It's also a little bit particular because over the years it's had some particular years like 2011.

So, let's say a good part of this portfolio is commodities that are usually a little bit more trend following than mean reverting.

On the other hand, some products seem to show encouraging results, at least as a starting point.

For example, this is the Eurostoxx, which in recent years hasn't done very well but still, for being a simple logic that does a lot of trades, it could be a starting point.

Or for example, the Nasdaq, with the ticker NQ, which is showing a very good trend but only in the past few years. In any case, this could still be a good starting point for creating new strategies.

The way I see it, also seeing the number of initial trends, I'd say that divergences don't look very promising at least with the MACD, however, they could be used, let's say, more as a filter, to perhaps improve some strategy.

Let's say that as a simple logic, I wouldn't use it as the main rationale for entering the market.

So today we've seen a possible approach based on divergences. 

And of course, this approach can also be used with other oscillators in the same way.

If you are interested in learning more about how systematic trading works, go and check out the link you find in the description of this video. It will take you to a page from where you can access a free presentation by Andrea Unger, our founder and the only 4-time world trading champion. You'll also be able to order your free copy of our best-selling book, "The Unger Method", or even book a free call with a member of our team.

And please, if you liked this video please don't forget to leave us a like, subscribe to our channel and go and click on the bell to stay updated on the release of all our new videos. We really do appreciate that!

Thanks so much again for watching and I will see you again in our next video, bye-bye!

Need More Help? Book Your FREE Strategy Session With Our Team Today!

We'll help you map out a plan to fix the problems in your trading and get you to the next level. Answer a few questions on our application and then choose a time that works for you.

Andrea Unger

Andrea Unger

Andrea Unger here and I help retail traders to improve their trading, scientifically. I went from being a cog in the machine in a multinational company to the only 4-Time World Trading Champion in a little more than 10 years.

I've been a professional trader since 2001 and in 2008 I became World Champion using just 4 automated trading systems. 

In 2015 I founded Unger Academy, where I teach my method of developing effecting trading strategies: a scientific, replicable and universal method, based on numbers and statistics, not hunches, which led me and my students to become Champions again and again.

Now I'm here to help you learn how to develop your own strategies, autonomously. This channel will help you improve your trading, know the markets better, and apply the scientific method to financial markets.

Becoming a trader is harder than you think, but if you have passion, will, and sufficient capital, you'll learn how to code and develop effective strategies, manage risk, and diversify a portfolio of trading systems to greatly improve your chances of becoming successful.