From e7999612738b3b5a0bc142d9c10d93586c5b96a2 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 06 Nov 2026 11:27:04 +0800
Subject: [PATCH] fix(gate): 修复网格交易重复建仓导致持仓翻倍问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 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 51e112a..d36dbaa 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -2323,10 +2323,6 @@
 
                     if (newEntryGrid != null) {
 
-//                        String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue()) + Integer.parseInt(config.getQuantity()));
-//                        String quantity = String.valueOf(config.getBaseQuantity());
-
-
                         String quantity = calculateEntryQuantity(true, false, 0);
                         // 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单)
                         boolean hasLongOrderBelow = false;
@@ -2334,7 +2330,7 @@
                         while (checkDownCursor != null) {
                             if (checkDownCursor.isHasLongOrder()) {
                                 hasLongOrderBelow = true;
-                                log.info("[Gate] 多仓仓位归零 gridId:{}, 下方gridId:{}已有多单, 跳过本次挂单",
+                                log.debug("[Gate] 多仓仓位归零 gridId:{}, 下方gridId:{}已有多单, 跳过本次挂单",
                                         newEntryGrid.getId(), checkDownCursor.getId());
                                 break;
                             }
@@ -2359,6 +2355,15 @@
                         }
 //                        log.info("[Gate-2] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
                         if (!newEntryGrid.isHasLongOrder() && !hasLongOrderBelow) {
+                            // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
+                            // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍(历史出现过 68→136 张)
+                            int realPos = queryPositionSize(Position.ModeEnum.DUAL_LONG);
+                            if (realPos > 0) {
+                                longActive = true;
+                                longPositionSize = BigDecimal.valueOf(realPos);
+                                log.warn("[Gate] 多仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
+                                return;
+                            }
                             BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                             String size = quantity;
                             log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}张多单",
@@ -2400,10 +2405,6 @@
 
                     if (newEntryGrid != null) {
 
-//                        String quantity = String.valueOf((accumulatedShortLossCount + 1) * Integer.parseInt(config.getQuantity()));
-//                        String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()) + Integer.parseInt(config.getQuantity()));
-//                        String quantity = String.valueOf(config.getBaseQuantity());
-
                         String quantity = calculateEntryQuantity(false, false, 0);
                         // 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单)
                         boolean hasShortOrderAbove = false;
@@ -2411,7 +2412,7 @@
                         while (checkUpCursor != null) {
                             if (checkUpCursor.isHasShortOrder()) {
                                 hasShortOrderAbove = true;
-                                log.info("[Gate] 空仓仓位归零 gridId:{}, 上方gridId:{}已有空单, 跳过本次挂单",
+                                log.debug("[Gate] 空仓仓位归零 gridId:{}, 上方gridId:{}已有空单, 跳过本次挂单",
                                         newEntryGrid.getId(), checkUpCursor.getId());
                                 break;
                             }
@@ -2436,6 +2437,15 @@
                         }
 //                        log.info("[Gate-4] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
                         if (!newEntryGrid.isHasShortOrder() && !hasShortOrderAbove){
+                            // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
+                            // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍
+                            int realPos = queryPositionSize(Position.ModeEnum.DUAL_SHORT);
+                            if (realPos > 0) {
+                                shortActive = true;
+                                shortPositionSize = BigDecimal.valueOf(realPos);
+                                log.warn("[Gate] 空仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
+                                return;
+                            }
                             BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                             String size = quantity;
                             log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}张空单",

--
Gitblit v1.9.1