Skip to main content

RSI Divergence Pine Script: A Concise Guide for Traders

· 3 min read

RSI divergence is a powerful technical analysis signal that occurs when price action and the Relative Strength Index (RSI) indicator move in opposite directions, often signaling potential trend reversals or continuations. Implementing RSI divergence detection in TradingView using Pine Script enables traders to automate this analysis, enhancing decision-making and timing.

What Is RSI Divergence?

  • Bullish Divergence: Price makes a lower low, but RSI forms a higher low, suggesting a potential upward reversal.
  • Bearish Divergence: Price makes a higher high, but RSI forms a lower high, indicating a possible downward reversal.

Detecting these divergences manually can be subjective and time-consuming, which is why Pine Script coding offers an efficient alternative.

The Best Pine Script Generator

How RSI Divergence Is Detected in Pine Script

Pine Script identifies divergence by comparing pivot points in price and RSI over a defined lookback period. Key steps include:

  • Calculating the RSI over a user-defined length (commonly 14 periods).
  • Detecting pivot highs and lows in both price and RSI using functions like ta.pivothigh and ta.pivotlow.
  • Comparing these pivots to identify divergence patterns within a maximum bar distance.
  • Drawing lines to visually highlight bullish or bearish divergences on the chart.
  • Optionally setting alerts to notify traders when divergences occur.

Advanced scripts also incorporate features such as:

  • Filtering divergences to overbought/oversold RSI zones for higher reliability.
  • Including hidden divergences that signal trend continuation.
  • Customizing visual elements like gradient fills and alert parameters for better usability.

Benefits of Using RSI Divergence Pine Script

  • Objective Analysis: Removes emotional bias by automating divergence detection.
  • Time Efficiency: Quickly identifies high-probability trade setups.
  • Customization: Allows traders to adjust sensitivity and visual preferences.
  • Integration: Can be combined with other indicators or strategies for enhanced trading systems.

Example Use Case

A trader using an RSI divergence Pine Script might spot a bullish divergence where price hits a new low but RSI forms a higher low within 50 bars. This signal, especially if RSI is in the oversold zone, could prompt a buy entry with a defined stop loss and target, improving trade timing and risk management.

Getting Started with RSI Divergence Pine Script

  • Explore publicly available RSI divergence scripts on TradingView to understand their logic and features.
  • Customize parameters such as RSI length, lookback periods, and divergence restrictions to fit your trading style.
  • Test scripts using TradingView's backtesting tools to evaluate performance.
  • Set alerts within the script to stay informed about new divergence signals without constant chart monitoring.