From e6e102b429677e5569338dad2c7d6a433049780c Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 20 Jul 2026 11:24:46 +0800
Subject: [PATCH] fix(gateApi): 解决网格交易止损追单并发问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java b/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
index 6ffbefd..c5ef00f 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
@@ -92,6 +92,21 @@
     /** 空仓止损订单 ID 列表(支持一个网格多个止损单) */
     private List<String> shortStopLossOrderIds = new ArrayList<>();
 
+    /**
+     * 止损追单是否正在进行中(防重入标记)。
+     * 同一网格存在多个入场单且相近时间成交时,确保只有第一次 extend 触发止损挂单,
+     * 后续入场单成交仅移除订单记录,不再重复触发 cancelAll + extend 流程。
+     * 异步回调全部完成后由 placeStopLossOrders 中计数器归零时重置。
+     */
+    private volatile boolean extendStopLossInProgress = false;
+
+    /**
+     * 是否有待重挂的止损追单请求。
+     * 当入场单成交因 extendStopLossInProgress=true 被跳过时置为 true,
+     * 延展完成后由回调检查此标记并触发一次仓位重查+重挂,确保止损覆盖最新持仓数。
+     */
+    private volatile boolean pendingStopLossReExtend = false;
+
     /** 索引重建锁,保证 refreshIndices() 与计数读取之间互斥,避免 clear→rebuild 窗口期读到 0 */
     private static final Object INDEX_LOCK = new Object();
 
@@ -507,6 +522,17 @@
     /** @return 是否有止损单 */
     public boolean hasShortStopLossOrders() { return !shortStopLossOrderIds.isEmpty(); }
 
+    // ==================== 止损追单防重入标记 ====================
+
+    /** @return 止损追单是否正在进行中 */
+    public boolean isExtendStopLossInProgress() { return extendStopLossInProgress; }
+    /** 设置止损追单进行标记 */
+    public void setExtendStopLossInProgress(boolean v) { this.extendStopLossInProgress = v; }
+    /** @return 是否有待重挂请求 */
+    public boolean isPendingStopLossReExtend() { return pendingStopLossReExtend; }
+    /** 设置待重挂标记 */
+    public void setPendingStopLossReExtend(boolean v) { this.pendingStopLossReExtend = v; }
+
     public static Builder builder() {
         return new Builder();
     }

--
Gitblit v1.9.1