-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathmacro_cycle_ui.py
More file actions
370 lines (296 loc) · 14 KB
/
macro_cycle_ui.py
File metadata and controls
370 lines (296 loc) · 14 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
宏观周期分析 - UI界面模块
展示康波周期 + 美林投资时钟 + 中国政策分析的综合结果
"""
import streamlit as st
import time
from datetime import datetime
from macro_cycle_engine import MacroCycleEngine
from macro_cycle_pdf import MacroCyclePDFGenerator, generate_macro_cycle_markdown
def display_macro_cycle():
"""显示宏观周期分析主界面"""
st.markdown("""
<div style="background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
padding: 2rem; border-radius: 15px; margin-bottom: 1.5rem;
box-shadow: 0 8px 32px rgba(0,0,0,0.3);">
<h1 style="color: #fff; margin: 0; font-size: 2rem;">
🧭 宏观周期分析
</h1>
<p style="color: rgba(255,255,255,0.8); margin: 0.5rem 0 0 0; font-size: 1.1rem;">
康波周期 × 美林投资时钟 × 中国政策分析 — AI驱动的宏观经济周期研判
</p>
</div>
""", unsafe_allow_html=True)
# 标签页
tab1, tab2 = st.tabs(["📊 周期分析", "📚 理论介绍"])
with tab1:
display_analysis_tab()
with tab2:
display_theory_tab()
def display_analysis_tab():
"""显示分析标签页"""
# 简介
st.markdown("""
> **分析说明**:本模块基于视频[康波周期理论](https://www.bilibili.com/video/BV1QNcEzREzY)(50-60年长周期)和视频[美林投资时钟](https://www.bilibili.com/video/BV1Zuf5BUEhH)(3-5年中短周期),
> 结合中国政策环境(第三维度),由4位AI分析师协同研判当前宏观经济所处的周期位置,并给出资产配置建议。
""")
st.markdown("""
**🤖 AI分析师团队:**
- 🌊 **康波周期分析师** — 60年长周期战略定位(回升→繁荣→衰退→萧条)
- ⏰ **美林时钟分析师** — 3-5年中短周期战术定位(复苏→过热→滞胀→衰退)
- 🏛️ **中国政策分析师** — 政策第三维度(货币/财政/产业/房地产)
- 👔 **首席宏观策略师** — 三维综合研判,最终资产配置建议
""")
st.markdown("---")
# 操作按钮
col1, col2 = st.columns([2, 2])
with col1:
analyze_button = st.button("🚀 开始宏观周期分析", type="primary", key="macro_analyze")
with col2:
if st.button("🔄 清除结果", key="macro_clear"):
if 'macro_cycle_result' in st.session_state:
del st.session_state.macro_cycle_result
st.success("已清除分析结果")
st.rerun()
st.markdown("---")
# 开始分析
if analyze_button:
if 'macro_cycle_result' in st.session_state:
del st.session_state.macro_cycle_result
run_macro_cycle_analysis()
# 显示结果
if 'macro_cycle_result' in st.session_state:
result = st.session_state.macro_cycle_result
if result.get("success"):
display_analysis_results(result)
else:
st.error(f"❌ 分析失败: {result.get('error', '未知错误')}")
def run_macro_cycle_analysis():
"""运行宏观周期分析"""
import config
model = config.DEFAULT_MODEL_NAME
progress_bar = st.progress(0)
status_text = st.empty()
def progress_callback(pct, text):
progress_bar.progress(pct)
status_text.text(text)
try:
engine = MacroCycleEngine(model=model)
result = engine.run_full_analysis(progress_callback=progress_callback)
if result.get("success"):
st.session_state.macro_cycle_result = result
time.sleep(1)
status_text.empty()
progress_bar.empty()
st.rerun()
else:
st.error(f"❌ 分析失败: {result.get('error', '未知错误')}")
except Exception as e:
st.error(f"❌ 分析过程出错: {str(e)}")
import traceback
st.code(traceback.format_exc())
finally:
progress_bar.empty()
status_text.empty()
def display_analysis_results(result):
"""显示分析结果"""
agents = result.get("agents_analysis", {})
timestamp = result.get("timestamp", "")
st.success(f"✅ 分析完成于 {timestamp}")
# 数据采集状态
data_errors = result.get("data_errors", [])
if data_errors:
with st.expander("⚠️ 部分数据采集失败(不影响分析)"):
for err in data_errors:
st.warning(f"• {err}")
# 四个分析师报告
report_tabs = st.tabs([
"👔 综合策略",
"🌊 康波周期",
"⏰ 美林时钟",
"🏛️ 政策分析"
])
# Tab 1: 首席宏观策略师(综合)
with report_tabs[0]:
chief = agents.get("chief", {})
if chief:
st.markdown("""
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 1.2rem; border-radius: 12px; margin-bottom: 1rem;
color: white;">
<h3 style="margin: 0; color: white;">👔 首席宏观策略师 — 综合研判</h3>
<p style="margin: 0.3rem 0 0 0; opacity: 0.9; font-size: 0.95rem;">
整合康波周期 + 美林投资时钟 + 中国政策三维分析,给出最终投资策略
</p>
</div>
""", unsafe_allow_html=True)
st.markdown(chief.get("analysis", "暂无分析结果"))
else:
st.info("暂无综合策略分析结果")
# Tab 2: 康波周期分析师
with report_tabs[1]:
kondratieff = agents.get("kondratieff", {})
if kondratieff:
st.markdown("""
<div style="background: linear-gradient(135deg, #0575E6 0%, #021B79 100%);
padding: 1.2rem; border-radius: 12px; margin-bottom: 1rem;
color: white;">
<h3 style="margin: 0; color: white;">🌊 康波周期分析师 — 60年长周期定位</h3>
<p style="margin: 0.3rem 0 0 0; opacity: 0.9; font-size: 0.95rem;">
基于康德拉季耶夫长波理论,判断当前处于第五轮信息技术康波的哪个阶段
</p>
</div>
""", unsafe_allow_html=True)
st.markdown(kondratieff.get("analysis", "暂无分析结果"))
else:
st.info("暂无康波周期分析结果")
# Tab 3: 美林时钟分析师
with report_tabs[2]:
merrill = agents.get("merrill", {})
if merrill:
st.markdown("""
<div style="background: linear-gradient(135deg, #f5af19 0%, #f12711 100%);
padding: 1.2rem; border-radius: 12px; margin-bottom: 1rem;
color: white;">
<h3 style="margin: 0; color: white;">⏰ 美林投资时钟分析师 — 中短周期定位</h3>
<p style="margin: 0.3rem 0 0 0; opacity: 0.9; font-size: 0.95rem;">
基于经济增长+通胀+政策三维度,判断当前处于美林时钟的哪个象限
</p>
</div>
""", unsafe_allow_html=True)
st.markdown(merrill.get("analysis", "暂无分析结果"))
else:
st.info("暂无美林时钟分析结果")
# Tab 4: 中国政策分析师
with report_tabs[3]:
policy = agents.get("policy", {})
if policy:
st.markdown("""
<div style="background: linear-gradient(135deg, #e53935 0%, #b71c1c 100%);
padding: 1.2rem; border-radius: 12px; margin-bottom: 1rem;
color: white;">
<h3 style="margin: 0; color: white;">🏛️ 中国政策分析师 — 政策第三维度</h3>
<p style="margin: 0.3rem 0 0 0; opacity: 0.9; font-size: 0.95rem;">
深度分析货币政策、财政政策、产业政策、房地产政策对周期和投资的影响
</p>
</div>
""", unsafe_allow_html=True)
st.markdown(policy.get("analysis", "暂无分析结果"))
else:
st.info("暂无政策分析结果")
# 导出报告
st.markdown("---")
display_pdf_export_section(result)
def display_pdf_export_section(result):
"""显示PDF/Markdown导出部分"""
st.subheader("📄 导出报告")
col1, col2, col3, col4 = st.columns([2, 1, 1, 1])
with col1:
st.write("将宏观周期分析报告导出为PDF或Markdown文件,方便保存和分享")
with col2:
if st.button("📥 生成PDF报告", type="primary", width='content', key="macro_pdf_gen"):
with st.spinner("正在生成PDF报告..."):
try:
generator = MacroCyclePDFGenerator()
pdf_path = generator.generate_pdf(result)
with open(pdf_path, "rb") as f:
pdf_bytes = f.read()
st.session_state.macro_pdf_data = pdf_bytes
ts = result.get('timestamp', datetime.now().strftime('%Y%m%d_%H%M%S')).replace(':', '').replace(' ', '_')
st.session_state.macro_pdf_filename = f"宏观周期报告_{ts}.pdf"
st.success("✅ PDF报告生成成功!")
st.rerun()
except Exception as e:
st.error(f"❌ PDF生成失败: {str(e)}")
with col3:
if st.button("📝 生成Markdown", type="secondary", width='content', key="macro_md_gen"):
with st.spinner("正在生成Markdown报告..."):
try:
markdown_content = generate_macro_cycle_markdown(result)
st.session_state.macro_md_data = markdown_content
ts = result.get('timestamp', datetime.now().strftime('%Y%m%d_%H%M%S')).replace(':', '').replace(' ', '_')
st.session_state.macro_md_filename = f"宏观周期报告_{ts}.md"
st.success("✅ Markdown报告生成成功!")
st.rerun()
except Exception as e:
st.error(f"❌ Markdown生成失败: {str(e)}")
with col4:
if 'macro_pdf_data' in st.session_state:
st.download_button(
label="💾 下载PDF",
data=st.session_state.macro_pdf_data,
file_name=st.session_state.macro_pdf_filename,
mime="application/pdf",
width='content',
key="macro_pdf_dl"
)
if 'macro_md_data' in st.session_state:
st.download_button(
label="💾 下载Markdown",
data=st.session_state.macro_md_data,
file_name=st.session_state.macro_md_filename,
mime="text/markdown",
width='content',
key="macro_md_dl"
)
def display_theory_tab():
"""显示理论介绍标签页"""
st.markdown("""
## 📖 两大周期理论简介
---
### 🌊 康德拉季耶夫长波(康波周期)
**创始人**:苏联经济学家尼古拉·康德拉季耶夫(1920s)
**中国推广者**:周金涛("周期天王",中信建投首席经济学家)
**核心思想**:资本主义经济存在约 **50-60年** 的超长周期,由重大技术革命驱动。
**四个阶段**:
| 阶段 | 持续时间 | 特征 | 最优资产 |
|------|---------|------|---------|
| 🌱 **回升期** | ~15年 | 新技术商业化,经济从底部爬起 | 股票、新兴产业 |
| ☀️ **繁荣期** | ~15年 | 技术全面铺开,高速增长 | 几乎所有资产 |
| 🍂 **衰退期** | ~10年 | 泡沫破裂,增速放缓 | 大宗商品→现金 |
| ❄️ **萧条期** | ~10年 | 全面收缩,资产便宜 | 现金→布局未来 |
**历史五轮康波**:
1. **1780s-1840s**:蒸汽机革命
2. **1840s-1890s**:铁路与钢铁
3. **1890s-1940s**:电力与化工
4. **1940s-1990s**:汽车与计算机
5. **1990s-2050s?**:信息技术革命(当前)
> *"人生发财靠康波。每个人的财富积累,一定不要以为是你多有本事,财富积累完全来源于经济周期运动的时间给你的机会。"* — 周金涛
---
### ⏰ 美林投资时钟
**创始人**:美林证券分析师(2004年)
**核心指标**:经济增长 × 通货膨胀
**四个象限**:
| 象限 | 经济 | 通胀 | 最优资产 | 典型特征 |
|------|------|------|---------|---------|
| 🟢 **复苏期** | ↑ | ↓ | **股票** | 盈利改善,利率低 |
| 🔴 **过热期** | ↑ | ↑ | **大宗商品** | 需求旺盛,加息 |
| 🟡 **滞胀期** | ↓ | ↑ | **现金** | 成本上升,利润缩水 |
| 🔵 **衰退期** | ↓ | ↓ | **债券** | 降息,避险需求 |
**中国化改造**:
- 增加 **政策方向** 作为第三维度
- 缩短时钟转动周期(中国约1-3年一轮,美国3-5年)
- 增加 **房地产** 作为第五类资产
- 重视 **政策友好型** 资产
---
### 🤝 两大理论的结合使用
| 维度 | 康波周期 | 美林时钟 |
|------|---------|---------|
| **时间尺度** | 50-60年 | 3-5年 |
| **驱动力** | 技术革命(供给侧) | 增长+通胀(需求侧) |
| **用途** | 人生战略决策 | 投资组合调整 |
| **比喻** | 🔭 望远镜 | 🔬 显微镜 |
| **角色** | 罗盘(大方向) | 航海图(风浪变化) |
**结合方法**:
- 康波定 **战略方向**(进攻/防守)
- 美林定 **战术节奏**(具体配什么)
- 政策作为 **催化剂**(加速/扭曲周期)
> *"双指针一致时信心更强,矛盾时要谨慎。"*
---
### ⚠️ 免责声明
本分析仅供学习研究参考,不构成任何投资建议。周期理论是认知框架而非精确预测工具。
投资有风险,入市需谨慎。
""")
# 主入口
if __name__ == "__main__":
display_macro_cycle()