-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
251 lines (215 loc) · 11 KB
/
Copy pathapp.py
File metadata and controls
251 lines (215 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
多因子策略平台 — Streamlit 本地应用
用法: streamlit run app.py
"""
import streamlit as st
import pandas as pd, time, os
from lqtp_connector import LQTPConnector
from factor_scanner import FactorScanner
from strategy_builder import StrategyBuilder
from portfolio_output import PortfolioOutput
st.set_page_config(page_title="多因子策略平台", layout="wide")
st.title("多因子策略平台")
# ---- Session State (热重载安全) ----
for key in ["token", "connector", "scan_df", "output_df"]:
if key not in st.session_state:
st.session_state[key] = None
if "selected_factors" not in st.session_state or not isinstance(st.session_state.selected_factors, dict):
st.session_state.selected_factors = {}
# ===== SIDEBAR: Login =====
with st.sidebar:
st.header("LQTP 连接")
server = st.text_input("服务器地址", "118.89.191.220:50051")
username = st.text_input("用户名", placeholder="输入邮箱/用户名")
password = st.text_input("密码", type="password")
if st.button("登录", type="primary"):
try:
conn = LQTPConnector(server)
token = conn.login(username, password)
st.session_state.connector = conn
st.session_state.token = token
st.success(f"登录成功")
except Exception as e:
st.error(f"登录失败: {e}")
if st.session_state.token:
st.info(f"Token: {st.session_state.token[:30]}...")
# ===== MAIN =====
if st.session_state.connector is None:
st.info("请在左侧登录")
st.stop()
conn = st.session_state.connector
scanner = FactorScanner(conn)
builder = StrategyBuilder()
tab1, tab2, tab3 = st.tabs(["因子扫描", "策略构建", "组合输出"])
# ===== TAB 1: 因子扫描 =====
with tab1:
st.header("因子 IC 扫描")
# Scan controls
ctrl_col, _ = st.columns([1, 2])
with ctrl_col:
c1, c2 = st.columns(2)
with c1:
ic_begin = st.text_input("IC 起始日", "20240102")
with c2:
ic_end = st.text_input("IC 结束日", "20260615")
neutralize_scan = st.selectbox("中性化", ["无", "行业", "市值(暂不可用)", "行业+市值(暂不可用)"],
help="行业中性化可用(ICIR更高); 市值需管理员补建 StockCapitalDaily 表")
with ctrl_col:
if st.button("开始扫描", type="primary"):
neu_map_scan = {"无": "", "行业": "industry", "市值(暂不可用)": "", "行业+市值(暂不可用)": ""}
neu = neu_map_scan[neutralize_scan]
with st.spinner("正在拉取因子列表..."):
factors = conn.list_factors()
st.session_state.all_factors = factors
st.success(f"获取 {len(factors)} 个因子")
begin = int(ic_begin); end = int(ic_end)
cached = scanner.load_cache(begin, end)
if cached.empty:
st.info("无缓存,全量扫描")
else:
new_count = sum(1 for f in factors if f.get("name","") not in set(cached["name"]))
if new_count > 0 and st.button(f"增量更新 ({new_count} 个新因子, 约{new_count*0.3:.0f}秒)", key="incr_scan"):
with st.spinner(f"增量扫描 {new_count} 个新因子..."):
merged = scanner.scan_new_only(factors, cached, begin, end, neutralize=neu)
scanner.save_cache(merged, begin, end)
st.session_state.scan_df = merged
st.success(f"更新完成! {len(merged)} 个因子")
if st.button("强制全量重扫", key="force_scan"):
cached = pd.DataFrame()
if cached.empty:
with st.spinner(f"全量扫描 {len(factors)} 个因子 ({ic_begin}~{ic_end}, 约需 {len(factors)*0.3:.0f}秒)..."):
progress_bar = st.progress(0)
status = st.empty()
def update(i, total, name):
progress_bar.progress(i / total)
status.text(f"[{i}/{total}] {name}")
df = scanner.scan_all(factors, begin, end, neutralize=neu, progress_callback=update)
scanner.save_cache(df, begin, end)
st.session_state.scan_df = df
progress_bar.empty()
status.empty()
st.success(f"完成! {len(df)} 个因子有效")
elif st.session_state.scan_df is None:
st.session_state.scan_df = cached
st.success(f"缓存加载 {len(cached)} 条 ({ic_begin}~{ic_end})")
st.session_state.scan_neutralize = neu # remember for strategy
# Results table + selection
df = st.session_state.scan_df
if df is not None and not df.empty:
# Filter by IC threshold
ic_min = 0.025
df_filtered = df[df["abs_ic"] > ic_min].copy()
st.caption(f"|IC| > {ic_min}: {len(df_filtered)} 个因子 (全部 {len(df)} 个)")
center_col, right_col = st.columns([3, 1])
with center_col:
# Column header
h1, h2, h3, h4, h5, h6 = st.columns([3, 1, 1, 1, 1, 0.8])
with h1: st.caption("因子名称")
with h2: st.caption("IC")
with h3: st.caption("ICIR")
with h4: st.caption("有效%")
with h5: st.caption("覆盖率")
with h6: st.caption("选择")
for i, (_, row) in enumerate(df_filtered.iterrows()):
name = row["name"]
uid = row.get("definition_id", str(i))
c1, c2, c3, c4, c5, c6 = st.columns([3, 1, 1, 1, 1, 0.8])
with c1:
st.text(name[:50])
with c2:
st.text(f"{row['ic']:+.4f}")
with c3:
st.text(f"{row['icir']:+.3f}")
with c4:
st.text(f"{row['eff']:.1%}")
with c5:
st.text(f"{row['coverage']:.3f}")
with c6:
if name in st.session_state.selected_factors:
if st.button("✅", key=f"rm_{uid}", help=f"移除 {name[:20]}"):
st.session_state.selected_factors.pop(name, None)
st.rerun()
else:
if st.button("+", key=f"add_{uid}", help=f"添加 {name[:20]}"):
st.session_state.selected_factors[name] = {
"formula": row["formula"], "weight": 1.0,
"direction": 1 if row["ic"] > 0 else -1,
}
st.rerun()
with right_col:
st.subheader(f"已选因子 ({len(st.session_state.selected_factors)})")
if st.session_state.selected_factors:
for name in st.session_state.selected_factors:
st.text(f"• {name[:30]}")
else:
st.caption("尚未选择")
# ===== TAB 2: 策略构建 =====
with tab2:
st.header("策略构建")
if not st.session_state.selected_factors:
st.info("请先在因子扫描页选择因子")
else:
st.subheader(f"已选 {len(st.session_state.selected_factors)} 个因子")
for idx, (fname, finfo) in enumerate(list(st.session_state.selected_factors.items())):
c1, c2, c3 = st.columns([3, 1, 1])
with c1:
st.text(fname[:60])
with c2:
w = st.number_input("权重", 0.0, 10.0, finfo["weight"], 0.1, key=f"w_{idx}")
st.session_state.selected_factors[fname]["weight"] = w
with c3:
if st.button("删除", key=f"del_{idx}"):
st.session_state.selected_factors.pop(fname)
st.rerun()
st.divider()
st.caption("策略公式预览: 默认对每个因子做 rank 截面标准化,然后加权合成再 rank 选 Top-N")
# ===== TAB 3: 组合输出 =====
with tab3:
st.header("组合输出")
if not st.session_state.selected_factors:
st.info("请先在因子扫描页选择因子并构建策略")
else:
target_n = st.number_input("标的数量", 10, 200, 50, 10)
rebalance = st.number_input("调仓频率 (交易日)", 1, 20, 1, 1, help="1=每日调仓, 3=每3天调一次")
# 板块选择
st.caption("板块过滤(默认全选)")
c1, c2, c3, c4 = st.columns(4)
with c1: sel_sh = st.checkbox("沪主板 (6xx)", True)
with c2: sel_sz = st.checkbox("深主板 (0xx)", True)
with c3: sel_cy = st.checkbox("创业板 (3xx)", True)
with c4: sel_kc = st.checkbox("科创板 (68x)", True)
selected_sectors = []
if sel_sh: selected_sectors.append("沪主板")
if sel_sz: selected_sectors.append("深主板")
if sel_cy: selected_sectors.append("创业板")
if sel_kc: selected_sectors.append("科创板")
c5, c6 = st.columns(2)
with c5: neutralize_out = st.selectbox("中性化", ["无", "行业", "市值(暂不可用)", "行业+市值(暂不可用)"])
with c6: norm_method = st.selectbox("截面标准化", ["rank", "zscore"],
help="rank=百分位排名(稳健), zscore=标准化(敏感)")
col1, col2 = st.columns(2)
with col1: begin_date = st.text_input("起始日期", "20260501")
with col2: end_date = st.text_input("结束日期", "20260531")
if st.button("生成组合", type="primary"):
factors = {k: v["formula"] for k, v in st.session_state.selected_factors.items()}
weights = {k: v["weight"] for k, v in st.session_state.selected_factors.items()}
directions = {k: v.get("direction", 1) for k, v in st.session_state.selected_factors.items()}
neu_map_out = {"无": "", "行业": "industry", "市值(暂不可用)": "", "行业+市值(暂不可用)": ""}
with st.spinner(f"生成 {begin_date}~{end_date} 组合..."):
df = PortfolioOutput.generate_batch(
conn, factors, weights, directions, builder,
int(begin_date), int(end_date), target_n,
rebalance_days=rebalance, warmup=120,
neutralize=neu_map_out[neutralize_out],
normalize_method=norm_method,
sectors=selected_sectors if len(selected_sectors)<4 else None,
progress_callback=lambda d: st.text(f"已完成 {d} 天"))
if not df.empty:
st.session_state.output_df = df
st.success(f"完成! {df['trade_date'].nunique()} 天, {len(df)} 行")
st.dataframe(df.head(20), use_container_width=True)
# Download
csv = df.to_csv(index=False)
st.download_button("下载 CSV", csv, f"portfolio_{begin_date}_{end_date}.csv", "text/csv")
else:
st.error("生成失败,请检查日期范围是否有数据")