OPEN-SOURCE SCRIPT
Divergence Toolkit (Real-Time)

The Divergence Toolkit is designed to automatically detect divergences between the price of an underlying asset and any other TradingView built-in or community-built indicator or script. This algorithm provides a comprehensive solution for identifying both regular and hidden divergences, empowering traders with valuable insights into potential trend reversals.
🔲 Methodology
Divergences occur when there is a disagreement between the price action of an asset and the corresponding indicator. Let's review the conditions for regular and hidden divergences.
Regular divergences indicate a potential reversal in the current trend.
Regular Bullish Divergence
Regular Bearish Divergence
Hidden divergences indicate a potential continuation of the existing trend.
Hidden Bullish Divergence
Hidden Bearish Divergence
In both regular and hidden divergences, the key is to observe the relationship between the price action and the indicator. Divergences can provide valuable insights into potential trend reversals or continuations.
The methodology employed in this script involves the detection of divergences through conditional price levels rather than relying on detected pivots. Traditionally, divergences are created by identifying pivots in both the underlying asset and the oscillator. However, this script employs a trailing stop on the oscillator to detect potential swings, providing a real-time approach to identifying divergences, you may find more info about it here (SuperTrend Toolkit). We detect swings or pivots simply by testing for crosses between the indicator and its trailing stop.
By testing for conditional price levels, the script achieves similar outcomes without the delays associated with pivot-based methods.
🔲 Setup Guide
A simple example on one of my public scripts, Standardized MACD



🔲 Utility
We may auto-detect divergences to spot trend reversals & continuations.

🔲 Settings
🔲 Alerts
The Divergence Toolkit provides traders with a dynamic tool for spotting potential trend reversals and continuations. Its innovative approach to real-time divergence detection enhances the timeliness of identifying market opportunities.
🔲 Methodology
Divergences occur when there is a disagreement between the price action of an asset and the corresponding indicator. Let's review the conditions for regular and hidden divergences.
Regular divergences indicate a potential reversal in the current trend.
Regular Bullish Divergence
- Price Action - Forms a lower low.
- Indicator - Forms a higher low.
- Interpretation - Suggests that while the price is making new lows, the indicator is showing increasing strength, signaling a potential upward reversal.
Regular Bearish Divergence
- Price Action - Forms a higher high.
- Indicator - Forms a lower high.
- Interpretation - Indicates that despite the price making new highs, the indicator is weakening, hinting at a potential downward reversal.
Hidden divergences indicate a potential continuation of the existing trend.
Hidden Bullish Divergence
- Price Action - Forms a higher low.
- Indicator - Forms a lower low.
- Interpretation - Suggests that even though the price is retracing, the indicator shows increasing strength, indicating a potential continuation of the upward trend.
Hidden Bearish Divergence
- Price Action - Forms a lower high.
- Indicator - Forms a higher high.
- Interpretation - Indicates that despite a retracement in price, the indicator is still strong, signaling a potential continuation of the downward trend.
In both regular and hidden divergences, the key is to observe the relationship between the price action and the indicator. Divergences can provide valuable insights into potential trend reversals or continuations.
The methodology employed in this script involves the detection of divergences through conditional price levels rather than relying on detected pivots. Traditionally, divergences are created by identifying pivots in both the underlying asset and the oscillator. However, this script employs a trailing stop on the oscillator to detect potential swings, providing a real-time approach to identifying divergences, you may find more info about it here (SuperTrend Toolkit). We detect swings or pivots simply by testing for crosses between the indicator and its trailing stop.
Pine Script®
type oscillator
float o = Oscillator Value
float s = Trailing Stop Value
oscillator osc = oscillator.new()
bool l = ta.crossunder(osc.o, osc.s) => Utilized as a formed high
bool h = ta.crossover (osc.o, osc.s) => Utilized as a formed low
// Note: these conditions alone could cause repainting when they are met but canceled at a later time before the bar closes. Hence, we wait for a confirmed bar.
// The script also includes the option to immediately alert when the conditions are met, if you choose so.
float o = Oscillator Value
float s = Trailing Stop Value
oscillator osc = oscillator.new()
bool l = ta.crossunder(osc.o, osc.s) => Utilized as a formed high
bool h = ta.crossover (osc.o, osc.s) => Utilized as a formed low
// Note: these conditions alone could cause repainting when they are met but canceled at a later time before the bar closes. Hence, we wait for a confirmed bar.
// The script also includes the option to immediately alert when the conditions are met, if you choose so.
By testing for conditional price levels, the script achieves similar outcomes without the delays associated with pivot-based methods.
Pine Script®
type bar
float o = open
float h = high
float l = low
float c = close
bar b = bar.new()
bool hi = b.h[of the last formed high] < b.h => A higher price level has been created
bool lo = b.l[of the last formed low ] > b.l => A lower price level has been created
// Note: These conditions do not check for certain price swings hence they may seldom result in inaccurate detection.
float o = open
float h = high
float l = low
float c = close
bar b = bar.new()
bool hi = b.h[of the last formed high] < b.h => A higher price level has been created
bool lo = b.l[of the last formed low ] > b.l => A lower price level has been created
// Note: These conditions do not check for certain price swings hence they may seldom result in inaccurate detection.
🔲 Setup Guide
A simple example on one of my public scripts, Standardized MACD
🔲 Utility
We may auto-detect divergences to spot trend reversals & continuations.
🔲 Settings
- Source - Choose an oscillator source of which to base the Toolkit on.
- Zeroing - The Mid-Line value of the oscillator, for example RSI & MFI use 50.
- Sensitivity - Calibrates the sensitivity of which Divergencies are detected, higher values result in more detections but less accuracy.
- Lifetime - Maximum timespan to detect a Divergence.
- Repaint - Switched on, the script will trigger Divergencies as they happen in Real-Time, could cause repainting when the conditions are met but canceled at a later time before bar closes.
🔲 Alerts
- Bearish Divergence
- Bullish Divergence
- Bearish Hidden Divergence
- Bullish Hidden Divergence
As well as the option to trigger 'any alert' call.
The Divergence Toolkit provides traders with a dynamic tool for spotting potential trend reversals and continuations. Its innovative approach to real-time divergence detection enhances the timeliness of identifying market opportunities.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
For quick access on a chart, add this script to your favorites — learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.