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.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.
Скрипт с открытым кодом
В истинном духе TradingView, создатель этого скрипта сделал его открытым исходным кодом, чтобы трейдеры могли проверить и убедиться в его функциональности. Браво автору! Вы можете использовать его бесплатно, но помните, что перепубликация кода подчиняется нашим Правилам поведения.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.