Realtime Divergence for Any Indicator
Aug 26, 2023

The Realtime Divergence for Any Indicator tool identifies and displays both historical and real-time bullish and bearish divergences between price action and any oscillating indicator. It provides traders with precise control over divergence identification, using solid lines for confirmed signals and dashed lines for potential, real-time setups.
Usage
The indicator can be used to spot four primary types of divergences: Regular Bullish, Hidden Bullish, Regular Bearish, and Hidden Bearish. By default, it plots confirmed historical divergences once all pivot bars have closed.
To use the tool effectively:
- Potential Divergences: Enable "Realtime Potential Divergences" to see dashed lines for emerging setups. This includes setups where the right-side pivot bar is still forming or lacks sufficient right-side confirmation bars.
- Price vs. Oscillator: Use the "Apply To Price Chart" toggle to switch the visual lines between the main price chart and the indicator pane.
- Filtering: Adjust the "Min/Max Length" to filter out divergences that are too short or too wide to be considered significant.
Details
The script utilizes a sophisticated pivot detection algorithm with two distinct patterns:
- Type 1: A strict pyramid pattern where bars must strictly ascend/descend.
- Type 2: A more flexible pattern that permits side bars to be equal in value to the pivot bar.
The "Real-time" aspect is calculated in three stages:
- No right-side bars present for the pivot.
- No right-side bars present, but the pivot bar itself has closed.
- Right-side bars are present, but the right-most bar of the confirmation period has not yet closed.
Settings
Sources
- Price Chart Bulls/Bears Source: The data source (e.g., Low, High) used for bullish and bearish price calculations.
- Oscillator Chart Bulls/Bears Source: The data source used for the corresponding oscillating indicator.
General Display Settings
- Apply To Price Chart: Toggles whether lines appear on the price chart or the indicator.
- Bull/Bear/Hidden Lines/Labels: Toggles for various types of divergence visuals.
Pivots
- Pivot Pattern: Selection between "Type 1" (Pyramid) and "Type 2" (Standard).
- Right/Left Side Bars: Defines the number of bars required on either side of a pivot to confirm its validity.
Divergences
- Min/Max Length: The minimum and maximum distance between the two pivots of a divergence.
- Intersection Allowance: Determines how much "price/oscillator penetration" is allowed between the two pivots before a divergence is invalidated.
- Normalization Factor: A setting used to define the "angle" of slopes for exclusion purposes.
FAQ
How do I access Realtime Divergence for Any Indicator?
You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Why do some divergence lines disappear from the left side of the chart?
TradingView has a technical limit on the number of lines a single script can draw (currently 500). If your settings generate many signals, older lines are automatically removed to display newer ones.
What should I do if I get a "max_bars_back" error?
This typically occurs when no divergence is found within the first 244 bars of data. You can try loosening your pivot or length settings to find an earlier signal, which allows Pine Script to properly calculate the required buffer.
Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.
Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.
Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.
As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.
Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.
Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.
This does not represent our full Disclaimer. Please read our full disclaimer.
© 2026 LuxAlgo Global, LLC.

