Get RSI Values for a Stock using Yahoo Finance Data

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

 

Issue with Getting All Zeros from the Camera Using OpenCV in Python

Reading images from the camera (on macOS, for example) using Python with OpenCV may return vectors full of zero values even though the camera turns on correctly.

This may be due to the fact that we are not waiting for the data properly.

The solution is to make sure to read the data in a loop, i.e. to wait for the data to arrive instead of expecting it to be available instantly.
One single read may not be enough. The code below illustrates the situation.

The following will most likely return all zeros. Note that even though the isOpened() property returns True and the success value from captureObject.read() is also True, the camera data will not be ready.

import cv2

captureObject = cv2.VideoCapture(0)

if captureObject.isOpened():
  success, testImage = captureObject.read()

  # This may be True despite all-zeros images.
  print(success)

  # Numeric values of the image.
  print(testImage)

The following code shows how to properly wait for the camera data to be available in a loop. Note that the first few reads will output zeros as well.
Afterward, a stream of numeric values should be output until we exit the script.

import cv2

captureObject = cv2.VideoCapture(0)

while captureObject.isOpened():
  success, testImage = captureObject.read()

  # Numeric values of the image.
  print(testImage)

  # Use CTRL-C to exit.