title: Why is my RSI calculation wildly off from Yahoo Finance?
body: <p>I'm using the <code>yfinance</code> 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.</p>
  <p>Expected behavior: My calculated RSI value will match what's seen on stock charts on Yahoo Finance.</p>
  <p>Actual behavior: My RSI can be off by 10 or 15 points sometimes, yet other times it matches perfectly.</p>
  <p>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&amp;T) my RSI is 41, while Yahoo shows it as 42.</p>
  <p>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.</p>
  <p>Below is my exact code:</p>
  <pre><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 &lt;= 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 &gt; 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 &lt; 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)
  </code></pre>