Initial commit: cc-cursor 全链路量化研究平台

7 Sprints 全部完成:
  Sprint 0: 基础设施 (DataManager + MariaDB)
  Sprint 1: 因子引擎 (34因子/12分类)
  Sprint 2: VectorBT 回测 (5策略+截面)
  Sprint 3: Optuna 优化 (+Walk-Forward)
  Sprint 4: ML 模型 (LightGBM+CatBoost)
  Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐)
  Sprint 6: Agent 系统 (4Agent+日报.md/.html)

生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping,
  save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复,
  RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4,
  CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化,
  indexDatas API修正

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 15:59:05 +08:00
co-authored by Claude Opus 4.7
commit 271a9343a5
293 changed files with 59598 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
# CLAUDE-agents.md — Agent 系统 + CLI
## Agent 架构
4 个 Agent,通过依赖注入复用已有引擎(不重建轮子)。
```python
from agents.orchestrator import AgentOrchestrator
orch = AgentOrchestrator(dm=dm, fe=fe, bt=bt, opt=opt, sent=sent)
orch.setup() # 注册 4 个 Agent
results = orch.run_daily() # 5 步流程
```
## 5 步每日流程
```
[Step 1/5] 增量同步 → 只更新已缓存股票 (0.04s/只)
[Step 2/5] 风险评估 → RiskAgent: high/medium/low + 仓位
[Step 3/5] 股票打分 → SelectionAgent: 多因子等权打分
[Step 4/5] 情绪因子 → SentimentEngine.compute()
[Step 5/5] 生成日报 → ReportAgent: .md + .html + 解读
```
## 各 Agent 职责
| Agent | 文件 | 职责 |
|-------|------|------|
| ResearchAgent | `research_agent.py` | 因子 IC/IC_IR 评估 |
| SelectionAgent | `selection_agent.py` | 多因子股票打分(等权) |
| RiskAgent | `risk_agent.py` | 波动率+回撤→仓位建议 |
| ReportAgent | `report_agent.py` | 市场+选股+情绪+风险→日报 |
## CLI (`finance/cli/agent_cli.py`)
```bash
python cli/agent_cli.py daily # 5 步流程
python cli/agent_cli.py picks 15 # 选股
python cli/agent_cli.py risk # 风险评估
python cli/agent_cli.py research # 因子研究
python cli/agent_cli.py report 20260603 # 生成日报
python cli/agent_cli.py warmup 50 # 首次预热缓存
```
## Demo 脚本 (`finance/cli/demo_*.py`)
全部支持 `--ts_code` `--date` 等参数。
| 脚本 | 用途 |
|------|------|
| `demo_data_manager.py` | Sprint 0: DB→建表→日线→同步 |
| `demo_factor_engine.py` | Sprint 1: 34因子→NaN检查→截面 |
| `demo_backtest.py` | Sprint 2: 5策略回测 |
| `demo_optimizer.py` | Sprint 3: Optuna寻优+Walk-Forward |
| `demo_ml.py` | Sprint 4: LightGBM+CatBoost+回测 |
| `demo_sentiment.py` | Sprint 5: 情绪因子快速验证 |
| `demo_sentiment_detail.py` | Sprint 5: 情绪因子详细演示(14参数) |
## 报告 (`finance/reports/`)
- `daily_YYYYMMDD.md` + `.html` — 每日双格式输出
- `storage.py``save_report()` / `query_reports()` → 存入 mac_report 表
- 日报包含"昨日对比"区块:标注数据是否与前一日相同
## ⚠️ 已知 Bug 速查(避免重复踩坑)
### B6: 5 日/20 日涨跌幅恒为 0
- **症状**: 日报中 `| +0.00% | +0.00% |`,实际数据应该有非零值
- **根因**: `idx = daily.index.get_loc(date) if date in daily.index else -1`,目标日期不在索引时 `-1 >= 5` → False,计算被短路
- **修复**: 用 `pos = len(daily) - 1` 替代 `idx = -1`,确保位置值非负
### B7: 两日报告完全一致
- **症状**: 20260604 和 20260605 报告的 TOP 15、风险评估完全一样
- **根因**: ① T+1 数据未产出,两份报告基于同一份 DB 快照 ② 多因子在 1 天内变化极小(非 bug,是特性)
- **缓解**: 日报增加"昨日对比"区块 + 数据截止标注
### B8: 风险回撤恒为 -52%
- **症状**: 不管哪天看,风险等级一直是 high,回撤一直是 -52%
- **根因**: RiskAgent 默认用 `market_index="000001.SZ"`(平安银行个股),其 2020 年历史高价导致永远极端回撤
- **修复**: 改为 `"000001.SH"`(上证指数)
### B9: report 命令只 sync 一只股票
- **症状**: generate_report 只调了 `dm.sync_daily("000001.SZ")`,其他 279 只不变
- **影响**: 日报的选股排名不会反映最新行情
- **状态**: 已加 sync 调用,范围仍为单只(全量 sync 走 `daily` 命令或 `warmup`