rsi The **RSI Divergence** indicator in this script is designed to highlight **divergence** between the **Relative Strength Index (RSI)** and **price action** on a chart. Divergence can be a key signal for potential trend reversals or continuation in technical analysis.
### **Key Components of the Indicator:**
1. **RSI Calculation:** - The **Relative Strength Index (RSI)** is calculated using a typical 14-period length, but the user can customize this input. - RSI is a momentum oscillator that measures the speed and change of price movements, oscillating between 0 and 100. Values above 70 indicate overbought conditions, and values below 30 indicate oversold conditions.
2. **Divergence Logic:** - **Bullish Divergence:** Occurs when the price forms a **lower low**, but the RSI forms a **higher low**. This suggests that despite price continuing to drop, momentum (RSI) is strengthening, which may indicate a potential price reversal to the upside. - **Bearish Divergence:** Occurs when the price forms a **higher high**, but the RSI forms a **lower high**. This indicates that even though price is rising, the momentum (RSI) is weakening, which could signal a price reversal to the downside.
3. **Pivot Identification:** - The script identifies **pivot points** (local highs and lows) on both price and RSI. - **Bullish Divergence:** A lower price low with a higher RSI low. - **Bearish Divergence:** A higher price high with a lower RSI high.
4. **Lookback Periods:** - **Lookback Left (lookbackLeft):** Defines the number of bars to look back for pivot confirmation. This allows for adjusting the sensitivity of the divergence. - The **divergence range** is constrained by two parameters: - **Minimum range (rangeLower):** The minimum number of bars for divergence to be considered. - **Maximum range (rangeUpper):** The maximum number of bars for divergence to be considered.
5. **Signal Generation and Plotting:** - When a **bullish divergence** is detected, a **green label** is plotted below the bar where the divergence occurs. - When a **bearish divergence** is detected, a **red label** is plotted above the bar. - The script uses **`plotshape()`** to plot these labels on the chart.
6. **Alerts:** - Alerts are configured for both **bullish** and **bearish divergences** so that you can be notified when a divergence signal occurs.
---
### **How the Indicator Works:** - The RSI and price action are compared using **pivots**: The script checks whether the price and RSI are forming new highs or lows within the specified **lookback period**. - If the conditions for divergence (higher/lower RSI pivot vs price pivot) are met, a signal is plotted on the chart. - The script helps to visually identify potential reversal points and allows users to set alerts for these divergence signals.
---
### **Use Case:** - This script is useful for traders looking to trade potential trend reversals based on **divergence** between price and RSI. - **Bullish divergence** can indicate a **buy** opportunity, while **bearish divergence** can suggest a **sell** opportunity. - The indicator works best in **volatile markets** and when combined with other technical analysis tools for confirmatio