freeradiantbunny.org

freeradiantbunny.org/blog

relative strength indicator

The Relative Strength Index (RSI) is a momentum oscillator used in technical analysis to measure the speed and change of price movements. It helps traders identify overbought or oversold conditions, potential reversals, and trend strength. Developed by J. Welles Wilder, RSI is calculated using price data over a specified period, typically 14 periods (e.g., days, hours, or other timeframes).

How RSI is Calculated

The RSI formula is:

RSI = 100 - (100 / (1 + RS))

Where:

Steps:

  1. Calculate price changes (gains or losses) for each period.
  2. Separate gains (positive changes) and losses (negative changes, taken as absolute values).
  3. Compute the average gain and average loss over the chosen period (e.g., 14 periods).
  4. Calculate RS = Average Gain / Average Loss.
  5. Plug RS into the RSI formula to get a value between 0 and 100.

Default Settings:

RSI Signals

RSI provides several types of signals for trading decisions:

1. Overbought and Oversold Conditions

2. Centerline Crossovers (RSI around 50)

3. Divergences

4. Trend Confirmation

5. Failure Swings

How to Trade RSI Signals

General Guidelines

Trading Strategies

  1. Overbought/Oversold Reversal Strategy:
    • Buy Setup:
      • RSI drops below 30 (oversold).
      • Wait for RSI to cross above 30, confirmed by a bullish candlestick (e.g., hammer) or support level.
      • Enter long position with stop-loss below recent lows.
      • Target: Next resistance or 1:2 risk-reward ratio.
    • Sell Setup:
      • RSI rises above 70 (overbought).
      • Wait for RSI to cross below 70, confirmed by a bearish candlestick (e.g., shooting star) or resistance level.
      • Enter short position with stop-loss above recent highs.
      • Target: Next support or 1:2 risk-reward ratio.
  2. Divergence Trading Strategy:
    • Bullish Divergence:
      • Identify lower low in price with higher low in RSI.
      • Confirm with support level or bullish price action (e.g., double bottom).
      • Enter long position with stop-loss below recent low.
      • Target: Next resistance or Fibonacci retracement level.
    • Bearish Divergence:
      • Identify higher high in price with lower high in RSI.
      • Confirm with resistance level or bearish price action (e.g., double top).
      • Enter short position with stop-loss above recent high.
      • Target: Next support or Fibonacci retracement level.
  3. Trend-Following with RSI:
    • Buy in Uptrend:
      • RSI stays above 50, ideally 50–70.
      • Wait for pullback to support or moving average (e.g., 20 EMA).
      • Enter long when RSI crosses above 50, confirmed by price breaking above moving average.
      • Stop-loss: Below recent swing low.
      • Target: Next resistance or trailing stop.
    • Sell in Downtrend:
      • RSI stays below 50, ideally 30–50.
      • Wait for rally to resistance or moving average.
      • Enter short when RSI crosses below 50, confirmed by price breaking below moving average.
      • Stop-loss: Above recent swing high.
      • Target: Next support or trailing stop.
  4. Failure Swing Strategy:
    • Bullish Failure Swing:
      • RSI drops below 30, rises above 30, pulls back, and breaks above prior RSI high.
      • Enter long on RSI breakout, confirmed by price breaking above recent high.
      • Stop-loss: Below recent price low.
      • Target: Next resistance level.
    • Bearish Failure Swing:
      • RSI rises above 70, drops below 70, rallies, and breaks below prior RSI low.
      • Enter short on RSI breakdown, confirmed by price breaking below recent low.
      • Stop-loss: Above recent price high.
      • Target: Next support level.

Programming RSI in Code

Below are examples of implementing RSI in Python and Pine Script (TradingView).

Python Example (Using pandas)


	    import pandas as pd
	    import numpy as np

	    def calculate_rsi(data, period=14):
	    # Calculate price changes
	    delta = data['Close'].diff()

	    # Separate gains and losses
	    gains = delta.where(delta > 0, 0)
	    losses = -delta.where(delta < 0, 0)

					  # Calculate average gains and losses
					  avg_gains = gains.rolling(window=period).mean()
					  avg_losses = losses.rolling(window=period).mean()

					  # Calculate RS and RSI
					  rs = avg_gains / avg_losses
					  rsi = 100 - (100 / (1 + rs))

					  return rsi

					  # Example usage with a DataFrame
					  # Assuming 'data' is a pandas DataFrame with 'Close' column
					  data = pd.DataFrame({'Close': [100, 102, 101, 103, ...]})  # Replace with real price data
					  data['RSI'] = calculate_rsi(data, period=14)

					  # Trading signals
					  data['Buy_Signal'] = np.where((data['RSI'] < 30) & (data['RSI'].shift(1) >= 30), 1, 0)
					  data['Sell_Signal'] = np.where((data['RSI'] > 70) & (data['RSI'].shift(1) <= 70), 1, 0)

														       print(data[['Close', 'RSI', 'Buy_Signal', 'Sell_Signal']])
														       

Explanation: The code calculates RSI using a 14-period lookback and generates buy/sell signals when RSI exits oversold/overbought zones. Extend this for divergence or failure swings by analyzing price and RSI highs/lows.

Pine Script (TradingView) Example


	    //@version=5
	    indicator("RSI Strategy", overlay=false)
	    rsi_period = input(14, "RSI Period")
	    rsi = ta.rsi(close, rsi_period)
	    overbought = 70
	    oversold = 30

	    // Plot RSI
	    plot(rsi, title="RSI", color=color.blue)
	    hline(overbought, "Overbought", color=color.red)
	    hline(oversold, "Oversold", color=color.green)
	    hline(50, "Centerline", color=color.gray)

	    // Buy and Sell Signals
	    buy_signal = ta.crossover(rsi, oversold)
	    sell_signal = ta.crossunder(rsi, overbought)
	    plotshape(buy_signal, title="Buy", location=location.bottom, color=color.green, style=shape.triangleup)
	    plotshape(sell_signal, title="Sell", location=location.top, color=color.red, style=shape.triangledown)
	

Explanation: This script calculates RSI, plots it with overbought/oversold lines, and generates visual buy/sell signals on crossovers. Add divergence or failure swing logic using ta.highest/ta.lowest.

Practical Tips for Trading RSI

Limitations of RSI

Conclusion

RSI is a versatile tool for identifying momentum, reversals, and trend strength. Focus on overbought/oversold levels, divergences, centerline crossovers, and failure swings to develop robust strategies. Combine RSI with other indicators, use proper risk management, and backtest strategies. Python or Pine Script can automate signal generation. For tailored code or strategies, provide more details on the platform or asset!

Note: The current date and time is 04:25 PM EDT on Friday, July 18, 2025.