From a986f3571c7e18ade4665fe5999b445b5762264d Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 11 May 2026 23:07:15 +0800
Subject: [PATCH] refactor(gateApi): 修改网格交易队列生成算法
---
src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md | 115 ++++++++++++++++++++++++++++++---------------------------
1 files changed, 60 insertions(+), 55 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md b/src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md
index e3207b4..525793f 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md
@@ -172,12 +172,14 @@
### 网格队列生成
-以基底入场价为基准,按 `gridRate`(百分比步长)生成 N 个价格(N = gridQueueSize,默认50):
+以空头基底入场价 `shortBaseEntryPrice` 为唯一基准,计算绝对步长 `step = shortBaseEntryPrice × gridRate`(保留1位小数),存入 config。
+
+两个队列均从 `shortBaseEntryPrice` 出发,按 `step` 绝对偏移生成 N 个价格(N = gridQueueSize,默认50):
| 队列 | 计算方式 | 排序 |
|------|---------|------|
-| 空仓队列 shortPriceQueue | 基底空入场价 × (1 − gridRate × i) (i=1..N) | 降序(大→小) |
-| 多仓队列 longPriceQueue | 基底多入场价 × (1 + gridRate × i) (i=1..N) | 升序(小→大) |
+| 空仓队列 shortPriceQueue | 首元素 = shortBaseEntryPrice − step,后续依次 −step (i=0..N-1) | 降序(大→小) |
+| 多仓队列 longPriceQueue | 首元素 = shortBaseEntryPrice + step,后续依次 +step (i=0..N-1) | 升序(小→大) |
### K线触发网格
@@ -186,39 +188,41 @@
│
├─ processShortGrid: 当前价 < 空仓队列元素(价格跌破了队列中的高价)
│ ├─ 匹配: 收集所有 > 当前价的空仓队列元素(降序,一旦遇≤即停止)
-│ ├─ 保证金检查: positionInitialMargin / initialPrincipal < marginRatioLimit(20%)
-│ │ ├─ 安全 → openShort 开空单
-│ │ │ └─ 额外条件: 多>空 且 空仓均价 < 当前价 < 多仓均价×(1−gridRate) → openLong 额外开多一次
-│ │ └─ 超限 → 跳过开仓,队列照常更新
-│ ├─ 空仓队列: 移除 matched,尾部补充新元素(尾价 × (1−gridRate) 循环递减)
-│ └─ 多仓队列转移:
-│ ├─ 以多仓队列首元素(最小价)为种子
-│ ├─ 生成 matched.size() 个递减元素: seed × (1 − gridRate × i)
-│ ├─ 贴近过滤: |elem − longEntryPrice| < longEntryPrice × gridRate → 跳过
-│ └─ 升序排列,截断到 gridQueueSize
+│ ├─ 空仓队列: 移除 matched,尾部补充新元素(尾价 − step 循环递减)
+│ ├─ 多仓队列转移:
+│ │ ├─ 以多仓队列首元素(最小价)为种子
+│ │ ├─ 生成 matched.size() 个递减元素: seed − step × i
+│ │ ├─ 贴近过滤: |currentPrice − longEntryPrice| < longEntryPrice × gridRate → 跳过
+│ │ └─ 升序排列,截断到 gridQueueSize
+│ └─ 下单(队列已更新,避免竞态):
+│ ├─ 保证金检查: positionInitialMargin / initialPrincipal < marginRatioLimit(20%)
+│ │ ├─ 安全 → openShort 开空单
+│ │ │ └─ 额外条件: 多>空 且 空仓均价 < 当前价 < 多仓均价×(1−gridRate) → openLong 额外开多一次
+│ │ └─ 超限 → 跳过开仓
│
└─ processLongGrid: 当前价 > 多仓队列元素(价格涨超了队列中的低价)
├─ 匹配: 收集所有 < 当前价的多仓队列元素(升序,一旦遇≥即停止)
- ├─ 保证金检查: 同上
- │ ├─ 安全 → openLong 开多单
- │ │ └─ 额外条件: 多>空 且 空仓均价×(1+gridRate) < 当前价 < 多仓均价 → openShort 额外开空一次
- │ └─ 超限 → 跳过开仓
- ├─ 多仓队列: 移除 matched,尾部补充新元素(尾价 × (1+gridRate) 循环递增)
- └─ 空仓队列转移:
- ├─ 以空仓队列首元素(最高价)为种子
- ├─ 生成 matched.size() 个递增元素: seed × (1 + gridRate × i)
- ├─ 贴近过滤: |elem − shortEntryPrice| < shortEntryPrice × gridRate → 跳过
- └─ 降序排列,截断到 gridQueueSize
+ ├─ 多仓队列: 移除 matched,尾部补充新元素(尾价 + step 循环递增)
+ ├─ 空仓队列转移:
+ │ ├─ 以空仓队列首元素(最高价)为种子
+ │ ├─ 生成 matched.size() 个递增元素: seed + step × i
+ │ ├─ 贴近过滤: |currentPrice − shortEntryPrice| < shortEntryPrice × gridRate → 跳过
+ │ └─ 降序排列,截断到 gridQueueSize
+ └─ 下单(队列已更新,避免竞态):
+ ├─ 保证金检查: 同上
+ │ ├─ 安全 → openLong 开多单
+ │ │ └─ 额外条件: 多>空 且 空仓均价×(1+gridRate) < 当前价 < 多仓均价 → openShort 额外开空一次
+ │ └─ 超限 → 跳过开仓
```
### 队列转移规则(新)
-触发后**不再简单复制** matched 元素到对方队列,而是以对方队列首元素为种子生成新元素:
+触发后**不再简单复制** matched 元素到对方队列,而是以对方队列首元素为种子,按绝对步长 step 生成新元素:
| 触发方向 | 目标队列 | 种子元素 | 生成公式 | 排序 |
|---------|---------|---------|---------|------|
-| 空仓触发 → | 多仓队列 | 多仓队列首元素(最小价) | 种子 × (1 − gridRate × i) | 升序 |
-| 多仓触发 → | 空仓队列 | 空仓队列首元素(最高价) | 种子 × (1 + gridRate × i) | 降序 |
+| 空仓触发 → | 多仓队列 | 多仓队列首元素(最小价) | 种子 − step × i | 升序 |
+| 多仓触发 → | 空仓队列 | 空仓队列首元素(最高价) | 种子 + step × i | 降序 |
### 贴近持仓均价过滤(新)
@@ -250,26 +254,28 @@
### 队列转移示意
```
-ETH_USDT, gridRate=0.0035, 空仓均价=2270, 多仓均价=2280, gridQueueSize=4:
+ETH_USDT, gridRate=0.0035, shortBaseEntryPrice=2270, step=2270×0.0035≈7.9, gridQueueSize=4:
初始状态:
- 空仓队列: [2567.1, 2570.0, 2572.5, 2575.0] (降序)
- 多仓队列: [2275.0, 2277.0, 2279.0, 2281.5] (升序)
+ 空仓队列: [2262.1, 2254.2, 2246.3, 2238.4] (降序, shortBaseEntryPrice−step 起递减)
+ 多仓队列: [2277.9, 2285.8, 2293.7, 2301.6] (升序, shortBaseEntryPrice+step 起递增)
-价格跌到 2571 → processShortGrid 触发:
- 匹配: [2575.0, 2572.5](都 > 2571)
-
- 空仓队列: 移除[2575.0,2572.5] → [2570.0,2567.1] → 补充递减 → [2570.0,2567.1,2560.0,2552.0]
- 多仓队列转移:
- 种子=多仓首元素 2275.0
- 生成: 2275.0×(1−0.0035×1)≈2267.0, 2275.0×(1−0.0035×2)≈2259.0
- 贴近过滤: |2267−2280|=13 > 2280×0.0035=7.98 → 通过
- |2259−2280|=21 > 7.98 → 通过
- 多仓队列: [2259.0, 2267.0, 2275.0, 2277.0] → 截断到4 → [2267.0, 2275.0, 2277.0]
-
- 当前价 2571 对比: 空仓均价=2270, 多仓均价=2280
- 多>空 成立,但 2270 < 2571 < 2280×(1−0.0035)=2272 ? 否
- → 不触发额外开多
+价格涨到 2290 → processLongGrid 触发:
+ 匹配: [2277.9, 2285.8](都 < 2290)
+
+ 多仓队列: 移除[2277.9,2285.8] → [2293.7,2301.6]
+ 补充: max=2301.6 → 2301.6+7.9=2309.5 → 2309.5+7.9=2317.4
+ → [2293.7, 2301.6, 2309.5, 2317.4]
+
+ 空仓队列转移:
+ 种子=空仓首元素 2262.1
+ 生成: 2262.1+7.9=2270.0, 2262.1+7.9×2=2277.9
+ 贴近过滤(shortEntryPrice=2270):
+ |2270.0−2270|=0.0 < 2270×0.0035=7.9 → 跳过
+ |2277.9−2270|=7.9 ≥ 7.9 → 通过
+ 空仓队列: [2277.9, 2262.1, 2254.2, 2246.3, 2238.4] → 截断到4 → [2262.1, 2254.2, 2246.3, 2238.4](2277.9被移除...)
+
+ 注:当空仓队列已满时,新元素可能因排序+截断被丢弃。缩小 gridRate 可增加步长密度。
```
---
@@ -316,10 +322,10 @@
每根K线 → onKline → updateUnrealizedPnl → processShortGrid + processLongGrid
仓位推送(每次开仓成交后自动触发):
- → DUAL_LONG, size>0, 非基底 → 设多头止盈单:止盈价=entryPrice×(1+gridRate),
- 使用 plan-close-long-position,平仓张数=-quantity(负=平多)
- → DUAL_SHORT, size<0, 非基底 → 设空头止盈单:止盈价=entryPrice×(1−gridRate),
- 使用 plan-close-short-position,平仓张数=+quantity(正=平空)
+ → DUAL_LONG, size>0, 非基底 → 设多头止盈单:止盈价=longPriceQueue[0](多仓队列首元素),
+ 使用 plan-close-long-position,平仓张数=-quantity(负=平多),rule=NUMBER_1(≥)
+ → DUAL_SHORT, size<0, 非基底 → 设空头止盈单:止盈价=shortPriceQueue[0](空仓队列首元素),
+ 使用 plan-close-short-position,平仓张数=+quantity(正=平空),rule=NUMBER_2(≤)
额外反向开仓(多>空倒挂时):
→ 空仓队列触发 + 条件满足 → 额外开多一次
@@ -355,7 +361,8 @@
| leverage | 10 | 倍数 |
| marginMode | cross | 全仓 |
| positionMode | dual | 双向持仓 |
-| gridRate | 0.0035 | 网格间距 0.35% |
+| gridRate | 0.0035 | 网格间距比率 0.35%(用于过滤阈值计算) |
+| step | 运行时计算 | 网格绝对步长 = shortBaseEntryPrice × gridRate(队列元素生成用) |
| overallTp | 0.5 USDT | 整体止盈 |
| maxLoss | 7.5 USDT | 最大亏损 |
| quantity | 1 | 下单张数 |
@@ -443,10 +450,8 @@
| 步骤 | processShortGrid | processLongGrid |
|------|-----------------|-----------------|
| 匹配 | 收集 shortPriceQueue 中 > currentPrice 的元素 | 收集 longPriceQueue 中 < currentPrice 的元素 |
-| 主开仓 | 保证金安全 → openShort | 保证金安全 → openLong |
-| 额外反向 | 条件满足 → openLong | 条件满足 → openShort |
-| 本队补充 | 尾价 × (1−gridRate) 循环递减 | 尾价 × (1+gridRate) 循环递增 |
-| 对方转移 | 以多仓首元素为种子,递减生成 | 以空仓首元素为种子,递增生成 |
+| 本队补充 | 尾价 − step 循环递减 | 尾价 + step 循环递增 |
+| 对方转移 | 以多仓首元素为种子,递减 step | 以空仓首元素为种子,递增 step |
| 贴近过滤 | 新增与 longEntryPrice 太近 → 跳过 | 新增与 shortEntryPrice 太近 → 跳过 |
**未实现盈亏计算** (`updateUnrealizedPnl()`):
@@ -473,10 +478,10 @@
| 方向 | 公式 | order_type | size(平仓张数) | rule |
|------|------|------------|-----------|------|
-| 多头 TP | entry × (1+gridRate) | `plan-close-long-position` | `-quantity`(负=平多) | NUMBER_1(≥触发价) |
-| 空头 TP | entry × (1−gridRate) | `plan-close-short-position` | `+quantity`(正=平空) | NUMBER_2(≤触发价) |
+| 多头 TP | longPriceQueue[0](多仓队列首元素) | `plan-close-long-position` | `-quantity`(负=平多) | NUMBER_1(≥触发价) |
+| 空头 TP | shortPriceQueue[0](空仓队列首元素) | `plan-close-short-position` | `+quantity`(正=平空) | NUMBER_2(≤触发价) |
-> 止盈单使用显式张数而非 autoSize。每次网格触发开仓 quantity 张,只为该批张数创建独立的条件单,多个止盈单之间互不覆盖。
+> 止盈价取自对应方向队列的首元素(多仓队列升序首=最小价,空仓队列降序首=最高价)。止盈单使用显式张数而非 autoSize。每次网格触发开仓 quantity 张,只为该批张数创建独立的条件单,多个止盈单之间互不覆盖。
**REST API 调用**:
--
Gitblit v1.9.1