QuantRocket logo
Disclaimer


Pipeline Tutorial › Lesson 14: Using Pipeline with Alphalens


Using Pipeline with Alphalens¶

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

  1. Feed the pipeline results to Alphalens to analyze whether your factors are predictive of forward returns (this lesson);
  2. Explore the pipeline results in the Data Browser (the next lesson); or
  3. 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 InitialUniverse and TradableStocksUS functions
from codeload.pipeline_tutorial.tradable_stocks import InitialUniverse, 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():

    initial_universe = InitialUniverse()
    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
        },
        initial_universe=initial_universe,
        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='2010-01-01', 
    end_date='2011-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 %
Percent Difference Quantile       
1-0.6150.020-0.0450.04495,036375.620.0%
2-0.0850.039-0.0080.02794,888375.120.0%
3-0.0610.0550.0090.02594,889375.120.0%
4-0.0430.0760.0260.02594,888375.120.0%
5-0.0250.7420.0650.04295,003375.520.0%
Returns Analysis
 1D5D21D
Ann. alpha-0.063-0.060-0.035
beta-0.029-0.035-0.026
Mean Relative Return Top Quantile (bps)-2.856-2.493-0.629
Mean Relative Return Bottom Quantile (bps)3.6513.3111.321
Mean Spread (bps)-6.507-5.796-1.945
Information Analysis
 1D5D21D
IC Mean-0.010-0.021-0.012
IC Std.0.1190.1020.089
Risk-Adjusted IC-0.085-0.210-0.138
t-stat(IC)-1.350-3.342-2.195
p-value(IC)0.1780.0010.029
IC Skew0.0140.3050.072
IC Kurtosis0.5290.5530.059
Turnover Analysis
 1D5D21D
Quantile 1 Mean Turnover0.0700.3070.757
Quantile 2 Mean Turnover0.1630.5620.787
Quantile 3 Mean Turnover0.1900.6140.784
Quantile 4 Mean Turnover0.1630.5670.787
Quantile 5 Mean Turnover0.0700.3100.771
1D5D21D
Mean Factor Rank Autocorrelation0.990.8180.05
Out[3]:
1D5D21Dfactorsectorfactor_quantile
dateasset
2010-01-04Equity(FIBBG000C2V3D6 [A])0.007403-0.008690-0.0505310.017276Technology2
Equity(FIBBG000M7KQ09 [AAI])-0.007663-0.022989-0.0555560.074104Industrials5
Equity(FIBBG000F7RCJ1 [AAP])-0.0024700.0039530.0175400.015796Consumer Discretionary2
Equity(FIBBG000B9XRY4 [AAPL])0.0155550.005922-0.0705730.020323Technology3
Equity(FIBBG000C5QZ62 [AAV])0.0475460.1180980.0138040.082847Energy5
........................
2011-01-03Equity(FIBBG000BGFWQ6 [ZOLT])0.0371650.0501300.0051860.091792Industrials5
Equity(FIBBG000FTMSF7 [ZQK])0.0710060.033531-0.0788950.074539Consumer Discretionary5
Equity(FIBBG000FRFNV2 [ZRAN])-0.0022730.0409090.1318180.076333Technology5
Equity(FIBBG000PYX812 [ZUMZ])0.010048-0.045776-0.097507-0.033094Consumer Discretionary1
Equity(FIBBG000C3CQP1 [ZVO])-0.004211-0.072632-0.0121050.091348Technology5

474704 rows × 6 columns

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


Next Lesson: Browsing Pipeline Output in the Data Browser