We can use the Python Technical Analysis Library (ta) for the RSI (Relative Strength Index) calculation.
A simple and free API for finance data is Yahoo Finance (yfinance); the most convenient way to call it is using the yfinance Python library.
First, install the Technical Analysis library:
$ pip install ta
Install the Yahoo Finance library:
$ pip install yfinance
We use NVDA as an example, on a 5 minute timeframe.
The following script will retrieve the data and pass the results to the TA library to calculate the RSI.
The results are stored in a Pandas series; after retrieval we print out a sample of the latest values.
import yfinance as yf from ta.momentum import RSIIndicator ticker = 'NVDA' # Make sure to use a window with enough data for the RSI calculation. data = yf.download(tickers=ticker, period='5d', interval='5m') closeValues = data['Close'] # Use the common 14 period setting. rsi_14 = RSIIndicator(close=closeValues, window=14) # This returns a Pandas series. rsiSeries = rsi_14.rsi() # Latest 10 values of the day for demonstration. print(rsiSeries.tail(10))
Below is the output for a single run. The series contains timestamps and RSI values.
Datetime 2024-05-24 12:35:00-04:00 52.733213 2024-05-24 12:40:00-04:00 56.698742 2024-05-24 12:45:00-04:00 56.991273 2024-05-24 12:50:00-04:00 62.709294 2024-05-24 12:55:00-04:00 55.964462 2024-05-24 13:00:00-04:00 56.631537 2024-05-24 13:05:00-04:00 51.238671 2024-05-24 13:10:00-04:00 53.740376 2024-05-24 13:15:00-04:00 51.827860 2024-05-24 13:20:00-04:00 52.300520 Name: rsi, dtype: float64