Identifying Divergences

by | Jul 11, 2019 | All Articles, Technical Analysis, Education

Optuma’s scripting language allows you to create any number of scans and tests on any number of tools and values. But when we were asked if we could create a scan to identify divergences between price and a momentum indicator, such as the RSI, I wasn’t sure how this could be done. After a bit of noodling with charts and working out the logic (ok, and a bit of help from my smart colleagues in Australian HQ) we came up with a way to mathematically determine when a divergence occurs.

What’s the deal with divergences?

Let’s back up a bit: why are we interested in divergences? We’ve looked at them before in terms of an index and market breadth (see Mathew’s article here) and it’s the same situation we are looking for: when the price movement is not confirmed by a momentum indicator – such as the RSI. When this happens could be an indication that a change in trend is imminent. A bullish (or positive) divergence occurs when prices are in a downtrend and making lower lows, but the RSI indicator is making higher lows. Conversely, a bearish/negative divergence is when higher price highs occur with lower RSI highs, indicating momentum may be fading and prices may fall.

We could physically scroll through hundreds of chart and try to identify when this happens, but we thought it would be easier to see if we could create a scripting formula to tell us when these events occur.

So how does it work?

By using Pivot Lables and the Pivot() function we can highlight any high or low of a price chart or indicator, with the higher the pivot value the more significant the pivot is. So in the example of a negative divergence we identify the last two highs of the RSI and take the corresponding price value when they occurred. If prices are making new highs but the RSI is making lower highs then negative divergence has been identified.

In this example, the Show Bar lines identify RSI pivots set to 15 bars, ie the high/low must be the most extreme value for 15 bars before and after:

Once we’ve identified when the RSI pivots, we take the corresponding price value to work out if the divergence has occurred. Here’s the complete script for a negative divergence, with a condition that the RSI peak must 4% lower than the previous peak, and the price must be at least 4% higher (this is an arbitrary value – you can adjust this tolerance and the pivot values as required):

Let me explain what each of these lines are doing (remember lines beginning // are comments and are ignored by the formula):

Line 4: variable RSI1 calculates the 14-period RSI for each day
Line 6: variable P1 finds all the 15 day pivot highs for the RSI
Line 8: variable V1 gets the RSI value when each pivot occurs (ie when the pivot value is not equal to 0)
Line 10: compare the RSI peak value with the previous peak, and if it’s more than 4% lower (V1[1]*0.96) then variable Sig1 is true. (To change the tolerance to eg 5% it would be 0.95.)
Line 12: variable V2 gets the high price of the stock on each day of the RSI peak
Line 14: compare the price high with the high at the previous RSI peak, and if it’s more than 4% (V2[1]*1.04) higher then variable Sig2 is true. (To change the tolerance to eg 5% it would be 1.05.)
Line 16: a divergence signal is triggered when both the RSI (Sig1) and price (Sig2) conditions are true on the same day

The concept is identical for positive divergences, but with pivot and price lows:

Of course, divergences don’t just work with RSI. You could try it with other momentum indicators, such as On Balance Volume, or Money Flow Index : simply replace the RSI function in Line 4 with OBV(), for example.

Once applied to a Show Bar or scan then the divergences can be identified. Click the buttons below to save a workbook for ASX and US data showing the divergences on the charts:

Next week we’ll look at how we can use these in quantitative tests to see if divergence really is a valuable Technical Analysis concept.

Share Link
Darren Hawkins, MSTA

Darren Hawkins, MSTA

Senior Software Specialist at Optuma

Darren is the senior Software Specialist at Optuma. He joined the company in 2009 after attending an introductory technical analysis course. Darren now instructs users all over the world, from experienced Wall Street traders and professional money managers to individual traders drawing their first trendlines.

Darren grew up in the UK and attended college in the USA where he earned a BA in Economics from St Mary's College of Maryland. He went on to spend a few years working at the Nasdaq Stock Market in Washington DC. Going on to live and work in Australia, the US and currently the UK, Darren has a broad understanding of the individual needs of traders, portfolio managers and investors utilising a wide range of methodologies.

In 2014 Darren passed the UK-based Society of Technical Analysts diploma course, and is in the process of studying for Level 3 of the CMT Program.

When not looking at charts, Darren keeps a keen eye on England's cricket team - especially if they are playing against Australia. He lives in the Essex countryside in England, with wife Wendy and their labrador, Gabba.

7 Comments

  1. Avatar

    Thanks Darren really appreciate this. To do this with MACD instead of RSI or both, would it be similar to changing line 4 OBV() etc?

    Thank you
    Steve

    Reply
    • Darren Hawkins, MSTA

      Yes – you would need to select the relevant MACD output, so for the 12/26/9 oscillator Line 4 could be OSC1=MACD(BAR1=12, BAR2=26, OSC=9, DEFAULT=Oscillator); and replace RSI1 in lines 6 and 8 to OSC1 for the new variable.

      Reply
      • Avatar

        Great thank you Darren
        Steve

  2. Avatar

    Hello Darren,

    These colored arrows (red & green) are the divergences by itself? Can be considered like a signal?

    Thank you!

    Reply
    • Darren Hawkins, MSTA

      The arrows are the RSI pivots. The red/green vertical bars below the RSI show the divergences.

      Reply
  3. Avatar

    Darren these articles great is it possible to have a wok book for last weeks gann works fabulous stuff very hard for non computer folks any help appreciated

    Reply
    • Darren Hawkins, MSTA

      Hi Andrew, what Gann examples are you referring to? Please contact us at support with a screenshot.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Blog Signup

Pin It on Pinterest

Share This

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close