From b2f38ec743094370dbf308439dc1ec6f8b374b2f Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 23 Jun 2026 22:54:34 +0800
Subject: [PATCH] fix(gateApi): 修复网格交易跨度重启逻辑

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 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 b473114..1ea2c85 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -651,6 +651,9 @@
                     int shortExcessCount = shortExcess.divide(shortGridQty, 0, RoundingMode.DOWN).intValue();
                     for (int i = 0; i < shortExcessCount; i++) {
                         int tpGridId = shortGridElement.getId() - 2 - i;
+                        if (i > 0){
+                            tpGridId = tpGridId - 1;
+                        }
                         GridElement tpElem = GridElement.findById(tpGridId);
                         if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) {
                             continue;
@@ -693,6 +696,10 @@
                     int longExcessCount = longExcess.divide(longGridQty, 0, RoundingMode.DOWN).intValue();
                     for (int i = 0; i < longExcessCount; i++) {
                         int tpGridId = longGridElement.getId() + 2 + i;
+                        if (i > 0){
+                            tpGridId = tpGridId + 1;
+                        }
+
                         GridElement tpElem = GridElement.findById(tpGridId);
                         if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) {
                             continue;
@@ -1331,6 +1338,14 @@
         if (span <= 0) {
             return;
         }
+
+        // 检查是否还有剩余止盈单,只有多空止盈全部清空才继续
+        if (GridElement.getLongTakeProfitCount() > 0 || GridElement.getShortTakeProfitCount() > 0) {
+            log.info("[Gate] 尚有未触发止盈单, 暂不检查跨度重启 longTpCount:{}, shortTpCount:{}",
+                    GridElement.getLongTakeProfitCount(), GridElement.getShortTakeProfitCount());
+            return;
+        }
+
         BigDecimal step = config.getStep();
         if (step == null || step.compareTo(BigDecimal.ZERO) == 0) {
             return;
@@ -1356,11 +1371,11 @@
         String reason = "";
 
         if (hasLong && hasShort) {
-            // 多空双边持仓:多均价 − 空均价 > span × step
-            BigDecimal gap = longAvgPrice.subtract(shortAvgPrice);
+            // 多空双边持仓:|多均价 − 空均价| > span × step
+            BigDecimal gap = shortAvgPrice.subtract(longAvgPrice);
             if (gap.compareTo(threshold) >= 0) {
                 shouldRestart = true;
-                reason = StrUtil.format("双边跨度 多均价:{} − 空均价:{} = {} > {} (span:{}×step:{})",
+                reason = StrUtil.format("双边跨度 |多均价:{} − 空均价:{}| = {} >= {} (span:{}×step:{})",
                         longAvgPrice, shortAvgPrice, gap, threshold, span, step);
             }
         } else if (hasLong) {

--
Gitblit v1.9.1