New to the Quant Quickstart series? Read parts one, two, and three.
Hello world, it's Leo Smigel. I'm back again discussing my favorite python trading framework. In this post, we're going to add fundamentals to our price data. Before we dive in, let's make sure we're clear on what fundamentals are.
Fundamentals
The SEC requires publicly traded companies to file annual and quarterly reports on an ongoing basis. These reports detail the financial results of operations, the financial position, and the cash flow of the organization. If you're interested, you can learn more about how to read financial statements. From these financial statements, we can calculate various metrics and ratios that can provide insight into the nature of the company or how it's performing. Thankfully, you don't have to import the financial statements and calculate these for yourself -- Intrinio has already done that for us.
Financial Calculations & Metrics
Intrinio provides us with the needed calculations and metrics. For this post, let's add the classic price-to-book ratio (P/B) ratio. We'll start by exploring the API through the REPL.
Exploring the Intrinio Fundamentals API
Start by activating the environment we created in part one, importing what we need, and grabbing both the CompanyApi and FundamentalsApi.
Now let's use the CompanyApi to get the 2018 filings for Apple. We'll add statement_code='calculations' to filter our data to receive only the calculations.
Now let's loop through the api_response to get the P/B using the tag pricetobook.
pricetobook = None
for calc in api_response.standardized_financials_dict:
if calc['data_tag']['tag'] == 'pricetobook':
pricetobook = calc['value']
print(pricetobook)
10.4128
Congratulations! You just grabbed your first fundamental value!
Adding Fundamentals to Data Feed
While there are many ways you can store the data, I prefer to add fundamental and alternative data directly to the data feed I'm working with. If you remember from part one, we created a CSV file with the OHLCV fields. We'll append pricetobook to the end of these values. We'll need to loop through each page and add that to our prices dictionary.
As you progress on your journey, you'll want to be able to slice and dice data to observe relationships and build various trading indicators. For this, you'll need Pandas.
pip install pandas
Pandas was developed at hedge fund AQR by Wes McKinney to enable quick analysis of financial data. Pandas is an extension of NumPy that supports vectorized operations, enabling fast manipulation of financial information. While out of scope for these posts, I would consider Pandas required learning for systematic and algorithmic trading. You can learn more by reading How to Manipulate Data with Pandas and Python.
With that out of the way, let's append our price to book to our OHLC values. First, we'll import pandas and read in our list of dictionaries to a pandas dataframe.
Writing the data from a pandas dataframe to a CSV file is easy.
df.to_csv('aapl.csv')
You've now learned to add Intrinio calculations and fundamentals to your data. Before next month's post, try adding this and other fundamental data into Backtrader.
New to Intrinio? Visit intrinio.com to learn more.