OPEN-SOURCE SCRIPT

SuperTrend Weighted by Divergence

291
█ OVERVIEW
SuperTrend Weighted by Divergence is a trend-following indicator based on the classic SuperTrend, enhanced with dynamic ATR weighting driven by divergences. Its key feature is adaptive behavior: when a divergence appears, the indicator temporarily reduces the ATR multiplier, allowing the trend line to react faster to potential market reversals.
The indicator remains clean, visually clear, and well suited for traders who want to combine trend-following with early detection of weakening momentum.

█ CONCEPT
One of the biggest drawbacks of trend indicators is their lagging nature, caused by the characteristics of source data. Classic SuperTrends react only after the trend has already developed, which often leads to late entries or exits.

The idea behind SuperTrend Weighted by Divergence is to introduce dynamic adjustment of the trend line in response to the first signs of trend weakening.
Instead of treating ATR as a constant volatility buffer, the indicator temporarily modifies its impact when the market sends warning signals in the form of price–oscillator divergences.

For divergence detection, a hidden auxiliary oscillator called “MPO4 Lines – Modal Engine” (default settings) is used. This oscillator is not displayed on the chart – only the points where divergences are detected are shown as markers on price bars.
Divergences do not generate direct entry signals; they are used solely to temporarily adjust the behavior of the SuperTrend.

If, after detecting a divergence against the current trend, a divergence in line with the trend appears, the previous divergence is invalidated and the SuperTrend returns to its standard behavior (base ATR multiplier).

█ FEATURES
Data sources:
- ATR (Average True Range)
- Reference point: HL2 (high/low average)
- MPO4 Lines – Modal Engine oscillator (hidden, used only for divergence detection)

Divergence logic:
- Bullish divergence: lower low in price + higher low in the oscillator
- Bearish divergence: higher high in price + lower high in the oscillator
- Divergences are detected using pivots (left/right)
- Divergence detection is delayed by the pivot length, as confirmation requires a fixed number of bars on the right side

Divergence impact:
- After a divergence is detected, the ATR multiplier is reduced
- The reduction strength is controlled by Divergence Sensitivity
- The effect is active only for a limited number of bars – 200 bars by default (divBars)
- The effect is canceled on trend change or when a trend-aligned divergence appears

Trend change logic:
- Trend changes only after a confirmed close beyond the trailing line
- No repainting
- Trend lines break at reversal points

Visual signals:
- “Buy” and “Sell” labels only on confirmed trend changes
- Optional bar coloring based on current trend (Color bars by trend)
- Soft fill between price and the trend line
- Divergence markers (dots above/below bars) shown at the point of divergence detection, not across the entire divergence structure

Alerts:
- Buy Signal – trend change to bullish
- Sell Signal – trend change to bearish
- Bullish Divergence
- Bearish Divergence

█ HOW TO USE
Adding the indicator:
Paste the code into Pine Editor or search for “SuperTrend Weighted by Divergence” on TradingView

Main settings:
- ATR Length – ATR period
- Base ATR Multiplier – base SuperTrend width
- Pivot Length – divergence sensitivity and detection delay
- Divergence Sensitivity – strength of divergence impact (0.0–1.0)
- Color bars by trend – enable / disable bar coloring
- Line and fill colors – fully customizable

Interpretation:
- Green line and bars = uptrend
- Red line and bars = downtrend
- Divergence against the trend = possible weakening and faster SuperTrend reaction
- Trend-aligned divergence = return to standard SuperTrend behavior
- No divergence = classic, stable SuperTrend behavior

█ APPLICATIONS
Ideal for:
- Trend-following
Entering positions only in the direction of the current trend, using the SuperTrend as a directional filter.
- Early detection of trend weakness
Repeated divergences against the trend may indicate decreasing momentum and a potential upcoming reversal.
- Markets with variable dynamics (crypto, indices, forex)
Entries based on trend changes, preferably confirmed by other tools such as Fibonacci levels, RSI, support/resistance, or market structure.
- Scalping, day trading, and swing trading (with parameter adjustments)
Increasing Divergence Sensitivity to around 0.4–0.5 produces many more signals on small, often short-lived moves.
These settings work well for scalping and day trading, but are not ideal for swing trading, as they tend to generate more false signals and frequent trend changes.

█ NOTES
- Works on all markets and timeframes
- Divergences are used to adapt SuperTrend behavior, not as standalone entry signals
- Higher Divergence Sensitivity = faster reaction and more signals
- Lower Divergence Sensitivity = smoother trend and fewer changes
- Best results are achieved by tuning parameters to the instrument and trading style

Declinazione di responsabilità

Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.