def test_align(): # ------------------------------------------------------------------------------------- # input and pre-process demo data ds = RemoteDataService() ds.init_from_config(data_config) raw, msg = ds.query_lb_fin_stat('income', '600000.SH', 20151225, 20170501, 'oper_rev') assert msg == '0,' idx_list = ['report_date', 'symbol'] raw_idx = raw.set_index(idx_list) raw_idx.sort_index(axis=0, level=idx_list, inplace=True) df_ann = raw_idx.loc[pd.IndexSlice[:, :], 'ann_date'] df_ann = df_ann.unstack(level=1) df_value = raw_idx.loc[pd.IndexSlice[:, :], 'oper_rev'] df_value = df_value.unstack(level=1) date_arr = ds.query_trade_dates(20160101, 20170501) df_close = pd.DataFrame(index=date_arr, columns=df_value.columns, data=1e3) # ------------------------------------------------------------------------------------- # demo usage of parser parser = Parser() parser.register_function('Myfunc', lambda x: x * 0 + 1) # simultaneously test register function and align expr_formula = 'signal / Myfunc(close)' expression = parser.parse(expr_formula) for i in range(100): df_res = parser.evaluate({'signal': df_value, 'close': df_close}, df_ann, date_arr) # ------------------------------------------------------------------------------------- sec = '600000.SH' """ # print to validate results print "\n======Expression Formula:\n{:s}".format(expr_formula) print "\n======Report date, ann_date and evaluation value:" tmp = pd.concat([df_ann.loc[:, sec], df_value.loc[:, sec]], axis=1) tmp.columns = ['df_ann', 'df_value'] print tmp print "\n======Selection of result of expansion:" print "20161028 {:.4f}".format(df_res.loc[20161028, sec]) print "20161031 {:.4f}".format(df_res.loc[20161031, sec]) print "20170427 {:.4f}".format(df_res.loc[20170427, sec]) """ assert abs(df_res.loc[20161028, sec] - 82172000000) < 1 assert abs(df_res.loc[20161031, sec] - 120928000000) < 1 assert abs(df_res.loc[20170427, sec] - 42360000000) < 1
def test_align(): # ------------------------------------------------------------------------------------- # input and pre-process demo data ds = RemoteDataService() ds.init_from_config(data_config) raw, msg = ds.query_lb_fin_stat('income', '600000.SH', 20151225, 20170501, 'oper_rev') assert msg == '0,' # print(raw) idx_list = ['report_date', 'symbol'] raw_idx = raw.set_index(idx_list) raw_idx.sort_index(axis=0, level=idx_list, inplace=True) print(raw_idx) df_ann = raw_idx.loc[pd.IndexSlice[:, :], 'ann_date'] df_ann = df_ann.unstack(level=1) df_value = raw_idx.loc[pd.IndexSlice[:, :], 'oper_rev'] df_value = df_value.unstack(level=1) date_arr = ds.query_trade_dates(20160101, 20170501) df_close = pd.DataFrame(index=date_arr, columns=df_value.columns, data=1e3) # ------------------------------------------------------------------------------------- # demo usage of parser parser = Parser() parser.register_function( 'Myfunc', lambda x: x * 0 + 1) # simultaneously test register function and align expr_formula = 'signal / Myfunc(close)' expression = parser.parse(expr_formula) for i in range(100): df_res = parser.evaluate({ 'signal': df_value, 'close': df_close }, df_ann, date_arr) # ------------------------------------------------------------------------------------- sec = '600000.SH' """ # print to validate results print "\n======Expression Formula:\n{:s}".format(expr_formula) print "\n======Report date, ann_date and evaluation value:" tmp = pd.concat([df_ann.loc[:, sec], df_value.loc[:, sec]], axis=1) tmp.columns = ['df_ann', 'df_value'] print tmp print "\n======Selection of result of expansion:" print "20161028 {:.4f}".format(df_res.loc[20161028, sec]) print "20161031 {:.4f}".format(df_res.loc[20161031, sec]) print "20170427 {:.4f}".format(df_res.loc[20170427, sec]) """ assert abs(df_res.loc[20161028, sec] - 82172000000) < 1 assert abs(df_res.loc[20161031, sec] - 120928000000) < 1 assert abs(df_res.loc[20170427, sec] - 42360000000) < 1
def my_globals(request): ds = RemoteDataService() ds.init_from_config(data_config) df, msg = ds.daily("000001.SH, 600030.SH, 000300.SH", start_date=20170801, end_date=20170820, fields="open,high,low,close,vwap,preclose") multi_index_names = ['trade_date', 'symbol'] df_multi = df.set_index(multi_index_names, drop=False) df_multi.sort_index(axis=0, level=multi_index_names, inplace=True) dfx = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['close']].unstack() dfy = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['open']].unstack() parser = Parser() request.function.__globals__.update({ 'parser': parser, 'dfx': dfx, 'dfy': dfy })
if __name__ == "__main__": ds = RemoteDataService() ds.init_from_config(data_config) df, msg = ds.daily("000001.SH, 600030.SH, 000300.SH", start_date=20170801, end_date=20170820, fields="open,high,low,close,vwap,preclose") ds.data_api.close() multi_index_names = ['trade_date', 'symbol'] df_multi = df.set_index(multi_index_names, drop=False) df_multi.sort_index(axis=0, level=multi_index_names, inplace=True) dfx = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['close']].unstack() dfy = df_multi.loc[pd.IndexSlice[:, :], pd.IndexSlice['open']].unstack() parser = Parser() g = globals() g = {k: v for k, v in g.items() if k.startswith('test_') and callable(v)} for test_name, test_func in g.items(): print("\n==========\nTesting {:s}...".format(test_name)) # try: test_func() # print "Successfully tested {:s}.".format(test_name) # except Exception, e: print("Test Complete.")