def get_(folder): # step 2 因子数据预处理 # 加载dataview数据集 dv = DataView() dataview_folder = folder dv.load_dataview(dataview_folder) # 定义信号过滤条件-非指数成分 # df_index_member = dv.get_ts('index_member') signal = dv.get_ts("pb") price = dv.get_ts("close_adj") group = dv.get_ts("sw1") # mask mask = dv.get_ts('index_member') == 0 # 定义信号过滤条件-非指数成分 # 定义可买入卖出条件——未停牌、未涨跌停 trade_status = dv.get_ts('trade_status') can_trade = trade_status == 1 # 可以交易 # 涨停 up_limit = dv.add_formula( 'up_limit', '(open - Delay(close, 1)) / Delay(close, 1) > 0.095', is_quarterly=False) # 跌停 down_limit = dv.add_formula( 'down_limit', '(open - Delay(close, 1)) / Delay(close, 1) < -0.095', is_quarterly=False) can_enter = np.logical_and(up_limit < 1, can_trade) # 未涨停未停牌 can_exit = np.logical_and(down_limit < 1, can_trade) # 未跌停未停牌 benchmark = dv.data_benchmark return signal, price, mask, group, can_enter, can_exit, benchmark
def test_add_formula(): dv = DataView() dv.load_dataview(folder_path=daily_path) nrows, ncols = dv.data_d.shape n_securities = len(dv.data_d.columns.levels[0]) formula = 'Delta(high - close, 1)' dv.add_formula('myvar1', formula, is_quarterly=False, add_data=True) assert dv.data_d.shape == (nrows, ncols + 1 * n_securities) formula2 = 'myvar1 - close' dv.add_formula('myvar2', formula2, is_quarterly=False, add_data=True) assert dv.data_d.shape == (nrows, ncols + 2 * n_securities)
def test_q_add_formula(): dv = DataView() folder_path = '../output/prepared/20160609_20170601_freq=1D' dv.load_dataview(folder_path=quarterly_path) nrows, ncols = dv.data_d.shape n_securities = len(dv.data_d.columns.levels[0]) formula = 'total_oper_rev / close' dv.add_formula('myvar1', formula, is_quarterly=False, add_data=True) df1 = dv.get_ts('myvar1') assert not df1.empty formula2 = 'Delta(oper_exp * myvar1 - open, 3)' dv.add_formula('myvar2', formula2, is_quarterly=False, add_data=True) df2 = dv.get_ts('myvar2') assert not df2.empty
def test_add_formula_directly(): ds = RemoteDataService() ds.init_from_config(data_config) dv = DataView() secs = '600030.SH,000063.SZ,000001.SZ' props = { 'start_date': 20160601, 'end_date': 20170601, 'symbol': secs, 'fields': 'open,close', 'freq': 1 } dv.init_from_config(props, data_api=ds) dv.prepare_data() dv.add_formula("myfactor", 'close / open', is_quarterly=False) assert dv.data_d.shape == (281, 36)
import pandas as pd from datetime import datetime def change_time(df): df.index = pd.Index( map(lambda x: datetime.strptime(str(x), "%Y%m%d"), df.index)) return df EMA = dv.get_ts('close').ewm(span=5, adjust=False).mean() EMA2 = EMA.ewm(span=5, adjust=False).mean() EMA3 = EMA2.ewm(span=5, adjust=False).mean() dv.append_df(EMA3, 'EMA3') dv.add_formula('a', 'close/Delay(close,1)-1', is_quarterly=False, add_data=True) dv.add_formula('b', '(close/Delay(EMA3,19))^(1/20)-1', is_quarterly=False, add_data=True) Factor1 = dv.add_formula( 'Factor1', '-Log((Ts_Sum(If(a>b,1,0),20)-1)*Ts_Sum(If(a<b,(a-b)^2,0),20)/(Ts_Sum(If(a<b,1,0),20))*Ts_Sum(If(a>b,(a-b)^2,0),20))', is_quarterly=False, add_data=True) EMA = dv.get_ts('close').ewm(span=60, adjust=False).mean() dv.append_df(EMA, 'EMA') Factor2 = dv.add_formula('Factor2', '-ps', is_quarterly=False, add_data=True)
#1_初始化 from jaqs_fxdayu.data import DataView import warnings warnings.filterwarnings("ignore") dataview_folder = 'G:/data/hs300_2' dv = DataView() dv.load_dataview(dataview_folder) dv.add_formula("momentum", "Return(close_adj, 20)", is_quarterly=False, add_data=True) #直接使用内置的函数,添加新因子,可能之前已经添加过了 dv.get_ts("momentum").head() print(dv.fields) #查看dv中取得的数据 import numpy as np #定义过滤条件 def mask_index_member(): df_index_member = dv.get_ts('index_member') mask_index_member = ~(df_index_member > 0) #定义信号过滤条件-非指数成分 return mask_index_member def limit_up_down(): # 定义可买卖条件——未停牌、未涨跌停 trade_status = dv.get_ts('trade_status') mask_sus = trade_status == u'停牌' # 涨停 dv.add_formula('up_limit',
dv.add_field('close',ds) dv.add_field('high',ds) dv.add_field('low',ds) dv.add_field('turnover',ds) dv.add_field('turnover_ratio',ds) dv.add_field('price_div_dps',ds) dv.add_field('oper_rev',ds) dv.add_field('roa',ds) dv.add_field('total_share',ds) dv.add_field('pe_ttm',ds) dv.add_field('roe',ds) dv.add_field('roa',ds) #alpha1= dv.add_formula('alpha1', "-1*Delta(close,200)/Delay(close,200)", is_quarterly=False, add_data=True) dv.add_formula('DP01',"close-Delay(Ts_Mean(close,20),11)",is_quarterly=False,add_data=True) alpha1=dv.add_formula('alpha1','-Ts_Mean(DP01,6)',is_quarterly=False,add_data=True) alpha2 = dv.add_formula('alpha2', "-Log(total_share*close)" , is_quarterly=False, add_data=True) #alpha3 = dv.add_formula('alpha3', # "roa" # , is_quarterly=False, add_data=True) #alpha4 = dv.add_formula('alpha4', # "roe" # , is_quarterly=False, add_data=True) #alpha5 = dv.add_formula('alpha5', # "-1*price_div_dps", is_quarterly=False, add_data=True)
from jaqs_fxdayu.data import DataView import warnings warnings.filterwarnings("ignore") dataview_folder = '../Factor' dv = DataView() dv.load_dataview(dataview_folder) dv.add_formula("Divert", "Corr(volume,close_adj,20)", is_quarterly=False).head() # 添加到数据集dv里,则计算结果之后可以反复调用 dv.add_formula("Divert", "Corr(volume,close_adj,20)", is_quarterly=False, add_data=True) dv.get_ts("Divert").head()