OPEN-SOURCE SCRIPT
Divergence Hunter [Proozac]

# Divergence Hunter — RSI · MACD · OBV
**Pine Script v5 — Overlay Indicator**
---
## What It Does
Scans three oscillators simultaneously — RSI, MACD, and OBV — and draws labelled lines directly on the price chart whenever a divergence is detected between price action and oscillator momentum.
Two divergence types are covered per oscillator:
- **Regular divergence** — signals a potential trend reversal
- **Hidden divergence** — signals a potential trend continuation
---
## Divergence Types Explained
### Regular Bullish Divergence
```
Price: Lower Low (LL)
Oscillator: Higher Low (HL)
Signal: Potential reversal UP
```
Price is making lower lows but the oscillator is making higher lows — sellers are losing momentum. The downtrend is weakening.
### Regular Bearish Divergence
```
Price: Higher High (HH)
Oscillator: Lower High (LH)
Signal: Potential reversal DOWN
```
Price is making higher highs but the oscillator is making lower highs — buyers are losing momentum. The uptrend is weakening.
### Hidden Bullish Divergence
```
Price: Higher Low (HL) ← trend is up
Oscillator: Lower Low (LL)
Signal: Continuation UP
```
Price holds higher lows (uptrend structure intact) while the oscillator dips lower — a temporary oscillator pullback during a healthy uptrend. A continuation signal.
### Hidden Bearish Divergence
```
Price: Lower High (LH) ← trend is down
Oscillator: Higher High (HH)
Signal: Continuation DOWN
```
Price is making lower highs (downtrend structure intact) while the oscillator makes higher highs — a temporary oscillator bounce during a healthy downtrend.
---
## The Three Oscillators
| Oscillator | What it measures | Why it diverges |
|-----------|-----------------|-----------------|
| **RSI** | Relative momentum of closes | Classic divergence tool — very reliable at extremes |
| **MACD** | Trend + momentum combined | Useful in trending markets, less noise than RSI |
| **OBV** | Cumulative volume flow | Measures whether volume confirms the price move |
Each oscillator runs through the same `detect_divs()` function. You can enable or disable each one independently. When multiple oscillators show the same divergence simultaneously, that is a high-conviction signal.
---
## Detection Logic
The indicator uses `ta.pivothigh` and `ta.pivotlow` on the **price** series to find swing points, then reads the **oscillator value at that same bar** to compare:
```
For each new pivot high in price:
Compare price level vs previous pivot high price
Compare oscillator vs previous pivot high oscillator
→ Regular Bear: price HH + osc LH
→ Hidden Bear: price LH + osc HH
For each new pivot low in price:
Compare price level vs previous pivot low price
Compare oscillator vs previous pivot low oscillator
→ Regular Bull: price LL + osc HL
→ Hidden Bull: price HL + osc LL
```
When detected, a **line** is drawn connecting the two price pivot points and a **label** marks the divergence type and oscillator name.
---
## Inputs
| Input | Default | Description |
|-------|---------|-------------|
| **RSI Divergence** | true | Enable RSI divergence scanning |
| **RSI Length** | 14 | RSI calculation period |
| **MACD Divergence** | true | Enable MACD line divergence scanning |
| **MACD Fast** | 12 | MACD fast EMA length |
| **MACD Slow** | 26 | MACD slow EMA length |
| **MACD Signal** | 9 | MACD signal line length |
| **OBV Divergence** | true | Enable OBV divergence scanning |
| **Pivot Lookback (left bars)** | 5 | Bars to the left required to confirm a swing point |
| **Pivot Lookback (right bars)** | 5 | Bars to the right required to confirm a swing point |
| **Show Hidden Divergences** | true | Toggle hidden divergence lines on/off |
| **Bullish Regular** | Bright green | Line/label color for regular bullish divergences |
| **Bearish Regular** | Bright red | Line/label color for regular bearish divergences |
| **Bullish Hidden** | Faded green | Line/label color for hidden bullish divergences |
| **Bearish Hidden** | Faded red | Line/label color for hidden bearish divergences |
---
## Visual Output
| Element | Meaning |
|---------|---------|
| Solid bright green line + label `"RSI Bull Div"` | Regular bullish divergence on RSI |
| Solid bright red line + label `"RSI Bear Div"` | Regular bearish divergence on RSI |
| Dashed faded green line + label `"RSI Hid Bull"` | Hidden bullish divergence on RSI |
| Dashed faded red line + label `"RSI Hid Bear"` | Hidden bearish divergence on RSI |
| Same patterns with `"MACD"` or `"OBV"` prefix | Divergence from that oscillator |
| Green background flash | Any bullish regular divergence firing |
| Red background flash | Any bearish regular divergence firing |
Lines connect the **two price swing points** that form the divergence, making it immediately clear which highs or lows are being compared.
---
## How to Use
**Entry logic:**
- Regular divergence → look for a reversal entry after the divergence confirms (pivot right bars have closed).
- Hidden divergence → look for a continuation entry in the direction of the main trend.
- Always wait for the pivot to fully confirm — the signal appears `pivot_rb` bars after the actual swing point.
**Confluence filter:**
A divergence is most reliable when:
1. Two or more oscillators show the same divergence simultaneously
2. Price is at a key structural level (support/resistance, Order Block, session high/low)
3. The MTF Dashboard (indicator 02 in this suite) confirms the higher timeframe trend
**Reducing noise:**
- Increase `Pivot Lookback` (left/right bars) to 8–10 for cleaner, less frequent signals
- Disable OBV if trading Forex (OBV is volume-dependent and FX volume data is tick-based)
- Disable hidden divergences if you only want reversal signals
---
## Configuration Tips
| Scenario | Recommended settings |
|----------|---------------------|
| Fast scalping (1–5m) | Pivot: 3/3, RSI only |
| Day trading (15m–1H) | Pivot: 5/5, RSI + MACD |
| Swing trading (4H–D) | Pivot: 8/8, all three oscillators |
| Crypto (high volatility) | Pivot: 7/7 to filter noise |
---
## Alerts
Two built-in `alertcondition` entries:
- `Bullish Divergence` — fires when any enabled oscillator confirms a regular bullish divergence
- `Bearish Divergence` — fires when any enabled oscillator confirms a regular bearish divergence
Set up in TradingView: Alerts → Condition → Divergence Hunter → select the desired condition.
**Pine Script v5 — Overlay Indicator**
---
## What It Does
Scans three oscillators simultaneously — RSI, MACD, and OBV — and draws labelled lines directly on the price chart whenever a divergence is detected between price action and oscillator momentum.
Two divergence types are covered per oscillator:
- **Regular divergence** — signals a potential trend reversal
- **Hidden divergence** — signals a potential trend continuation
---
## Divergence Types Explained
### Regular Bullish Divergence
```
Price: Lower Low (LL)
Oscillator: Higher Low (HL)
Signal: Potential reversal UP
```
Price is making lower lows but the oscillator is making higher lows — sellers are losing momentum. The downtrend is weakening.
### Regular Bearish Divergence
```
Price: Higher High (HH)
Oscillator: Lower High (LH)
Signal: Potential reversal DOWN
```
Price is making higher highs but the oscillator is making lower highs — buyers are losing momentum. The uptrend is weakening.
### Hidden Bullish Divergence
```
Price: Higher Low (HL) ← trend is up
Oscillator: Lower Low (LL)
Signal: Continuation UP
```
Price holds higher lows (uptrend structure intact) while the oscillator dips lower — a temporary oscillator pullback during a healthy uptrend. A continuation signal.
### Hidden Bearish Divergence
```
Price: Lower High (LH) ← trend is down
Oscillator: Higher High (HH)
Signal: Continuation DOWN
```
Price is making lower highs (downtrend structure intact) while the oscillator makes higher highs — a temporary oscillator bounce during a healthy downtrend.
---
## The Three Oscillators
| Oscillator | What it measures | Why it diverges |
|-----------|-----------------|-----------------|
| **RSI** | Relative momentum of closes | Classic divergence tool — very reliable at extremes |
| **MACD** | Trend + momentum combined | Useful in trending markets, less noise than RSI |
| **OBV** | Cumulative volume flow | Measures whether volume confirms the price move |
Each oscillator runs through the same `detect_divs()` function. You can enable or disable each one independently. When multiple oscillators show the same divergence simultaneously, that is a high-conviction signal.
---
## Detection Logic
The indicator uses `ta.pivothigh` and `ta.pivotlow` on the **price** series to find swing points, then reads the **oscillator value at that same bar** to compare:
```
For each new pivot high in price:
Compare price level vs previous pivot high price
Compare oscillator vs previous pivot high oscillator
→ Regular Bear: price HH + osc LH
→ Hidden Bear: price LH + osc HH
For each new pivot low in price:
Compare price level vs previous pivot low price
Compare oscillator vs previous pivot low oscillator
→ Regular Bull: price LL + osc HL
→ Hidden Bull: price HL + osc LL
```
When detected, a **line** is drawn connecting the two price pivot points and a **label** marks the divergence type and oscillator name.
---
## Inputs
| Input | Default | Description |
|-------|---------|-------------|
| **RSI Divergence** | true | Enable RSI divergence scanning |
| **RSI Length** | 14 | RSI calculation period |
| **MACD Divergence** | true | Enable MACD line divergence scanning |
| **MACD Fast** | 12 | MACD fast EMA length |
| **MACD Slow** | 26 | MACD slow EMA length |
| **MACD Signal** | 9 | MACD signal line length |
| **OBV Divergence** | true | Enable OBV divergence scanning |
| **Pivot Lookback (left bars)** | 5 | Bars to the left required to confirm a swing point |
| **Pivot Lookback (right bars)** | 5 | Bars to the right required to confirm a swing point |
| **Show Hidden Divergences** | true | Toggle hidden divergence lines on/off |
| **Bullish Regular** | Bright green | Line/label color for regular bullish divergences |
| **Bearish Regular** | Bright red | Line/label color for regular bearish divergences |
| **Bullish Hidden** | Faded green | Line/label color for hidden bullish divergences |
| **Bearish Hidden** | Faded red | Line/label color for hidden bearish divergences |
---
## Visual Output
| Element | Meaning |
|---------|---------|
| Solid bright green line + label `"RSI Bull Div"` | Regular bullish divergence on RSI |
| Solid bright red line + label `"RSI Bear Div"` | Regular bearish divergence on RSI |
| Dashed faded green line + label `"RSI Hid Bull"` | Hidden bullish divergence on RSI |
| Dashed faded red line + label `"RSI Hid Bear"` | Hidden bearish divergence on RSI |
| Same patterns with `"MACD"` or `"OBV"` prefix | Divergence from that oscillator |
| Green background flash | Any bullish regular divergence firing |
| Red background flash | Any bearish regular divergence firing |
Lines connect the **two price swing points** that form the divergence, making it immediately clear which highs or lows are being compared.
---
## How to Use
**Entry logic:**
- Regular divergence → look for a reversal entry after the divergence confirms (pivot right bars have closed).
- Hidden divergence → look for a continuation entry in the direction of the main trend.
- Always wait for the pivot to fully confirm — the signal appears `pivot_rb` bars after the actual swing point.
**Confluence filter:**
A divergence is most reliable when:
1. Two or more oscillators show the same divergence simultaneously
2. Price is at a key structural level (support/resistance, Order Block, session high/low)
3. The MTF Dashboard (indicator 02 in this suite) confirms the higher timeframe trend
**Reducing noise:**
- Increase `Pivot Lookback` (left/right bars) to 8–10 for cleaner, less frequent signals
- Disable OBV if trading Forex (OBV is volume-dependent and FX volume data is tick-based)
- Disable hidden divergences if you only want reversal signals
---
## Configuration Tips
| Scenario | Recommended settings |
|----------|---------------------|
| Fast scalping (1–5m) | Pivot: 3/3, RSI only |
| Day trading (15m–1H) | Pivot: 5/5, RSI + MACD |
| Swing trading (4H–D) | Pivot: 8/8, all three oscillators |
| Crypto (high volatility) | Pivot: 7/7 to filter noise |
---
## Alerts
Two built-in `alertcondition` entries:
- `Bullish Divergence` — fires when any enabled oscillator confirms a regular bullish divergence
- `Bearish Divergence` — fires when any enabled oscillator confirms a regular bearish divergence
Set up in TradingView: Alerts → Condition → Divergence Hunter → select the desired condition.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.