Notebook
In [1]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data import Fundamentals

import alphalens

#import anything else as need be
In [2]:
def make_pipeline():
    #put alpha to test here
    testingFactor = Fundamentals.peg_ratio.latest
    sectorInfo = Fundamentals.morningstar_sector_code.latest
    
    #specify any filters here into universe. Make sure alphas have .notnull() in universe.    
    universe = (testingFactor.notnull() &
                Q500US())
    
    #Use print statements to make sure the BETTER one is ranked HIGHER. Eg Rank 150 is BETTER than rank 149.
    #first do it without ranking them, so you can ensure raw numbers make sense.
    testingFactor = testingFactor.rank(method='average', 
                    ascending=False,
                    mask = universe,
                    groupby = sectorInfo)
    
    testingFactor = testingFactor.zscore(groupby = sectorInfo)

    
    #Seperate into the desired quantiles, top X%, whatever you want here.
    
    #if long and short strategy, specify which sections you would long/short. Returns a pipeline object.
    pipe = Pipeline(columns = {'testingFactor':testingFactor,
                              'sector': sectorInfo},
                   screen = universe)
    
    return pipe
In [3]:
#Makes a dataframe with the given universe, ranked by the testing factor.
results = run_pipeline(make_pipeline(), start_date = '2015-01-01', end_date = '2016-01-01')

#print the results to make sure it fuckin worked. Print once without rankings to verify.
results.head(15)
Out[3]:
sector testingFactor
2015-01-02 00:00:00+00:00 Equity(2 [ARNC]) 101 1.024544
Equity(24 [AAPL]) 311 0.780720
Equity(62 [ABT]) 206 -0.850963
Equity(67 [ADSK]) 311 -1.219875
Equity(76 [TAP]) 205 -1.311220
Equity(114 [ADBE]) 311 1.512645
Equity(122 [ADI]) 311 -0.048795
Equity(128 [ADM]) 205 0.374634
Equity(161 [AEP]) 207 0.547723
Equity(168 [AET]) 206 0.850963
Equity(185 [AFL]) 103 -0.469199
Equity(205 [AGN]) 206 1.215661
Equity(216 [HES]) 309 -1.299278
Equity(239 [AIG]) 103 -1.191043
Equity(328 [ALTR]) 311 0.927105
In [4]:
#Gets each unique stock ticker and puts it in assets
assets = results.index.levels[1].unique()

#gets pricing data. Needs a month before and after. Dunno why.
pricing = get_pricing(assets, start_date='2014-12-01', end_date='2016-02-01', fields='open_price')
In [7]:
#Look up documentation to see how to customize this. This all organizes info so alphalens can get to work.
factor_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = results['testingFactor'],  
                                                                   prices = pricing,  
                                                                   quantiles =5,
                                                                   periods = (3,10,30))

# Runs alphalens
alphalens.tears.create_full_tear_sheet(factor_data)  
Dropped 4.9% entries from factor data: 4.9% in forward returns computation and 0.0% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).
max_loss is 35.0%, not exceeded: OK!
Quantiles Statistics
min max mean std count count %
factor_quantile
1 -1.711561 -1.020836 -1.385074 0.199588 22781 20.085523
2 -1.048710 -0.325396 -0.689552 0.201419 22657 19.976195
3 -0.360879 0.365148 0.003697 0.199970 22623 19.946218
4 0.330289 1.048710 0.694475 0.199761 22650 19.970023
5 1.033312 1.711561 1.386481 0.198616 22709 20.022042
Returns Analysis
3D 10D 30D
Ann. alpha 0.008 0.011 0.004
beta 0.038 0.017 0.002
Mean Period Wise Return Top Quantile (bps) -2.643 -1.618 -3.020
Mean Period Wise Return Bottom Quantile (bps) -4.697 -4.539 -4.191
Mean Period Wise Spread (bps) 2.064 2.908 1.216
<matplotlib.figure.Figure at 0x7f684c6e7510>
Information Analysis
3D 10D 30D
IC Mean 0.010 0.010 -0.003
IC Std. 0.067 0.058 0.071
Risk-Adjusted IC 0.144 0.163 -0.045
t-stat(IC) 2.239 2.535 -0.698
p-value(IC) 0.026 0.012 0.486
IC Skew 0.177 -0.323 -0.750
IC Kurtosis -0.448 0.009 -0.523
Turnover Analysis
10D 30D 3D
Quantile 1 Mean Turnover 0.086 0.176 0.040
Quantile 2 Mean Turnover 0.183 0.335 0.091
Quantile 3 Mean Turnover 0.209 0.372 0.107
Quantile 4 Mean Turnover 0.181 0.319 0.093
Quantile 5 Mean Turnover 0.095 0.184 0.045
3D 10D 30D
Mean Factor Rank Autocorrelation 0.989 0.969 0.912