Administrator
14 hours ago a986f3571c7e18ade4665fe5999b445b5762264d
refactor(gateApi): 修改网格交易队列生成算法

- 将基于百分比的价格计算改为基于绝对步长的计算方式
- 引入 step 概念,使用 shortBaseEntryPrice × gridRate 作为固定步长
- 重构 generateShortQueue 和 generateLongQueue 方法的计算逻辑
- 更新 processShortGrid 和 processLongGrid 的队列补充和转移逻辑
- 添加 step 字段到 GateConfig 配置类中
- 修改文档中的算法描述以反映新的计算方式
3 files modified
141 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 69 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/gateApi-logic.md 63 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -76,6 +76,8 @@
    private final BigDecimal contractMultiplier;
    /** 未实现盈亏计价模式:最新价 / 标记价格 */
    private final PnLPriceMode unrealizedPnlPriceMode;
    /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
    private BigDecimal step;
    private GateConfig(Builder builder) {
        this.apiKey = builder.apiKey;
@@ -172,6 +174,13 @@
    /** @return 未实现盈亏计价模式:LAST_PRICE(最新成交价)/ MARK_PRICE(标记价格) */
    public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
    // ==================== 运行时参数 ====================
    /** @return 网格绝对步长(shortBaseEntryPrice × gridRate),运行时设置 */
    public BigDecimal getStep() { return step; }
    /** 设置网格绝对步长(由 generateShortQueue 在运行时计算并注入) */
    public void setStep(BigDecimal step) { this.step = step; }
    // ==================== 环境 ====================
    /** @return 是否为生产环境(true=实盘生产网 / false=模拟盘测试网) */
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -479,32 +479,35 @@
    /**
     * 生成空仓价格队列。
     * 以空头基底入场价 shortBaseEntryPrice 为基准,按 gridRate 递减生成 gridQueueSize 个价格元素:
     * <pre>shortBaseEntryPrice × (1 − gridRate × i), i=1..gridQueueSize</pre>
     * 以 shortBaseEntryPrice × gridRate 作为绝对步长 step,存到 config。
     * 第1个元素 = shortBaseEntryPrice − step,后续依次递减 step,共 gridQueueSize 个。
     * 队列降序排列(大→小),方便 processShortGrid 中从头遍历。
     */
    private void generateShortQueue() {
        shortPriceQueue.clear();
        BigDecimal step = config.getGridRate();
        for (int i = 1; i <= config.getGridQueueSize(); i++) {
            shortPriceQueue.add(shortBaseEntryPrice.multiply(BigDecimal.ONE.subtract(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP));
        BigDecimal step = shortBaseEntryPrice.multiply(config.getGridRate()).setScale(1, RoundingMode.HALF_UP);
        config.setStep(step);
        BigDecimal elem = shortBaseEntryPrice.subtract(step).setScale(1, RoundingMode.HALF_UP);
        for (int i = 0; i < config.getGridQueueSize(); i++) {
            shortPriceQueue.add(elem);
            elem = elem.subtract(step).setScale(1, RoundingMode.HALF_UP);
        }
        shortPriceQueue.sort((a, b) -> b.compareTo(a));
        //输出队列:shortPriceQueue;
        log.info("[Gate] 空队列:{}", shortPriceQueue);
    }
    /**
     * 生成多仓价格队列。
     * 以多头基底入场价 longBaseEntryPrice 为基准,按 gridRate 递增生成 gridQueueSize 个价格元素:
     * <pre>longBaseEntryPrice × (1 + gridRate × i), i=1..gridQueueSize</pre>
     * 以 shortBaseEntryPrice + step 为首元素,后续依次递增 step,共 gridQueueSize 个。
     * 队列升序排列(小→大),方便 processLongGrid 中从头遍历。
     */
    private void generateLongQueue() {
        longPriceQueue.clear();
        BigDecimal step = config.getGridRate();
        for (int i = 1; i <= config.getGridQueueSize(); i++) {
            longPriceQueue.add(longBaseEntryPrice.multiply(BigDecimal.ONE.add(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP));
        BigDecimal step = config.getStep();
        BigDecimal elem = shortBaseEntryPrice.add(step).setScale(1, RoundingMode.HALF_UP);
        for (int i = 0; i < config.getGridQueueSize(); i++) {
            longPriceQueue.add(elem);
            elem = elem.add(step).setScale(1, RoundingMode.HALF_UP);
        }
        longPriceQueue.sort(BigDecimal::compareTo);
        log.info("[Gate] 多队列:{}", longPriceQueue);
@@ -520,8 +523,8 @@
     * <h3>执行流程</h3>
     * <ol>
     *   <li>匹配队列元素 → 为空则直接返回</li>
     *   <li>空仓队列:移除 matched 元素,尾部补充新元素(尾价 × (1 − gridRate) 循环递减)</li>
     *   <li>多仓队列:以多仓队列首元素(最小价)为种子,生成 matched.size() 个递减元素加入</li>
     *   <li>空仓队列:移除 matched 元素,尾部补充新元素(尾价 − step 循环递减)</li>
     *   <li>多仓队列:以多仓队列首元素(最小价)为种子,递减 step 生成 matched.size() 个元素加入</li>
     *   <li>保证金检查 → 安全则开空一次</li>
     *   <li>额外反向开多:若多仓均价 > 空仓均价 且 当前价夹在中间且远离多仓均价</li>
     * </ol>
@@ -550,9 +553,9 @@
        synchronized (shortPriceQueue) {
            shortPriceQueue.removeAll(matched);
            BigDecimal min = shortPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : shortPriceQueue.get(shortPriceQueue.size() - 1);
            BigDecimal step = config.getGridRate();
            BigDecimal gridStep = config.getStep();
            for (int i = 0; i < matched.size(); i++) {
                min = min.multiply(BigDecimal.ONE.subtract(step)).setScale(1, RoundingMode.HALF_UP);
                min = min.subtract(gridStep).setScale(1, RoundingMode.HALF_UP);
                shortPriceQueue.add(min);
                log.info("[Gate] 空队列增加:{}", min);
            }
@@ -564,14 +567,14 @@
        synchronized (longPriceQueue) {
            BigDecimal first = longPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : longPriceQueue.get(0);
            BigDecimal step = config.getGridRate();
            BigDecimal gridStep = config.getStep();
            for (int i = 1; i <= matched.size(); i++) {
                BigDecimal elem = first.multiply(BigDecimal.ONE.subtract(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                if (longEntryPrice.compareTo(BigDecimal.ZERO) > 0
                        && currentPrice.subtract(longEntryPrice).abs().compareTo(longEntryPrice.multiply(step)) < 0) {
                    log.info("[Gate] 多队列跳过(price≈longEntry):{}", elem);
                    continue;
                }
                BigDecimal elem = first.subtract(gridStep.multiply(BigDecimal.valueOf(i))).setScale(1, RoundingMode.HALF_UP);
//                if (longEntryPrice.compareTo(BigDecimal.ZERO) > 0
//                        && currentPrice.subtract(longEntryPrice).abs().compareTo(longEntryPrice.multiply(config.getGridRate())) < 0) {
//                    log.info("[Gate] 多队列跳过(price≈longEntry):{}", elem);
//                    continue;
//                }
                longPriceQueue.add(elem);
                log.info("[Gate] 多队列增加:{}", elem);
            }
@@ -607,8 +610,8 @@
     * <h3>执行流程</h3>
     * <ol>
     *   <li>匹配队列元素 → 为空则直接返回</li>
     *   <li>多仓队列:移除 matched 元素,尾部补充新元素(尾价 × (1 + gridRate) 循环递增)</li>
     *   <li>空仓队列:以空仓队列首元素(最高价)为种子,生成 matched.size() 个递增元素加入</li>
     *   <li>多仓队列:移除 matched 元素,尾部补充新元素(尾价 + step 循环递增)</li>
     *   <li>空仓队列:以空仓队列首元素(最高价)为种子,递增 step 生成 matched.size() 个元素加入</li>
     *   <li>保证金检查 → 安全则开多一次</li>
     *   <li>额外反向开空:若多仓均价 > 空仓均价 且 当前价夹在中间且远离空仓均价</li>
     * </ol>
@@ -638,9 +641,9 @@
        synchronized (longPriceQueue) {
            longPriceQueue.removeAll(matched);
            BigDecimal max = longPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : longPriceQueue.get(longPriceQueue.size() - 1);
            BigDecimal step = config.getGridRate();
            BigDecimal gridStep = config.getStep();
            for (int i = 0; i < matched.size(); i++) {
                max = max.multiply(BigDecimal.ONE.add(step)).setScale(1, RoundingMode.HALF_UP);
                max = max.add(gridStep).setScale(1, RoundingMode.HALF_UP);
                longPriceQueue.add(max);
                log.info("[Gate] 多队列增加:{}", max);
@@ -652,14 +655,14 @@
        synchronized (shortPriceQueue) {
            BigDecimal first = shortPriceQueue.isEmpty() ? matched.get(0) : shortPriceQueue.get(0);
            BigDecimal step = config.getGridRate();
            BigDecimal gridStep = config.getStep();
            for (int i = 1; i <= matched.size(); i++) {
                BigDecimal elem = first.multiply(BigDecimal.ONE.add(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                if (shortEntryPrice.compareTo(BigDecimal.ZERO) > 0
                        && currentPrice.subtract(shortEntryPrice).abs().compareTo(shortEntryPrice.multiply(step)) < 0) {
                    log.info("[Gate] 空队列跳过(price≈shortEntry):{}", elem);
                    continue;
                }
                BigDecimal elem = first.add(gridStep.multiply(BigDecimal.valueOf(i))).setScale(1, RoundingMode.HALF_UP);
//                if (shortEntryPrice.compareTo(BigDecimal.ZERO) > 0
//                        && currentPrice.subtract(shortEntryPrice).abs().compareTo(shortEntryPrice.multiply(config.getGridRate())) < 0) {
//                    log.info("[Gate] 空队列跳过(price≈shortEntry):{}", elem);
//                    continue;
//                }
                shortPriceQueue.add(elem);
                log.info("[Gate] 空队列增加:{}", elem);
            }
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,10 +188,10 @@
├─ processShortGrid: 当前价 < 空仓队列元素(价格跌破了队列中的高价)
│   ├─ 匹配: 收集所有 > 当前价的空仓队列元素(降序,一旦遇≤即停止)
│   ├─ 空仓队列: 移除 matched,尾部补充新元素(尾价 × (1−gridRate) 循环递减)
│   ├─ 空仓队列: 移除 matched,尾部补充新元素(尾价 − step 循环递减)
│   ├─ 多仓队列转移:
│   │   ├─ 以多仓队列首元素(最小价)为种子
│   │   ├─ 生成 matched.size() 个递减元素: seed × (1 − gridRate × i)
│   │   ├─ 生成 matched.size() 个递减元素: seed − step × i
│   │   ├─ 贴近过滤: |currentPrice − longEntryPrice| < longEntryPrice × gridRate → 跳过
│   │   └─ 升序排列,截断到 gridQueueSize
│   └─ 下单(队列已更新,避免竞态):
@@ -200,10 +202,10 @@
└─ processLongGrid: 当前价 > 多仓队列元素(价格涨超了队列中的低价)
    ├─ 匹配: 收集所有 < 当前价的多仓队列元素(升序,一旦遇≥即停止)
    ├─ 多仓队列: 移除 matched,尾部补充新元素(尾价 × (1+gridRate) 循环递增)
    ├─ 多仓队列: 移除 matched,尾部补充新元素(尾价 + step 循环递增)
    ├─ 空仓队列转移:
    │   ├─ 以空仓队列首元素(最高价)为种子
    │   ├─ 生成 matched.size() 个递增元素: seed × (1 + gridRate × i)
    │   ├─ 生成 matched.size() 个递增元素: seed + step × i
    │   ├─ 贴近过滤: |currentPrice − shortEntryPrice| < shortEntryPrice × gridRate → 跳过
    │   └─ 降序排列,截断到 gridQueueSize
    └─ 下单(队列已更新,避免竞态):
@@ -215,12 +217,12 @@
### 队列转移规则(新)
触发后**不再简单复制** matched 元素到对方队列,而是以对方队列首元素为种子生成新元素:
触发后**不再简单复制** matched 元素到对方队列,而是以对方队列首元素为种子,按绝对步长 step 生成新元素:
| 触发方向 | 目标队列 | 种子元素 | 生成公式 | 排序 |
|---------|---------|---------|---------|------|
| 空仓触发 → | 多仓队列 | 多仓队列首元素(最小价) | 种子 × (1 − gridRate × i) | 升序 |
| 多仓触发 → | 空仓队列 | 空仓队列首元素(最高价) | 种子 × (1 + gridRate × i) | 降序 |
| 空仓触发 → | 多仓队列 | 多仓队列首元素(最小价) | 种子 − step × i | 升序 |
| 多仓触发 → | 空仓队列 | 空仓队列首元素(最高价) | 种子 + step × i | 降序 |
### 贴近持仓均价过滤(新)
@@ -252,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)
价格涨到 2290 → processLongGrid 触发:
  匹配: [2277.9, 2285.8](都 < 2290)
  
  空仓队列: 移除[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]
  多仓队列: 移除[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]
  
  当前价 2571 对比: 空仓均价=2270, 多仓均价=2280
    多>空 成立,但 2270 < 2571 < 2280×(1−0.0035)=2272 ? 否
    → 不触发额外开多
  空仓队列转移:
    种子=空仓首元素 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 可增加步长密度。
```
---
@@ -357,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 | 下单张数 |
@@ -445,10 +450,8 @@
| 步骤 | processShortGrid | processLongGrid |
|------|-----------------|-----------------|
| 匹配 | 收集 shortPriceQueue 中 > currentPrice 的元素 | 收集 longPriceQueue 中 < currentPrice 的元素 |
| 主开仓 | 保证金安全 → openShort | 保证金安全 → openLong |
| 额外反向 | 条件满足 → openLong | 条件满足 → openShort |
| 本队补充 | 尾价 × (1−gridRate) 循环递减 | 尾价 × (1+gridRate) 循环递增 |
| 对方转移 | 以多仓首元素为种子,递减生成 | 以空仓首元素为种子,递增生成 |
| 本队补充 | 尾价 − step 循环递减 | 尾价 + step 循环递增 |
| 对方转移 | 以多仓首元素为种子,递减 step | 以空仓首元素为种子,递增 step |
| 贴近过滤 | 新增与 longEntryPrice 太近 → 跳过 | 新增与 shortEntryPrice 太近 → 跳过 |
**未实现盈亏计算** (`updateUnrealizedPnl()`):