Skip to content

Commit

Permalink
0.9.58 优化streamlit组件
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Aug 31, 2024
1 parent 3aaa9cb commit c27973f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
1 change: 1 addition & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
remove_beta_effects, vwap, twap,
cross_sectional_strategy,
judge_factor_direction,
monotonicity,
)


Expand Down
11 changes: 11 additions & 0 deletions czsc/eda.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,14 @@ def judge_factor_direction(df: pd.DataFrame, factor, target='n1b', by='symbol',
dfc = df.groupby(by)[[factor, target]].corr(method=method).unstack().iloc[:, 1].reset_index()
return "positive" if dfc[factor].mean().iloc[0] >= 0 else "negative"


def monotonicity(sequence):
"""计算序列的单调性
原理:计算序列与自然数序列的相关系数,系数越接近1,表示单调递增;系数越接近-1,表示单调递减;接近0表示无序
:param sequence: list, tuple 序列
:return: float, 单调性系数
"""
from scipy.stats import spearmanr
return spearmanr(sequence, range(len(sequence)))[0]
26 changes: 24 additions & 2 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 飞书文档:https://s0cqcxuy3p.feishu.cn/wiki/AATuw5vN7iN9XbkVPuwcE186n9f

import czsc
import hashlib
import optuna
Expand Down Expand Up @@ -195,8 +197,10 @@ def show_sectional_ic(df, x_col, y_col, method="pearson", **kwargs):
:param y_col: str,收益列名
:param method: str,计算IC的方法,可选 pearson 和 spearman
:param kwargs:
- show_cumsum_ic: bool,是否展示累计IC曲线,默认为 True
- show_factor_histgram: bool,是否展示因子数据分布图,默认为 False
"""
dfc, res = czsc.cross_sectional_ic(df, x_col=x_col, y_col=y_col, dt_col="dt", method=method)

Expand Down Expand Up @@ -325,7 +329,6 @@ def show_factor_layering(df, factor, target="n1b", **kwargs):
:param kwargs:
- n: 分层数量,默认为10
"""
n = kwargs.get("n", 10)
df = czsc.feture_cross_layering(df, factor, n=n)
Expand All @@ -335,7 +338,26 @@ def show_factor_layering(df, factor, target="n1b", **kwargs):
if "第00层" in mrr.columns:
mrr.drop(columns=["第00层"], inplace=True)

czsc.show_daily_return(mrr, stat_hold_days=False)
# 计算每层的累计收益率
dfc = mrr.sum(axis=0).to_frame("绝对收益")

dfc["text"] = dfc["绝对收益"].apply(lambda x: f"{x:.2%}")
fig = px.bar(
dfc,
y="绝对收益",
title="因子分层绝对收益 | 单调性:{:.2%}".format(czsc.monotonicity(dfc["绝对收益"])),
color="绝对收益",
color_continuous_scale="RdYlGn_r",
text="text",
)
st.plotly_chart(fig, use_container_width=True)

czsc.show_daily_return(
mrr,
stat_hold_days=False,
yearly_days=kwargs.get("yearly_days", 252),
show_dailys=kwargs.get("show_dailys", False),
)


def show_symbol_factor_layering(df, x_col, y_col="n1b", **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions examples/Streamlit组件库使用案例/因子分析.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import czsc
import pandas as pd
import streamlit as st

st.set_page_config(layout="wide")

df = pd.read_feather(r"C:\Users\zengb\Downloads\ST组件样例数据\因子数据样例.feather")
factor = [x for x in df.columns if x.startswith("F#")][0]
# czsc.show_factor_layering(df, factor=factor, target="n1b", n=10)

# czsc.show_feature_returns(df, factor, target="n1b", fit_intercept=True)

czsc.show_sectional_ic(df, factor, "n1b", show_factor_histgram=True, show_cumsum_ic=True)
31 changes: 19 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
requests>=2.24.0
pyecharts>=1.9.1
tqdm
tqdm>=4.66.4
pandas>=1.1.0
numpy>=1.16.5
tushare
tushare>=1.4.6
python-docx>=0.8.11
matplotlib
matplotlib>=3.9.0
seaborn
Deprecated>=1.2.12
scikit-learn>=0.24.1
dill
dill>=0.3.8
openpyxl
pyarrow
loguru>=0.6.0
click
pytest
click>=8.1.7
pytest>=8.2.1
tenacity>=8.1.0
requests-toolbelt>=0.10.1
plotly>=5.11.0
parse>=1.19.0
lightgbm>=4.0.0
streamlit
redis
oss2
statsmodels
optuna
cryptography
streamlit>=1.37.1
redis>=5.0.4
oss2>=2.18.5
statsmodels>=0.14.2
optuna>=3.6.1
cryptography>=42.0.7
czsc>=0.9.58
tqsdk>=3.6.0
pyautogui>=0.9.54
pytz>=2024.1
flask>=3.0.3
setuptools>=68.0.0
scipy>=1.13.1

0 comments on commit c27973f

Please sign in to comment.