def get_equity_returns(bundle, data_dates, run_dates): """Gets the close price for all assets over all trading days in run_dates.""" pipe = Pipeline(columns={'Close': USEquityPricing.close.latest}, ) # create the pipeline engine spe = make_pipeline_engine(bundle, data_dates) # stocks = spe.run_pipeline(pipe, run_dates[0], run_dates[1]) stocks = spe.run_pipeline(pipe, run_dates[0], run_dates[1]) unstacked_results = stocks.unstack() # what does this do? prices = (unstacked_results['Close'].fillna(method='ffill').fillna( method='bfill').dropna(axis=1, how='any').shift(periods=-1).dropna()) return prices.pct_change() #[1:]
from zipline.data.bundles.core import register from zipline.pipeline import Pipeline from zipline.pipeline.data import USEquityPricing from alphacompiler.util.zipline_data_tools import make_pipeline_engine def str2dt(datestr): return pd.to_datetime(datestr, utc=True) # constants BUNDLE = 'crsp' data_dates = ('2015-01-06', '2015-01-30') backtest_dates = ('2015-01-06', '2015-01-30') pipeline_data_dates = (pd.to_datetime(data_dates[0], utc=True), pd.to_datetime(data_dates[1], utc=True)) # Step 1. Run Pipeline # 1.0 dummy bundle register register(BUNDLE, int) # dummy register of a bundle # 1.1 create the pipeline engine spe = make_pipeline_engine(BUNDLE, pipeline_data_dates) # 1.2 create your pipeline (this could be more elaborate) pipe = Pipeline(columns={'Close': USEquityPricing.close.latest},) # 1.3 run your pipeline with the pipeline engine stocks = spe.run_pipeline(pipe, str2dt(backtest_dates[0]), str2dt(backtest_dates[1])) print stocks
def get_style_cov(bundle, data_dates, run_dates): """Calculates the style covariance matrix.""" my_pipeline = make_pipeline( Momentum) # Momentum is just placeholder to reuse code spe = make_pipeline_engine(bundle, data_dates) results = spe.run_pipeline(my_pipeline, pd.to_datetime(run_dates[0], utc=True), pd.to_datetime(run_dates[1], utc=True)) results.drop(['my_factor', 'my_longs', 'my_shorts'], axis=1) # print results.head() # mean returns of the biggest/smallest by market cap R_biggest = results[results.biggest]['returns'].groupby(level=0).mean() R_smallest = results[results.smallest]['returns'].groupby(level=0).mean() R_highpb = results[results.highpb]['returns'].groupby(level=0).mean() R_lowpb = results[results.lowpb]['returns'].groupby(level=0).mean() R_low_momentum = results[results.low_momentum]['returns'].groupby( level=0).mean() R_high_momentum = results[results.high_momentum]['returns'].groupby( level=0).mean() R_lowvol = results[results.lowvol]['returns'].groupby(level=0).mean() R_highvol = results[results.highvol]['returns'].groupby(level=0).mean() R_low_streversal = results[results.low_streversal]['returns'].groupby( level=0).mean() R_high_streversal = results[results.high_streversal]['returns'].groupby( level=0).mean() SMB = R_smallest - R_biggest HML = R_highpb - R_lowpb MOMENTUM = R_high_momentum - R_low_momentum VOL = R_highvol - R_lowvol STREVERSAL = R_high_streversal - R_low_streversal smb_n_hml = pd.DataFrame( { 'SMB': SMB, # company size 'HML': HML, # company PB ratio value 'MOMENTUM': MOMENTUM, 'VOL': VOL, 'STREVERSAL': STREVERSAL # short term reversal }, columns=["SMB", "HML", "MOMENTUM", "VOL", "STREVERSAL"]).shift(periods=-1).dropna() # get SPY data (not included in bundle) spy_series = pd.read_csv(SPY_PATH, index_col=0, parse_dates=True, usecols=[0, 4], date_parser=date_utc) assert "close" in spy_series.columns MKT = spy_series.pct_change()[1:].rename(columns={"close": "MKT"}) # market returns F = pd.concat([MKT, smb_n_hml], axis=1).dropna() print(F) # calculate cov return F.cov()
def get_factor_returns(alpha_str, bundle, data_dates, run_dates): """Calculates factor returns.""" # create the pipeline engine spe = make_pipeline_engine(bundle, data_dates) my_pipeline = make_pipeline() results = spe.run_pipeline(my_pipeline, run_dates[0], run_dates[1]) print(results.head()) # mean returns of the biggest/smallest by market cap R_biggest = results[results.biggest]['returns'].groupby(level=0).mean() R_smallest = results[results.smallest]['returns'].groupby(level=0).mean() R_highpb = results[results.highpb]['returns'].groupby(level=0).mean() R_lowpb = results[results.lowpb]['returns'].groupby(level=0).mean() R_low_momentum = results[results.low_momentum]['returns'].groupby( level=0).mean() R_high_momentum = results[results.high_momentum]['returns'].groupby( level=0).mean() R_lowvol = results[results.lowvol]['returns'].groupby(level=0).mean() R_highvol = results[results.highvol]['returns'].groupby(level=0).mean() R_low_streversal = results[results.low_streversal]['returns'].groupby( level=0).mean() R_high_streversal = results[results.high_streversal]['returns'].groupby( level=0).mean() R_longs = results[results.my_longs]['returns'].groupby(level=0).mean() R_shorts = results[results.my_shorts]['returns'].groupby(level=0).mean() SMB = R_smallest - R_biggest HML = R_highpb - R_lowpb MOMENTUM = R_high_momentum - R_low_momentum VOL = R_highvol - R_lowvol STREVERSAL = R_high_streversal - R_low_streversal MyReturns = R_longs - R_shorts # smb_n_hml = pd.DataFrame( { 'SMB': SMB, # company size 'HML': HML, # company PB ratio value 'MOMENTUM': MOMENTUM, 'VOL': VOL, 'STREVERSAL': STREVERSAL, # short term reversal 'MyReturns': MyReturns }, columns=["SMB", "HML", "MOMENTUM", "VOL", "STREVERSAL", "MyReturns"]).shift(periods=-1).dropna() # # get SPY data (not included in quantopian-quandl bundle) # spy_series = pd.read_csv(SPY_PATH, index_col=0, parse_dates=True, usecols=[0, 4], date_parser=date_utc) # spy_series = spy_series[spy_series.index >= run_dates[0]] # added to only use same dates as data from the Pipeline # # assert "close" in spy_series.columns # MKT = spy_series.pct_change()[1:].rename(columns={"close": "MKT"}) # market returns # # return pd.concat([MKT, smb_n_hml], axis=1).dropna() return smb_n_hml