RSI Indicator Formula and Calculation: Trading Strategies and Python Implementation

·

The Relative Strength Index (RSI), developed by J. Welles Wilder in 1986, remains a cornerstone of technical analysis. Originally designed for commodities trading, this momentum oscillator now aids traders across all asset classes by measuring the speed and change of price movements.

Key Topics Covered


How RSI Works: Formula and Calculation

The RSI oscillates between 0 and 100, calculated using average gains and losses over a specified period (typically 14 days). Here’s the breakdown:

Step-by-Step Process

  1. Price Changes: Compute daily closing price differences.
  2. Gain/Loss Separation: Categorize positive changes as "Gains" and absolute negative changes as "Losses."
  3. Smoothing Averages:

    • First 14-day period: Simple average of Gains and Losses.
    • Subsequent days: Use the formula:
      [ \text{Avg Gain} = \frac{(\text{Previous Avg Gain} \times 13) + \text{Current Gain}}{14} ]
  4. Relative Strength (RS):
    [ RS = \frac{\text{Avg Gain}}{\text{Avg Loss}} ]
  5. RSI Calculation:
    [ RSI = 100 - \left( \frac{100}{1 + RS} \right) ]

👉 Explore advanced trading tools to integrate RSI into your strategies.


Python Implementation with Plotly Visualization

import pandas as pd
import plotly.graph_objects as go

# Load OHLCV data
data = pd.read_csv('AAPL_2022-2025.csv')
close_prices = data['Close']

# Calculate 14-day RSI
delta = close_prices.diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
avg_gain = gain.rolling(window=14).mean()
avg_loss = loss.rolling(window=14).mean()
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))

# Visualize
fig = go.Figure()
fig.add_trace(go.Scatter(x=data['Date'], y=close_prices, name='Close Price'))
fig.add_trace(go.Scatter(x=data['Date'], y=rsi, name='14-day RSI', yaxis='y2'))
fig.update_layout(title='AAPL RSI Indicator (2022-2025)', yaxis2=dict(overlaying='y', side='right'))
fig.show()

Trading Strategies Using RSI

1. Overbought/Oversold Signals

2. Divergence Trading

3. Pattern Recognition

👉 Learn more about backtesting strategies to validate these patterns.


Backtesting Results

A simple RSI strategy (buy ≤30, sell ≥70) tested on AAPL (2022-2025) yielded:

Limitation: False signals occur during strong trends (e.g., RSI >70 in bull markets).


FAQs

Q1: Can RSI be used for cryptocurrencies?
Yes, but crypto’s volatility may require adjusting thresholds (e.g., 80/20 for extreme swings).

Q2: What’s the best timeframe for RSI?
14-day is standard, but shorter periods (e.g., 7-day) increase sensitivity; longer periods (e.g., 21-day) reduce noise.

Q3: How to filter false signals?
Combine RSI with:

Q4: Does RSI work in sideways markets?
Yes, but pair it with Bollinger Bands® to identify range-bound conditions.


Key Limitations

  1. Trend Bias: Strong trends render overbought/oversold levels ineffective.
  2. Lagging Nature: RSI reacts to price changes rather than predicting them.
  3. False Divergences: Prices may continue trending despite RSI signals.

Pro Tip: Use RSI with other indicators like Stochastic Oscillator or ADX for robust analysis.


Next Steps

For structured learning, consider Algorithmic Trading Courses covering Python and advanced indicators.