Main Page > Articles > Welles Wilder > Implementing Wilder's RSI from Scratch in Python: A Step-by-Step Guide

Implementing Wilder's RSI from Scratch in Python: A Step-by-Step Guide

From TradingHabits, the trading encyclopedia · 5 min read · February 28, 2026
The Black Book of Day Trading Strategies
Free Book

The Black Book of Day Trading Strategies

1,000 complete strategies · 31 chapters · Full trade plans

While most trading platforms and charting software provide a built-in Relative Strength Index (RSI) indicator, professional traders and quantitative analysts often find it beneficial to implement the indicator themselves. Building the RSI from scratch not only provides a deeper understanding of its inner workings but also allows for greater flexibility in backtesting and strategy development. This article provides a step-by-step guide to implementing J. Welles Wilder Jr.'s RSI, with a focus on his specific smoothing method, using the Python programming language.

We will break down the implementation into logical steps, from calculating the initial average gains and losses to applying Wilder's smoothing for subsequent calculations. The code will be accompanied by detailed explanations and a sample dataset to ensure clarity and facilitate practical application.

The Python Implementation

The following Python code implements the Wilder's RSI. The function takes a list of closing prices and a lookback period as input and returns a list of RSI values.

python
def wilders_rsi(prices, period=14):
    if len(prices) < period:
        return []

    gains = []
    losses = []

    for i in range(1, len(prices)):
        change = prices[i] - prices[i-1]
        if change > 0:
            gains.append(change)
            losses.append(0)
        else:
            gains.append(0)
            losses.append(abs(change))

    avg_gain = sum(gains[:period]) / period
    avg_loss = sum(losses[:period]) / period

    rsi_values = []

    if avg_loss == 0:
        rsi_values.append(100)
    else:
        rs = avg_gain / avg_loss
        rsi = 100 - (100 / (1 + rs))
        rsi_values.append(rsi)

    for i in range(period, len(gains)):
        avg_gain = (avg_gain * (period - 1) + gains[i]) / period
        avg_loss = (avg_loss * (period - 1) + losses[i]) / period

        if avg_loss == 0:
            rsi_values.append(100)
        else:
            rs = avg_gain / avg_loss
            rsi = 100 - (100 / (1 + rs))
            rsi_values.append(rsi)

    return rsi_values

Step-by-Step Explanation

  1. Initialization: The function first checks if there is enough price data to calculate the RSI for the given period. It then initializes two lists, gains and losses, to store the upward and downward price movements.

  2. Calculating Gains and Losses: The code iterates through the price data, calculating the change in price from one period to the next. If the change is positive, it is appended to the gains list, and a zero is appended to the losses list. If the change is negative, the absolute value of the change is appended to the losses list, and a zero is appended to the gains list.

  3. Initial Average Gain and Loss: The initial average gain and loss are calculated using a simple moving average of the first period (typically 14) gains and losses.

  4. Initial RSI: The first RSI value is calculated using the initial average gain and loss.

  5. Wilder's Smoothing: For all subsequent periods, the function applies Wilder's smoothing method to calculate the new average gain and loss. The new average is the previous average multiplied by (period - 1), plus the current gain or loss, all divided by period.

  6. Subsequent RSI Values: The RSI is then calculated for each subsequent period using the updated average gain and loss.

Sample Usage and Data Table

Let's use the wilders_rsi function with a sample dataset to see it in action. The following table shows the closing prices for a stock over 21 days, along with the calculated 14-day RSI values.

DayClose14-Day RSI
144.34
244.09
344.15
443.61
544.33
644.83
745.10
845.12
945.18
1045.17
1145.23
1245.61
1345.82
1445.8468.57
1545.9569.70
1646.0070.83
1745.5060.24
1845.2054.17
1945.3055.83
2045.0050.00
2145.2553.57

Conclusion

Implementing the RSI from scratch in Python is a valuable exercise for any serious trader or quantitative analyst. It provides a deeper understanding of the indicator's mechanics and allows for greater customization and flexibility in strategy development. The provided code and explanation serve as a solid foundation for building your own RSI-based trading tools and strategies.