Skip to main content

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

  1. ta.pivothigh detects swing highs in price, lagged by pivotLen bars.
  2. Series stores the RSI value and price level at each confirmed pivot.
  3. On the next pivot, compare: if price is higher but RSI is lower, it is bearish divergence.

Notes

  • The lag from ta.pivothigh means divergence signals are confirmed pivotLen bars after the actual pivot. This is unavoidable - see Pivots.
  • The Series carry-forward pattern (lastPivotPrice.set(lastPivotPrice.get(1))) propagates the last known pivot value across non-pivot bars so the comparison always has data.
  • Adjust pivotLen to control sensitivity: smaller = more signals, larger = only major swings.

Oscillators

ta.rsi reference.

Pivots

ta.pivothigh lag explanation.

Series

Persistent storage used to remember prior pivot values.