Notebook

Alphalens Quickstart Template

In [13]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data import factset, USEquityPricing
from quantopian.pipeline.classifiers.fundamentals import Sector
from quantopian.pipeline.factors import Returns, SimpleMovingAverage, CustomFactor, RSI
from quantopian.pipeline.data.user_53bc0c4831bb052ad000002f import roc


from alphalens.performance import mean_information_coefficient
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_information_tear_sheet, create_returns_tear_sheet

Define Your Alpha Factor Here

Spend your time in this cell, creating good factors. Then simply run the rest of the notebook to analyze factor_to_analyze!

In [ ]:
def before_trading_start(context, data):
In [21]:
def make_pipeline():
    
    eroclatest = roc.eroc.latest, window_len
    
    eezlatest = roc.eez.latest
    
    erocspread = eroclatest.iloc[[0, -5]]
    
    factor_to_analyze = (current_assets - diff)
    
    sector = Sector()
    
    return Pipeline(
        columns = {'eroclatest': eroclatest, 'eezlatest': eezlatest, 'erocspread': erocspread, 'sector': sector, },
        screen = QTradableStocksUS() & erocspread.notnull() & sector.notnull()
    )

factor_data = run_pipeline(make_pipeline(), '2015-1-1', '2016-1-1')
pricing_data = get_pricing(factor_data.index.levels[1], '2015-1-1', '2016-6-1', fields='open_price')

    
    
    

AttributeErrorTraceback (most recent call last)
<ipython-input-21-0c027774245f> in <module>()
     16     )
     17 
---> 18 factor_data = run_pipeline(make_pipeline(), '2015-1-1', '2016-1-1')
     19 pricing_data = get_pricing(factor_data.index.levels[1], '2015-1-1', '2016-6-1', fields='open_price')
     20 

<ipython-input-21-0c027774245f> in make_pipeline()
      5     eezlatest = roc.eez.latest
      6 
----> 7     erocspread = eroclatest.iloc[[0, -5]]
      8 
      9     factor_to_analyze = (current_assets - diff)

/build/src/qexec_repo/zipline_repo/zipline/pipeline/factors/factor.pyc in __getattribute__(self, name)
   1585         outputs = object.__getattribute__(self, 'outputs')
   1586         if outputs is NotSpecified:
-> 1587             return super(CustomFactor, self).__getattribute__(name)
   1588         elif name in outputs:
   1589             return RecarrayField(factor=self, attribute=name)

AttributeError: 'Latest' object has no attribute 'iloc'

Determine The Decay Rate Of Your Alpha Factor.

In [16]:
longest_look_forward_period = 63 # week = 5, month = 21, quarter = 63, year = 252
range_step = 5

merged_data = get_clean_factor_and_forward_returns(
    factor = factor_data['factor_to_analyze'],
    prices = pricing_data,
    periods = range(1, longest_look_forward_period, range_step)
)

mean_information_coefficient(merged_data).plot(title="IC Decay")
Dropped 0.0% entries from factor data: 0.0% 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!
Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff251e62190>

Create Group Neutral Tear Sheets

In [17]:
sector_labels, sector_labels[-1] = dict(Sector.SECTOR_NAMES), "Unknown"

merged_data = get_clean_factor_and_forward_returns(
    factor = factor_data['factor_to_analyze'],
    prices = pricing_data,
    groupby = factor_data['sector'],
    groupby_labels = sector_labels,
    binning_by_group = True,
    periods = (1,5,10)
)

create_information_tear_sheet(merged_data, by_group=True, group_neutral=True)
create_returns_tear_sheet(merged_data, by_group=True, group_neutral=True)
Dropped 3.1% entries from factor data: 0.0% in forward returns computation and 3.1% in binning phase (set max_loss=0 to see potentially suppressed Exceptions).
max_loss is 35.0%, not exceeded: OK!
Information Analysis
1D 5D 10D
IC Mean -0.010 -0.020 -0.031
IC Std. 0.200 0.196 0.193
Risk-Adjusted IC -0.049 -0.102 -0.160
t-stat(IC) -0.777 -1.630 -2.551
p-value(IC) 0.438 0.104 0.011
IC Skew -0.011 0.055 -0.182
IC Kurtosis -0.267 -0.196 -0.149
<matplotlib.figure.Figure at 0x7ff251bb5450>
Returns Analysis
1D 5D 10D
Ann. alpha -0.032 -0.035 -0.039
beta 0.087 0.016 -0.004
Mean Period Wise Return Top Quantile (bps) -4.256 -3.406 -2.183
Mean Period Wise Return Bottom Quantile (bps) 1.623 1.942 2.554
Mean Period Wise Spread (bps) -5.878 -5.356 -4.756
<matplotlib.figure.Figure at 0x7ff251c6dd50>
In [ ]: