Skip to content

Commit

Permalink
0.9.54 update
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Jun 28, 2024
1 parent 6381734 commit 9692913
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions czsc/connectors/tq_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def create_symbol_trader(api: TqApi, symbol, **kwargs):


future_name_map = {
"EC": "欧线集运",
"LC": "碳酸锂",
"PG": "LPG",
"EB": "苯乙烯",
"CS": "玉米淀粉",
Expand Down
15 changes: 15 additions & 0 deletions czsc/traders/weight_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ def alpha_stats(self):
stats["结束日期"] = df["date"].max().strftime("%Y-%m-%d")
return stats

@property
def bench_stats(self):
"""基准收益统计"""
df = self.alpha.copy()
stats = czsc.daily_performance(df["基准"].to_list())
stats["开始日期"] = df["date"].min().strftime("%Y-%m-%d")
stats["结束日期"] = df["date"].max().strftime("%Y-%m-%d")
return stats

def get_symbol_daily(self, symbol):
"""获取某个合约的每日收益率
Expand Down Expand Up @@ -585,6 +594,12 @@ def report(self, res_path):
fig.write_html(res_path.joinpath("daily_return.html"))
logger.info(f"费后日收益率资金曲线已保存到 {res_path.joinpath('daily_return.html')}")

# 绘制alpha曲线
alpha = self.alpha.copy()
alpha[["策略", "基准", "超额"]] = alpha[["策略", "基准", "超额"]].cumsum()
fig = px.line(alpha, x="date", y=["策略", "基准", "超额"], title="策略超额收益")
fig.write_html(res_path.joinpath("alpha.html"))

# 所有开平交易记录的表现
stats = res["绩效评价"].copy()
logger.info(f"绩效评价:{stats}")
Expand Down
3 changes: 3 additions & 0 deletions examples/test_offline/test_weight_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def run_by_weights():
wb = czsc.WeightBacktest(dfw, digits=1, fee_rate=0.0002, n_jobs=1)
# wb = czsc.WeightBacktest(dfw, digits=1, fee_rate=0.0002)
dailys = wb.dailys
print(wb.stats)
print(wb.alpha_stats)
print(wb.bench_stats)

# 计算等权组合的超额
df1 = dailys.groupby("date").agg({"return": "mean", "n1b": "mean"})
Expand Down

0 comments on commit 9692913

Please sign in to comment.