Skip to content

Commit e16269e

Browse files
committed
docs: 生成项目文档结构 (DeepWiki 风格)
- ARCHITECTURE.md: 系统架构文档,包含技术栈、项目结构、子系统、架构图 - INTERFACES.md: 接口文档,包含 CLI、REST API、Bot 命令、数据 Schema - DEVELOPER_GUIDE.md: 开发者指南,包含环境搭建、开发工作流、编码规范 - INDEX.md: 文档索引 - 专有概念/: 核心概念页面 - StockAnalysisPipeline.md: 核心分析流水线 - Agent系统.md: AI Agent 架构 - DataFetcherManager.md: 多数据源管理 - 决策仪表盘.md: AI 分析输出结构 Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
1 parent 4cf874d commit e16269e

8 files changed

Lines changed: 1364 additions & 0 deletions

File tree

.monkeycode/docs/ARCHITECTURE.md

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
# 系统架构文档
2+
3+
## 概述
4+
5+
股票智能分析系统是一个基于 AI 大模型的 A股/港股/美股自选股智能分析系统,每日自动分析并推送「决策仪表盘」到企业微信/飞书/Telegram/Discord/Slack/邮箱等渠道。
6+
7+
系统支持两种分析模式:
8+
1. **传统模式**:数据获取 → 技术分析 → AI 决策生成
9+
2. **Agent 模式**:基于多 Agent 协作的智能分析,支持单 Agent 和多 Agent 架构
10+
11+
系统通过多数据源故障切换策略确保数据获取的可靠性,支持 Tushare、AkShare、YFinance 等多个数据源。AI 分析基于 LiteLLM 统一接口,支持 Gemini、GPT、Claude、DeepSeek 等多种模型。
12+
13+
## 技术栈
14+
15+
**语言与运行时**
16+
- Python 3.10+
17+
18+
**框架**
19+
- FastAPI - Web API 服务
20+
- SQLAlchemy - ORM 数据访问
21+
- LiteLLM - LLM 统一接口
22+
- Jinja2 - 报告模板渲染
23+
24+
**数据存储**
25+
- SQLite - 本地数据库(历史分析、持仓、配置)
26+
- 文件系统 - 缓存和临时文件
27+
28+
**基础设施**
29+
- GitHub Actions - CI/CD 与定时任务
30+
- Docker - 容器化部署
31+
32+
**外部服务**
33+
- AI 模型:AIHubMix、Gemini、OpenAI、DeepSeek、Claude、Ollama
34+
- 行情数据:AkShare、Tushare、YFinance、Baostock、Pytdx
35+
- 新闻搜索:Tavily、SerpAPI、Bocha、Brave Search
36+
- 社交舆情:Stock Sentiment API(Reddit/X/Polymarket)
37+
38+
## 项目结构
39+
40+
```
41+
/workspace/
42+
├── main.py # CLI 主入口
43+
├── server.py # FastAPI 服务入口
44+
├── webui.py # Web 管理界面入口
45+
├── src/
46+
│ ├── core/ # 核心流程编排
47+
│ │ ├── pipeline.py # 股票分析流水线 (StockAnalysisPipeline)
48+
│ │ ├── market_review.py # 大盘复盘
49+
│ │ └── trading_calendar.py # 交易日历
50+
│ ├── agent/ # AI Agent 模块
51+
│ │ ├── executor.py # 单 Agent 执行器
52+
│ │ ├── orchestrator.py # 多 Agent 编排器
53+
│ │ ├── factory.py # Agent 工厂
54+
│ │ ├── llm_adapter.py # LiteLLM 适配器
55+
│ │ ├── runner.py # 共享执行循环
56+
│ │ ├── protocols.py # 共享数据结构
57+
│ │ ├── agents/ # 专业化 Agent
58+
│ │ ├── skills/ # 技能系统
59+
│ │ ├── strategies/ # 策略系统
60+
│ │ └── tools/ # 工具注册表
61+
│ ├── services/ # 业务服务层
62+
│ ├── repositories/ # 数据访问层
63+
│ ├── notification_sender/ # 通知渠道发送器
64+
│ ├── schemas/ # 数据 Schema
65+
│ ├── config.py # 配置管理(单例模式)
66+
│ ├── storage.py # 数据库层(SQLAlchemy)
67+
│ ├── analyzer.py # AI 分析器
68+
│ ├── stock_analyzer.py # 技术分析器
69+
│ ├── search_service.py # 搜索服务
70+
│ ├── notification.py # 通知服务
71+
│ └── scheduler.py # 定时任务
72+
├── data_provider/ # 数据源适配层
73+
│ ├── base.py # 数据源管理器
74+
│ ├── akshare_fetcher.py # AkShare 数据源
75+
│ ├── tushare_fetcher.py # Tushare 数据源
76+
│ ├── yfinance_fetcher.py # YFinance 数据源
77+
│ └── ...
78+
├── api/ # FastAPI 接口
79+
│ └── v1/ # API v1 版本
80+
├── bot/ # 机器人接入
81+
│ ├── commands/ # 命令处理器
82+
│ └── platforms/ # 平台适配器
83+
├── apps/
84+
│ ├── dsa-web/ # Web 前端
85+
│ └── dsa-desktop/ # Electron 桌面端
86+
├── tests/ # 测试
87+
├── scripts/ # 脚本
88+
├── templates/ # 报告模板
89+
└── docs/ # 文档
90+
```
91+
92+
**入口点**
93+
- `main.py` - CLI 主入口,支持定时任务、单次分析、大盘复盘等模式
94+
- `server.py` - FastAPI 服务入口,提供 REST API
95+
- `webui.py` - Web 管理界面入口
96+
97+
## 子系统
98+
99+
### StockAnalysisPipeline
100+
**目的**: 管理整个股票分析流程,协调各模块完成数据获取、AI 分析、通知推送
101+
102+
**位置**: `src/core/pipeline.py`
103+
104+
**关键方法**:
105+
- `run()` - 运行完整分析流程,线程池并发处理多只股票
106+
- `process_single_stock()` - 处理单只股票的完整流程
107+
- `analyze_stock()` - 执行单只股票 AI 分析
108+
- `_analyze_with_agent()` - Agent 模式分析
109+
110+
**依赖**: DataFetcherManager、StockAnalyzer、NotificationService、SearchService、DatabaseManager
111+
112+
**被依赖**: main.py、API endpoints
113+
114+
### Agent 模块
115+
**目的**: 提供基于 AI Agent 的智能分析能力,支持单 Agent 和多 Agent 协作
116+
117+
**位置**: `src/agent/`
118+
119+
**关键组件**:
120+
- `executor.py` - 单 Agent 执行器(ReAct 循环)
121+
- `orchestrator.py` - 多 Agent 编排器
122+
- `factory.py` - Agent 工厂
123+
- `agents/` - 专业化 Agent(TechnicalAgent、IntelAgent、RiskAgent、DecisionAgent)
124+
125+
**依赖**: LiteLLM、ToolRegistry、SkillManager
126+
127+
**被依赖**: StockAnalysisPipeline
128+
129+
### DataFetcherManager
130+
**目的**: 多数据源管理,实现故障自动切换
131+
132+
**位置**: `data_provider/base.py`
133+
134+
**关键方法**:
135+
- `get_daily_data()` - 获取日线数据
136+
- `get_realtime_quote()` - 获取实时行情
137+
- `get_chip_distribution()` - 获取筹码分布
138+
139+
**数据源优先级**: Efinance → AkShare → Tushare → Pytdx → Baostock → YFinance
140+
141+
**依赖**: 各数据源 Fetcher
142+
143+
**被依赖**: StockAnalysisPipeline、Agent Tools
144+
145+
### NotificationService
146+
**目的**: 聚合多渠道通知,统一发送分析报告
147+
148+
**位置**: `src/notification_sender/`
149+
150+
**支持的渠道**: 企业微信、飞书、Telegram、Discord、Slack、钉钉、邮件、PushPlus、Server酱
151+
152+
**依赖**: 各渠道 Sender
153+
154+
**被依赖**: StockAnalysisPipeline
155+
156+
### SearchService
157+
**目的**: 多维度情报搜索,整合新闻、社交舆情
158+
159+
**位置**: `src/search_service.py`
160+
161+
**支持的搜索源**: Tavily、SerpAPI、Bocha、Brave Search、SearXNG
162+
163+
**依赖**: 外部搜索 API
164+
165+
**被依赖**: StockAnalysisPipeline、Agent Tools
166+
167+
### DatabaseManager
168+
**目的**: 统一数据库操作,管理分析历史、持仓、配置
169+
170+
**位置**: `src/storage.py`
171+
172+
**关键表**: analysis_history、backtest_results、portfolio、system_config
173+
174+
**依赖**: SQLAlchemy、SQLite
175+
176+
**被依赖**: 所有需要持久化的模块
177+
178+
## 系统架构图
179+
180+
```mermaid
181+
flowchart TB
182+
subgraph Entry["入口层"]
183+
CLI["main.py<br>CLI 入口"]
184+
API["server.py<br>FastAPI 服务"]
185+
WebUI["webui.py<br>Web 管理界面"]
186+
Bot["bot/<br>机器人接入"]
187+
end
188+
189+
subgraph Core["核心编排层"]
190+
Pipeline["StockAnalysisPipeline<br>src/core/pipeline.py"]
191+
Schedule["Scheduler<br>定时任务"]
192+
end
193+
194+
subgraph Agent["Agent 模块"]
195+
Factory["AgentFactory"]
196+
Executor["AgentExecutor<br>单 Agent"]
197+
Orchestrator["AgentOrchestrator<br>多 Agent"]
198+
subgraph Agents["专业化 Agent"]
199+
TechAgent["TechnicalAgent"]
200+
IntelAgent["IntelAgent"]
201+
RiskAgent["RiskAgent"]
202+
DecisionAgent["DecisionAgent"]
203+
end
204+
subgraph AgentCore["Agent 核心"]
205+
LLMAdapter["LLMAdapter<br>LiteLLM"]
206+
ToolRegistry["ToolRegistry"]
207+
SkillManager["SkillManager"]
208+
end
209+
end
210+
211+
subgraph Data["数据层"]
212+
DB["DatabaseManager<br>SQLite"]
213+
Cache["文件缓存"]
214+
end
215+
216+
subgraph DataProvider["数据源层"]
217+
DFM["DataFetcherManager"]
218+
subgraph Fetchers["数据源"]
219+
Efinance["EfinanceFetcher"]
220+
AkShare["AkShareFetcher"]
221+
Tushare["TushareFetcher"]
222+
YFinance["YFinanceFetcher"]
223+
end
224+
end
225+
226+
subgraph Search["搜索层"]
227+
SearchService["SearchService"]
228+
subgraph SearchSources["搜索源"]
229+
Tavily["Tavily"]
230+
SerpAPI["SerpAPI"]
231+
Bocha["Bocha"]
232+
end
233+
Sentiment["SocialSentimentService"]
234+
end
235+
236+
subgraph Notification["通知层"]
237+
Notify["NotificationService"]
238+
subgraph Senders["通知渠道"]
239+
WeChat["企业微信"]
240+
Feishu["飞书"]
241+
Telegram["Telegram"]
242+
Discord["Discord"]
243+
Email["邮件"]
244+
end
245+
end
246+
247+
CLI --> Pipeline
248+
API --> Pipeline
249+
WebUI --> Pipeline
250+
Bot --> Pipeline
251+
252+
Pipeline --> DFM
253+
Pipeline --> SearchService
254+
Pipeline --> Notify
255+
Pipeline --> DB
256+
257+
DFM --> Efinance
258+
DFM --> AkShare
259+
DFM --> Tushare
260+
DFM --> YFinance
261+
262+
SearchService --> Tavily
263+
SearchService --> SerpAPI
264+
SearchService --> Bocha
265+
266+
Notify --> WeChat
267+
Notify --> Feishu
268+
Notify --> Telegram
269+
Notify --> Discord
270+
Notify --> Email
271+
272+
Pipeline --> Factory
273+
Factory --> Executor
274+
Factory --> Orchestrator
275+
Executor --> LLMAdapter
276+
Orchestrator --> TechAgent
277+
Orchestrator --> IntelAgent
278+
Orchestrator --> RiskAgent
279+
Orchestrator --> DecisionAgent
280+
TechAgent --> LLMAdapter
281+
IntelAgent --> LLMAdapter
282+
RiskAgent --> LLMAdapter
283+
DecisionAgent --> LLMAdapter
284+
LLMAdapter --> ToolRegistry
285+
LLMAdapter --> SkillManager
286+
```
287+
288+
## Agent 协作流程图
289+
290+
```mermaid
291+
sequenceDiagram
292+
participant User as 用户
293+
participant Pipeline as StockAnalysisPipeline
294+
participant Orch as AgentOrchestrator
295+
participant Tech as TechnicalAgent
296+
participant Intel as IntelAgent
297+
participant Risk as RiskAgent
298+
participant Decision as DecisionAgent
299+
participant LLM as LLMAdapter
300+
participant Tools as ToolRegistry
301+
302+
User->>Pipeline: 请求分析 (股票代码)
303+
Pipeline->>Pipeline: 获取实时行情/筹码/趋势数据
304+
Pipeline->>Orch: 构建 AgentContext
305+
Orch->>Tech: 注入初始上下文
306+
Tech->>LLM: 请求技术分析
307+
LLM->>Tools: 调用市场数据工具
308+
Tools-->>LLM: 返回技术指标
309+
LLM-->>Tech: 技术分析意见
310+
Tech-->>Orch: StageResult
311+
312+
Orch->>Intel: 注入技术上下文
313+
Intel->>LLM: 请求情报搜索
314+
LLM->>Tools: 调用搜索工具
315+
Tools-->>LLM: 返回新闻/舆情
316+
LLM-->>Intel: 情报分析意见
317+
Intel-->>Orch: StageResult
318+
319+
alt full/specialist 模式
320+
Orch->>Risk: 注入情报上下文
321+
Risk->>LLM: 请求风险评估
322+
LLM-->>Risk: 风险评估意见
323+
Risk-->>Orch: StageResult
324+
end
325+
326+
Orch->>Decision: 聚合所有意见
327+
Decision->>LLM: 生成决策仪表盘
328+
LLM-->>Decision: 决策建议
329+
Decision-->>Orch: AgentOpinion
330+
331+
Orch->>Orch: _apply_risk_override()
332+
Orch->>Orch: _parse_dashboard()
333+
Orch-->>Pipeline: AgentResult
334+
Pipeline->>Notify: 发送通知
335+
Notify-->>User: 推送报告
336+
```
337+
338+
## 分析流水线时序图
339+
340+
```mermaid
341+
sequenceDiagram
342+
participant Main as main.py
343+
participant Pipeline as StockAnalysisPipeline
344+
participant DFM as DataFetcherManager
345+
participant Analyzer as GeminiAnalyzer
346+
participant Search as SearchService
347+
participant Notify as NotificationService
348+
participant DB as DatabaseManager
349+
350+
Main->>Main: parse_arguments()
351+
Main->>Pipeline: run_full_analysis()
352+
Pipeline->>DFM: get_daily_data(stock_code)
353+
DFM-->>Pipeline: 日线数据
354+
355+
alt Agent 模式
356+
Pipeline->>Pipeline: _analyze_with_agent()
357+
else 传统模式
358+
Pipeline->>DFM: get_realtime_quote()
359+
Pipeline->>DFM: get_chip_distribution()
360+
Pipeline->>Search: search_comprehensive_intel()
361+
Pipeline->>Analyzer: analyze()
362+
end
363+
364+
Analyzer-->>Pipeline: AnalysisResult
365+
Pipeline->>DB: save_analysis_history()
366+
Pipeline->>Notify: send()
367+
Notify-->>Main: 推送完成
368+
```

0 commit comments

Comments
 (0)