|
| 1 | +<div align="center"> |
| 2 | + |
| 3 | +# Quant Flow |
| 4 | + |
| 5 | +**基于 LangChain/LangGraph 的 AI 加密货币永续合约交易机器人,支持 Hyperliquid DEX** |
| 6 | + |
| 7 | +[](https://python.org) |
| 8 | +[](https://python.langchain.com/) |
| 9 | +[](LICENSE) |
| 10 | +[](https://loadchange.github.io/quant-flow/zh-Hans/docs/intro) |
| 11 | + |
| 12 | +[**📖 完整文档**](https://loadchange.github.io/quant-flow/zh-Hans/docs/intro) · [**English README**](README.md) · [**测试网水龙头**](https://app.hyperliquid-testnet.xyz/faucet) |
| 13 | + |
| 14 | +</div> |
| 15 | + |
| 16 | +> ⚠️ **免责声明**:本项目仅供学习研究使用。杠杆交易存在重大亏损风险,请先在测试网验证策略,生产环境使用需自行承担风险。 |
| 17 | +
|
| 18 | +--- |
| 19 | + |
| 20 | +## 项目简介 |
| 21 | + |
| 22 | +Quant Flow 是基于 [Hyperliquid DEX](https://hyperliquid.xyz/) 的 AI 自动交易系统,构建在 LangChain/LangGraph 之上,支持两种独立的交易策略: |
| 23 | + |
| 24 | +| 策略 | 入口 | 说明 | |
| 25 | +|------|------|------| |
| 26 | +| **永续合约 Agent** | `main.py` | 多 Agent 架构,每个交易对独立决策上下文 | |
| 27 | +| **网格交易 Grid Flow** | `grid_main.py` | AI 驱动的动态网格做市,LLM 判断方向和宽度,数学引擎计算参数 | |
| 28 | + |
| 29 | +## 核心功能 |
| 30 | + |
| 31 | +### 基础能力 |
| 32 | + |
| 33 | +- 🤖 **多 Agent 架构** — 每个交易对独立决策,互不干扰 |
| 34 | +- 🔌 **多 LLM 支持** — OpenAI、NVIDIA、Google、Cloudflare、LiteLLM |
| 35 | +- 📊 **网格交易策略** — AI 驱动的动态网格做市 |
| 36 | +- 📐 **凯利公式仓位管理** — 动态计算最优仓位 |
| 37 | +- 🛡️ **ATR 动态止盈止损** — 波动率自适应风险管理 |
| 38 | +- 🔒 **账户保护** — 最大回撤限制、持仓超时机制 |
| 39 | +- 🔍 **决策验证** — 多周期趋势共振、信号质量评估 |
| 40 | +- 📈 **回测支持** — `single/grid` 双策略,支持中断恢复 |
| 41 | +- 🔄 **API 回退机制** — LLM 和 Hyperliquid API 双重回退 |
| 42 | + |
| 43 | +### AI 决策增强功能(基于论文研究) |
| 44 | + |
| 45 | +| 功能 | 论文 | 配置开关 | 说明 | |
| 46 | +|------|------|----------|------| |
| 47 | +| **FinCoT 结构化推理** | [arXiv:2506.16123](https://arxiv.org/abs/2506.16123) | `prompt.set: nof1-improved` | 6步强制推理链,准确率 +17%,token 消耗 -8.9x | |
| 48 | +| **多空辩论 Agent** | [arXiv:2412.20138](https://arxiv.org/abs/2412.20138) | `debate.enabled` | Bull/Bear 双 Agent 消除确认偏见 | |
| 49 | +| **CEX 领先信号 + 链上数据** | [MDPI 2026](https://www.mdpi.com/2227-7390/14/2/346) | `enhanced_analysis.enabled` | Binance 资金费率、恐惧贪婪指数、MVRV/SOPR | |
| 50 | +| **Regime 自适应策略** | [Springer 2025](https://link.springer.com/article/10.1007/s42521-024-00123-2) | `regime_adaptive.enabled` | 趋势/震荡/高波动三种市场状态动态调参 | |
| 51 | +| **市场主动监控** | — | `market_monitor.enabled` | 独立线程,异常波动触发决策循环 | |
| 52 | + |
| 53 | +所有增强功能通过配置文件**独立开关控制,默认关闭**,不影响现有流程。 |
| 54 | + |
| 55 | +## 快速开始 |
| 56 | + |
| 57 | +### Docker 部署(推荐) |
| 58 | + |
| 59 | +```bash |
| 60 | +# 1. 初始化(自动配置 UID/GID、创建目录) |
| 61 | +bash init-deployment.sh |
| 62 | + |
| 63 | +# 2. 配置 |
| 64 | +cp config.yaml.example config.yaml |
| 65 | +cp config.grid.yaml.example config.grid.yaml # 可选:网格模式 |
| 66 | +vim .env # 填入 API 密钥和私钥 |
| 67 | +vim config.yaml # 配置交易参数 |
| 68 | + |
| 69 | +# 3. 启动(默认运行主策略) |
| 70 | +docker compose up -d |
| 71 | + |
| 72 | +# 仅运行网格策略 |
| 73 | +RUN_MODE=grid docker compose up -d |
| 74 | + |
| 75 | +# 同时运行主策略 + 网格策略 |
| 76 | +RUN_MODE=all docker compose up -d |
| 77 | + |
| 78 | +# 查看日志 |
| 79 | +docker compose logs -f |
| 80 | +``` |
| 81 | + |
| 82 | +### 本地开发 |
| 83 | + |
| 84 | +```bash |
| 85 | +# 安装 uv(如未安装) |
| 86 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 87 | + |
| 88 | +# 安装依赖(需要 Python 3.11+) |
| 89 | +uv sync |
| 90 | + |
| 91 | +# 配置 |
| 92 | +cp .env.example .env |
| 93 | +cp config.yaml.example config.yaml |
| 94 | + |
| 95 | +# 运行主策略 |
| 96 | +uv run python main.py |
| 97 | + |
| 98 | +# 运行网格策略 |
| 99 | +uv run python grid_main.py --config config.grid.yaml --env-file .env |
| 100 | +``` |
| 101 | + |
| 102 | +## 配置说明 |
| 103 | + |
| 104 | +### 环境变量 (`.env`) |
| 105 | + |
| 106 | +```bash |
| 107 | +# LLM API(按 config.yaml 中的 client_type 选择配置) |
| 108 | +NVIDIA_API_KEY=xxx |
| 109 | +OPENAI_API_KEY=xxx |
| 110 | +OPENAI_API_BASE=xxx |
| 111 | + |
| 112 | +# Hyperliquid |
| 113 | +HYPERLIQUID_PRIVATE_KEY=0x... # 钱包私钥 |
| 114 | +HYPERLIQUID_TESTNET=true # true=测试网,false=主网 |
| 115 | +``` |
| 116 | + |
| 117 | +> **钱包模式**:单钱包模式只填 `HYPERLIQUID_PRIVATE_KEY`;API 钱包代理模式额外填 `HYPERLIQUID_ACCOUNT_ADDRESS`,需在主钱包网页端授权。 |
| 118 | +
|
| 119 | +### 交易配置 (`config.yaml`) |
| 120 | + |
| 121 | +```yaml |
| 122 | +llm: |
| 123 | + client_type: langchain_nvidia # openai / cloudflare / google / litellm / nvidia |
| 124 | + model: deepseek-ai/deepseek-v3.2 |
| 125 | + temperature: 0.2 |
| 126 | + |
| 127 | +trading: |
| 128 | + symbols: [BTC, ETH] |
| 129 | + max_trade_amount: 100 # 单笔上限(美元) |
| 130 | + max_leverage: 10 |
| 131 | + |
| 132 | +prompt: |
| 133 | + set: nof1-improved # 推荐:集成 FinCoT 6步推理链 |
| 134 | + |
| 135 | +enhanced_analysis: |
| 136 | + enabled: true |
| 137 | + |
| 138 | +debate: |
| 139 | + enabled: false # 启用后每次决策额外 2 次 LLM 调用 |
| 140 | + |
| 141 | +regime_adaptive: |
| 142 | + enabled: false # 依赖 enhanced_analysis: true |
| 143 | + |
| 144 | +account_protection: |
| 145 | + enabled: true |
| 146 | + max_drawdown_pct: 0.10 # 最大回撤 10% |
| 147 | + max_daily_loss_pct: 0.05 # 单日亏损 5% |
| 148 | + |
| 149 | +market_monitor: |
| 150 | + enabled: false |
| 151 | + alert_threshold_pct: 3.0 # 波动超过 3% 触发决策 |
| 152 | +``` |
| 153 | +
|
| 154 | +完整配置参考 [`config.yaml.example`](config.yaml.example)。 |
| 155 | + |
| 156 | +## 回测 |
| 157 | + |
| 158 | +```bash |
| 159 | +# 永续合约策略回测 |
| 160 | +uv run python backtest.py --symbol BTC --strategy single \ |
| 161 | + --start-date 2024-01-01 --end-date 2024-12-01 |
| 162 | +
|
| 163 | +# 网格策略回测 |
| 164 | +uv run python backtest.py --symbol BTC --strategy grid \ |
| 165 | + --start-date 2024-01-01 --end-date 2024-12-01 |
| 166 | +
|
| 167 | +# 从检查点恢复中断的回测 |
| 168 | +uv run python backtest.py \ |
| 169 | + --resume-from backtest_results/backtest_BTC_xxx/live_report.json |
| 170 | +
|
| 171 | +# A/B 对比回测(验证各功能效果) |
| 172 | +uv run python backtest_comparison.py --symbol BTC --compare all |
| 173 | +uv run python backtest_comparison.py --symbol BTC --compare fincot |
| 174 | +``` |
| 175 | + |
| 176 | +详细说明参考 [`BACKTEST_README.md`](BACKTEST_README.md)。 |
| 177 | + |
| 178 | +## 测试 |
| 179 | + |
| 180 | +```bash |
| 181 | +uv run pytest tests/ |
| 182 | +uv run pytest tests/test_decision_validator.py -v |
| 183 | +uv run pytest tests/ --cov=src |
| 184 | +``` |
| 185 | + |
| 186 | +## 项目结构 |
| 187 | + |
| 188 | +``` |
| 189 | +quant-flow/ |
| 190 | +├── main.py # 主策略入口(永续合约 Agent) |
| 191 | +├── grid_main.py # 网格策略入口(Grid Flow) |
| 192 | +├── backtest.py # 回测运行器 |
| 193 | +├── backtest_comparison.py # A/B 对比工具 |
| 194 | +├── src/ |
| 195 | +│ ├── agent/ # Agent 实现 |
| 196 | +│ ├── trading/ # 交易核心(客户端、订单、风控) |
| 197 | +│ ├── data/ # 市场数据、指标、增强器 |
| 198 | +│ ├── llm/ # LLM 客户端封装 |
| 199 | +│ ├── backtest/ # 回测引擎 |
| 200 | +│ └── notification/ # 通知模块 |
| 201 | +├── prompts/ # 8 套 Prompt 策略模板 |
| 202 | +├── website/ # Docusaurus 文档站点 |
| 203 | +└── tests/ # 测试套件 |
| 204 | +``` |
| 205 | +
|
| 206 | +## Docker 管理 |
| 207 | +
|
| 208 | +```bash |
| 209 | +docker compose up -d # 启动 |
| 210 | +docker compose down # 停止 |
| 211 | +docker compose logs -f # 查看日志 |
| 212 | +docker compose ps # 查看状态 |
| 213 | +
|
| 214 | +# 更新到最新版本 |
| 215 | +git pull && docker compose build && docker compose up -d |
| 216 | +``` |
| 217 | + |
| 218 | +**运行模式**(通过 `RUN_MODE` 环境变量控制): |
| 219 | +- `main` — 仅主交易策略(默认) |
| 220 | +- `grid` — 仅网格交易策略 |
| 221 | +- `all` — 同时运行两种策略 |
| 222 | + |
| 223 | +## 常见问题 |
| 224 | + |
| 225 | +| 错误 | 解决方案 | |
| 226 | +|------|----------| |
| 227 | +| `PermissionError: /app/logs/...` | 运行 `bash init-deployment.sh` | |
| 228 | +| `open interest is at cap` | 该资产达到开放利益上限,换其他交易对 | |
| 229 | +| `Leverage exceeds maximum allowed` | 降低配置中的 `max_leverage` | |
| 230 | +| API 钱包能查余额但不能下单 | 在主钱包网页端授权 API 钱包地址 | |
| 231 | + |
| 232 | +## 相关链接 |
| 233 | + |
| 234 | +- 📖 [完整文档](https://loadchange.github.io/quant-flow/zh-Hans/docs/intro) |
| 235 | +- 🏦 [Hyperliquid](https://hyperliquid.xyz/) |
| 236 | +- 🚰 [测试网水龙头](https://app.hyperliquid-testnet.xyz/faucet) |
| 237 | +- ⛓️ [LangChain](https://python.langchain.com/) |
| 238 | +- 📊 [回测文档](BACKTEST_README.md) |
| 239 | + |
| 240 | +--- |
| 241 | + |
| 242 | +免责声明:本项目仅供学习研究,生产环境使用需自行承担风险。 |
0 commit comments