QuantRocket logo
Disclaimer


Pipeline Tutorial › Lesson 13: Using Pipeline with Alphalens


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.

In [1]:
from zipline.pipeline import Pipeline, EquityPricing, master
from zipline.pipeline.factors import SimpleMovingAverage
# import our TradableStocksUS function
from codeload.pipeline_tutorial.tradable_stocks import TradableStocksUS

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 us to view some additional Alphalens plots breaking down the factor's performance by sector.

In [2]:
def make_pipeline():
    
    universe = TradableStocksUS()
    
    # 10-day close price average.
    mean_10 = SimpleMovingAverage(inputs=EquityPricing.close, window_length=10, mask=universe)

    # 30-day close price average.
    mean_30 = SimpleMovingAverage(inputs=EquityPricing.close, window_length=30, mask=universe)

    # Percent difference factor.
    percent_difference = (mean_10 - mean_30) / mean_30
        
    return Pipeline(
        columns={
            'percent_difference': percent_difference,
            'sector': master.SecuritiesMaster.usstock_Sector.latest
        },
        screen=universe
    )

Instead of executing the pipeline with run_pipeline, we pass the pipeline to the Alphalens from_pipeline function. This function executes the pipeline, splits the data into quantiles based on a specified column in the pipeline output (in this example, the percent_difference column), and plots the relative forward performance of each quantile. In this example we analyze returns for the next day, next week (5 trading days), and next month (approx. 21 trading days), using the periods argument. We also use the groupby argument to tell Alphalens to group by the sector column in the pipeline output, which provides additional plots showing performance by sector.

In the resulting tear sheet, the "Mean Relative Return By Factor Quantile" plot reveals that forward returns generally increase from quantiles 1 to 5. Intuitively, this tells us that stocks tend to perform better when their 10-day moving average is above their 30-day moving average.

In [3]:
import alphalens as al

al.from_pipeline(
    make_pipeline(), 
    start_date='2015-01-01', 
    end_date='2016-01-01',
    factor="percent_difference",
    # analyze forward returns for the next day, next week, and next month
    periods=[1,5,21],
    groupby="sector"
)
Factor Distribution
 minmaxmeanstdcountavg daily countcount %
Factor Quantile       
1-0.0830.029-0.0260.0205062.028.6%
2-0.0500.038-0.0060.0162531.014.3%
3-0.0430.042-0.0000.0172531.014.3%
4-0.0410.0450.0070.0172531.014.3%
5-0.0350.1010.0240.0255062.028.6%
Long/Short Returns Analysis
 1D5D21D
Ann. alpha0.1150.1110.005
beta-0.068-0.0760.163
Mean Relative Return Top Quantile (bps)5.8154.7612.527
Mean Relative Return Bottom Quantile (bps)-10.708-7.6851.925
Mean Spread (bps)16.52312.5000.799
Information Analysis
 1D5D21D
IC Mean0.0500.0650.002
IC Std.0.4230.4040.362
Risk-Adjusted IC0.1180.1610.005
t-stat(IC)1.8732.5600.081
p-value(IC)0.0620.0110.936
IC Skew-0.0790.061-0.091
IC Kurtosis-0.573-0.558-0.275
Turnover Analysis
 1D5D21D
Quantile 1 Mean Turnover0.0600.2300.700
Quantile 2 Mean Turnover0.2740.6610.953
Quantile 3 Mean Turnover0.2620.6210.780
Quantile 4 Mean Turnover0.2420.6450.832
Quantile 5 Mean Turnover0.0650.2620.685
1D5D21D
Mean Factor Rank Autocorrelation0.9680.7830.072

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