Fundamental Analysis is a program that allows me to screen stocks using fundamental indicators and estimate the intrinsic value of qualified stocks using the Discounted Cash Flow method of valuation.
The logic accomplishes 5 primary tasks:
- Downloads all stock tickers available via the FinancialModelingPrep API and generates profiles on each company
- Retrieves and cleans financial statement data on each company
- Screens companies on any chosen indicators of value (ex: 8-Year Median ROE)
- Plots the stability of core indicators over an N-year period
- Includes: EPS, Dividend Per Share, Book Value per Share, ROE, Current Ratio, Debt to Equity Ratio
- Estimates the intrinsic value, margin of safety value for chosen companies
I've listed the available functions separated by module below for anyone that wishes to build upon this logic.
Notes:
- The FinancialModelingPrep API appears to be updated frequently. As a result, changes in data availability / functionality may occur.
- It is not necessary to use the functions prefixed with the word 'calculate' in the company_fundamentals module in order to calculate the intrinsic value of a company. You can get a given company's discounted cash flow directly from the FinancialModeling Prep API here. I simply opted to build the DCF logic myself in order to better understand how it works. It was not a great programming choice as errors will result if column names change.
- FinancialModelingPrep also offers a tutorial on the discounted cash flow methodology here.
- company_profiles
get_company_data
- Retrieves 'symbol', 'name', 'price', and 'exchange' information for all available stock tickersselect_stock_exchanges
- Filters out stock tickers that are not listed on one of the major exchanges: 'Nasdaq Global Select', 'NasdaqGS', 'Nasdaq', 'New York Stock Exchange', 'NYSE', 'NYSE American'select_minimum_price
- Filters out stock tickers that have a price less than the one specified by the usercreate_company_profile
- Retrieves additional company information for each stock ticker provided, writes data to csv file, stores file in the specified directory
- company_financials
select_sector
- Filters out stock tickers that do not belong to the specified sectorsselect_industries
- Filters out stock tickers that do not belong to the specified industriesget_financial_data
- Retrieves any of the following financial datasets for each of the stock tickers provided: Financial Statements, Financial Ratios, Financial Growth, Key Company Metrics, Enterprise Valueclean_financial_data
- Scans DataFrame containing financial data on N stock stickers and removes rows with corrupted date values. Adds a new 'year' column as well for future useselect_analysis_years
- Remove stock tickers without financial reports in a specified timeframe (ex: L10Y) and subset DataFrame to only include the years specified (ex: 2015 - 2020)
- company_fundamentals
combine_data
- Read and join all files in a specified directory (ex: 'data/') with a specified year pattern (ex: '2Y')calculate_stats
- Calculate the mean, median, or percent change of provided columns over an N-year periodscreen_stocks
- Filter out stock tickers with column values that do not fall within specified rangesplot_performance
- Plot stock performance over time with respect to the following column values: 'Earnings per Share', 'Dividend per Share', 'Book Value per Share', 'Return on Equity', 'Current Ratio', 'Debt to Equity Ratio'. Note that 'Dividend per Share' is commented out as the column name seems to have disappeared in a recent API updateprepare_valuation_inputs
- Subset DataFrame to include only the data required for Discounted Cash Flow model calculationscalculate_discount_rate
- Calculated the Weighted Average Cost of Capital (WACC) for each stock ticker providedcalculate_discounted_free_cash_flow
- Calculate the present value of discounted future cash flows for each stock ticker providedcalculate_terminal_value
- Calculate the terminal value for each stock ticker for each stock ticker providedcalculate_intrinsic_value
- Calculate the intrinsic value of each stock ticker providedcalculate_margin_of_safety
- Calculate the margin of safety value of each stock ticker provided
- Data Sources: All stock data is pulled from the FinancialModelingPrep API
- Graphs: The "Stability Graph" concept was taken from the great folks at www.buffettsbooks.com. They explain why this concept is so important here
- Calculations: Guides to the Discounted Cash Flow method of valuation can be found in many places across the internet. Personally, I used the Advanced Value Investing Udemy course