diff --git a/czsc/traders/weight_backtest.py b/czsc/traders/weight_backtest.py index 32da2b1bd..2052507ee 100644 --- a/czsc/traders/weight_backtest.py +++ b/czsc/traders/weight_backtest.py @@ -554,8 +554,7 @@ def backtest(self, n_jobs=1): dret = pd.concat([v["daily"] for k, v in res.items() if k in symbols], ignore_index=True) dret = pd.pivot_table(dret, index="date", columns="symbol", values="return").fillna(0) dret["total"] = dret[list(res.keys())].mean(axis=1) - # dret = dret.shift(1).fillna(0).round(4).reset_index() - # ret 中的 date 对应的是上一日;date 后移一位,对应的才是当日收益 + # dret 中的 date 对应的是上一日;date 后移一位,对应的才是当日收益 dret = dret.round(4).reset_index() res["品种等权日收益"] = dret @@ -572,6 +571,8 @@ def backtest(self, n_jobs=1): stats.update({"多头占比": round(long_rate, 4), "空头占比": round(short_rate, 4)}) alpha = self.alpha.copy() + stats["波动比"] = round(alpha["策略"].std() / alpha["基准"].std(), 4) + stats["与基准波动相关性"] = round(alpha["策略"].corr(alpha["基准"].abs()), 4) stats["与基准相关性"] = round(alpha["策略"].corr(alpha["基准"]), 4) alpha_short = alpha[alpha["基准"] < 0].copy() stats["与基准空头相关性"] = round(alpha_short["策略"].corr(alpha_short["基准"]), 4) diff --git a/examples/develop/weight_backtest.py b/examples/develop/weight_backtest.py index 8762b8c9b..e508dbdf2 100644 --- a/examples/develop/weight_backtest.py +++ b/examples/develop/weight_backtest.py @@ -1,6 +1,7 @@ # https://s0cqcxuy3p.feishu.cn/wiki/Pf1fw1woQi4iJikbKJmcYToznxb import sys -sys.path.insert(0, r"D:\ZB\git_repo\waditu\czsc") + +sys.path.insert(0, r"A:\ZB\git_repo\waditu\czsc") import czsc import pandas as pd @@ -9,7 +10,6 @@ def test_ensemble_weight(): """从持仓权重样例数据中回测""" - dfw = pd.read_feather(r"C:\Users\zengb\Desktop\weight_example.feather") + dfw = pd.read_feather(r"C:\Users\zengb\Downloads\weight_example.feather") wb = czsc.WeightBacktest(dfw, digits=1, fee_rate=0.0002, res_path=r"C:\Users\zengb\Desktop\weight_example") - res = wb.backtest() - + # res = wb.backtest()