Files
dekun 169136dd4a Add modular docs with index, WeChat templates, and AI guide.
Document per-module order logic, risk rules, and WeChat message templates with a central INDEX for navigation.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-28 10:49:10 +08:00

153 lines
4.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AI 分析
**页面路径**`/ai`(须在系统设置 → 导航显示 中开启「AI 分析」)
**相关文件**`ai_client.py``ai_worker.py``ai_messages.py``templates/ai_messages.html`
---
## 功能概述
| 能力 | 说明 |
|------|------|
| 开仓分析 | 手动/关键位开仓成交后后台分析 |
| 平仓分析 | 写入 `trade_logs` 平仓记录后分析 |
| 日终报告 | 每日定时汇总当日交易与持仓 |
| 消息存档 | 全部写入 `ai_messages` 表,页面可浏览 |
| 微信推送 | 可选:分析成功后推送到企业微信 |
---
## 配置项(系统设置 → AI 分析)
| 设置键 | 说明 | 默认 |
|--------|------|------|
| `ai_enabled` | 总开关 | 关 |
| `ai_provider` | `ollama` / `openai` | ollama |
| `ai_ollama_base_url` | Ollama 地址 | `http://127.0.0.1:11434` |
| `ai_ollama_model` | Ollama 模型 | `qwen2.5:7b` |
| `ai_openai_base_url` | OpenAI 兼容 API | `https://api.openai.com/v1` |
| `ai_openai_api_key` | API Key | 空 |
| `ai_openai_model` | 模型名 | `gpt-4o-mini` |
| `ai_daily_report_enabled` | 日终报告开关 | 开 |
| `ai_daily_report_hour` | 报告时刻(时) | 15 |
| `ai_daily_report_minute` | 报告时刻(分) | 5 |
---
## 触发时机
### 1. 开仓分析(`kind=open`
- **触发**:下单监控手动开仓 **成交** 且填写止损 → `notify_manual_open_filled()` 调度。
- **标题**`{品种名} 开仓`
- **Payload**symbol、direction、entry、stop_loss、take_profit、lots、capital
### 2. 关键位开仓(`kind=key_open`
- **触发**:箱体/收敛突破自动单成交 → `notify_key_breakout_open()` 调度。
- **标题**`{品种名} 关键位开仓`
- **Payload**monitor_type、trade_mode、break_side、entry、stop_loss、take_profit、lots
### 3. 平仓分析(`kind=close`
- **触发**`trade_logs` 新增平仓 → `notify_trade_log_close()` 调度。
- **标题**`{品种名} 平仓`
- **Payload**source、result、pnl_net、entry、close_price、lots
### 4. 日终报告(`kind=daily_report`
- **触发**:后台 `ai_worker` 每分钟检查;到达设定时刻且当日未生成过。
- **标题**`{YYYY-MM-DD} 日终持仓与交易报告`
- **Payload**:当日成交汇总、胜负次数、净盈亏、active 持仓列表
> 开仓/平仓的 AI 分析 **默认不推微信**(仅写入页面);日终报告与可在设置中开启的推送见下文。
---
## 分析逻辑
**System Prompt**(固定):
```
你是国内期货交易复盘助手。根据提供的结构化交易数据,
用简洁中文给出 3~6 条要点:风险、纪律、改进建议。
不要编造未提供的数据;金额单位为元。
```
**User Prompt**
```
事件类型:{event_kind}
数据:
{JSON payload}
```
**接口**
- Ollama`POST {base}/api/chat`
- OpenAI 兼容:`POST {base}/chat/completions`temperature=0.4
失败时内容前加 `⚠`,仍写入 `ai_messages`
---
## 微信推送
| 类型 | 条件 | 模板 |
|------|------|------|
| 事件分析 | AI 调用成功 **且** 调度时传入 `send_wechat_fn` | 见 [WECHAT.md §12](./WECHAT.md#12-ai-分析) |
| 日终报告 | `ai_daily_report_enabled=1` 且 AI 成功 | `🤖 {date} 日终持仓与交易报告\n\n{content[:1800]}` |
**说明**
- 当前实现中,开仓/平仓触发的 `schedule_ai_event_analysis()` **未绑定** `send_wechat_fn`,故 **仅存档到 /ai 页面**
- 日终报告在 `maybe_run_daily_ai_report()` 中会推微信(成功时)。
- 正文截断为 **1800 字符**,避免超长。
---
## 下单逻辑
AI **不参与下单**,仅事后分析。无 `assert_can_open` 或 CTP 交互。
---
## 风控规则
AI 模块无独立风控。其输入数据来自已发生的交易事件,不改变账户冻结、保证金等状态。
---
## 后台任务
- 线程名:`ai-worker`
- 启动延迟 30 秒后,每 **60 秒** 检查是否该跑日终报告
-`background_task`(计划/关键位)独立运行
---
## 数据存储
`ai_messages`
| 字段 | 说明 |
|------|------|
| kind | open / key_open / close / daily_report |
| title | 标题 |
| content | AI 回复正文 |
| meta | JSON payload |
| created_at | 创建时间 |
页面 `/ai` 按时间倒序展示;上方含使用说明卡片。
---
## 相关文档
- [WECHAT.md §12](./WECHAT.md#12-ai-分析) — 推送模板
- [ORDER_MONITOR.md](./ORDER_MONITOR.md) — 手动开仓触发 AI
- [KEY_MONITORS.md](./KEY_MONITORS.md) — 关键位开仓触发 AI
- [SETTINGS.md](./SETTINGS.md) — 配置入口