def test_calendar(): ds = RemoteDataService() ds.init_from_config(data_config) res1 = ds.get_trade_date_range(20121224, 20130201) assert len(res1) == 27 res2 = ds.get_next_trade_date(20170102) assert res2 == 20170103 res3 = ds.get_next_trade_date(20170104) assert res3 == 20170105 res11 = ds.get_trade_date_range(20161224, 20170201) assert len(res11) == 23 assert not ds.is_trade_date(20150101) assert not ds.is_trade_date(20130501)
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.get_trade_date_range(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