From 7ebe033e5cf2011c76ce60c2bc0df3cb667f78e3 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 22 Jun 2026 13:31:16 +0800
Subject: [PATCH] feat(gateApi): 添加网格策略重启跨度阈值功能

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java       |   90 ---------------------------------------------
 src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java |    1 
 src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java                 |    9 ++++
 3 files changed, 10 insertions(+), 90 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
index 4a641e7..81e2209 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -101,6 +101,8 @@
     private final PnLPriceMode unrealizedPnlPriceMode;
     /** 最大持仓张数(单方向),0=不限制 */
     private final int maxPositionSize;
+    /** 策略重启跨度阈值:多空两边止盈触发数量均达到此值后触发重启,0=禁用 */
+    private final int restartGridSpan;
     /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
     private BigDecimal step;
     /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */
@@ -131,6 +133,7 @@
         this.priceScale = builder.priceScale;
         this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode;
         this.maxPositionSize = builder.maxPositionSize;
+        this.restartGridSpan = builder.restartGridSpan;
     }
 
     // ==================== REST/WS 地址 ====================
@@ -216,6 +219,8 @@
     public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; }
     /** @return 最大持仓张数(单方向),0=不限制 */
     public int getMaxPositionSize() { return maxPositionSize; }
+    /** @return 策略重启跨度阈值:多空两边止盈触发数均达到此值后触发重启,0=禁用 */
+    public int getRestartGridSpan() { return restartGridSpan; }
 
     // ==================== 运行时参数 ====================
 
@@ -306,6 +311,8 @@
         private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE;
         /** 最大持仓张数(单方向),默认 0=不限制 */
         private int maxPositionSize = 0;
+        /** 策略重启跨度阈值:多空两边止盈触发数量均达到此值后触发重启,默认 0=禁用 */
+        private int restartGridSpan = 0;
 
         /** 设置 API Key */
         public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
@@ -347,6 +354,8 @@
         public Builder unrealizedPnlPriceMode(PnLPriceMode mode) { this.unrealizedPnlPriceMode = mode; return this; }
         /** 设置最大持仓张数(单方向),0=不限制 */
         public Builder maxPositionSize(int maxPositionSize) { this.maxPositionSize = maxPositionSize; return this; }
+        /** 设置策略重启跨度阈值:多空两边止盈触发数均达到此值后触发重启,0=禁用 */
+        public Builder restartGridSpan(int restartGridSpan) { this.restartGridSpan = restartGridSpan; return this; }
 
         public GateConfig build() {
             return new GateConfig(this);
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 01bea3a..2a52130 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1254,96 +1254,6 @@
         }
     }
 
-    // ========== 止盈/止损取消辅助方法 ==========
-
-    /**
-     * 取消最远的多仓止损订单。
-     * 多仓止损在 gridId 负方向,最远 = id 最小。
-     */
-    private void cancelFarthestLongStopLoss() {
-        GridElement farthest = null;
-        for (GridElement e : config.getGridElements()) {
-            if (e.getLongStopLossOrderId() != null) {
-                if (farthest == null || e.getId() < farthest.getId()) {
-                    farthest = e;
-                }
-            }
-        }
-        if (farthest != null) {
-            String slId = farthest.getLongStopLossOrderId();
-            farthest.setLongStopLossOrderId(null);
-            GridElement.refreshIndices();
-            GridElement finalFarthest = farthest;
-            executor.cancelConditionalOrder(slId, oid ->
-                    log.info("[Gate] 止盈触发, 取消最远多仓止损 gridId:{}, orderId:{}", finalFarthest.getId(), slId));
-        }
-    }
-
-    /**
-     * 取消最远的空仓止损订单。
-     * 空仓止损在 gridId 正方向,最远 = id 最大。
-     */
-    private void cancelFarthestShortStopLoss() {
-        GridElement farthest = null;
-        for (GridElement e : config.getGridElements()) {
-            if (e.getShortStopLossOrderId() != null) {
-                if (farthest == null || e.getId() > farthest.getId()) {
-                    farthest = e;
-                }
-            }
-        }
-        if (farthest != null) {
-            String slId = farthest.getShortStopLossOrderId();
-            farthest.setShortStopLossOrderId(null);
-            GridElement.refreshIndices();
-            GridElement finalFarthest = farthest;
-            executor.cancelConditionalOrder(slId, oid ->
-                    log.info("[Gate] 止盈触发, 取消最远空仓止损 gridId:{}, orderId:{}", finalFarthest.getId(), slId));
-        }
-    }
-
-    /**
-     * 取消所有多仓止盈 + 多仓止损订单(加仓后重建前清场)。
-     */
-    private void cancelAllLongTakeProfitsAndStopLosses() {
-        for (GridElement e : config.getGridElements()) {
-            String tpId = e.getLongTakeProfitOrderId();
-            if (tpId != null) {
-                e.setLongTakeProfitOrderId(null);
-                executor.cancelConditionalOrder(tpId, oid -> {});
-            }
-            String slId = e.getLongStopLossOrderId();
-            if (slId != null) {
-                e.setLongStopLossOrderId(null);
-                executor.cancelConditionalOrder(slId, oid -> {});
-            }
-        }
-        GridElement.refreshIndices();
-        log.info("[Gate] 已提交取消所有多仓止盈+止损");
-    }
-
-    /**
-     * 取消所有空仓止盈 + 空仓止损订单(加仓后重建前清场)。
-     */
-    private void cancelAllShortTakeProfitsAndStopLosses() {
-        for (GridElement e : config.getGridElements()) {
-            String tpId = e.getShortTakeProfitOrderId();
-            if (tpId != null) {
-                e.setShortTakeProfitOrderId(null);
-                executor.cancelConditionalOrder(tpId, oid -> {});
-            }
-            String slId = e.getShortStopLossOrderId();
-            if (slId != null) {
-                e.setShortStopLossOrderId(null);
-                executor.cancelConditionalOrder(slId, oid -> {});
-            }
-        }
-        GridElement.refreshIndices();
-        log.info("[Gate] 已提交取消所有空仓止盈+止损");
-    }
-
-    // ========== 止损追单 ==========
-
     private void extendLongStopLoss(int filledQty,int gridId) {
         int furthestSlId = 0;
         for (GridElement e : config.getGridElements()) {
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
index a7d6ccd..d38bbbb 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
@@ -69,6 +69,7 @@
                     .maxLoss(new BigDecimal("15"))
                     .baseQuantity("10")
                     .quantity("10")
+                    .restartGridSpan(5)
                     .maxPositionSize(2)
                     .priceScale(2)
                     .contractMultiplier(new BigDecimal("0.01"))

--
Gitblit v1.9.1