From 63f1968d7943be5c767149a2107d738a97ff9bd3 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 09 Nov 2026 11:03:08 +0800
Subject: [PATCH] refactor(grid): 重构止损挂单逻辑为单位置全量止损

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   88 +++++++++++++++++++++++++++-----------------
 1 files changed, 54 insertions(+), 34 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 b47e02d..7da0847 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1641,7 +1641,11 @@
         int longPos = longPositionSize.intValue();
         if (longPos > 0) {
             GridElement anchor = findNearestLongStopLossGrid();
-            if (anchor != null) {
+            if (anchor == null) {
+                // 止损位全部丢失(断线期间撤掉未重挂/幽灵被对账清空)→ 按默认位整补止盈+止损
+                log.warn("[Gate] 多仓有持仓{}张但无任何止损位, 对账按默认位补挂止盈止损", longPos);
+                rebuildLongProtection();
+            } else {
                 int covered = 0;
                 for (GridElement e : config.getGridElements()) {
                     covered += e.getLongStopLossTotalSize();
@@ -1658,7 +1662,11 @@
         int shortPos = shortPositionSize.intValue();
         if (shortPos > 0) {
             GridElement anchor = findNearestShortStopLossGrid();
-            if (anchor != null) {
+            if (anchor == null) {
+                // 止损位全部丢失(断线期间撤掉未重挂/幽灵被对账清空)→ 按默认位整补止盈+止损
+                log.warn("[Gate] 空仓有持仓{}张但无任何止损位, 对账按默认位补挂止盈止损", shortPos);
+                rebuildShortProtection();
+            } else {
                 int covered = 0;
                 for (GridElement e : config.getGridElements()) {
                     covered += e.getShortStopLossTotalSize();
@@ -1672,6 +1680,48 @@
                 }
             }
         }
+    }
+
+    /**
+     * 按默认位(基底网格 ±2)重建多仓止盈+止损:撤旧 → 按最新持仓挂单位置全量止损 + 超额/基础止盈。
+     * 供对账兜底(止损位全部丢失时)与手动重建共用。
+     */
+    private void rebuildLongProtection() {
+        cancelAllLongTakeProfitsAndStopLosses();
+        int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
+        if (posSize <= 0) {
+            log.warn("[Gate] 生成多仓止盈止损失败:无多仓持仓");
+            return;
+        }
+        extendLongStopLoss(posSize, 0);
+        if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
+            placeExcessTakeProfit(posSize, true);
+        } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
+            placeDispersedTakeProfit(posSize, true, 0);
+        }
+        placeBaseTakeProfit(posSize, true, 0);
+        log.info("[Gate] 多仓止盈止损已按默认位重建, 仓位:{}张", posSize);
+    }
+
+    /**
+     * 按默认位(基底网格 ±2)重建空仓止盈+止损:撤旧 → 按最新持仓挂单位置全量止损 + 超额/基础止盈。
+     * 供对账兜底(止损位全部丢失时)与手动重建共用。
+     */
+    private void rebuildShortProtection() {
+        cancelAllShortTakeProfitsAndStopLosses();
+        int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
+        if (posSize <= 0) {
+            log.warn("[Gate] 生成空仓止盈止损失败:无空仓持仓");
+            return;
+        }
+        extendShortStopLoss(posSize, 0);
+        if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
+            placeExcessTakeProfit(posSize, false);
+        } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
+            placeDispersedTakeProfit(posSize, false, 0);
+        }
+        placeBaseTakeProfit(posSize, false, 0);
+        log.info("[Gate] 空仓止盈止损已按默认位重建, 仓位:{}张", posSize);
     }
 
     /**
@@ -3270,22 +3320,7 @@
      * 先撤多仓所有止盈止损,再按交易所最新持仓重挂单位置全量止损,超额部分挂止盈到空仓第一止损位。
      */
     public void rebuildLongTakeProfitAndStopLoss() {
-        submitManualTask(() -> {
-            cancelAllLongTakeProfitsAndStopLosses();
-            int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
-            if (posSize <= 0) {
-                log.warn("[Gate] 生成多仓止盈止损失败:无多仓持仓");
-                return;
-            }
-            extendLongStopLoss(posSize, 0);
-            if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
-                placeExcessTakeProfit(posSize, true);
-            } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
-                placeDispersedTakeProfit(posSize, true, 0);
-            }
-            placeBaseTakeProfit(posSize, true, 0);
-            log.info("[Gate] 生成多仓止盈止损完成, 仓位:{}张", posSize);
-        });
+        submitManualTask(this::rebuildLongProtection);
     }
 
     /**
@@ -3293,22 +3328,7 @@
      * 先撤空仓所有止盈止损,再按交易所最新持仓重挂单位置全量止损,超额部分挂止盈到多仓第一止损位。
      */
     public void rebuildShortTakeProfitAndStopLoss() {
-        submitManualTask(() -> {
-            cancelAllShortTakeProfitsAndStopLosses();
-            int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
-            if (posSize <= 0) {
-                log.warn("[Gate] 生成空仓止盈止损失败:无空仓持仓");
-                return;
-            }
-            extendShortStopLoss(posSize, 0);
-            if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
-                placeExcessTakeProfit(posSize, false);
-            } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
-                placeDispersedTakeProfit(posSize, false, 0);
-            }
-            placeBaseTakeProfit(posSize, false, 0);
-            log.info("[Gate] 生成空仓止盈止损完成, 仓位:{}张", posSize);
-        });
+        submitManualTask(this::rebuildShortProtection);
     }
 
     /**

--
Gitblit v1.9.1