In the previous lesson, you learned what Alphalens is. In this lesson, you will learn a four step process for how to use it:
get_pricing()
.get_clean_factor_and_forward_returns()
.create_full_tear_sheet()
.
In [ ]:
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import factset
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import QTradableStocksUS
def make_pipeline():
# Measures a company's asset growth rate.
asset_growth = factset.Fundamentals.assets_gr_qf.latest
return Pipeline(
columns = {'Asset Growth': asset_growth},
screen = QTradableStocksUS() & asset_growth.notnull()
)
pipeline_output = run_pipeline(pipeline=make_pipeline(), start_date='2014-1-1', end_date='2016-1-1')
# Show the first 5 rows of factor_data
pipeline_output.head(5)
Now that we have factor data, let's get pricing data for the same time period. get_pricing()
returns pricing data for a list of assets over a specified time period. It requires four arguments:
Execute the following cell to get pricing data.
In [ ]:
pricing_data = get_pricing(
symbols=pipeline_output.index.levels[1], # Finds all assets that appear at least once in "factor_data"
start_date='2014-1-1',
end_date='2016-2-1', # must be after run_pipeline()'s end date. Explained more in lesson 4
fields='open_price' # Generally, you should use open pricing.
)
# Show the first 5 rows of pricing_data
pricing_data.head(5)
get_clean_factor_and_forward_returns()
aligns the factor data created by run_pipeline()
with the pricing data created by get_pricing()
, and returns an object suitable for analysis with Alphalens' charting functions. It requires two arguments:
run_pipeline()
.get_pricing()
.Execute the following cell to align the factor data with the pricing data.
In [ ]:
from alphalens.utils import get_clean_factor_and_forward_returns
factor_data = get_clean_factor_and_forward_returns(
factor=pipeline_output,
prices=pricing_data
)
# Show the first 5 rows of merged_data
factor_data.head(5)
In [ ]:
from alphalens.tears import create_full_tear_sheet
create_full_tear_sheet(factor_data)
In [ ]: