Notebook
In [23]:
# Imports the pipeline which will let us filter down our universe of potential stocks.
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline

# The potential universe of stocks. This is updated by Quantopian every night, info in docs.
from quantopian.pipeline.filters import QTradableStocksUS

# Alphalens, to analyze alphafactors
import alphalens

# Import Sectors
from quantopian.pipeline.classifiers.fundamentals import Sector

# Others
from quantopian.pipeline.data.morningstar import operation_ratios, valuation_ratios, income_statement, asset_classification, valuation
from quantopian.pipeline.data import factset
from quantopian.pipeline.data.sentdex import sentiment
from quantopian.pipeline.data.zacks import EarningsSurprises
from quantopian.pipeline.data.factset import Fundamentals


# Inputs for user
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pipe_starting_date = "2016-06-01"
pipe_ending_date = "2018-01-01"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


def make_pipeline():
    
    factor_to_test = factset.Fundamentals.div_yld_af.latest

    mkt_val = Fundamentals.mkt_val.latest
    
    universe = ((QTradableStocksUS()) & (factor_to_test.notnull()) & (mkt_val.top(100)))
    

    factor_to_test = factor_to_test.rank(mask=universe, method ='average', ascending=False)
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    # analyze by sector performance
    sector = Sector()
    
    
    # Filter to a specific sector
    #sector_filter = asset_classification.morningstar_sector_code.latest.element_of([103])
    
    # Create the pipeline
    pipe = Pipeline(columns={"Factor_to_test":factor_to_test,
                            "Sector":sector
                                    },
                   screen = (universe))
    
    return pipe


# Return resulting information, based on our pipeline of filters, and our test factor.
result = run_pipeline(make_pipeline(),start_date = pipe_starting_date, end_date = pipe_ending_date)
    
    
result.head()
Out[23]:
Factor_to_test Sector
2016-06-01 00:00:00+00:00 Equity(24 [AAPL]) 48.0 311
Equity(368 [AMGN]) 44.0 206
Equity(698 [BA]) 37.0 310
Equity(700 [BAC]) 57.0 103
Equity(980 [BMY]) 41.0 206
In [24]:
assets = result.index.levels[1].unique()


pricing = get_pricing(assets, start_date='2015-12-01', end_date='2018-06-01', fields='close_price')


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

len(assets)
Out[24]:
89
In [25]:
quantiles = 5
periods = (1,5,10)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GENERATION OF THE TEAR SHEET ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Combines pipeline and pricing data so alphalens can read it.
factor_alpha_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = result["Factor_to_test"],
                                                                        prices=pricing,
                                                                        quantiles=quantiles,
                                                                        periods=periods,
                                                                         groupby=result['Sector'],
                                                                         groupby_labels=sector_labels)



info_tear_sheet = alphalens.tears.create_information_tear_sheet(factor_alpha_data,by_group=True)
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!
Information Analysis
1D 5D 10D
IC Mean 0.014 0.027 0.047
IC Std. 0.211 0.198 0.191
Risk-Adjusted IC 0.066 0.135 0.244
t-stat(IC) 1.314 2.712 4.881
p-value(IC) 0.190 0.007 0.000
IC Skew -0.065 -0.008 -0.060
IC Kurtosis -0.515 -0.502 -0.052
<matplotlib.figure.Figure at 0x7fabc8a388d0>
In [26]:
factor_alpha_data = alphalens.utils.get_clean_factor_and_forward_returns(factor = result["Factor_to_test"],
                                                                        prices=pricing,
                                                                        quantiles=quantiles,
                                                                        periods=periods,
                                                                         groupby=result['Sector'],
                                                                         groupby_labels=sector_labels)


returns_tear_sheet = alphalens.tears.create_returns_tear_sheet(factor_alpha_data, by_group=True)
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!
Returns Analysis
1D 5D 10D
Ann. alpha -0.004 -0.001 0.010
beta 0.174 0.180 0.144
Mean Period Wise Return Top Quantile (bps) 1.822 1.886 2.222
Mean Period Wise Return Bottom Quantile (bps) -1.330 -1.574 -1.727
Mean Period Wise Spread (bps) 3.152 3.436 3.928
<matplotlib.figure.Figure at 0x7fabc8a49e50>
In [ ]: