From 96929131ce3965b71d60f93fb66c50bd56a77e5e Mon Sep 17 00:00:00 2001 From: zengbin93 Date: Fri, 28 Jun 2024 20:18:59 +0800 Subject: [PATCH] 0.9.54 update --- czsc/connectors/tq_connector.py | 2 ++ czsc/traders/weight_backtest.py | 15 +++++++++++++++ examples/test_offline/test_weight_backtest.py | 3 +++ 3 files changed, 20 insertions(+) diff --git a/czsc/connectors/tq_connector.py b/czsc/connectors/tq_connector.py index b6b754631..fe2f744e1 100644 --- a/czsc/connectors/tq_connector.py +++ b/czsc/connectors/tq_connector.py @@ -189,6 +189,8 @@ def create_symbol_trader(api: TqApi, symbol, **kwargs): future_name_map = { + "EC": "欧线集运", + "LC": "碳酸锂", "PG": "LPG", "EB": "苯乙烯", "CS": "玉米淀粉", diff --git a/czsc/traders/weight_backtest.py b/czsc/traders/weight_backtest.py index bcfe72136..b14e3b00f 100644 --- a/czsc/traders/weight_backtest.py +++ b/czsc/traders/weight_backtest.py @@ -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): """获取某个合约的每日收益率 @@ -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}") diff --git a/examples/test_offline/test_weight_backtest.py b/examples/test_offline/test_weight_backtest.py index 0b8fd1d78..a7be4e278 100644 --- a/examples/test_offline/test_weight_backtest.py +++ b/examples/test_offline/test_weight_backtest.py @@ -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"})