0

I'm using the yfinance library to pull closing stock prices daily and calculate various technical indicators. Sometimes, my RSI (relative strength index, for those who are wondering) matches up with what I see on the Yahoo Finance chart. Other times, however, it's off by a lot. I will assume that Yahoo Finance know what they are doing and it is me who is making the mistake, but I don't see where.

Expected behavior: My calculated RSI value will match what's seen on stock charts on Yahoo Finance.

Actual behavior: My RSI can be off by 10 or 15 points sometimes, yet other times it matches perfectly.

For example, today, December 29, 2020, the RSI I calculate for FB from yesterday is 38. Yahoo shows it as 52. Yet for T (the symbol for AT&T) my RSI is 41, while Yahoo shows it as 42.

I have verified that my code matches other examples I have seen, but otherwise I don't know what to try here. I am no mathematician.

Below is my exact code:

import pandas as pd
import yfinance as yf

# Calculate Relative Strength Indicator (RSI) #
    gainz = []
    losses = []

    # Initialize variable for counting rows of prices
    n = 1

    # For each of the last 14 trading sessions...
    while n <= 14:  

        # ... calculate difference between closing price of each day and of the day before it.
        difference = ((df['Close'][-n]) - (df['Close'][-(n+1)]))

        # If difference is positive, add it to the positive list, and add 0 to the losses list 
        if difference > 0:
            gainz.append(difference)
            losses.append(0)

        # If negative, get the absolute value and add to the negative list, and add 0 to the gainz list
        elif difference < 0:
            losses.append(abs(difference))
            gainz.append(0)

        # Otherwise it must be zero, so add 0 to both lists
        else:
            gainz.append(0)
            losses.append(0)

        # Increment n to move to the next row
        n += 1
        
    avg_gainz = (sum(gainz))/14
    avg_losses = (sum(losses))/14

    RSvalue = (avg_gainz/avg_losses)

    RSI = (100 - (100/(1+RSvalue)))
    RSI = int(RSI)
MBWD
  • 122
  • 12
  • RSI calculation with pandas are already explained [here](https://stackoverflow.com/questions/57006437/calculate-rsi-indicator-from-pandas-dataframe/57037866). – Robert Altena Dec 29 '20 at 11:00
  • I searched but for some reason did not find this... but it helps tremendously, so thank you. I am just going to use the `talib` library as it seems to do this calculation very easily. – MBWD Dec 29 '20 at 11:21

1 Answers1

1

Well for starters I believe RSI Calculations are done with percentage gains/losses not raw dollar gains/losses. Secondly, you need to divide the gains array and the losses array by the number of gains and losses, respectively, to get the avg gain or loss during your calculation period (Not 14). E.g. 7 gains and 7 losses would mean dividing each array by 7. Correct me if I'm wrong but you could give those fixes a try and see if they work.

This link was helpful for me: https://www.investopedia.com/terms/r/rsi.asp