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 |   80 ++++++++++++++++++++++++++++-----------
 1 files changed, 57 insertions(+), 23 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 e5fca3e..2a52130 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -597,6 +597,23 @@
             return;
         }
 
+        // [Gate-需求1] 多仓止盈触发:清空止盈状态 + 取消最远多仓止损
+        GridElement longTpElem = GridElement.findByLongTakeProfitOrderId(orderId);
+        if (longTpElem != null && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
+            longTakeProfitTraderIdParam(longTpElem, null, false);
+            log.info("[Gate] 多仓止盈触发 gridId:{}, orderId:{}", longTpElem.getId(), orderId);
+            cancelFarthestLongStopLoss();
+            return;
+        }
+        // [Gate-需求1] 空仓止盈触发:清空止盈状态 + 取消最远空仓止损
+        GridElement shortTpElem = GridElement.findByShortTakeProfitOrderId(orderId);
+        if (shortTpElem != null && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
+            shortTakeProfitTraderIdParam(shortTpElem, null, false);
+            log.info("[Gate] 空仓止盈触发 gridId:{}, orderId:{}", shortTpElem.getId(), orderId);
+            cancelFarthestShortStopLoss();
+            return;
+        }
+
         GridElement longStopLossElem = GridElement.findByLongStopLossOrderId(orderId);
 //        if (longStopLossElem != null && longPositionSize.compareTo(BigDecimal.ZERO) > 0 && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
         if (longStopLossElem != null && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
@@ -615,6 +632,8 @@
             if (shortGridElement.isHasShortOrder() && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0") ){
                 int filledQty = Integer.parseInt(shortGridElement.getShortTraderParam().getQuantity());
                 shortEntryTraderIdParam(shortGridElement, null, false);
+                // [Gate-需求2] 加仓后先撤空仓所有止盈+止损,再重挂
+                cancelAllShortTakeProfitsAndStopLosses();
                 extendShortStopLoss(filledQty,shortGridElement.getId());
                 accumulatedShortLossCount = 0; // 加仓订单成交,重置止损累计
                 log.info("[Gate] 空单成交 gridId:{}", filledQty);
@@ -654,6 +673,8 @@
 
                 int filledQty = Integer.parseInt(longGridElement.getLongTraderParam().getQuantity());
                 longEntryTraderIdParam(longGridElement, null, false);
+                // [Gate-需求2] 加仓后先撤多仓所有止盈+止损,再重挂
+                cancelAllLongTakeProfitsAndStopLosses();
                 extendLongStopLoss(filledQty,longGridElement.getId());
                 accumulatedLongLossCount = 0; // 加仓订单成交,重置止损累计
                 log.info("[Gate] 多单成交 gridId:{}", filledQty);
@@ -1127,18 +1148,22 @@
             return;
         }
 
-        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+        // [Gate-BugFix] 防止与"仓位归零"重复下单:若该网格已有挂单则跳过
+        if (!newEntryGrid.isHasLongOrder()) {
+            BigDecimal triggerPrice = newEntryGrid.getGridPrice();
 
-        // 累计止损张数 + 当前止损量作为追单size,不再依赖positionSize(避免WS竞态)
-        accumulatedLongLossCount += Integer.parseInt(config.getQuantity());
-        String size = String.valueOf(accumulatedLongLossCount + Integer.parseInt(config.getQuantity()));
-        log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}基础张多单",
-                gridId, newEntryGridId, size);
+            // 累计止损张数 + 当前止损量作为追单size,不再依赖positionSize(避免WS竞态)
+            accumulatedLongLossCount += Integer.parseInt(config.getQuantity());
+            String size = String.valueOf(accumulatedLongLossCount + Integer.parseInt(config.getQuantity()));
+            log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}基础张多单",
+                    gridId, newEntryGridId, size);
 
-        newEntryGrid.getLongTraderParam().setQuantity(size);
-        placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
-                FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
-
+            newEntryGrid.getLongTraderParam().setQuantity(size);
+            placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
+                    FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
+        }else{
+            log.warn("[Gate] 多仓止损触发 gridId:{}, 目标gridId:{}已有挂单,跳过重复下单", gridId, newEntryGridId);
+        }
 
         int cancelGridId = gridId + 2;
         GridElement cancelGrid = GridElement.findById(cancelGridId);
@@ -1182,16 +1207,23 @@
             return;
         }
 
-        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+        // [Gate-BugFix] 防止与"仓位归零"重复下单:若该网格已有挂单则跳过
+        if (!newEntryGrid.isHasShortOrder()) {
+            BigDecimal triggerPrice = newEntryGrid.getGridPrice();
 
-        // 累计止损张数 + 当前止损量作为追单size,不再依赖positionSize(避免WS竞态)
-        accumulatedShortLossCount += Integer.parseInt(config.getQuantity());
-        String size = String.valueOf(accumulatedShortLossCount + Integer.parseInt(config.getQuantity()));
-        log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}基础张空单",
-                gridId, newEntryGridId, size);
-        newEntryGrid.getShortTraderParam().setQuantity(size);
-        placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
-                FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+            // 累计止损张数 + 当前止损量作为追单size,不再依赖positionSize(避免WS竞态)
+            accumulatedShortLossCount += Integer.parseInt(config.getQuantity());
+            String size = String.valueOf(accumulatedShortLossCount + Integer.parseInt(config.getQuantity()));
+            log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}基础张空单",
+                    gridId, newEntryGridId, size);
+            newEntryGrid.getShortTraderParam().setQuantity(size);
+            placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
+                    FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+        }else{
+            log.warn("[Gate] 空仓止损触发 gridId:{}, 目标gridId:{}已有挂单,跳过重复下单", gridId, newEntryGridId);
+        }
+
+
 
 
         int cancelGridId = gridId - 2;
@@ -1235,8 +1267,9 @@
             furthestSlId = gridId;
             interval = 2;
         }
-        log.info("[Gate] 多仓追挂止损, 当前最远止损gridId:{}, 追加{}张", furthestSlId, filledQty);
-        for (int i = 0; i < filledQty; i++) {
+        int stopLossCount = filledQty / Integer.parseInt(config.getQuantity());
+        log.info("[Gate] 多仓追挂止损, 当前最远止损gridId:{}, 成交{}张, 追加{}个止损单", furthestSlId, filledQty, stopLossCount);
+        for (int i = 0; i < stopLossCount; i++) {
             int newSlId = furthestSlId - i - interval;
             GridElement elem = GridElement.findById(newSlId);
             if (elem == null) {
@@ -1271,8 +1304,9 @@
             furthestSlId = gridId;
             interval = 2;
         }
-        log.info("[Gate] 空仓追挂止损, 当前最远止损gridId:{}, 追加{}张", furthestSlId, filledQty);
-        for (int i = 0; i < filledQty; i++) {
+        int stopLossCount = filledQty / Integer.parseInt(config.getQuantity());
+        log.info("[Gate] 空仓追挂止损, 当前最远止损gridId:{}, 成交{}张, 追加{}个止损单", furthestSlId, filledQty, stopLossCount);
+        for (int i = 0; i < stopLossCount; i++) {
             int newSlId = furthestSlId + i + interval;
             GridElement elem = GridElement.findById(newSlId);
             if (elem == null) {

--
Gitblit v1.9.1