Skip to content

Commit

Permalink
0.9.34 新增 show_weight_backtest
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Nov 4, 2023
1 parent e09e02b commit 9d48ef6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
show_factor_returns,
show_factor_layering,
show_symbol_factor_layering,
show_weight_backtest,
)

from czsc.utils.bi_info import (
Expand All @@ -106,6 +107,7 @@
from czsc.utils.features import (
normalize_feature,
normalize_ts_feature,
feture_cross_layering,
)

__version__ = "0.9.34"
Expand Down
47 changes: 47 additions & 0 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,50 @@ def show_symbol_factor_layering(df, x_col, y_col='n1b', **kwargs):
dfr['空头'] = -dfr[short].sum(axis=1)
dfr['多空'] = dfr['多头'] + dfr['空头']
show_daily_return(dfr[['多头', '空头', '多空']])


@st.cache_data(ttl=3600 * 24)
def show_weight_backtest(dfw, **kwargs):
"""展示权重回测结果
:param dfw: 回测数据,任何字段都不允许有空值;数据样例:
=================== ======== ======== =======
dt symbol weight price
=================== ======== ======== =======
2019-01-02 09:01:00 DLi9001 0.5 961.695
2019-01-02 09:02:00 DLi9001 0.25 960.72
2019-01-02 09:03:00 DLi9001 0.25 962.669
2019-01-02 09:04:00 DLi9001 0.25 960.72
2019-01-02 09:05:00 DLi9001 0.25 961.695
=================== ======== ======== =======
:param kwargs:
- fee: 单边手续费,单位为BP,默认为2BP
"""
fee = kwargs.get("fee", 2)
if (dfw.isnull().sum().sum() > 0) or (dfw.isna().sum().sum() > 0):
st.warning("数据中存在空值,请检查数据后再试")
st.stop()

from czsc.traders.weight_backtest import WeightBacktest

wb = WeightBacktest(dfw, fee=fee / 10000)
stat = wb.results['绩效评价']

st.divider()
c1, c2, c3, c4, c5, c6, c7, c8 = st.columns([1, 1, 1, 1, 1, 1, 1, 1])
c1.metric("盈亏平衡点", f"{stat['盈亏平衡点']:.2%}")
c2.metric("单笔收益", f"{stat['单笔收益']} BP")
c3.metric("交易胜率", f"{stat['交易胜率']:.2%}")
c4.metric("持仓K线数", f"{stat['持仓K线数']}")
c5.metric("最大回撤", f"{stat['最大回撤']:.2%}")
c6.metric("年化收益率", f"{stat['年化']:.2%}")
c7.metric("夏普比率", f"{stat['夏普']:.2f}")
c8.metric("卡玛比率", f"{stat['卡玛']:.2f}")
st.divider()

dret = wb.results['品种等权日收益']
dret.index = pd.to_datetime(dret.index)
show_daily_return(dret, legend_only_cols=dfw['symbol'].unique().tolist())
4 changes: 0 additions & 4 deletions examples/test_offline/debug_weight_backtest.py

This file was deleted.

0 comments on commit 9d48ef6

Please sign in to comment.