From a6cfe49c18edeef351e61bbef46ca7facc01c94a Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 23 Jun 2026 11:18:32 +0800
Subject: [PATCH] fix(gateApi): 修复网格交易止盈订单ID计算逻辑

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   13 ++++++++++---
 1 files changed, 10 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..7f6533d 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;
@@ -1356,11 +1363,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