Skip to content

Commit

Permalink
0.9.59 daily_performance 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Sep 18, 2024
1 parent de3c8cc commit aacbf3b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 111 deletions.
3 changes: 2 additions & 1 deletion czsc/traders/weight_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,10 @@ 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.round(4).reset_index()
res["品种等权日收益"] = dret

stats = {"开始日期": dret.index.min().strftime("%Y%m%d"), "结束日期": dret.index.max().strftime("%Y%m%d")}
stats = {"开始日期": dret["date"].min().strftime("%Y%m%d"), "结束日期": dret["date"].max().strftime("%Y%m%d")}
stats.update(daily_performance(dret["total"]))
dfp = pd.concat([v["pairs"] for k, v in res.items() if k in symbols], ignore_index=True)
pairs_stats = evaluate_pairs(dfp)
Expand Down
193 changes: 85 additions & 108 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@
from sklearn.linear_model import LinearRegression


def __stats_style(stats):
stats = stats.style.background_gradient(cmap="RdYlGn_r", axis=None, subset=["年化"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["绝对收益"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["夏普"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["最大回撤"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["卡玛"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["年化波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["下行波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["盈亏平衡点"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日胜率"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日盈亏比"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日赢面"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["非零覆盖"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["新高间隔"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["回撤风险"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["新高占比"])
stats = stats.format(
{
"盈亏平衡点": "{:.2f}",
"年化波动率": "{:.2%}",
"下行波动率": "{:.2%}",
"最大回撤": "{:.2%}",
"卡玛": "{:.2f}",
"年化": "{:.2%}",
"夏普": "{:.2f}",
"非零覆盖": "{:.2%}",
"绝对收益": "{:.2%}",
"日胜率": "{:.2%}",
"日盈亏比": "{:.2f}",
"日赢面": "{:.2%}",
"新高间隔": "{:.2f}",
"回撤风险": "{:.2f}",
"新高占比": "{:.2%}",
}
)
return stats


def show_daily_return(df: pd.DataFrame, **kwargs):
"""用 streamlit 展示日收益
Expand Down Expand Up @@ -50,36 +88,41 @@ def _stats(df_, type_="持有日"):
stats.append(col_stats)

stats = pd.DataFrame(stats).set_index("日收益名称")
stats = stats.style.background_gradient(cmap="RdYlGn_r", axis=None, subset=["年化"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["绝对收益"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["夏普"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["最大回撤"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["卡玛"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["年化波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["下行波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["盈亏平衡点"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日胜率"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["非零覆盖"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["新高间隔"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["回撤风险"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["新高占比"])
stats = stats.format(
{
"盈亏平衡点": "{:.2f}",
"年化波动率": "{:.2%}",
"下行波动率": "{:.2%}",
"最大回撤": "{:.2%}",
"卡玛": "{:.2f}",
"年化": "{:.2%}",
"夏普": "{:.2f}",
"非零覆盖": "{:.2%}",
"绝对收益": "{:.2%}",
"日胜率": "{:.2%}",
"新高间隔": "{:.2f}",
"回撤风险": "{:.2f}",
"新高占比": "{:.2%}",
}
)
stats = __stats_style(stats)
# stats = stats.style.background_gradient(cmap="RdYlGn_r", axis=None, subset=["年化"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["绝对收益"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["夏普"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["最大回撤"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["卡玛"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["年化波动率"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["下行波动率"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["盈亏平衡点"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日胜率"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日盈亏比"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日赢面"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["非零覆盖"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["新高间隔"])
# stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["回撤风险"])
# stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["新高占比"])
# stats = stats.format(
# {
# "盈亏平衡点": "{:.2f}",
# "年化波动率": "{:.2%}",
# "下行波动率": "{:.2%}",
# "最大回撤": "{:.2%}",
# "卡玛": "{:.2f}",
# "年化": "{:.2%}",
# "夏普": "{:.2f}",
# "非零覆盖": "{:.2%}",
# "绝对收益": "{:.2%}",
# "日胜率": "{:.2%}",
# "日盈亏比": "{:.2f}",
# "日赢面": "{:.2%}",
# "新高间隔": "{:.2f}",
# "回撤风险": "{:.2f}",
# "新高占比": "{:.2%}",
# }
# )
return stats

use_st_table = kwargs.get("use_st_table", False)
Expand Down Expand Up @@ -469,7 +512,9 @@ def show_weight_backtest(dfw, **kwargs):
st.divider()

dret = wb.results["品种等权日收益"].copy()
dret.index = pd.to_datetime(dret.index)
dret["dt"] = pd.to_datetime(dret["date"])
dret = dret.set_index("dt").drop(columns=["date"])
# dret.index = pd.to_datetime(dret.index)
show_daily_return(dret, legend_only_cols=dfw["symbol"].unique().tolist(), **kwargs)

if kwargs.get("show_drawdowns", False):
Expand Down Expand Up @@ -538,53 +583,16 @@ def show_splited_daily(df, ret_col, **kwargs):
rows = []
for name, sdt in sdt_map.items():
df1 = df.loc[sdt:last_dt].copy()
row = czsc.daily_performance(df1[ret_col], yearly_days=yearly_days)
row["开始日期"] = sdt.strftime("%Y-%m-%d")
row["结束日期"] = last_dt.strftime("%Y-%m-%d")
row["收益名称"] = name
# row['绝对收益'] = df1[ret_col].sum()
row = {
"收益名称": name,
"开始日期": sdt.strftime("%Y-%m-%d"),
"结束日期": last_dt.strftime("%Y-%m-%d"),
}
row_ = czsc.daily_performance(df1[ret_col], yearly_days=yearly_days)
row.update(row_)
rows.append(row)
dfv = pd.DataFrame(rows).set_index("收益名称")
cols = [
"开始日期",
"结束日期",
"绝对收益",
"年化",
"夏普",
"最大回撤",
"卡玛",
"年化波动率",
"下行波动率",
"非零覆盖",
"日胜率",
"盈亏平衡点",
]
dfv = dfv[cols].copy()

dfv = dfv.style.background_gradient(cmap="RdYlGn_r", subset=["绝对收益"])
dfv = dfv.background_gradient(cmap="RdYlGn_r", subset=["年化"])
dfv = dfv.background_gradient(cmap="RdYlGn_r", subset=["夏普"])
dfv = dfv.background_gradient(cmap="RdYlGn", subset=["最大回撤"])
dfv = dfv.background_gradient(cmap="RdYlGn_r", subset=["卡玛"])
dfv = dfv.background_gradient(cmap="RdYlGn", subset=["年化波动率"])
dfv = dfv.background_gradient(cmap="RdYlGn", subset=["下行波动率"])
dfv = dfv.background_gradient(cmap="RdYlGn", subset=["盈亏平衡点"])
dfv = dfv.background_gradient(cmap="RdYlGn_r", subset=["日胜率"])
dfv = dfv.background_gradient(cmap="RdYlGn_r", subset=["非零覆盖"])
dfv = dfv.format(
{
"盈亏平衡点": "{:.2f}",
"年化波动率": "{:.2%}",
"下行波动率": "{:.2%}",
"最大回撤": "{:.2%}",
"卡玛": "{:.2f}",
"年化": "{:.2%}",
"夏普": "{:.2f}",
"非零覆盖": "{:.2%}",
"日胜率": "{:.2%}",
"绝对收益": "{:.2%}",
}
)
dfv = __stats_style(dfv)
st.dataframe(dfv, use_container_width=True)


Expand Down Expand Up @@ -615,38 +623,7 @@ def show_yearly_stats(df, ret_col, **kwargs):
_stats.append(_yst)

stats = pd.DataFrame(_stats).set_index("年份")

stats = stats.style.background_gradient(cmap="RdYlGn_r", axis=None, subset=["年化"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["夏普"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["绝对收益"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["最大回撤"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["卡玛"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["年化波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["下行波动率"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["盈亏平衡点"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["日胜率"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["非零覆盖"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["新高间隔"])
stats = stats.background_gradient(cmap="RdYlGn", axis=None, subset=["回撤风险"])
stats = stats.background_gradient(cmap="RdYlGn_r", axis=None, subset=["新高占比"])

stats = stats.format(
{
"盈亏平衡点": "{:.2f}",
"年化波动率": "{:.2%}",
"下行波动率": "{:.2%}",
"最大回撤": "{:.2%}",
"卡玛": "{:.2f}",
"年化": "{:.2%}",
"夏普": "{:.2f}",
"非零覆盖": "{:.2%}",
"绝对收益": "{:.2%}",
"日胜率": "{:.2%}",
"新高间隔": "{:.2f}",
"回撤风险": "{:.2f}",
"新高占比": "{:.2%}",
}
)
stats = __stats_style(stats)

sub_title = kwargs.get("sub_title", "")
if sub_title:
Expand Down
11 changes: 9 additions & 2 deletions examples/Streamlit组件库使用案例/weight_backtest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import sys

sys.path.insert(0, ".")
sys.path.insert(0, "..")

import czsc
import pandas as pd
import streamlit as st

st.set_page_config(layout="wide")

st.write(czsc.__version__)
dfw = pd.read_feather(r"C:\Users\zengb\Downloads\ST组件样例数据\时序持仓权重样例数据.feather")

st.subheader("时序持仓策略回测样例", anchor="时序持仓策略回测样例", divider="rainbow")
czsc.show_weight_backtest(dfw, fee=2, digits=2, show_drawdowns=True, show_monthly_return=True, show_yearly_stats=True)
czsc.show_weight_backtest(
dfw, fee=2, digits=2, show_drawdowns=True, show_monthly_return=True, show_yearly_stats=True, show_splited_daily=True
)

0 comments on commit aacbf3b

Please sign in to comment.