OPEN-SOURCE SCRIPT
Stochastic Divergence (live) AA

What it does:
This script detects live regular bullish and bearish divergence between price and a smoothed Stochastic D line. It uses confirmed fractal swing highs and lows on price to define the first anchor point of the divergence, and those pivot settings can be adjusted in the UI. The first pivot is confirmed, but the second pivot is live, so the script can identify divergence as it is forming rather than waiting for a second confirmed swing.
A bullish divergence is found when price makes a lower low while the Stochastic D makes a higher low. A bearish divergence is found when price makes a higher high while the Stochastic D makes a lower high.
The oscillator used here is a double-smoothed Stochastic D built from TradingView’s built-in stochastic calculation. Instead of using the close as the source, it uses ohlc4, which helps reduce some of the wick-driven noise that can make divergence readings less consistent. If you want to compare or debug the signals against TradingView’s built-in Stochastic, make sure to change its source from close to ohlc4 so the readings line up properly.
This script uses a double invalidation model. A divergence setup is cleared not only when the Stochastic D loses its side of the 50 line, but also when price breaks the structure of the latest anchor pivot bar in the opposite direction. That combination helps reset the setup faster, keeps stale divergence from lingering, and makes the active signals more structurally relevant.
How to use:
Use this script as a price-action context tool, not as a blind entry or exit trigger. Bullish divergences tend to matter most when they appear after an extended move down and line up with support, liquidity sweeps, equal lows, demand zones, or other signs of downside exhaustion. Bearish divergences tend to matter most when they appear after an extended move up and line up with resistance, equal highs, trapped breakouts, supply zones, or other signs of upside exhaustion. The Pivot Left and Pivot Right settings control how strict the first anchor pivot is, with higher values giving fewer but more confirmed structures and lower values making the script more reactive. The Min and Max Bar Distance settings control how far apart the anchor point and the live opposing price extreme can be, which helps filter out weak or overly stretched divergence structures. It works best when combined with structure, location, and real price behavior around an important level, rather than used in isolation.
These are my own takes on widely used market calculations and are not meant to be a standalone trading system. If you found this useful, leave a like 🚀 and follow for more.
Thanks, and good luck on your trading journey.
This script detects live regular bullish and bearish divergence between price and a smoothed Stochastic D line. It uses confirmed fractal swing highs and lows on price to define the first anchor point of the divergence, and those pivot settings can be adjusted in the UI. The first pivot is confirmed, but the second pivot is live, so the script can identify divergence as it is forming rather than waiting for a second confirmed swing.
A bullish divergence is found when price makes a lower low while the Stochastic D makes a higher low. A bearish divergence is found when price makes a higher high while the Stochastic D makes a lower high.
The oscillator used here is a double-smoothed Stochastic D built from TradingView’s built-in stochastic calculation. Instead of using the close as the source, it uses ohlc4, which helps reduce some of the wick-driven noise that can make divergence readings less consistent. If you want to compare or debug the signals against TradingView’s built-in Stochastic, make sure to change its source from close to ohlc4 so the readings line up properly.
This script uses a double invalidation model. A divergence setup is cleared not only when the Stochastic D loses its side of the 50 line, but also when price breaks the structure of the latest anchor pivot bar in the opposite direction. That combination helps reset the setup faster, keeps stale divergence from lingering, and makes the active signals more structurally relevant.
How to use:
Use this script as a price-action context tool, not as a blind entry or exit trigger. Bullish divergences tend to matter most when they appear after an extended move down and line up with support, liquidity sweeps, equal lows, demand zones, or other signs of downside exhaustion. Bearish divergences tend to matter most when they appear after an extended move up and line up with resistance, equal highs, trapped breakouts, supply zones, or other signs of upside exhaustion. The Pivot Left and Pivot Right settings control how strict the first anchor pivot is, with higher values giving fewer but more confirmed structures and lower values making the script more reactive. The Min and Max Bar Distance settings control how far apart the anchor point and the live opposing price extreme can be, which helps filter out weak or overly stretched divergence structures. It works best when combined with structure, location, and real price behavior around an important level, rather than used in isolation.
These are my own takes on widely used market calculations and are not meant to be a standalone trading system. If you found this useful, leave a like 🚀 and follow for more.
Thanks, and good luck on your trading journey.
Script de código abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que cualquier republicación del código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.
Script de código abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que cualquier republicación del código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.