Notebook
In [1]:
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.factset import Fundamentals, EquityMetadata  
from quantopian.pipeline.data import EquityPricing  
from quantopian.pipeline.factors import RSI,BollingerBands,SimpleMovingAverage,EWMA,EWMSTD  
from quantopian.pipeline.domain import US_EQUITIES


is_share = EquityMetadata.security_type.latest.eq('SHARE')  
is_primary = EquityMetadata.is_primary.latest  
primary_shares = (is_share & is_primary)  
market_cap = Fundamentals.mkt_val.latest

universe = market_cap.top(1000, mask=primary_shares)  
# 1-month (21 trading day) EWMA factor.  
fast_ewma = EWMA.from_span(  
    inputs=[EquityPricing.close],  
    window_length=21,  
    span=15)

# 6-month (126 trading day) EWMA factor.  
slow_ewma = EWMA.from_span(  
    inputs=[EquityPricing.close],  
    window_length=126,  
    span=15)  
# Divide fast_ma by slow_ma to get momentum factor and z-score.  
momentum = fast_ewma / slow_ewma  
momentum_factor = momentum.zscore()

#Now that we defined a universe and a factor, we can choose a market and time period and simulate the factor.  
#The code below creates a Pipeline instance that adds our factor as a column and screens down to equities in our universe. The Pipeline is then run over the US equities market from 2016 to 2019.  
# Create a US equities pipeline with our momentum factor, screening down to our universe.  
pipe = Pipeline(  
    columns={  
        'momentum_factor': momentum_factor,  
    },  
    screen=universe,  
    domain=US_EQUITIES,  
)
# Run the pipeline from 2016 to 2019 and display the first few rows of output.  
from quantopian.research import run_pipeline  
factor_data = run_pipeline(pipe, '2016-01-01', '2019-07-15')  
print("Result contains {} rows of output.".format(len(factor_data)))  
factor_data.head()  
#Step 3 - Test the factor.  
from quantopian.pipeline.factors import Returns  
# Create and run a Pipeline to get day-over-day returns.  
returns_pipe = Pipeline(  
    columns={  
        '1D': Returns(window_length=2),  
    },  
    domain=US_EQUITIES,  
)
returns_data = run_pipeline(returns_pipe, '2016-01-01', '2019-07-15')  
# Import alphalens and pandas.  
import alphalens as al  
import pandas as pd

# Shift the returns so that we can compare our factor data to forward returns.  
shifted_returns = al.utils.backshift_returns_series(returns_data['1D'], 2)

# Merge the factor and returns data.  
al_returns = pd.DataFrame(  
    data=shifted_returns,  
    index=factor_data.index,  
    columns=['1D'],  
)
al_returns.index.levels[0].name = "date"  
al_returns.index.levels[1].name = "asset"

factor_data['momentum_factor'] = - factor_data['momentum_factor']

# Format the factor and returns data so that we can run it through Alphalens.  
al_data = al.utils.get_clean_factor(  
    factor_data['momentum_factor'],  
    al_returns,  
    quantiles=5,  
    bins=None,  
)
from alphalens.tears import create_full_tear_sheet
create_full_tear_sheet(al_data)

Pipeline Execution Time: 15.93 Seconds
Result contains 888000 rows of output.

Pipeline Execution Time: 2.17 Seconds
Dropped 1.6% entries from factor data: 1.6% 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 -5.880759 -0.181724 -0.679709 0.336428 175070 20.040867
2 -0.662848 -0.027951 -0.312422 0.117406 174536 19.979738
3 -0.443782 0.187641 -0.124208 0.099002 174526 19.978593
4 -0.252554 0.471519 0.076691 0.106273 174536 19.979738
5 -0.023678 11.548030 0.547846 0.452594 174897 20.021063
Returns Analysis
1D
Ann. alpha 0.028
beta 0.135
Mean Period Wise Return Top Quantile (bps) 1.838
Mean Period Wise Return Bottom Quantile (bps) -1.670
Mean Period Wise Spread (bps) 3.508
/venvs/py35/lib/python3.5/site-packages/alphalens/tears.py:275: UserWarning: 'freq' not set in factor_data index: assuming business day
  UserWarning,
<matplotlib.figure.Figure at 0x7f21368df6d8>
Information Analysis
1D
IC Mean 0.007
IC Std. 0.153
Risk-Adjusted IC 0.046
t-stat(IC) 1.373
p-value(IC) 0.170
IC Skew 0.146
IC Kurtosis 0.494
/venvs/py35/lib/python3.5/site-packages/statsmodels/nonparametric/kdetools.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
/venvs/py35/lib/python3.5/site-packages/alphalens/utils.py:912: UserWarning: Skipping return periods that aren't exact multiples of days.
  + " of days."
Turnover Analysis
1D
Quantile 1 Mean Turnover 0.056
Quantile 2 Mean Turnover 0.130
Quantile 3 Mean Turnover 0.149
Quantile 4 Mean Turnover 0.126
Quantile 5 Mean Turnover 0.054
1D
Mean Factor Rank Autocorrelation 0.994
In [ ]: