DOWNLOAD FUNDAMENTALS DATA
05 May 2020
Last updated
05 May 2020
Last updated
In this post we will explore how to download fundamentals data with Python. Weâll be extracting fundamentals data from Yahoo Finance using the yahoo_fin package. For more on yahoo_fin, including installation instructions, check out its full documentation here.
Now, letâs import the stock_info module from yahoo_fin. This will provide us with the functionality we need to scrape fundamentals data from Yahoo Finance. Weâll also import the pandas package as weâll be using that later to work with data frames.
Next, weâll dive into getting common company metrics, starting with P/E ratios.
Thereâs a couple ways to get the current P/E ratio for a company. First, we can use the get_quote_table method, which will extract the data found on the summary page of a stock (see here).
Next, letâs pull the P/E ratio from the dictionary that is returned.
A companyâs P/E ratio can also be extracted from the get_stats_valuation method. Running this method returns a data frame of the âValuation Measuresâ on the statistics tab for a stock.
Next, letâs extract the P/E ratio.
Another popular metric is the P/S ratio. We can get the P/S ratio, along with several other other metrics, using the same get_stats_valuation method. Letâs use the object we pulled above, currently stored as val.
Then, we can get the Price/Sales ratio like below.
Now, letâs get the Price-to-Earnings and Price-to-Sales ratios for each stock in the Dow. We could also do this for a custom list of tickers as well.
The P/E ratio for each stock can be obtained in a single line:
After the above code, we can get the Price / Sales ratios for each stock like below.
Similarly, we can get the Price-to-Book ratio for every stock in our list below.
Next, letâs get the PEG (Price / Earnings-to-Growth ratio).
We can get forward P/E ratios like this:
In addition to the âValuation Measuresâ table on the stats tab, we can also scrape the remaining data points on the webpage using the get_stats method. Calling this method lets us extract metrics like Return on Equity (ROE), Return on Assets, profit margin, etc. Click here to see the webpage for Apple.
Similar to above, we can get this information for each stock in the Dow.
Using the result data frame, combined_extra_stats, letâs get Return on Equity for each stock in our list.
A simple tweak gives us Return on Assets for each stock.
To get profit margin, we just need to adjust our filter like below.
We can extract balance sheets from Yahoo Finance using the get_balance_sheet method. Using the data frame that is returned, we can get several attributes about the stockâs financials, including total cash on hand, assets, liabilities, stockholdersâ equity, etc.
We can see the âTotal Cashâ row in the balance sheet by filtering for âcashâ. This will give us the total cash value for the last several years.
Next, we can also get Total Stockholdersâ Equity.
Now, letâs get Total Assets.
Like with the company statistics tables we pulled earlier, we can also download the balance sheet for all the stocks in the Dow (or again, a custom list of your choice).
From here, we could then look at values from the balance sheets across multiple companies at once. For example, the code below combines the balance sheets from each stock in the Dow. Since each individual balance sheet may have different column headers (from different dates), weâll just get the most recent column of data from the balance sheet for each stock.
Now we have a data frame containing the balance sheet information for each stock in our list. For example, we can look at the Total Assets for each Dow stock like this:
Next, letâs examine income statements. Income statements can be downloaded from Yahoo Finance using the get_income_statement method. See an example income statement here.
Using the income statement, we can examine specific values, such as total revenue, gross profit, total expenses, etc.
To get the total revenue, we just need to apply a filter like previously.
Similarly, we can get the gross profit:
Next, letâs pull the income statement for each Dow stock.
Now, we can look at metrics in the income statement across multiple companies at once. First, we just need to combine the income statements together, similar to how we combined the balance sheets above.
Now that we have a combined view of the income statements across stocks, we can examine specific values in the income statements, such as Total Revenue, for example.
In this section, weâll extract cash flow statements. We can do that using the get_cash_flow method.
Hereâs the first few rows of the cash flow statement:
Now letâs get the cash flow statements of each Dow stock.
Again, we combine the datasets above, using similar code as before.
Now, we can examine information in the cash flow statements across all the stocks in our list.
One example to look at in a cash flow statement is the amount of dividends paid, which we can see across the companies in our list by using the filter below.
Hereâs another example â this time, weâll look at debt-related numbers across the cash flow statements.
Thatâs it for this post! Learn more about web scraping by checking out this online course on Udemy that I co-created with 365 Data Science! Youâll learn all about scraping data from different sources, downloading files programmatically, working with APIs, scraping JavaScript-rendered content, and more! Check it out here!
Reference : http://theautomatic.net/2020/05/05/how-to-download-fundamentals-data-with-python/