Skip to content

Commit

Permalink
0.9.41 优化 streamlit 组件
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Jan 21, 2024
1 parent b855775 commit 67ac046
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
8 changes: 5 additions & 3 deletions czsc/connectors/cooperation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):
raise ValueError(f"symbol {symbol} 无法识别,获取数据失败!")


def stocks_daily_klines(years=None, **kwargs):
@czsc.disk_cache(path=cache_path, ttl=-1)
def stocks_daily_klines(sdt='20170101', edt="20240101", **kwargs):
"""获取全市场A股的日线数据"""
adj = kwargs.get('adj', 'hfq')
if years is None:
years = ['2017', '2018', '2019', '2020', '2021', '2022', '2023']
sdt = pd.to_datetime(sdt).year
edt = pd.to_datetime(edt).year
years = [str(year) for year in range(sdt, edt + 1)]

res = []
for year in years:
Expand Down
56 changes: 44 additions & 12 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,34 @@ def _stats(df_, type_='持有日'):
col_stats['日收益名称'] = col
stats.append(col_stats)
stats = pd.DataFrame(stats).set_index('日收益名称')
fmt_cols = ['年化', '夏普', '最大回撤', '卡玛', '年化波动率', '非零覆盖', '日胜率', '盈亏平衡点']
stats = stats.style.background_gradient(cmap='RdYlGn_r', axis=None, subset=fmt_cols).format('{:.4f}')
# fmt_cols = ['年化', '夏普', '最大回撤', '卡玛', '年化波动率', '非零覆盖', '日胜率', '盈亏平衡点', '新高间隔', '新高占比']
# stats = stats.style.background_gradient(cmap='RdYlGn_r', axis=None, subset=fmt_cols).format('{:.4f}')

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', 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.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.format(
{
'盈亏平衡点': '{:.2f}',
'年化波动率': '{:.2%}',
'最大回撤': '{:.2%}',
'卡玛': '{:.2f}',
'年化': '{:.2%}',
'夏普': '{:.2f}',
'非零覆盖': '{:.2%}',
'日胜率': '{:.2%}',
'新高间隔': '{:.2f}',
'新高占比': '{:.2%}',
}
)
return stats

with st.container():
Expand All @@ -50,15 +76,12 @@ def _stats(df_, type_='持有日'):
st.subheader(title)
st.divider()

st.write("交易日绩效指标")
st.dataframe(_stats(df, type_='交易日'), use_container_width=True)

if kwargs.get("stat_hold_days", True):
col1, col2 = st.columns([1, 1])
col1.write("交易日绩效指标")
col1.dataframe(_stats(df, type_='交易日'), use_container_width=True)
col2.write("持有日绩效指标")
col2.dataframe(_stats(df, type_='持有日'), use_container_width=True)
else:
st.write("绩效指标")
st.dataframe(_stats(df, type_='交易日'), use_container_width=True)
st.write("持有日绩效指标")
st.dataframe(_stats(df, type_='持有日'), use_container_width=True)

df = df.cumsum()
fig = px.line(df, y=df.columns.to_list(), title="日收益累计曲线")
Expand Down Expand Up @@ -339,11 +362,20 @@ def show_weight_backtest(dfw, **kwargs):
dret.index = pd.to_datetime(dret.index)
show_daily_return(dret, legend_only_cols=dfw['symbol'].unique().tolist())

if kwargs.get("show_daily_detail", False):
with st.expander("查看品种等权日收益详情", expanded=False):
if kwargs.get("show_backtest_detail", False):
c1, c2 = st.columns([1, 1])
with c1.expander("品种等权日收益", expanded=False):
df_ = wb.results['品种等权日收益'].copy()
st.dataframe(df_.style.background_gradient(cmap='RdYlGn_r').format("{:.2%}"), use_container_width=True)

with c2.expander("查看开平交易对", expanded=False):
dfp = pd.concat([v['pairs'] for k, v in wb.results.items() if k in wb.symbols], ignore_index=True)
st.dataframe(dfp, use_container_width=True)

if kwargs.get("show_splited_daily", False):
with st.expander("品种等权日收益分段表现", expanded=False):
show_splited_daily(dret[['total']].copy(), ret_col='total')

return wb


Expand Down

0 comments on commit 67ac046

Please sign in to comment.