def alpha_factor_function(): return 10 def make_pipeline(): return Pipeline(columns={'column_name': alpha_factor_function()}) my_pipe = make_pipeline() pipeline_data = run_pipeline(my_pipe, start_date='2014-1-1', end_date='2016-1-1').dropna() # Alphalens (second cell) from alphalens.utils import get_clean_factor_and_forward_returns from alphalens.tears import create_full_tear_sheet pricing_data = get_pricing( symbols=pipeline_data.index. levels[1], # Finds all assets that appear at least once in the pipeline start_date='2014-1-1', end_date='2016-2-1', #1 trading day after end date of pipeline fields='open_price') merged_data = get_clean_factor_and_forward_returns( factor=pipeline_data['column_name'], prices=pricing_data) create_full_tear_sheet(merged_data)
# 申万行业分类 industry_dict = sw_industry.set_index('code')['index_code'].to_dict() industry_labels = sw_industry.set_index('index_code')['index_name'].to_dict() # 分析因子 factor_data = get_clean_factor_and_forward_returns(stock_factor, stock_close, groupby=industry_dict, quantiles=5, binning_by_group=False, periods=(1, 5, 10), filter_zscore=None) create_full_tear_sheet(factor_data, long_short=False, group_neutral=False, by_group=False) # create_summary_tear_sheet(factor_data, long_short=True, group_neutral=True) # create_event_returns_tear_sheet(factor_data, stock_close, avgretplot=(3, 11), # long_short=False, group_neutral=True, by_group=True) # create_event_study_tear_sheet(factor_data, stock_close) # returns, positions, benchmark_rets = create_pyfolio_input(factor_data, # period='1D', # capital=None, # long_short=False, # group_neutral=True, # equal_weight=False, # quantiles=None, # groups=None, # benchmark_period='1D') factor_data
def create_tear_sheet(self): return create_full_tear_sheet(self.factor_and_forward_returns)
import pandas as pd import tushare as ts from alphalens.utils import get_clean_factor_and_forward_returns from alphalens.tears import create_full_tear_sheet pro = ts.pro_api() df = pro.daily(ts_code='000001.SZ,600982.SH', start_date='20200101', end_date='20211122') df.index = pd.to_datetime(df['trade_date']) df.index.name = None df.sort_index(inplace=True) # 多索引的因子列,第一个索引为日期,第二个索引为股票代码 assets = df.set_index([df.index, df['ts_code']], drop=True) # column为股票代码,index为日期,值为收盘价 close = df.pivot_table(index='trade_date', columns='ts_code', values='close') close.index = pd.to_datetime(close.index) from alphalens.utils import get_clean_factor_and_forward_returns from alphalens.tears import create_full_tear_sheet ret = get_clean_factor_and_forward_returns(assets[['pct_chg']], close) create_full_tear_sheet(ret, long_short=False) # https://github.com/Ckend/alphalens