I Built a Machine Learning Model to Trade Stocks Like Warren Buffett (Part 1)
Use Machine Learning to Analyze a Stock’s Fundamentals
Click below for Part 2:
When it comes to trading in the Stock Market, there are many different approaches to find the right stock. Many forms of analysis have emerged to detect which stock is worth the money. Technical Analysis — determines price patterns over time in hopes that these patterns will emerge again. Sentimental Analysis — is based on the public’s general feeling or attitude towards a specific stock. If they feel negative, then the stock will tank. If they feel positive, then the sky’s the limit. Fundamental Analysis — observes a company’s financials like their Balance Sheet or Cash Flow Statement to determine if the company has value relative to their current stock price. These are not the only forms of stock analysis out there but they are arguably the most popular.
Warren Buffett, considered to be one of the most successful investors in the world, is a big proponent of Fundamental Analysis. Now if one of the most successful investors in the world uses this strategy, then we should learn this strategy for ourselves. But not all of us have patience and time to learn Fundamental Analysis. So what if we can get a machine to do the learning for us?
Classification in Machine Learning
This is where Machine Learning comes into play. We are going to be implementing a machine learning classifier to determine whether a stock is a Buy, Sell, or a Hold. In order to determine whether a stock or company falls into one of these three categories or classes, we will be taking a look at each company’s quarterly report. These quarterly reports contain the necessary financial information we require to train our machine learning classifier in the ways of Fundamental Analysis.
The Fundamental Data — Quarterly Reports
A large amount of quarterly reports will need to be collected in order to train our classifier model. Stockpup.com contains the quarterly report history from numerous companies and has made them available to download in convenient CSV or Excel files. (Note: Stockpup.com is no longer active.) But, how many companies’ quarterly report history should you download? As many as you can, if not all quarterly reports on the site. Some companies listed on the website no longer exist but that doesn’t mean that their quarterly report history is useless. We can still use that data when training our classifier.
Since Stockpup is no longer active, I suggest going with EOD Historical Data. This site contains an API that can access the necessary fundamental data to train our classifier along with other stock market data as well. It is free to sign up and try out! Disclosure: I earn a small commission from any purchases made through the link above.
Examining the Quarterly Reports
Once we have downloaded the necessary fundamental data (quarterly reports), we can take a look at how the data is formatted below displayed using a Pandas DataFrame:

Here we can observe all the different columns and dates corresponding to each column. Now let’s take a look at the Price, Price High, and Price Low columns. This information is usually not provided in quarterly reports but Stockpup.com is kind enough to supply this, which is important for us in determining if the stock is a Buy, Hold, or Sell.
Now, there are various and unique ways to determine whether a stock is worth investing in or not. Could we potentially classify it as a Buy if the Assets increased and Liabilities decreased over the course of three quarters? Or maybe if the Shares increased and Long-term debt decreased? Anyways, we can see there are numerous options to determine the stock’s class. Simply put, this is the basis of Fundamental Analysis.
Performing our own Fundamental Analysis
How do we fundamentally analyze these quarterly reports without knowing Fundamental Analysis? Since we are not Fundamental experts like Mr. Buffett, let’s try to simplify it with our own method of fundamental analysis:
- Based on a quarterly report from any chosen quarter, observe how much the values changed from the previous report to the currently selected one.
- Then, observe the price values from the next quarterly report to see if there were any significant jumps in price.
- Finally, with the present report containing the changes from the past report and the future report’s price behavior, determine if it is a Buy, Hold, or Sell.
Essentially, we are detecting if fundamental changes from the previous to the current quarter affect future prices. We will be judging the performance of each quarterly report relative to its last report and then observing the future’s price behavior. See the diagram below for clarification:

We will be applying this method of analysis to each quarterly report to create our new fundamental data. This method will classify if a stock is worth investing in for the quarter. Evidently, we won’t be able to use this method on the very first QR or on the most recent QR because the nature of the analysis requires a past and future quarterly report.
Cleaning and Formatting the Data
Creating our Class Labels
To categorize or classify each quarterly report, we will keep things uncomplicated. If the price went up a significant amount in the next quarter, then it’s a Buy. If down, then it’s a Sell. If neither, then a Hold.
Here are our specific class requirements for each observed quarterly report:
- Buy — Price high and Price low increase by 3% or more in the next quarter.
- Sell — Price high and Price low decrease by -3% or more in the next quarter.
- Hold — If neither happens.
There are other possible outcomes we could consider but for the sake of simplicity, let’s keep these conditions as they are. We won’t know the next quarter’s Price high and Price low for the most recent quarterly report because that would be knowing the future, which is impossible.
Fundamental Data from Quarterly Reports
For our fundamental data (Shares, Assets, etc.), like mentioned before, we will be observing two QRs to create new values. The changes from the previous QR to the current or present QR will be measured as a percent change instead of their actual value. For example:
Let’s say in the previous quarter, Shares show a value of 1,000. Then in the current quarter, Shares now show a value of 1,100; an increase of 10%. Now, we replace the Share value of 1,100 with 10% for our current QR.
We do this process for every QR (excluding the very first one because we can’t compare it to something that doesn’t exist). Now every QR has percent change for each Fundamental value.
Coding out our Fundamental Analysis
Now that we have the QRs formatted with percent changes and labeled as Buy, Hold, or Sell, we can move on to showing the programming process:
Importing Libraries and Dataset










