Relative Strength Index: Trading Strategies, RSI Divergence and Python Implementation

·

Introduction

The Relative Strength Index (RSI) is a cornerstone momentum oscillator in technical analysis, designed to measure the speed and magnitude of price movements. By identifying overbought (RSI > 70) and oversold (RSI < 30) conditions, traders can refine entry/exit points and develop robust RSI trading strategies.

This guide explores:


Origins of the RSI Indicator

Developed by J. Welles Wilder Jr. in 1978, the RSI was introduced in "New Concepts in Technical Trading Systems" alongside other pioneering indicators. Wilder’s goal was to quantify price momentum, addressing the challenges of timing trades in volatile markets.

Key features:


Mathematical Construction

RSI Formula

  1. Calculate Average Gain/Loss:

    • Average Gain = Mean of upward price changes over 14 periods.
    • Average Loss = Absolute mean of downward price changes.
  2. Compute Relative Strength (RS):
    [
    RS = \frac{\text{Average Gain}}{\text{Average Loss}}
    ]
  3. Derive RSI:
    [
    RSI = 100 - \left( \frac{100}{1 + RS} \right)
    ]

👉 Explore advanced RSI trading techniques


Trading Strategies Using RSI

1. Basic Overbought/Oversold Strategy

2. RSI Divergence

3. Swing Rejection Strategy


Implementing RSI in Python

import yfinance as yf
import pandas as pd
import mplfinance as mpf

def calculate_rsi(data, lookback=14):
    delta = data['Close'].diff()
    gain = delta.where(delta > 0, 0)
    loss = -delta.where(delta < 0, 0)
    avg_gain = gain.ewm(com=lookback-1, min_periods=lookback).mean()
    avg_loss = loss.ewm(com=lookback-1, min_periods=lookback).mean()
    rs = avg_gain / avg_loss
    return 100 - (100 / (1 + rs))

# Example usage
data = yf.download('META', start='2022-07-01', end='2023-07-01')
data['RSI'] = calculate_rsi(data)

Output:


Pros and Cons of RSI

ProsCons
Works across stocks, forex, cryptoFalse signals in strong trends
Simple interpretationRequires confirmation (e.g., MACD)
Effective in ranging marketsSensitivity to period selection

FAQs

Q: Can RSI be used alone for trading decisions?
A: No—combine it with trend analysis (e.g., moving averages) or volume indicators for higher accuracy.

Q: What’s the optimal RSI period?
A: While 14 is standard, shorter periods (e.g., 9) increase sensitivity; longer periods (e.g., 25) reduce noise.

Q: How does RSI differ from MACD?
A: RSI measures momentum magnitude; MACD tracks trend direction and strength via moving averages.

👉 Master RSI backtesting strategies


Key Takeaways

  1. RSI identifies momentum shifts but should be paired with trend-confirming tools.
  2. Divergences often precede reversals—watch for price/RSI discrepancies.
  3. Python implementation enables customizable RSI strategies (e.g., screener for oversold stocks).

For deeper insights, read Wilder’s original work or integrate RSI with other indicators like Williams %R.