Alphalens is an open-source performance analysis library which pairs well with the Pipeline API. In this notebook we will use Alphalens to analyze whether our momentum factor is predictive of forward returns.
Using Alphalens makes sense when you believe your end-of-day Pipeline rules have alpha. In contrast, if your Pipeline rules simply perform a basic screen and the alpha is entirely provided by your intraday trading rules, it might make more sense to omit this step.
Let's re-run our pipeline from the previous notebook:
from zipline.pipeline import Pipeline
from zipline.pipeline.factors import AverageDollarVolume, Returns
from zipline.research import run_pipeline
pipeline = Pipeline(
columns={
"1y_returns": Returns(window_length=252),
},
screen=AverageDollarVolume(window_length=30) > 10e6
)
factors = run_pipeline(pipeline, start_date="2017-01-01", end_date="2019-01-01")
To see if our momentum factor is predictive of forward returns, we use the factor data to request forward returns for the corresponding assets and dates, then format the factor and returns data for use with Alphalens:
from zipline.research import get_forward_returns
import alphalens as al
# Get forward returns (this provides forward 1-day returns by default)
forward_returns = get_forward_returns(factors)
# format the data for Alphalens
al_data = al.utils.get_clean_factor(
factors["1y_returns"],
forward_returns,
quantiles=2 # For a very small sample universe, you might only want 2 quantiles
)
Then we create a tear sheet to look at the factor. For a predictive factor, the higher quantiles should perform better than the lower quantiles.
from alphalens.tears import create_full_tear_sheet
create_full_tear_sheet(al_data)