From 1232b58afcf26544b50db0014c122fc6c1cd4d2e Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 23 Nov 2026 10:17:12 +0800
Subject: [PATCH] fix(gateApi): 忽略Gate配置反序列化未知属性

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 53 insertions(+), 2 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 5490fca..6d8228f 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -189,6 +189,14 @@
     private final Map<String, Integer> reduceReplenishLongOrderMap = new ConcurrentHashMap<>();
     /** 原始仓位减仓:空头补仓单 orderId → 张数(补仓单挂在多头第一止损位) */
     private final Map<String, Integer> reduceReplenishShortOrderMap = new ConcurrentHashMap<>();
+    /** 多头止损事件待处理标记:仓位已归零、autoorders finished 尚未到达时置位,阻止价格驱动抢先挂单 */
+    private volatile boolean longStopLossPending = false;
+    /** 空头止损事件待处理标记:同上 */
+    private volatile boolean shortStopLossPending = false;
+    /** 多头止损待处理置位时间戳(0=未置位),用于兜底超时复位 */
+    private volatile long longStopLossPendingSince = 0L;
+    /** 空头止损待处理置位时间戳(0=未置位),用于兜底超时复位 */
+    private volatile long shortStopLossPendingSince = 0L;
 
     private volatile BigDecimal lastKlinePrice;
     private volatile BigDecimal markPrice = BigDecimal.ZERO;
@@ -459,6 +467,10 @@
         tpReplenishShortOrderMap.clear();
         currentLongChaseQty = 0;
         currentShortChaseQty = 0;
+        longStopLossPending = false;
+        shortStopLossPending = false;
+        longStopLossPendingSince = 0L;
+        shortStopLossPendingSince = 0L;
         reduceReplenishLongOrderMap.clear();
         reduceReplenishShortOrderMap.clear();
         shortPriceQueue.clear();
@@ -578,10 +590,14 @@
             executor.submitMaintenanceTask(this::syncPositionsFromExchange);
         }
 
+        // 止损事件兜底:置位超时仍未收到 finished(事件丢失且对账补触发未生效),解除拦截让价格驱动接管
+        releaseExpiredStopLossPending();
+
         if (state == StrategyState.ACTIVE &&
                 config.isPriceDriveEnabled() &&
                 longActive == false &&
-                    longPositionSize.compareTo(BigDecimal.ZERO) == 0){
+                    longPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
+                    !longStopLossPending){
             processLongGrid(closePrice);
         }
 
@@ -589,8 +605,31 @@
         if (state == StrategyState.ACTIVE &&
                 config.isPriceDriveEnabled() &&
                 shortActive == false &&
-                        shortPositionSize.compareTo(BigDecimal.ZERO) == 0){
+                        shortPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
+                        !shortStopLossPending){
             processShortGrid(closePrice);
+        }
+    }
+
+    /** 止损事件兜底超时:置位超过该时长仍未收到 autoorders finished,自动解除价格驱动拦截 */
+    private static final long STOP_LOSS_PENDING_TIMEOUT_MS = 30_000L;
+
+    /**
+     * 止损事件兜底:pending 置位超过 {@link #STOP_LOSS_PENDING_TIMEOUT_MS} 仍无 autoorders finished,
+     * 说明止损事件已丢失且对账补触发未生效(如无 trade_id 的幽灵单),主动解除拦截,
+     * 让价格驱动接管重挂,避免永久停摆。每次 onKline 进入价格驱动判定前调用。
+     */
+    private void releaseExpiredStopLossPending() {
+        long now = System.currentTimeMillis();
+        if (longStopLossPending && now - longStopLossPendingSince >= STOP_LOSS_PENDING_TIMEOUT_MS) {
+            longStopLossPending = false;
+            longStopLossPendingSince = 0L;
+            log.warn("[Gate] 多头止损事件{}秒未达, 兜底解除价格驱动拦截", STOP_LOSS_PENDING_TIMEOUT_MS / 1000);
+        }
+        if (shortStopLossPending && now - shortStopLossPendingSince >= STOP_LOSS_PENDING_TIMEOUT_MS) {
+            shortStopLossPending = false;
+            shortStopLossPendingSince = 0L;
+            log.warn("[Gate] 空头止损事件{}秒未达, 兜底解除价格驱动拦截", STOP_LOSS_PENDING_TIMEOUT_MS / 1000);
         }
     }
 
@@ -737,24 +776,32 @@
                     longActive = true;
                     longPositionSize = size;
                     longEntryPrice = entryPrice;
+                    longStopLossPending = false;
+                    longStopLossPendingSince = 0L;
                 } else {
 
                     log.info("[Gate-0]多仓: {}", longBaseEntryPrice);
                     longActive = false;
                     longPositionSize = BigDecimal.ZERO;
                     longEntryPrice = BigDecimal.ZERO;
+                    longStopLossPendingSince = System.currentTimeMillis();
+                    longStopLossPending = true;
                 }
             } else if (Position.ModeEnum.DUAL_SHORT == mode) {
                 if (hasPosition) {
                     shortActive = true;
                     shortPositionSize = size.abs();
                     shortEntryPrice = entryPrice;
+                    shortStopLossPending = false;
+                    shortStopLossPendingSince = 0L;
                 } else {
 
                     log.info("[Gate-0]空仓: {}", shortBaseEntryPrice);
                     shortActive = false;
                     shortPositionSize = BigDecimal.ZERO;
                     shortEntryPrice = BigDecimal.ZERO;
+                    shortStopLossPendingSince = System.currentTimeMillis();
+                    shortStopLossPending = true;
                 }
             }
         }
@@ -3066,6 +3113,8 @@
         }
 
         lastLongStopLossGridId = gridId;
+        longStopLossPending = false;
+        longStopLossPendingSince = 0L;
 
         accumulatedLongLossCount++;
         log.info("[Gate] 多仓止损触发 gridId:{}, 止损次数:{}{}, 开始追单",
@@ -3151,6 +3200,8 @@
             return;
         }
         lastShortStopLossGridId = gridId;
+        shortStopLossPending = false;
+        shortStopLossPendingSince = 0L;
         accumulatedShortLossCount++;
         log.info("[Gate] 空仓止损触发 gridId:{}, 止损次数:{}{}, 开始追单",
                 gridId, accumulatedShortLossCount, sameGrid ? "(同网格)" : "");

--
Gitblit v1.9.1