OPEN-SOURCE SCRIPT

Better Divergence On Any Indicator [DoctaBot]

Updated
This is an expansion of the Tradingview built in Divergences indicator (bottom) with 2 MAJOR differences.

First, and most importantly, the built in indicator identifies pivots in your chosen oscillator, but then utilizes the corresponding candle's HIGH or LOW to identify potential divergences. I'm not a fan of this method because oscillator values are typically calculated using the candle CLOSE values, so, in my opinion, divergences should be identified using the candle CLOSE value as well, as they are in this script.

Second, the built in divergence indicator only looks back one oscillator pivot for potential divergences. I coded this to look back one additional pivot as well to identify more valid potential divergences. The script will only identify these types of "multiple pivot divergences" if the oscillator pivot in between the two diverging pivots DOES NOT intersect the line being drawn them.

Notes for chart:

#1: This built in Divergence indicator misses this hidden bearish divergence because of the pivot in between (marked with red vertical line). No divergence exists between the most recent pivots, but it does if we compare it to the next one back.

#2: The RSI14 is making a lower high here, the first criteria for a bearish divergence. The built in Divergence indicator then references the candles' HIGHS. Because the most recent HIGH exceeds the previous one, it is considered a higher high and incorrectly identified as a bearish divergence. If we use the candle CLOSE price to identify divergences, this does not qualify.

#3: Here, we see both of the updates in action. Neither of these bearish divergences are identified with the built in Divergence Indicator. The first divergence s missed due to the use of candle HIGHS rather than closes; the original HIGH is greater than the next HIGH, however, comparison of closes shows that it is, in fact, a higher CLOSE. The second divergence is missed because original indicator can only look back one pivot and, consequently, misses the divergence between the next one back.

Please note, you may notice while using this script that some of the older divergences do not show any lines between the oscillator pivots. THIS IS NOT A BUG! In order to draw divergence lines properly for multiple pivots back, I had to use the line.new functions rather than plot functions. These line functions will delete old lines when a certain number have been drawn on the chart so these old ones are automatically erased as time passes.

Release Notes
Added alerts for all divergences once per bar close or options to set each type of divergence alert individually.
Release Notes
Added additional filter to only identify divergences when your oscillator is in overbought or oversold territory. You can also change this level to better suit your trading strategy or the particular oscillator you are using.
Release Notes
Added option to print divergences on price action instead of on the oscillator.
Release Notes
'Better Divs' just got even better. Never miss a divergence again.

- My original revision (seen second from the bottom) was limited in the number of oscillator pivots it was able to scan for divergences. Here, I used arrays to store all previous pivot levels to allow the code to scan every pivot in within your chosen lookback range for divergence. (The Tradingview built in Divergence Indicator can be seen at the bottom for reference of differences)

- Plotting all of the divergence lines and labels can make the chart a little busy, so I've also added an option to either plot or hide the lines and labels.

- Option to scan for potential divergences so you don't miss your move. If you choose to plot these, they will print dashed divergence lines and transparent labels until they are either confirmed or invalidated. If confirmed, they will change to solid lines and opaque labels. If invalidated, they will simply be deleted from the chart.

- Plotting every single divergence line and label can make the chart very busy, so I added the option to either plot or hide them while providing alerts.

- Alert messages will include the type of divergence identified (potential or confirmed, bullish or bearish, regular or hidden) as well as the number of points of divergence identified on that bar.

- Finally, you can choose the maximum number of divergences to keep displayed on the chart. This is mainly to limit the number of items stored in the arrays to reduce memory usage and improve script efficiency.

As always, let me know if you have any questions or recommendations for improvements and happy trading!
Release Notes
Fixed typo in 'Max Divs to Show' setting text
Release Notes
Overbought/Oversold filter back by popular demand:
  • 'Off' will identify any and every divergence.
  • 'On' will require that the current oscillator value be in the overbought or oversold area, but have no restriction on where the divergence originates from.
  • 'Strict' will limit divergences to only those where the oscillator value both originate from and finishes at an overbought or oversold value.
Centered OscillatorsDivergencedivergencedetectiondivergencesdivergencetradingOscillatorsTrend Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


Reach me on Twitter: @DoctaBot
Sign up for TradingView: tradingview.com/gopro/?share_your_love=federalPlayer66324
USDT (TRC20): TBELyC4PwYpKkdvNdr1iqE1j8abBijibqt
Also on:

Disclaimer