def save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = { 'start_date': 20140101, 'end_date': 20171001, 'universe': '000300.SH', 'fields': 'volume,turnover,float_mv,pb,total_mv', 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() # for convenience to check limit reachers dv.add_formula('limit_reached', 'Abs((open - Delay(close, 1)) / Delay(close, 1)) > 0.095', is_quarterly=False) dv.add_formula('random', 'StdDev(volume, 20)', is_quarterly=False) dv.add_formula('momentum', 'Return(close_adj, 20)', is_quarterly=False) # dv.add_formula('size', '', is_quarterly=False) dv.save_dataview(dataview_folder)
def test_save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = { 'start_date': 20170101, 'end_date': 20171001, 'universe': '000300.SH', 'fields': ('float_mv,pb,pe_ttm,sw2'), 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'GroupQuantile(-float_mv, sw2, 10)' dv.add_formula('rank_mv', factor_formula, is_quarterly=False) factor_formula = 'GroupQuantile(If(pb >= 0.2, pb, 100), sw2, 10)' dv.add_formula('rank_pb', factor_formula, is_quarterly=False) factor_formula = 'GroupQuantile(If(pe_ttm >= 3, pe_ttm, 9999.0), sw2, 10)' dv.add_formula('rank_pe', factor_formula, is_quarterly=False) dv.save_dataview(folder_path=dataview_dir_path)
def save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = {'start_date': 20150101, 'end_date': 20170930, 'universe': '000905.SH', 'fields': ('turnover,float_mv,close_adj,pe,pb'), 'freq': 1} dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'Cutoff(Standardize(turnover / 10000 / float_mv), 2)' dv.add_formula('TO', factor_formula, is_quarterly=False) factor_formula = 'Cutoff(Standardize(1/pb), 2)' dv.add_formula('BP', factor_formula, is_quarterly=False) factor_formula = 'Cutoff(Standardize(Return(close_adj, 20)), 2)' dv.add_formula('REVS20', factor_formula, is_quarterly=False) factor_formula = 'Cutoff(Standardize(Log(float_mv)), 2)' dv.add_formula('float_mv_factor', factor_formula, is_quarterly=False) factor_formula = 'Delay(Return(close_adj, 1), -1)' dv.add_formula('NextRet', factor_formula, is_quarterly=False) dv.save_dataview(folder_path=dataview_dir_path)
def test_write(): ds = RemoteDataService() ds.init_from_config() dv = DataView() secs = '600030.SH,000063.SZ,000001.SZ' props = { 'start_date': 20160601, 'end_date': 20170601, 'symbol': secs, 'fields': 'open,close,high,low,volume,pb,net_assets,pcf_ncf', 'freq': 1 } dv.init_from_config(props, data_api=ds) dv.prepare_data() assert dv.data_d.shape == (281, 48) assert dv.dates.shape == (281, ) # TODO """ PerformanceWarning: your performance may suffer as PyTables will pickle object types that it cannot map directly to c-types [inferred_type->mixed,key->block1_values] [items->[('000001.SZ', 'int_income'), ('000001.SZ', 'less_handling_chrg_comm_exp'), ('000001.SZ', 'net_int_income'), ('000001.SZ', 'oper_exp'), ('000001.SZ', 'symbol'), ('000063.SZ', 'int_income'), ('000063.SZ', 'less_handling_chrg_comm_exp'), ('000063.SZ', 'net_int_income'), ('000063.SZ', 'oper_exp'), ('000063.SZ', 'symbol'), ('600030.SH', 'int_income'), ('600030.SH', 'less_handling_chrg_comm_exp'), ('600030.SH', 'net_int_income'), ('600030.SH', 'oper_exp'), ('600030.SH', 'symbol')]] """ dv.save_dataview(folder_path=daily_path)
def save_dataview(sub_folder='test_dataview'): ds = RemoteDataService() dv = DataView() props = { 'start_date': 20141114, 'end_date': 20160327, 'universe': '000300.SH', 'fields': ( 'open,high,low,close,vwap,volume,turnover,' # + 'pb,net_assets,' + 's_fa_eps_basic,oper_exp,tot_profit,int_income'), 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'close > Ts_Max(close, 20)' # 20 days new high factor_name = 'new_high' dv.add_formula(factor_name, factor_formula, is_quarterly=False) dv.save_dataview( folder_path=fileio.join_relative_path('../output/prepared'), sub_folder=sub_folder)
def test_save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = {'start_date': 20150101, 'end_date': 20170930, 'universe': '000905.SH', 'fields': ('tot_cur_assets,tot_cur_liab,inventories,pre_pay,deferred_exp,' 'eps_basic,ebit,pe,pb,float_mv,sw1'), 'freq': 1} dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'pe < 30' dv.add_formula('pe_condition', factor_formula, is_quarterly=False) factor_formula = 'pb < 3' dv.add_formula('pb_condition', factor_formula, is_quarterly=False) factor_formula = 'Return(eps_basic, 4) > 0' dv.add_formula('eps_condition', factor_formula, is_quarterly=True) factor_formula = 'Return(ebit, 4) > 0' dv.add_formula('ebit_condition', factor_formula, is_quarterly=True) factor_formula = 'tot_cur_assets/tot_cur_liab > 2' dv.add_formula('current_condition', factor_formula, is_quarterly=True) factor_formula = '(tot_cur_assets - inventories - pre_pay - deferred_exp)/tot_cur_liab > 1' dv.add_formula('quick_condition', factor_formula, is_quarterly=True) dv.add_formula('mv_rank', 'Rank(float_mv)', is_quarterly=False) dv.save_dataview(folder_path=dataview_dir_path)
def save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = { 'start_date': 20170101, 'end_date': 20171030, 'universe': '000300.SH', 'fields': ( 'open,high,low,close,vwap,volume,turnover,sw1,' # + 'pb,net_assets,' + 'eps_basic,total_mv,tot_profit,int_income'), 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'close >= Delay(Ts_Max(close, 20), 1)' # 20 days new high factor_name = 'new_high' dv.add_formula(factor_name, factor_formula, is_quarterly=False) dv.add_formula('total_profit_growth', formula='Return(tot_profit, 4)', is_quarterly=True) dv.save_dataview(folder_path=dataview_dir_path)
def save_dataview(): ds = RemoteDataService() ds.init_from_config(data_config) dv = DataView() start_date = get_index_basic_information()[2] end_date = get_index_basic_information()[3] props = { 'universe': index, 'start_date': start_date, 'end_date': end_date, 'fields': fields, 'freq': 1 } dv.init_from_config(props, data_api=ds) dv.prepare_data() dv.save_dataview(folder_path=dataview_dir_path)
def test_save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = {'start_date': 20170201, 'end_date': 20171001, 'universe': '000300.SH', 'fields': ('float_mv,sw2,sw1'), 'freq': 1} dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'GroupQuantile(float_mv, sw1, 10)' dv.add_formula('gq30', factor_formula, is_quarterly=False) dv.save_dataview(folder_path=dataview_dir_path)
def store_ic_weight(): """ Calculate IC weight and save it to file """ dv = DataView() dv.load_dataview(folder_path=dataview_dir_path) factorList = ['TO', 'BP', 'REVS20', 'float_mv_factor'] orthFactor_dic = {} for factor in factorList: orthFactor_dic[factor] = {} # add the orthogonalized factor to dataview for trade_date in dv.dates: snapshot = dv.get_snapshot(trade_date) factorPanel = snapshot[factorList] factorPanel = factorPanel.dropna() if len(factorPanel) != 0: orthfactorPanel = Schmidt(factorPanel) orthfactorPanel.columns = [x + '_adj' for x in factorList] snapshot = pd.merge(left=snapshot, right=orthfactorPanel, left_index=True, right_index=True, how='left') for factor in factorList: orthFactor_dic[factor][trade_date] = snapshot[factor] for factor in factorList: dv.append_df(pd.DataFrame(orthFactor_dic[factor]).T, field_name=factor + '_adj', is_quarterly=False) dv.save_dataview(dataview_dir_path) factorList_adj = [x + '_adj' for x in factorList] fileio.save_json(factorList_adj, custom_data_path) w = get_ic_weight(dv) store = pd.HDFStore(ic_weight_hd5_path) store['ic_weight'] = w store.close()
def test_q(): from jaqs.data.dataservice import RemoteDataService ds = RemoteDataService() dv = DataView() secs = '600030.SH,000063.SZ,000001.SZ' props = {'start_date': 20160609, 'end_date': 20170601, 'universe': '000300.SH', 'symbol': secs, 'fields': ('open,close,' + 'pb,net_assets,' + 'total_oper_rev,oper_exp,' + 'cash_paid_invest,' + 'capital_stk,' + 'roe'), 'freq': 1} dv.init_from_config(props, data_api=ds) dv.prepare_data() folder_path = '../output/prepared' dv.save_dataview(folder_path=folder_path)
def test_save_dataview(): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = { 'start_date': 20170101, 'end_date': 20171001, 'universe': '000300.SH', 'fields': 'pe_ttm,net_profit_incl_min_int_inc', 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'Return(net_profit_incl_min_int_inc, 4)' factor_name = 'net_profit_growth' dv.add_formula(factor_name, factor_formula, is_quarterly=True) dv.save_dataview(folder_path=dataview_dir_path)
def save_dataview(): # total 130 seconds ds = RemoteDataService() dv = DataView() props = {'start_date': 20141114, 'end_date': 20170327, 'universe': '000300.SH', # 'symbol': 'rb1710.SHF,rb1801.SHF', 'fields': ('open,high,low,close,vwap,volume,turnover,' # + 'pb,net_assets,' + 's_fa_eps_basic,oper_exp,tot_profit,int_income' ), 'freq': 1} dv.init_from_config(props, ds) dv.prepare_data() dv.add_formula('eps_ret', 'Return(s_fa_eps_basic, 3)', is_quarterly=True) dv.add_formula('ret20', 'Delay(Return(close_adj, 20), -20)', is_quarterly=False) dv.save_dataview(folder_path=fileio.join_relative_path('../output/prepared'))
def build_stock_selection_factor(): ds = RemoteDataService() dv = DataView() props = {'start_date': 20120101, 'end_date': 20170901, 'universe': '000300.SH', # 'symbol': 'rb1710.SHF,rb1801.SHF', 'fields': ('open,high,low,close,vwap,volume,turnover,' # + 'pb,net_assets,' + 's_fa_eps_basic,oper_exp,tot_profit,int_income' ), 'freq': 1} dv.init_from_config(props, ds) dv.prepare_data() dv.add_formula('eps_ret', 'Return(s_fa_eps_basic, 4)', is_quarterly=True) dv.add_formula('rule1', '(eps_ret > 0.2) && (Delay(eps_ret, 1) > 0.2)', is_quarterly=True) dv.add_formula('rule2', 'close > Ts_Max(close, 120)', is_quarterly=False) # dv.add_formula('ytan', 'rule1 && rule2', is_quarterly=False) dv.add_formula('ret20', 'Delay(Return(close_adj, 20), -20)', is_quarterly=False) dv.save_dataview(folder_path=fileio.join_relative_path('../output/prepared'))
def test_q(): ds = RemoteDataService() ds.init_from_config() dv = DataView() secs = '600030.SH,000063.SZ,000001.SZ' props = { 'start_date': 20160609, 'end_date': 20170601, 'symbol': secs, 'fields': ('open,close,' + 'pb,net_assets,' + 'total_oper_rev,oper_exp,' + 'cash_paid_invest,' + 'capital_stk,' + 'roe'), 'freq': 1 } dv.init_from_config(props, data_api=ds) dv.prepare_data() dv.save_dataview(folder_path=quarterly_path)
def test_save_dataview(sub_folder='test_dataview'): ds = RemoteDataService() ds.init_from_config() dv = DataView() props = { 'start_date': 20150101, 'end_date': 20170930, 'universe': '000905.SH', 'fields': ('float_mv,tot_shrhldr_eqy_excl_min_int,deferred_tax_assets,sw2'), 'freq': 1 } dv.init_from_config(props, ds) dv.prepare_data() factor_formula = 'Quantile(-float_mv,5)' dv.add_formula('rank_mv', factor_formula, is_quarterly=False) factor_formula = 'Quantile(float_mv/(tot_shrhldr_eqy_excl_min_int+deferred_tax_assets), 5)' dv.add_formula('rank_pb', factor_formula, is_quarterly=False) dv.save_dataview(folder_path=dataview_dir_path)
# RemoteDataService communicates with a remote server to fetch data ds = RemoteDataService() # Use username and password in data_config to login ds.init_from_config(data_config) # DataView utilizes RemoteDataService to get various data and store them dv = DataView() dv.init_from_config(dataview_props, ds) dv.prepare_data() # In[35]: type(dv) # In[ ]: props = { 'start_date': 20150101, 'end_date': 20170901, 'universe': '000300.SH', 'fields': ('open,high,low,close,vwap,volume,turnover,eps_basic,roe,sw2'), 'freq': 1 } dv.init_from_config(props, ds) # In[12]: dv.prepare_data() dv.save_dataview('save_folder')