Files
sanguo_vnpy_v2/sanguo_qmt_bridge/README.md
T
claude_dev 434d7cb357
CI/CD / test (push) Failing after 7s
CI/CD / nas-deploy (push) Has been cancelled
CI/CD / nas-verify (push) Has been cancelled
feat(config): QMT 部署身份单一权威源 config/qmt_identity.json——P2-18 账号硬编码根治(⚖️-2 用户拍板:确认永为模拟号+令根治硬编码)
根因三层(全案=audit/20261005_qmt_identity_env_plan.md): schtask 无 per-task env+机器级 env 未设→wrapper 被迫字面量;「部署身份」无单一权威源(env 名分裂三个);模拟号零安全压力致 32 文件 88 处 copy-paste 扩散。

- qmt_gate_common.load_identity 正典装载器(:14-59): env SANGUO_QMT_ACCOUNT/BIGQMT_ACCOUNT_ID(兼容别名)>json>RuntimeError fail-loud;SANGUO_IDENTITY_JSON 显式指路不存在=立即报错;查找=repo 相对>C:\sanguo_vnpy_v2\config\(生产副本回退位)
- 灾备腿同源: relogin/qmt_bridge_probe/setclip 改 from qmt_gate_common import ACCOUNT
- 桥腿镜像同链: xt_gateway._qmt_account/qmt_gateway_client._identity_default_account/is_price_source_vps/bridge_switch_ops(3 处 env 注入+1 处内嵌探针串)/check_xtquant——env 带默认的硬编码默认值全数退役
- xt_eod_wrapper.ps1 改 Get-Content identity json(保持 ASCII);码内 docstring 字面量→占位符(engine/runner/runner_live/live.yaml 注释/README)
- .gitignore 白名单 !config/qmt_identity.json(全局 *.json 曾吞之,git check-ignore 实证)
- tests/trader/test_qmt_identity.py 五钉: 装载链语义(env 双名覆盖/显式指路 fail)+单源一致性(identity≡live.yaml≡watch_accounts≡gate_common≡各腿镜像,休市表 CI 同步测先例)+码面字面量 grep-pin(生产码面零 66639661,只 config 三件套可含)
- 监控 spec: §3.2 账号来源 bullet/§9.A-18 坑条/§12 行数+行号勘正(插入块致 bridge_ping/calendar_probe/relogin_running 引用行号推移);runbook: 非代码资产两行(副本回退路径+生产 wrapper 待改清单=10-08 窗口后 §五③)

生产 wrapper(C:\sanguo_bigqmt 库外)字面量收尾=10-08 复市验证窗口后非代码资产流程;VPS 班车同窗口(护 10-08 07:50 relogin 首班旧码纯净)。trader+api 620 绿。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-05 17:32:42 +08:00

133 lines
3.6 KiB
Markdown
Raw 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.
> **⚠️ 已归档(2026-10-01,audit/20261001_docs_audit/ §2.1 归档波)**
>
> - 归档原因:运维语义已退役:本文描述的 FastAPI bridge(:8765) 通道 09-08 随 miniQMT 整体退役;生产栈=大QMT 桥栈(XtItClient+redis RPC,runbook §1/§3)。代码保留(默认值与代码自洽),本文仅作包内历史说明。
> - 现行权威:docs/deployment/vps-production-runbook.md(§1/§3 桥栈)。
> - 处置:保留原地(包内 README 不移动);原文全文见 git 历史(归档 commit 前一版)。
# sanguo QMT bridge (D-1 MVP)
Windows 端 FastAPI 服务,封装 xtquant,供 sanguo(NAS) 跨网调 miniQMT 下单/查询。
## 前提
1. **miniQMT 客户端已登录并保持运行**(极简模式即可)
2. **xtquant 可 import**(miniQMT 安装目录自带,或 `pip install xtquant`)
3. Python 3.10+
## 安装
```powershell
cd C:\sanguo_qmt_bridge # bridge 代码所在目录
pip install -r requirements.txt
```
> xtquant 来自 miniQMT 安装目录(`bin.x64\Lib\site-packages`),无需 pip install。
> 若 import 失败,确认 miniQMT 的 site-packages 在 PYTHONPATH,或 `pip install xtquant`。
## 配置(环境变量)
| 变量 | 说明 | 默认值 |
|------|------|--------|
| `BRIDGE_TOKEN` | **必填**。共享密钥,sanguo 端同值。不设则受保护接口返回 503 | — |
| `MINIQMT_USERDATA` | miniQMT userdata_mini 路径 | `D:\国金QMT交易端模拟\userdata_mini` |
| `ACCOUNT_ID` | miniQMT 账户 ID(env 覆盖;不设则读部署身份单一权威源 `config/qmt_identity.json`,P2-18) | 身份文件值 |
| `BRIDGE_SESSION_ID` | xtquant session ID(int) | `20260710` |
**设置 BRIDGE_TOKEN**(PowerShell):
```powershell
# 临时(当前终端)
$env:BRIDGE_TOKEN = "你的随机密钥"
# 永久(系统环境变量)
[Environment]::SetEnvironmentVariable("BRIDGE_TOKEN", "你的随机密钥", "User")
```
> 生成密钥:`python -c "import secrets; print(secrets.token_urlsafe(32))"`
## 启动
```powershell
cd C:\sanguo_qmt_bridge
uvicorn bridge:app --host 127.0.0.1 --port 8765
```
启动日志看到 `xtquant 连接成功` 即就绪。若 miniQMT 未连上,服务仍运行(`/health` 报 `disconnected`)。
## 接口
所有受保护接口需 header `X-Bridge-Token: <BRIDGE_TOKEN>`,`/health` 豁免。
### GET /health(无需鉴权)
```json
{"status": "ok", "miniqmt_connected": true}
```
### POST /order
请求:
```json
{
"code": "sh600000",
"action": "buy",
"price": 10.50,
"volume": 100,
"price_type": "limit",
"reason": "策略信号"
}
```
成功:
```json
{"ok": true, "order_id": 12345}
```
失败:
```json
{"ok": false, "error": "报单失败 order_id=-1"}
```
### GET /account
```json
{"ok": true, "cash": 100000.0, "frozen": 0.0, "market_value": 50000.0, "total": 150000.0}
```
### GET /positions
```json
{
"ok": true,
"positions": [
{"code": "sh600000", "volume": 200, "can_use": 200, "avg_price": 10.30}
]
}
```
## 代码格式
- 入参(/order):sanguo 格式 `sh600000` / `sz000001`
- 返回(/positions):sanguo 格式 `sh600000` / `sz000001`
- bridge 内部自动转 xtquant 格式 `600000.SH` / `000001.SZ`
## 验证
跑过 `check_xtquant.py` 确认 xtquant 可用后,启动 bridge 再验证:
```powershell
# 健康检查(无需 token)
curl http://127.0.0.1:8765/health
# 查资金(需 token)
curl -H "X-Bridge-Token: 你的密钥" http://127.0.0.1:8765/account
```
## 自启(可选,D-2)
任务计划程序创建 `sanguo-bridge`,onstart,命令:
```
uvicorn bridge:app --host 127.0.0.1 --port 8765
```
工作目录设为 bridge 代码目录。依赖 miniQMT 客户端已先启动。