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.
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.
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.
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.
Let's get the FY calculations.
Now we have the id of the calculations filing for 2018. Now it's time to grab the data!
Now let's loop through the api_response to get the P/B using the tag pricetobook.
Congratulations! You just grabbed your first fundamental value!
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.
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.
Let's update our dataframe only to contain our adjusted values in OHLCV + BV format.
Writing the data from a pandas dataframe to a CSV file is easy.
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.