How to Find Stocks to Invest in Using Python (2024)

How to Find Stocks to Invest in Using Python (1)

I am fascinated with the stock market and find it an inspiration for countless ideas to turn into projects using Python. This article outlines the process to download company financial reports, run a series of tests on the financial data, and summarize which stocks have passed the tests. This article aims to make as simple as possible the process of selecting stocks to invest in.

The following sections will be covered:

  1. How to Find Stocks to Invest In
  2. What is Needed to Run the Scripts
  3. Overview of Main Function
  4. Description of the Output Files
  5. How to Use the Output Files to Select Stocks
  6. Proof in the Pudding

It should not be assumed that the methods, techniques, or indicators presented here will be profitable or will not result in losses.

You can check out the repository here!

How to Find Stocks to Invest in Using Python (4)

Most of my research on finding promising stocks has come from the book How to Make Money in Stocks by William J. O’Neil. Even though the edition I read was published in 2009 and the market has changed tremendously since then, I believe there are fundamental metrics that will always identify a successful company.

Here are the metrics used by my scripts:

O’Neil’s book is largely based on an acronym called CAN SLIM. The Python scripts I have developed utilize the first two letters.

Current Quarterly Earnings and Sales

Quarterly earnings per share must be up at least 18–20%, but preferably up 40–100% or more. The higher the better. They should also be accelerating at some point in recent quarters. Quarterly sales should also be accelerating or up 25% or more.

Annual Earnings Increases

There must be significant (25% or more) earnings growth in each of the last three years and a return on equity of 17% or more (25–50% preferred). If return on equity is too low, pre-tax profit margin must be strong.

Using this knowledge, I have set up the scripts to test stocks using the following:

  1. Annual reports must have an EPS increase of at least 20%
  2. Annual reports must have an ROE increase of at least 17%
  3. Quarterly reports must have an EPS increase of at least 20%
  4. Quarterly reports must have a Sales increase of at least 20%

The percent increases are year over year, e.g., the percent change from the first quarter of 2020 to the first quarter of 2021.

How to Find Stocks to Invest in Using Python (5)

To begin using the scripts, you need a list of stock symbols to process. You can generate your own list of symbols, or you can download a list from the web. I have used TradingView, StockCharts, and finfiz to download a *.csv of stock symbols. These websites have a free one month trial if you do not want to pay for the subscription. When I use these scripts, I typically download a list of symbols in a price range or look for stocks in a specific industry.

How to Find Stocks to Invest in Using Python (6)

The most important part about this *.csv is that it needs to have a single column labeled “Symbol” with all the stock symbols you want to process.

You will also need a free API key from Alpha Vantage, which will be used to download the financial reports.

How to Find Stocks to Invest in Using Python (7)
How to Find Stocks to Invest in Using Python (8)

The API will download four reports: quarterly income statement, quarterly balance sheet, annual income statement, and annual balance sheet. These reports will provide the same financial information you’ll find on SEC.gov. The two figures above have highlighted the columns used in the scripts: total revenue, net income, shareholder equity, and shares outstanding.

The two pieces you need to run the scripts are the *.csv of all the stocks you want to process and the API key.

Once you have downloaded the repository and the necessary Python packages, simply run the following from your command-line interpreter:

python findStocks.py --stock_list "Your Stock List.csv" --key "YOURAPIKEY"
How to Find Stocks to Invest in Using Python (9)

Below you will see the general process of the scripts.

Pseudocode

Download the data
for stock in all_stocks
Run preliminary tests on stock
Calculate additional metrics
Run main tests
Save financial data for stock
Save results for all stocks

Below you will see the main function with detailed notes explaining the process.

findStocks.py

if __name__ == '__main__': # Get user inputs
stockFile, dataFolder, apikey, flexible, record = getInputs()
# Get financial report data
symbols = getData(stockFile, dataFolder, apikey)
# Loop through all stocks
for s in symbols:
print('>>>', s)
# Initialize Stock object
stock = Stock(s, dataFolder, flexible)
# Run preliminary tests to check if stock is disqualified
preliminaryTests(stock)
# Skip stock if it failed preliminary tests. Update
# the *Processed.csv
if stock.errorMessage != 'processed':
updateProcessed(stockFile, s, stock.errorMessage)
continue
for r in stock.reports:
# Manage missing data and other inconsistencies in data
manageBadData(stock, r)
# Calculate new metrics and run stock tests
analyzeStock(stock, r)
# Save data and update files
record = saveResults(stock, s, stockFile, record)
# Save the test results for all stocks
columns = [x for x in stock.record.keys()]
saveAll(record, columns)

I created a preliminaryTests function that will run preliminary tests that can disqualify a stock from further analysis. Disqualifying stocks speeds up the runtime and reduces the number of files saved. It also reduces the number of stocks in the *Results.csv which allows for a less cluttered and overwhelming final analysis. The preliminary tests include the following:

  1. The two most recent quarterly reports and most recent annual report can not have negative net income.
  2. The stock can not be experiencing a correction. This preliminary test will be discussed in detail later.
  3. Over half of the data can not be missing from the financial reports downloaded from Alpha Vantage.
  4. Financial reports must be reported in U.S. dollars.
How to Find Stocks to Invest in Using Python (10)

After running the scripts, the following files and folders will be saved:

  • *Results.csv summarizing the test results.
  • *Processed.csv keeps track of which stocks have been processed along with any error messages.
  • Data folder includes data downloaded using the Alpha Vantage API.
  • Processed folder will contain condensed financial reports. All data included in *Results.csv was calculated using the files in the Processed folder.
How to Find Stocks to Invest in Using Python (11)

