Lesson 3 of 15

RSI (Relative Strength Index)

RSI — Relative Strength Index

The RSI is a momentum oscillator that measures the speed and magnitude of price changes. It ranges from 0 to 100. Values above 70 are typically considered overbought; values below 30 are considered oversold.

Algorithm

For each time step t (after window prices):

  1. Compute daily differences: diff[i] = prices[i] - prices[i-1]
  2. Separate into gains (max(0, diff)) and losses (max(0, -diff))
  3. Compute rolling averages over the window:
    avg_gain = mean of gains in window
    avg_loss = mean of losses in window
  4. RS = avg_gain / avg_loss
  5. RSI = 100 − 100 / (1 + RS)

If avg_loss == 0, RSI = 100.

The first window values are None because there is insufficient data.

Task

Implement rsi(prices, window=14) returning a list where the first window elements are None and the rest are RSI values.

Python runtime loading...
Loading...
Click "Run" to execute your code.