Python Stock Analysis

Will Garvin
5 min readJun 26, 2022

--

This tutorial will walk you through using the yfinance library to analyze stock data. We will begin by setting up our script with the necessary imports, then move on to exploring the basics of yfinance. Finally, we will create a working stock screener which will filter out stocks based on our preferences and return a list of the stocks that match our desired underlying fundamentals.

Step 1: Importing the necessary libraries

The first thing we need to do is import yfinance to get our stock data and panda for data analysis.

  1. Open the terminal and type:

pip install yfinance

2. When yfinance has finished installing, type:

pip install pandas

3. Once these are installed, create a new python script and type:

Step 2: Exploring the basics of yfinance

Now that our script is set up, we can begin to explore some basic stock analysis using yfinance and python.

Begin by creating a new ticker object called msft

.financials

The .financials() method is quite helpful for analyzing the basic financials of our stock. if we type:

Then we will get an output displaying the financials of the Microsoft stock

.earnings

Another helpful method for getting a glimpse at our stock’s underlying value is the .earnings method. If we call this method using our Microsoft ticker:

Then we will get an output which displays Microsoft’s revenue and earnings for the past few years:

.info

The .info() method is by far the most powerful tool available to us while using yfinance. If we print the output of this method using our msft ticker (as done below), we will see a very large output displaying a number of important attributes for the Microsoft stock.

While this can output can be helpful, the cool part about this method is that it returns a dictionary with a number of the stocks underlying attributes which we can use to help with our analysis. For example, if we wanted to see the trailing price to earnings ratio of Microsoft, we could type:

and as an output we would see:

Step 3: Creating the stock screener

Now that we have gained a basic understanding of how to use yfinance, it is time to create our stock screener. For the purposes of this tutorial we will filter the S&P 500 down to a much smaller list of stocks which have a price to book ratio of less than 1. For information on what exactly the price to book ratio is, check out this helpful link.

Screening process:

In order to create the screener, our code will need to do two things:

  1. Get the tickers of every stock within the S&P 500
  2. Cycle through each stock within the S&P 500 and check if its price to book ratio is less than 1. Add any stock with a price to book ratio of less than 1 to a list.

How its done:

  1. Get the tickers of each stock within the S&P 500

In order to analyze all stocks within the S&P 500, we must first know the tickers for each of these stocks. This will be done by parsing a csv file found here which contains the ticker and name of each stock in the S&P 500.

Once the csv file has been downloaded and placed into your working directory, begin the parsing process by loading the data into a variable named data by typing:

2. Cycle through each stock within the S&P 500 and check if its price to book ratio is less than 1, and add it to a list if it is.

While it may seem as though a straightforward for-each loop could analyze each stock within the S&P 500, doing it in this manner would be extremely inefficient and lead to very long wait times for your screener. Instead, we will use a process known as multithreading in order to greatly speed up the process.

If you are unfamiliar with multithreading, it can be helpful to think of it as a way to run multiple operations at the same time.

The first thing we will need to do to begin cycling through each stock is create a function which will analyze a stock and return its price to book ratio (this function will also return its ticker and name in order to help with formatting later on):

Something important to note while writing this function is that yfinance will occasionally not be able to return the price to book ratio for a given stock, so we must check for the value before calling it. In the event that the price to book ratio is not returned, this function will set it to a value significantly higher than our filtering threshold in order to ensure that it is filtered out.

Next, we will need to write the code which cycles through each of the stocks within the S&P 500 and compares it to our desired filter. Below the getPB function, add the following

This code will use multithreading to loop through each stock in the ‘Symbol’ column of the data variable and compare its price to book ratio to our desired threshold (in this case 1). If the stock has a price to book ratio within our desired range then it will be added to the goodPB list.

Finally, the code will let the user know that it has finished its analysis and print out the stocks which have a desired price to book ratio, as well as the number of stocks with the desired ratio.

--

--

Will Garvin

Computer Science student at University of Michigan College of Engineering