def test_dividend_000333(self): pipe = make_dividend_pipeline() df = run_pipeline(pipe, '2014-1-1','2017-1-10') result = select_by_sid(df, 333).resample('AS').first() expected = pd.DataFrame({'股利':[2.0, 1.0, 1.2, 1.0]}, index = pd.date_range('2014', '2017',tz='utc',freq='AS')) assert_frame_equal(result, expected)
def test_dividend_002006(self): # 重点测试,无现金分红年度,其值为0.0 pipe = make_dividend_pipeline() df = run_pipeline(pipe, '2012-1-1','2017-1-10') result = select_by_sid(df, 2006).resample('AS').first() expected = pd.DataFrame({'股利':[0.10, 0.00, 0.00, 0.00,0.00,0.02]}, index = pd.date_range('2012', '2017',tz='utc',freq='AS')) assert_frame_equal(result, expected)
def test_finance_report_000333(self): # 重点测试日期偏移。默认报告期后45天 pipe = make_finance_report_pipeline() df = run_pipeline(pipe, '2017-2-14','2017-2-16') result = select_by_sid(df, 333) a001 = [1944482.0, 1793125.0, 1793125.0] b001 = [11707795.0, 15984170.0, 15984170.0] expected = pd.DataFrame({'货币资金(万元)':a001, '营业总收入(万元)':b001}, index = pd.date_range('2017-02-14', '2017-02-16',tz='utc')) assert_frame_equal(result, expected)
def test_margin_000333(self): # 当天查询上一交易日数据 pipe = make_margin_pipeline() df = run_pipeline(pipe, '2017-11-29','2017-11-30') result = select_by_sid(df, 333) # 测试数据类型float64 long_amount = [2976903630.0, 3021909993.0] total_amount = [2994651757.0, 3038726032.0] expected = pd.DataFrame({'融资余额(万元)':long_amount, '融资融券余额(万元)':total_amount}, index = pd.date_range('2017-11-29', '2017-11-30',tz='utc')) assert_frame_equal(result, expected)
def test_stock_concept(self): pipe = make_concept_pipeline() start = '2017-11-27' end = '2017-11-30' df = run_pipeline(pipe, start,end) # 浪潮信息 result_1 = select_by_sid(df, 977) expected_1 = pd.DataFrame({'大数据':[True] * 4, '特种药':[False] * 4, '融资融券':[True] * 4,}, index = pd.date_range(start, end, tz='utc')) assert_frame_equal(result_1, expected_1) # 丰原药业 result_2 = select_by_sid(df, 153) expected_2 = pd.DataFrame({'大数据':[False] * 4, '特种药':[True] * 4, '融资融券':[False] * 4,}, index = pd.date_range(start, end, tz='utc')) assert_frame_equal(result_2, expected_2)
def test_HistoricalZscore(self): # 因读取原始数据时,并没有考虑调整项。故不得选择送转股期间测试。 # 如果原始数据做同样的调整处理,才会通过测试。 # 关注点:按列计算z值, 单列输入 start = '2017-11-20' end = '2017-12-1' stock_codes = ['000001', '000002'] df = run_pipeline(make_pipeline(), start, end) result = df.loc[end].values expected = np.array([[-1.59106704, -1.23624812], [-0.51367549, 1.18221339]]) assert_array_almost_equal(result, expected) with self.assertRaises(ValueError): HistoricalZScore(window_length=2, inputs=[USEquityPricing.close]) with self.assertRaises(ValueError): HistoricalZScore( window_length=4, inputs=[USEquityPricing.close, USEquityPricing.volume])
def compute_factors(one_day): """ 根据每天运行的pipeline结果,近似计算Fama-French 因子 """ factors = run_pipeline(make_pipeline(), one_day, one_day) # 获取后续使用的值 returns = factors['returns'] mkt_cap = factors.sort_values(['market_cap'], ascending=True) be_me = factors.sort_values(['be_me'], ascending=True) prior_returns = factors['prior_returns'] # 根据市值划分总体,构造6个资产组合 half = int(len(mkt_cap) * 0.5) small_caps = mkt_cap[:half] big_caps = mkt_cap[half:] thirty = int(len(be_me) * 0.3) seventy = int(len(be_me) * 0.7) growth = be_me[:thirty] neutral = be_me[thirty:seventy] value = be_me[seventy:] small_value = small_caps.index.intersection(value.index) small_neutral = small_caps.index.intersection(neutral.index) small_growth = small_caps.index.intersection(growth.index) big_value = big_caps.index.intersection(value.index) big_neutral = big_caps.index.intersection(neutral.index) big_growth = big_caps.index.intersection(growth.index) # 假设等权分配资金,取其投资组合回报率平均值 sv = returns[small_value].mean() sn = returns[small_neutral].mean() sg = returns[small_growth].mean() bv = returns[big_value].mean() bn = returns[big_neutral].mean() bg = returns[big_growth].mean() # 按降序排列 # 小市值组合收益率(序列没有列可以指定) s_p = prior_returns.loc[small_caps.index].sort_values(ascending=False) # 大市值组合收益率 b_p = prior_returns.loc[big_caps.index].sort_values(ascending=False) shp = s_p.iloc[:int(len(s_p) * 0.3)].mean() # 小市值组合收益率前70%的均值 bhp = b_p.iloc[:int(len(b_p) * 0.3)].mean() # 大市值组合收益率前70%的均值 slp = s_p.iloc[int(len(s_p) * 0.7):].mean() # 小市值组合收益率后30%的均值 blp = b_p.iloc[int(len(b_p) * 0.7):].mean() # 大市值组合收益率后30%的均值 # 计算 SMB smb = (sv + sn + sg) / 3 - (bv + bn + bg) / 3 # 计算 HML hml = (sv + bv) / 2 - (sg + bg) / 2 # 计算 MOM mom = (shp + bhp) / 2 - (slp + blp) / 2 return smb, hml, mom