Welcome to part 4 of my Quantopian tutorial! In this tutorial we will go over how to use fundamentals data!
This tutorial is going to cover how to select a dynamic portfolio from fundamentals data. This means that we will be selecting securities based upon a property like market cap rather than just hardcoding a list at the beginning of the algorithm. Dynamic portfolio selection leads to more robust algorithms as it reduces the chances of over-fitting your strategy to a particular backtest period. We can use the get_fundamentals function to access the skeleton of a fundamentals query featured below.
import numpy as np
# This function is run once at the beginning of the algorithm, REQUIRED
def initialize(context):
# AAPL stock
# context.stock = sid(24)
# SPY stock
# context.market = sid(8554)
schedule_function(check_mean, date_rules.every_day(), time_rules.market_close(minutes=30))
# This function is run once per day before any calls to handle_data, OPTIONAL
def before_trading_start(context, data):
get_fundamentals(
#Fundamentals Reference Page: https://www.quantopian.com/help/fundamentals
context.fundamental_df = get_fundamentals(
query(
fundamentals.income_statement.total_revenue
)
.filter(
fundamentals.valuation.market_cap > 30000000000
)
.order_by(
fundamentals.valuation.market_cap.desc()
)
.limit(10)
)
update_universe(context.fundamental_df.columns.values)
Ok, so let's explain what all this code is above us. First we have context.fundamental_df = fundamentals(query(, what this does is open up the fundamentals skeleton that will allow us to choose our criteria and values per category. An example of this would be the .fundamentals.valuation.market_cap > 3000000000 which tells the algorithm "hey look, I only want to include companies with a market cap of greater than 300 billion dollars!"
The query( section at the beginning of our algorithm on the second line above, tells the algorithm what data we actually want to access. As you can see, we are telling our algorithm to look at the income statement of the company to determine whether or not it is a 300 billion dollar company!
The .order_by( section will allow us to sort the results of our query, and in this situation we are going to sort them by market-cap in descending order.
And finally, our .limit(10) will limit the number of securities returned, in this case we are limiting the securities to 10.
Also, we are going to store our results (information) in a dataframe denoted by the df listed above and we are giving it the name context.fundamental. The results will be a pandas dataframe where the columns are securities and the rows are the fundamental values that we queried for.
update_universe(context.fundamental_df.columns.values) , this will update our securities universe by adding in our information that we queried above, notice how the context.fundamental_df is the name we gave to our query for market cap info? It is indeed the same!
# This function is run once per bar, REQUIRED
def handle_data(context, data):
pass
def check_mean(context, data):
print get_datetime('US/Eastern')
# Get the close price of each of the last 10 minutes for all stocks
# in our universe.
hist = history(10, '1m', 'close_price')
# Print the 10-minute mean close price for each stock in our universe.
for stock in data:
print '%s - mean price: %.2f, total revenue: %.2f' % (stock.symbol, np.mean(hist[stock]), context.fundamental_df[stock][0])
print '-------------------------------'
print ''
The print '%s - mean price: %.2f, total revenue: %.2f' % (stock.symbol, np.mean(hist[stock]) function will go through and check the mean of the information we have gathered, and then it will print it for us so we can look through it. Additionally, it will print our total revenue through the use of the context.fundamental_df[stock][0] function.
And that's it! You have now mastered the ability to build your own algorithm! Next lesson, we will move into more complex and powerful functions that will allow us to check a greater amount of information using mathematical tools!
No comments:
Post a Comment