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

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java                        |   30 ++++++++++++++++++++----------
 src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java |    9 +++++++++
 2 files changed, 29 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:{}, 挂{}张空单",
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
index ae14239..3bd9310 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
@@ -35,6 +35,8 @@
     private final GateGridTradeService gridTradeService;
 
     private volatile boolean subscribed = false;
+    /** 已处理的最新烛线起始时间戳(秒),用于跳过重连后历史快照的逐条重放 */
+    private volatile long lastCandleT = 0;
 
     /**
       * @param contract         合约名称(如 ETH_USDT)
@@ -140,6 +142,13 @@
                 }
             }
             if (data == null) { log.warn("[{}] 烛线无有效条目", CHANNEL_NAME); return true; }
+            // 跨消息单调过滤:重连后快照按历史顺序逐条推送(每条消息一根),
+            // t 早于已处理最新烛线的历史数据直接跳过,避免重放旧价格驱动策略(历史出现过 950 条风暴 + 持仓翻倍)
+            if (maxT < lastCandleT) {
+                log.debug("[{}] 跳过历史烛线 t:{} (已处理最新 t:{})", CHANNEL_NAME, maxT, lastCandleT);
+                return true;
+            }
+            lastCandleT = maxT;
             BigDecimal closePx = new BigDecimal(data.getString("c"));
 
             if (gridTradeService != null) {

--
Gitblit v1.9.1