The above image is an example of the *Processed.csv. This *.csv is used to track which stocks have been processed as well as any errors that occurred while processing the stock.

There is often missing or incorrect data downloaded from Alpha Vantage. I have written the code to save an error message to the *Processed.csv if something went wrong.

Some of the error messages can be corrected by manually cleaning and correcting the financial reports. If you would like to correct the errors or read more information about the error messages, you can read the docstring at the top of the script findStocksClasses.py.

How to Find Stocks to Invest in Using Python (12)

The Processed folder will have condensed financial data for each stock (see above figure).

The fiscal date, total revenue, net income, shareholder equity, and shares outstanding were taken directly from the balance sheet and income statements. All other columns were calculated and are used to test the quality of the stock.

To reduce the number of files I concatenated the quarterly and annual reports. This is why the date jumps from 2017–07–31 to 2020–01–31. Percent changes are calculated year over year, and the empty cells in the figure above are the result of not having data to calculate a percentage change.

How to Find Stocks to Invest in Using Python (13)

After running the scripts, a *Results.csv will provide information on which stocks are worth investing in (see figure above).

Slope

The second column slope is a way of measuring how much the stock price has been advancing recently. I created a short algorithm that weights recent stock price changes more heavily than price changes months ago. The larger the slope value, the larger the price increase has been recently. The rate of the price change is calculated using data downloaded with the Yahoo! Finance API.

Below you can see how the slope is calculated. You can also view the code at lines 179-206 in findStocksClasses.py.

# Download ticker data
df = yf.download(self.s, period='1y', interval='1d')
# Get the most recent close price
last = df.iloc[-1, 3]
self.changes = []
adjSlope = 0
normalizer = 0
# Example dates: [9/1/2020, 10/1/2020, 11/1/2020, 12/1/2020,
# 1/1/2021, 2/1/2021]
for i in range(len(dates)):
# Fist of the month might not be a day that the stock market is
# open. If not then subtract one day until it is in df.index
d = dates[i]
while d not in df.index or d in self.daysClosed:
d -= td(days=1)

# Get close price
price = df.loc[d, 'Close']

# Calculate percent change
pc = (last - price) / price

# Weight increases each month
# More weight is added to recent slopes
weight = i + 1

# Add to normalizer, this will normalize the cumulative slopes.
normalizer += weight

# Add to the cumulative slope
adjSlope += pc * (weight)
self.changes.append(pc)

self.record['slope'] = round(adjSlope / normalizer, 3)

Average Percent Change

The third column avgpc is the average of the total revenue percent change and EPS percent change of the three most recent quarters (the average of six values). The average percent change gives a measure of how, on average, the revenue and EPS have increased year over year for each of the last three quarters.

Copied

I have added a copied column that will have a TRUE value if financial information (besides shares outstanding) was copied from a recent quarterly/annual report. There is often data missing using the Alpha Vantage API. This is especially true for shares outstanding. If data is missing, data from the next oldest report is copied to avoid having the user input missing data. If it is copied, there will be a TRUE value in the copied column. It is up to the user if they want to find and input any missing data. The scripts can be configured to wait for a user input when missing data is encountered. See the repository for more information.

Stock Tests

The next four columns annualeps, annualroe, quarterlyeps, and quarterlyrevenue are the tests I described in the first section of this article. These tests include annual report EPS increase over 20%, annual report ROE increase over 17%, quarterly report EPS increase over 20%, and quarterly report Sales increase over 20%. If any of these tests fail, a value of 1 will be saved. The numfailed column is simply the number of tests that failed (sum of the four test columns).

How to Find Stocks to Invest in Using Python (14)

When I analyze the *Results.csv I sort the numfailed column by ascending. The stocks that passed all the tests will have a 0 and be at the top of the table.

After I find the stocks that passed the acceptable number of tests, I begin investigating the companies. Below are some research topics covered in O’Neil’s How to Make Money in Stocks.

What does the company do?

Is the company apart of a promising industry like technology? Has the company benefitted (perhaps temporarily) from the COVID-19 pandemic?

Whether they have new products or management

Do they have new products that will increase their revenue substantially? Likewise, do they have new management? Pick entrepreneurial management rather than caretakers. Big company size often begets a lack of imagination and productive efficiency. They are often slow to change and conservatively run by older management.

If they have institutional sponsorship

It takes significant demand to push up prices, and by far the biggest source of demand for stocks is institutional investors. The stock should also have ownership by top management.

How to Find Stocks to Invest in Using Python (15)

To back up my stock finding efforts with evidence, I analyzed my employer 401(k) and the stocks I found using the process outlined in this article.

How to Find Stocks to Invest in Using Python (16)

Above you will see how the eight stocks I found by running the scripts compare to my 401(k). Each data point shows the monthly percent change in the investment account. I have also plotted the two best-performing stocks: Etsy (ETSY) and Renewable Energy Group (REGI).

The data in the figure above starts in August 2020, when I felt confident I could move my investments in bonds to stocks after the high volatility period during the spring of 2020. Even though this is not a long enough period to make definitive conclusions, it is promising that the stocks I chose outperformed my 401(k) for all months except one.

How to Find Stocks to Invest in Using Python (17)

This article touched briefly on the metrics I use to find high quality stocks. Most of the information I have provided acts as a manual for using the scripts and how to interpret the files and data. All you need to get started is a list of stock symbols and an API key from Alpha Vantage.

I have spent hours streamlining the process of finding stocks using sound research and Python. I have had success finding stocks using these scripts, and I believe in the spirit of open-source software. I hope you can have the same success too!

You can check out the repository to see all data included in this article, plus more information about running the scripts. Thank you for reading, and happy trading!

How to Find Stocks to Invest in Using Python (2024)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5775

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.