QuantRocket logo

Disclaimer

Using Pipeline with Alphalens

Once you have a pipeline, there are two main things you can do with it:

  1. Feed the pipeline results to Alphalens to analyze whether your factors are predictive of forward returns;
  2. Use the pipeline in a Zipline strategy for the purpose of universe selection and/or trading signal generation.

Analyzing your pipeline output with Alphalens makes sense if your pipeline includes factors that might be predictive of forward returns; it doesn't make sense if you are only using your pipeline for universe selection.

In this notebook, we will create a pipeline with a moving average factor for the TradableStocksUS universe, then use Alphalens to analyze whether the factor is predictive.

Let's create a factor that measures the percent difference between the 10-day and 30-day moving averages (close price). In other words, we are computing the degree to which the 10-day moving average is above the 30-day moving average.

As an added benefit, we will include each stock's sector in our pipeline output, which will allow use to view some additional Alphalens plots breaking down the factor's performance by sector.

Running this pipeline will result in a DataFrame containing 2 columns, percent_difference, our predictive factor, and sector.

To see if our factor is predictive of forward returns, we use the factor data to request forward returns for the assets and dates in our pipeline output. In this example we request returns for the next day, next week (5 trading days), and next month (approx. 21 trading days).

Then, we format the factor and returns data for use with Alphalens, passing in our sectors as the grouping key (grouping is optional):

Finally, we create the Alphalens tear sheet.

In this example, the "Mean Period Wise Return By Factor Quantile" plot reveals that forward returns increase from quantiles 1 to 4, but then drop off in quantile 5. Intuitively, this tells us that stocks tend to perform better when their 10-day moving average is above their 30-day moving average, up to a point. But if the 10-day moving average gets too far stretched above the 30-day moving average, the forward returns are poor.

For more information on interpreting an Alphalens tear sheet, please see Lecture 38 of the Quant Finance Lecture series in the Code Library.


Back to Introduction