Once you have a pipeline, there are several things you can do with it:
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.
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.
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.
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"
)
min | max | mean | std | count | avg daily count | count % | |
---|---|---|---|---|---|---|---|
Percent Difference Quantile | |||||||
1 | -0.615 | 0.020 | -0.045 | 0.044 | 95,036 | 375.6 | 20.0% |
2 | -0.085 | 0.039 | -0.008 | 0.027 | 94,888 | 375.1 | 20.0% |
3 | -0.061 | 0.055 | 0.009 | 0.025 | 94,889 | 375.1 | 20.0% |
4 | -0.043 | 0.076 | 0.026 | 0.025 | 94,888 | 375.1 | 20.0% |
5 | -0.025 | 0.742 | 0.065 | 0.042 | 95,003 | 375.5 | 20.0% |
1D | 5D | 21D | |
---|---|---|---|
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.651 | 3.311 | 1.321 |
Mean Spread (bps) | -6.507 | -5.796 | -1.945 |
1D | 5D | 21D | |
---|---|---|---|
IC Mean | -0.010 | -0.021 | -0.012 |
IC Std. | 0.119 | 0.102 | 0.089 |
Risk-Adjusted IC | -0.085 | -0.210 | -0.138 |
t-stat(IC) | -1.350 | -3.342 | -2.195 |
p-value(IC) | 0.178 | 0.001 | 0.029 |
IC Skew | 0.014 | 0.305 | 0.072 |
IC Kurtosis | 0.529 | 0.553 | 0.059 |
1D | 5D | 21D | |
---|---|---|---|
Quantile 1 Mean Turnover | 0.070 | 0.307 | 0.757 |
Quantile 2 Mean Turnover | 0.163 | 0.562 | 0.787 |
Quantile 3 Mean Turnover | 0.190 | 0.614 | 0.784 |
Quantile 4 Mean Turnover | 0.163 | 0.567 | 0.787 |
Quantile 5 Mean Turnover | 0.070 | 0.310 | 0.771 |
1D | 5D | 21D | |
---|---|---|---|
Mean Factor Rank Autocorrelation | 0.99 | 0.818 | 0.05 |
1D | 5D | 21D | factor | sector | factor_quantile | ||
---|---|---|---|---|---|---|---|
date | asset | ||||||
2010-01-04 | Equity(FIBBG000C2V3D6 [A]) | 0.007403 | -0.008690 | -0.050531 | 0.017276 | Technology | 2 |
Equity(FIBBG000M7KQ09 [AAI]) | -0.007663 | -0.022989 | -0.055556 | 0.074104 | Industrials | 5 | |
Equity(FIBBG000F7RCJ1 [AAP]) | -0.002470 | 0.003953 | 0.017540 | 0.015796 | Consumer Discretionary | 2 | |
Equity(FIBBG000B9XRY4 [AAPL]) | 0.015555 | 0.005922 | -0.070573 | 0.020323 | Technology | 3 | |
Equity(FIBBG000C5QZ62 [AAV]) | 0.047546 | 0.118098 | 0.013804 | 0.082847 | Energy | 5 | |
... | ... | ... | ... | ... | ... | ... | ... |
2011-01-03 | Equity(FIBBG000BGFWQ6 [ZOLT]) | 0.037165 | 0.050130 | 0.005186 | 0.091792 | Industrials | 5 |
Equity(FIBBG000FTMSF7 [ZQK]) | 0.071006 | 0.033531 | -0.078895 | 0.074539 | Consumer Discretionary | 5 | |
Equity(FIBBG000FRFNV2 [ZRAN]) | -0.002273 | 0.040909 | 0.131818 | 0.076333 | Technology | 5 | |
Equity(FIBBG000PYX812 [ZUMZ]) | 0.010048 | -0.045776 | -0.097507 | -0.033094 | Consumer Discretionary | 1 | |
Equity(FIBBG000C3CQP1 [ZVO]) | -0.004211 | -0.072632 | -0.012105 | 0.091348 | Technology | 5 |
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