When it comes to algorithmic trading, we can also include fundamental data on companies into our algorithm. This is using things like the PE ratio, debt to equity, and a bunch of others. Luckily for us, Quantopian has a data provider for fundamental data built right in, MorningStar. There's data on over 8,000 companies and over 600 metrics per company, so there should be everything you need here.
Why might we want to use fundamental data over more quant-like strategies like moving averages or Moving Average Convergence Divergence? Fundamental data is far more likely to be a leading indicator of pricing information. We can look at purely quant-measures to get an idea about the current state of the market, but it's fairly futile to use these basic metrics to predict future results, certainly out over a long term.
You may get away in the short term by using purely quant types of data, but the long term is almost always based on the fundamentals of a company, like revenue, earnings, growth, and so on. This tutorial is going to be focused on showing you how to access the company fundamentals from Quantopian.
Generally, given the massive size of the fundamentals database, you are most likely to use some sort of filtration on fundamentals. Say you're looking for companies with a PE ratio less than 12. There might be thousands of these, and Quantopian puts limits on how much you can pull at one time into your universe (255 companies at the moment). So we end up writing an SQLAlchemy-like query to pull from the fundamentals database. First, let's write our
def initialize(context): context.limit = 10
Put what you like in this context.limit. This will be used to limit the number of companies returned from the fundamentals query. Because you may have more results than you limit to, you will want to remember to sort the data, using order_by. We will sort by market cap descending, so we get the top 10 largest companies that meet our criteria.
Now, we include a new method
before_trading_start, which is run before each day of trading. Fundamentals change, so we definitely want something to run in a somewhat scheduled fashion.
before_trading_start is built into Quantopian so it will automatically be handled. Later on, I will be showing you all how we can schedule functions to be run as well, with even more customization.
def before_trading_start(context): context.fundamentals = get_fundamentals( query( fundamentals.valuation_ratios.pb_ratio, fundamentals.valuation_ratios.pe_ratio, ) .filter( # put your filters here ) .order_by( # sort your query ) .limit(context.limit) )
So here's a query. Usually, if you start typing
get_fundamentals and use the tab to complete or click on what you want, the framework of the query should pop in for you, but you may have to write it yourself anyway.
First, our query contains just a general statement, we want to collect the pb_ratio and pe_ratio for companies. That's our query, and in theory this would return over 8000 companies and their ratios. We limit this by then using
.filter, which then limits based on our logic, which we will work on in the next tutorial.
There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.