Administrator
2026-09-24 aee71021d40341b95b0d36a0942c6ff2603bbbb6
fix(gateApi): 修正分散止盈逻辑中网格数量分配算法

- 调整分散止盈范围至对手盘止损位的前一格(不含止损位)
- 优化数量分配策略:当超额仓位小于网格数时前移各挂1张避免合并止盈
- 完善余数处理逻辑确保仓位合理分布在目标网格范围内
- 更新止盈位计算避免越界问题
1 files modified
22 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 22 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -2891,9 +2891,11 @@
    }
    /**
     * 分散止盈:把超出基础仓位的部分,从入场网格下一格开始逐格挂止盈单,一直挂到对手盘止损位。
     * 分散止盈:把超出基础仓位的部分,从入场网格下一格开始逐格挂止盈单,
     * 一直挂到对手盘止损位的前一格(不含止损位那一格)。
     *
     * <p>数量分配:excessQty ÷ 网格数向下取整,余数全部挂在最后一个止盈网格(对手盘止损位)。
     * <p>数量分配:每个网格至少挂 1 张;当 excessQty &lt; 网格数时,张数不够则从起点逐格各挂 1 张、
     * 挂满 excessQty 张即停止;当 excessQty ≥ 网格数时,每格 excessQty÷n,余数堆最后一格(即止损位的前一格)。
     *
     * <p>方向:多仓成交向价格更高方向(id 递增)铺到空仓第一止损位;
     * 空仓成交向价格更低方向(id 递减)铺到多仓第一止损位。
@@ -2914,9 +2916,10 @@
            log.warn("[Gate] {}分散止盈失败:未找到对手盘止损位", isLong ? "多仓" : "空仓");
            return;
        }
        int endId = endElem.getId();
        int dir = isLong ? 1 : -1;
        int startId = entryGridId + dir;
        // 分配范围到止损位的前一格截止(不含止损位那一格)
        int endId = endElem.getId() - dir;
        // 终点必须在起点前进方向上,否则退化为集中止盈(全部挂对手盘止损位)
        boolean valid = isLong ? (endId >= startId) : (endId <= startId);
@@ -2953,9 +2956,16 @@
        int remainder = excessQty % n;
        for (int i = 0; i < n; i++) {
            int qty = perGrid;
            if (i == n - 1) {
                qty += remainder;
            int qty;
            if (excessQty >= n) {
                // 超额仓位 ≥ 网格数:每格 perGrid,余数堆最后一格(即止损位的前一格)
                qty = perGrid;
                if (i == n - 1) {
                    qty += remainder;
                }
            } else {
                // 超额仓位 < 网格数:前 excessQty 个网格各挂 1 张,其余不挂(避免退化为合并止盈)
                qty = (i < excessQty) ? 1 : 0;
            }
            if (qty <= 0) {
                continue;