From f8210ffa3250f1eae4701e9360dd7a0995580fda Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 24 Sep 2026 14:23:34 +0800
Subject: [PATCH] fix(gateApi): 修正分散止盈逻辑中网格数量分配算法

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   22 ++++++++++++++++------
 1 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
index e801234..d8d8c1c 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/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;

--
Gitblit v1.9.1