How to Get Fundamental Stock Data in Python for Free
Alpha Vantage offers a free API for enterprise-grade financial market data
Intro
This post is a solution to a problem I had, and I am sharing it with the hope it makes stock analysis more accessible for retail investors and traders. While trying to find data for analyzing stocks I ran into an issue, specifically one of cost. I was searching for APIs with fundamental data, that is the data that comes from the underlying business(revenue, earnings, cash flow) of a stock rather than the technical performance of a stock(price, volume). The common problem I ran into was that most APIs I came across gated the fundamental data behind either a paywall or the amount of data was cut down(typically 2 years or less). Not wanting to pay for data(which can get quite expensive), I was searching for an API that had fundamental data for free, which also provided a decent amount of history. Enter Alpha Vantage.
What is Alpha Vantage?
Alpha Vantage is an API that offers some of the most complete and accessible data I have seen for free. So what’s the catch you may ask? The data is limited by the amount and time between API calls. The free option at the time of writing allows for 500 calls daily and a maximum of 5 per minute. Not too bad for free. But for heavier applications, this likely won’t be sufficient, in which case they do also offer paid tiers(all with unlimited daily calls) ranging from 75 calls per minute to 1200 per minute (costing \$50 and \$250 respectively)with other options in between.
Useful Info
In this post, I will be covering the Python API but they also support a wide range of other languages and even have built-in support for Excel and Google Sheets. I’d highly recommend checking out their documentation and their tutorials if you want to learn more. Finally, I will also be utilizing this great library, alpha_vantage, which is a wrapper for the API. It simplifies the API calls and allows for the conversion of the JSON to a Pandas dataframe. This will just help cut out one more step. But now let’s dive in.
The first step is to import libraries that will be used, as well as our API key. The main libraries used will be the pandas and alpha_vantage libraries. I will just be using the fundamental data, but the library supports all of the types of data/API calls Alpha Vantage offers such as Time Series, Forex, Crypto and more. My API key is in a text file so that it isn’t visible here, but feel free to replace my line with just the string of your key. Be careful not to share this with anyone as they could then use your key leaving you unable to make API calls.
Now let’s try to call the API for the first time without the wrapper library to help us out. As you can see a JSON is returned. This looks fairly tidy, but let’s see how that compares to the alpha_vantage helper output.
Below we supply the same information as before with key and symbol for the stock we want but there are two differences. The first difference is that we call a specific method based on the data group we want, in this case, the company overview. We can also now specify the output format for the data. I will use pandas as this parameter will return the data in the form of a dataframe but also supports JSON(default) and CSV(with some exceptions). As you can see the dataframe nicely formats all of the data, similar to what one might find in a relational database or an Excel sheet. Now we can also start to see all of the different data which Alpha Vantage provides. The data I will eventually be using breaks down to these four groups, which once again you can find more information in the documentation but I’ll provide a brief description.
- Company Overview — Provides key metrics and information about a company
- Income Statement(Annual) — The annual income statement
- Balance Sheet(Annual) — The annual balance sheet data
- Cash Flow(Annual) — The annual cash flow statement
Creating a Row with Desired Columns
Now that we can easily call the data for a single company it would be really useful if we could repeat that process for multiple companies. That way we can append each row and our table will be made up of a list of companies with current data. From there we can decide what we want to do with the data, whether it’s doing analyses or training models. In order to get the data in a repeatable way, I think it would be useful to make a helper function that extracts any relevant columns from each table we want. Below I implemented a function that takes in each relevant table’s data, subsets the columns that I want(it also calculates the 3-year revenue growth rate), and adds that to a single row.
Now to test if this function works as intended. To do this I call the API on the four tables I want. I then supply that data to the function, add the returned row to the test table, and display the table. As you can see all information appears to be correct but some additional formatting may be desired in the future, such as converting the data types to floating-point or integers. But for now, this looks sufficient.
Making a Table with Multiple Companies
Now that we have a method for creating a row with the data we want, the final part should be looping through a list of companies whose data we want and combining it all into a single table. To do that I’ll import a CSV which is a list of approximately 100 companies I want to analyze.
Due to the constraints of the free API, I will end up subsetting the list to just the first 15 companies to save time, but if you wish to do more that is also possible. As a reminder, the restrictions for the free version are 500 calls per day, and 5 calls per minute. But then the rest is just implementing a for loop which iterates through each company, calculates a row for each, and appends that row to the table. After that, I sleep the loop for 65 seconds and continue to the next company.
The last task is to save and load the table so that the loop is not required in the future. This can be a checkpoint unless additional companies’ data is desired. As you can see when displaying the final table all of the companies with relevant data are displayed.
Conclusion
Hopefully, this demonstration of Alpha Vantage in python proved useful and will enable others easier access to financial data. Also this post was made using jupyter_to_medium which you should checkout if you want to publish jupyter notebooks to medium.
If you want to access the code or connect more, check out my GitHub, Twitter, and the rest of my Medium.