-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.py
More file actions
355 lines (302 loc) · 10.3 KB
/
app.py
File metadata and controls
355 lines (302 loc) · 10.3 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
QLib因子分析与回测可视化平台
主应用入口
"""
import streamlit as st
import sys
import os
from pathlib import Path
# 页面配置
st.set_page_config(
page_title="QLib因子分析平台",
page_icon="📊",
layout="wide",
initial_sidebar_state="expanded"
)
# 自定义CSS样式
st.markdown("""
<style>
.main-header {
font-size: 2.5rem;
color: #1f77b4;
text-align: center;
margin-bottom: 2rem;
}
.section-header {
font-size: 1.5rem;
color: #2c3e50;
margin-top: 2rem;
margin-bottom: 1rem;
}
.metric-card {
background-color: #f8f9fa;
padding: 1rem;
border-radius: 0.5rem;
border-left: 4px solid #1f77b4;
}
.success-message {
background-color: #d4edda;
color: #155724;
padding: 1rem;
border-radius: 0.25rem;
border: 1px solid #c3e6cb;
}
.warning-message {
background-color: #fff3cd;
color: #856404;
padding: 1rem;
border-radius: 0.25rem;
border: 1px solid #ffeaa7;
}
.error-message {
background-color: #f8d7da;
color: #721c24;
padding: 1rem;
border-radius: 0.25rem;
border: 1px solid #f5c6cb;
}
</style>
""", unsafe_allow_html=True)
def main():
"""主应用入口"""
# 侧边栏导航
st.sidebar.markdown("# 📊 QLib因子分析平台")
st.sidebar.markdown("---")
# 导航菜单
page_options = {
"🏠 首页": "home",
"📈 数据管理": "data",
"🧮 因子管理": "factors",
"📊 因子分析": "analysis",
"🎯 策略回测": "backtest",
"⚙️ 系统设置": "settings"
}
selected_page = st.sidebar.selectbox(
"选择功能模块",
list(page_options.keys()),
index=0
)
page_key = page_options[selected_page]
# 页面内容
if page_key == "home":
show_home_page()
elif page_key == "data":
from qlib_factor_platform.ui.data_page import show_data_page
show_data_page()
elif page_key == "factors":
from qlib_factor_platform.ui.factor_page import show_factor_page
show_factor_page()
elif page_key == "analysis":
from qlib_factor_platform.ui.analysis_page import show_analysis_page
show_analysis_page()
elif page_key == "backtest":
from qlib_factor_platform.ui.backtest_page import show_backtest_page
show_backtest_page()
elif page_key == "settings":
show_settings_page()
def show_home_page():
"""显示首页"""
st.markdown('<h1 class="main-header">QLib因子分析与回测可视化平台</h1>', unsafe_allow_html=True)
# 平台介绍
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("### 🎯 平台简介")
st.markdown("""
本平台基于微软QLib框架构建,提供一站式的量化因子研究与策略回测解决方案:
- **📈 数据管理**:集成akshare数据源,支持A股实时行情获取与本地缓存
- **🧮 因子管理**:内置Alpha158/360等经典因子,支持自定义因子编辑
- **📊 因子分析**:IC分析、分层回测、相关性分析等完整因子评价体系
- **🎯 策略回测**:可视化策略配置、绩效分析、风险评估
- **📱 用户友好**:Streamlit界面,操作简单,结果直观
""")
st.markdown("### 🚀 快速开始")
st.markdown("""
1. **数据准备**:在"数据管理"中配置股票代码和时间范围
2. **因子选择**:在"因子管理"中选择或创建因子
3. **因子分析**:在"因子分析"中评估因子有效性
4. **策略回测**:在"策略回测"中配置参数并运行回测
""")
with col2:
st.markdown("### 📋 系统状态")
# 检查QLib状态
try:
import qlib
qlib.init()
qlib_status = "✅ 已安装"
qlib_version = qlib.__version__
except:
qlib_status = "❌ 未安装"
qlib_version = "N/A"
st.metric("QLib状态", qlib_status)
st.metric("QLib版本", qlib_version)
# 检查数据状态
data_path = "~/.qlib/qlib_data/cn_data"
if os.path.exists(os.path.expanduser(data_path)):
data_status = "✅ 已就绪"
else:
data_status = "⚠️ 需要初始化"
st.metric("数据状态", data_status)
# 检查依赖包
required_packages = ["streamlit", "plotly", "pandas", "numpy"]
missing_packages = []
for pkg in required_packages:
try:
__import__(pkg)
except ImportError:
missing_packages.append(pkg)
if not missing_packages:
deps_status = "✅ 完整"
else:
deps_status = f"⚠️ 缺失: {', '.join(missing_packages)}"
st.metric("依赖状态", deps_status)
# 功能特性展示
st.markdown("---")
st.markdown("### ✨ 核心功能")
feature_cols = st.columns(4)
features = [
("📊", "数据可视化", "K线图、因子分布、绩效曲线"),
("🧮", "因子计算", "158+预置因子,支持自定义"),
("📈", "IC分析", "信息系数、IR比率、分层收益"),
("🎯", "策略回测", "多策略支持,详细绩效报告"),
("💾", "数据缓存", "本地存储,避免重复下载"),
("🔧", "参数调优", "可视化参数配置与优化"),
("📱", "界面友好", "响应式设计,操作简单"),
("🚀", "高性能", "并行计算,快速响应")
]
for i, (icon, title, desc) in enumerate(features):
with feature_cols[i % 4]:
st.markdown(f"""
<div class="metric-card">
<h3>{icon} {title}</h3>
<p>{desc}</p>
</div>
""", unsafe_allow_html=True)
# 使用指南
st.markdown("---")
st.markdown("### 📖 使用指南")
with st.expander("🔧 环境配置指南"):
st.markdown("""
**1. 安装依赖包**
```bash
pip install qlib streamlit plotly pandas numpy akshare
```
**2. 初始化QLib数据**
```python
import qlib
from qlib.constant import REG_CN
qlib.init(provider_uri="~/.qlib/qlib_data/cn_data", region=REG_CN)
```
**3. 启动应用**
```bash
streamlit run app.py
```
""")
with st.expander("📚 因子开发指南"):
st.markdown("""
**自定义因子示例**
```python
# 动量因子
momentum = "Ref($close, -20) / $close - 1"
# 波动率因子
volatility = "Std($close, 20) / Mean($close, 20)"
# 成交量因子
volume_factor = "Mean($volume, 5) / Mean($volume, 20)"
```
""")
with st.expander("🎯 策略配置指南"):
st.markdown("""
**策略参数说明**
- **topk**: 选择因子值最高的前N只股票
- **n_drop**: 每次调仓时随机剔除的股票数量
- **调仓频率**: 支持1d、1w、1m等周期
- **交易成本**: 佣金、印花税等费用设置
""")
def show_settings_page():
"""显示设置页面"""
st.markdown('<h1 class="main-header">⚙️ 系统设置</h1>', unsafe_allow_html=True)
st.markdown("### 📊 基础配置")
# 数据路径配置
with st.expander("📁 数据路径配置"):
provider_uri = st.text_input(
"QLib数据路径",
value="~/.qlib/qlib_data/cn_data",
help="QLib数据存储路径,支持相对路径和绝对路径"
)
cache_dir = st.text_input(
"缓存目录",
value="./cache",
help="应用缓存数据存储目录"
)
if st.button("保存路径配置"):
# TODO: 实现配置保存逻辑
st.success("路径配置已保存")
# 性能配置
with st.expander("⚡ 性能配置"):
n_jobs = st.slider(
"并行进程数",
min_value=1,
max_value=8,
value=4,
help="数据计算时使用的并行进程数"
)
chunk_size = st.slider(
"数据块大小",
min_value=100,
max_value=5000,
value=1000,
step=100,
help="数据处理时的块大小,影响内存使用"
)
cache_expire_days = st.slider(
"缓存过期天数",
min_value=1,
max_value=30,
value=7,
help="数据缓存的有效期(天)"
)
# 显示配置
with st.expander("🖥️ 界面配置"):
theme = st.selectbox(
"界面主题",
["Light", "Dark"],
index=0
)
chart_style = st.selectbox(
"图表样式",
["Plotly", "Matplotlib", "Altair"],
index=0
)
auto_refresh = st.checkbox(
"自动刷新数据",
value=False,
help="是否自动检查并刷新过期数据"
)
# 系统信息
st.markdown("### 📋 系统信息")
col1, col2 = st.columns(2)
with col1:
st.markdown("**环境信息**")
st.text(f"Python版本: {sys.version}")
st.text(f"工作目录: {os.getcwd()}")
st.text(f"平台: {sys.platform}")
with col2:
st.markdown("**包版本信息**")
try:
import streamlit
st.text(f"Streamlit: {streamlit.__version__}")
except:
st.text("Streamlit: 未安装")
try:
import pandas
st.text(f"Pandas: {pandas.__version__}")
except:
st.text("Pandas: 未安装")
try:
import plotly
st.text(f"Plotly: {plotly.__version__}")
except:
st.text("Plotly: 未安装")
if __name__ == "__main__":
main()