What this builds
- Standard RSI plot with overbought/oversold levels
- Bearish divergence detection: price makes a higher pivot high but RSI makes a lower pivot high
- Visual markers on detected divergence bars
Full script
How divergence detection works
ta.pivothighdetects swing highs in price, lagged bypivotLenbars.Seriesstores the RSI value and price level at each confirmed pivot.- On the next pivot, compare: if price is higher but RSI is lower, it is bearish divergence.
Notes
- The lag from
ta.pivothighmeans divergence signals are confirmedpivotLenbars after the actual pivot. This is unavoidable - see Pivots. - The
Seriescarry-forward pattern (lastPivotPrice.set(lastPivotPrice.get(1))) propagates the last known pivot value across non-pivot bars so the comparison always has data. - Adjust
pivotLento control sensitivity: smaller = more signals, larger = only major swings.
Related pages
Oscillators
ta.rsi reference.
Pivots
ta.pivothigh lag explanation.
Series
Persistent storage used to remember prior pivot values.