QuantRocket logo
Disclaimer


Zipline Intro › Part 3: Alphalens


End-of-Day Analysis with Alphalens¶

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-define our pipeline from the previous notebook:

In [1]:
from zipline.pipeline import Pipeline
from zipline.pipeline.factors import AverageDollarVolume, Returns

pipeline = Pipeline(
    columns={
        "returns": Returns(window_length=252),
    },
    screen=AverageDollarVolume(window_length=30) > 10e6
)

To see if our momentum factor is predictive of forward returns, we can use the Alphalens from_pipeline function. This function executes a pipeline for a specified date range, splits the data into quantiles based on one of the pipeline columns, and provides a tear sheet of forward returns by quantile. For a predictive factor, the higher quantiles should perform better than the lower quantiles.

In [2]:
import alphalens as al

al.from_pipeline(
    pipeline, 
    # the `factor` argument tells which Pipeline column to use to form the quantiles
    factor="returns",
    start_date="2018-01-01", 
    end_date="2020-01-01",
    # For a very small sample universe, you might only want 2 quantiles 
    quantiles=2
)
Factor Distribution
 minmaxmeanstdcountavg daily countcount %
Returns Quantile       
1-0.6190.397-0.0220.1982,0164.055.5%
2-0.1070.9630.2850.1771,6193.244.5%
Returns Analysis
 1D
Ann. alpha0.147
beta-0.104
Mean Relative Return Top Quantile (bps)0.158
Mean Relative Return Bottom Quantile (bps)-0.304
Mean Spread (bps)0.462
Information Analysis
 1D
IC Mean0.034
IC Std.0.465
Risk-Adjusted IC0.073
t-stat(IC)1.648
p-value(IC)0.100
IC Skew0.005
IC Kurtosis-0.915
Turnover Analysis
 1D
Quantile 1 Mean Turnover0.042
Quantile 2 Mean Turnover0.056
1D
Mean Factor Rank Autocorrelation0.979
Out[2]:
1Dfactorfactor_quantile
dateasset
2018-01-02Equity(FIBBG00B3T3HD3 [AA])-0.0121440.9184472
Equity(FIBBG000B9XRY4 [AAPL])-0.0001740.4846432
Equity(FIBBG000BKZB36 [HD])0.0052120.4461282
Equity(FIBBG000BMHYD1 [JNJ])0.0095530.2442521
Equity(FIBBG000BFWKC0 [MON])0.0040840.1312041
...............
2020-01-02Equity(FIBBG000BKZB36 [HD])-0.0033230.3011301
Equity(FIBBG000BMHYD1 [JNJ])-0.0115780.1740271
Equity(FIBBG000BPH459 [MSFT])-0.0124520.5825932
Equity(FIBBG000BDTBL9 [SPY])-0.0075720.3108722
Equity(FIBBG000GZQ728 [XOM])-0.0080390.0492481

3635 rows × 3 columns


Next Up¶

Part 4: Intraday Trading Rules