Skip to content

Commit

Permalink
0.9.60 新增两个统计指标
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Oct 30, 2024
1 parent e8de97f commit 4741fbc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions czsc/traders/weight_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/develop/weight_backtest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()

0 comments on commit 4741fbc

Please sign in to comment.