Skip to content

Commit

Permalink
0.9.62 新增 show_weight_distribution 组件
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Dec 21, 2024
1 parent 5fec0be commit 7ab3320
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
show_classify,
show_df_describe,
show_date_effect,
show_weight_distribution,
)

from czsc.utils.bi_info import (
Expand Down
24 changes: 24 additions & 0 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ def show_factor_layering(df, factor, target="n1b", **kwargs):
)


def show_weight_distribution(dfw, abs_weight=True, **kwargs):
"""展示权重分布
:param dfw: pd.DataFrame, 包含 symbol, dt, price, weight 列
:param abs_weight: bool, 是否取权重的绝对值
:param kwargs:
- percentiles: list, 分位数
"""
dfw = dfw.copy()
if abs_weight:
dfw["weight"] = dfw["weight"].abs()

default_percentiles = [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95]
percentiles = kwargs.get("percentiles", default_percentiles)

dfs = dfw.groupby("symbol").apply(lambda x: x["weight"].describe(percentiles=percentiles)).reset_index()
show_df_describe(dfs)


def show_symbol_factor_layering(df, x_col, y_col="n1b", **kwargs):
"""使用 streamlit 绘制单个标的上的因子分层收益率图
Expand Down Expand Up @@ -568,6 +588,10 @@ def show_weight_backtest(dfw, **kwargs):
with st.expander("月度累计收益", expanded=False):
show_monthly_return(dret, ret_col="total", sub_title="")

if kwargs.get("show_weight_distribution", True):
with st.expander("策略分品种的 weight 分布", expanded=False):
show_weight_distribution(dfw, abs_weight=True)

return wb


Expand Down

0 comments on commit 7ab3320

Please sign in to comment.