Notebook
In [1]:
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.factors import VWAP
from quantopian.pipeline.filters import Q1500US
import numpy as np
import pandas as pd
import scipy
import math
In [2]:
import alphalens
In [3]:
class ROE(CustomFactor):   
    inputs = [morningstar.operation_ratios.roe] 
    window_length = 1
    def compute(self, today, assets, out, close):        
        out[:] = close[-1]        

class Alpha41(CustomFactor):   
    inputs = [USEquityPricing.low, USEquityPricing.high] 
    window_length = 1
    def compute(self, today, assets, out, low, high):        
        out[:] = high[0]*low[0]
In [4]:
def make_pipeline():
    
    universe = Q1500US()
    
    alpha41 = Alpha41(mask=universe)
    vwap = VWAP(window_length=1, mask=universe)
    
    alpha41 = alpha41**.5 - vwap
    
    roe =  ROE(mask=universe)
    
    my_screen = roe > .005
    
    return Pipeline(columns={"alpha41": alpha41}, screen=my_screen)
In [5]:
pipe = make_pipeline()
In [6]:
start = pd.Timestamp("2015-01-01") # Can't choose a too long time-period or we run out of RAM
end = pd.Timestamp("2016-03-01")
results = run_pipeline(pipe, start_date=start, end_date=end)
In [7]:
results['alpha41'].head()
Out[7]:
2015-01-02 00:00:00+00:00  Equity(2 [AA])        0.099469
                           Equity(24 [AAPL])     1.230455
                           Equity(41 [ARCB])     0.167920
                           Equity(62 [ABT])      0.496914
                           Equity(114 [ADBE])    0.735857
Name: alpha41, dtype: float64
In [8]:
assets = results.index.levels[1]
pricing = get_pricing(assets, start, end + pd.Timedelta(days=30), fields="open_price")
In [9]:
alphalens.tears.create_factor_tear_sheet(results['alpha41'], pricing)
Returns Analysis
1 5 10
Ann. alpha 0.077 0.025 0.019
beta 0.069 0.060 0.023
Mean Daily Return Top Quantile (bps) 2.609 1.416 0.982
Mean Daily Return Bottom Quantile (bps) -2.428 -1.190 -0.603
Mean Daily Spread (bps) 5.020 2.610 1.573
Information Analysis
1 5 10
IC Mean 0.011 0.010 0.008
IC Std. 0.091 0.089 0.087
t-stat(IC) 2.046 1.951 1.567
p-value(IC) 0.042 0.052 0.118
IC Skew -0.082 -0.296 -0.164
IC Kurtosis 0.305 0.397 0.567
Ann. IR 1.901 1.813 1.455
Turnover Analysis
Top Quantile Bottom Quantile
Mean Turnover 0.746 0.728
Mean Factor Rank Autocorrelation -0.004
<matplotlib.figure.Figure at 0x7f55645e39d0>
In [ ]: