From eb365b75c8388d1a30a6659f4692b802a3066097 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 30 Oct 2026 17:49:32 +0800
Subject: [PATCH] ``` fix(grid): 修复止损订单张数统计和追单时序竞态问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 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 94cab0d..7305099 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1104,7 +1104,7 @@
                 ORDER_TYPE_CLOSE_SHORT,
                 String.valueOf(qty),
                 profitId -> {
-                    slElem.addShortStopLossOrderId(profitId);
+                    slElem.addShortStopLossOrderId(profitId, qty);
                     GridElement.refreshIndices();
                     log.info("[Gate] 对向加仓空仓止损已挂, 开仓gridId:{}, 止损gridId:{}, 数量:{}张, stopLossId:{}",
                             oppositeGridId, stopLossGridId, qty, profitId);
@@ -1132,7 +1132,7 @@
                 ORDER_TYPE_CLOSE_LONG,
                 negate(String.valueOf(qty)),
                 profitId -> {
-                    slElem.addLongStopLossOrderId(profitId);
+                    slElem.addLongStopLossOrderId(profitId, qty);
                     GridElement.refreshIndices();
                     log.info("[Gate] 对向加仓多仓止损已挂, 开仓gridId:{}, 止损gridId:{}, 数量:{}张, stopLossId:{}",
                             oppositeGridId, stopLossGridId, qty, profitId);
@@ -1326,7 +1326,7 @@
                         ORDER_TYPE_CLOSE_LONG,
                         negate(baseQty),
                         profitId -> {
-                            longElem.addLongStopLossOrderId(profitId);
+                            longElem.addLongStopLossOrderId(profitId, Integer.parseInt(baseQty));
                             GridElement.refreshIndices();
                             log.info("[Gate] 自定义多仓止损已挂, 目标价:{}, 匹配gridId:{}, 触发价:{}, stopLossId:{}",
                                     longPrice, longElem.getId(), triggerPrice, profitId);
@@ -1347,7 +1347,7 @@
                         ORDER_TYPE_CLOSE_SHORT,
                         baseQty,
                         profitId -> {
-                            shortElem.addShortStopLossOrderId(profitId);
+                            shortElem.addShortStopLossOrderId(profitId, Integer.parseInt(baseQty));
                             GridElement.refreshIndices();
                             log.info("[Gate] 自定义空仓止损已挂, 目标价:{}, 匹配gridId:{}, 触发价:{}, stopLossId:{}",
                                     shortPrice, shortElem.getId(), triggerPrice, profitId);
@@ -1390,7 +1390,7 @@
                     ORDER_TYPE_CLOSE_SHORT,
                     size,
                     profitId -> {
-                        elem.addShortStopLossOrderId(profitId);
+                        elem.addShortStopLossOrderId(profitId, Integer.parseInt(size));
                         GridElement.refreshIndices();
                         log.info("[Gate] 空仓止损已挂, gridId:{}, 触发价:{}, stopLossId:{}", finalId, triggerPrice, profitId);
                     }
@@ -1417,7 +1417,7 @@
                     ORDER_TYPE_CLOSE_LONG,
                     negate(size),
                     profitId -> {
-                        elem.addLongStopLossOrderId(profitId);
+                        elem.addLongStopLossOrderId(profitId, Integer.parseInt(size));
                         GridElement.refreshIndices();
                         log.info("[Gate] 多仓止损已挂, gridId:{}, 触发价:{}, stopLossId:{}", finalId, triggerPrice, profitId);
                     }
@@ -2006,7 +2006,7 @@
      *
      * @param isLong                 {@code true}=多仓方向,{@code false}=空仓方向
      * @param respectCurrentPosition {@code true}=按「单边总持仓 ≤ maxPositionPerSide」扣减当前持仓;{@code false}=仅限制单次下单量
-     * @param stopLossSize           本次止损单张数(仅止损追单场景传入,用于修正 REST 时序竞态;其余场景传 0)
+     * @param stopLossSize           本次触发网格的全部止损单张数(仅止损追单场景传入,用于修正 REST 持仓时序竞态;其余场景传 0)
      * @return 实际下单张数(字符串)
      */
     private String calculateEntryQuantity(boolean isLong, boolean respectCurrentPosition, int stopLossSize) {
@@ -2021,7 +2021,7 @@
             if (respectCurrentPosition) {
                 // 止损追单:单边总持仓不得超过 maxPerSide,追单量 = maxPerSide - 当前持仓。
                 // 止损单刚成交、REST 查询可能仍返回止损前持仓(时序竞态),
-                // 减去本次止损单张数得到止损后的真实持仓,避免追单量被错误扣减。
+                // 减去本网格全部止损张数得到止损后的真实持仓,避免追单量被错误扣减。
                 int currentPos = isLong
                         ? queryPositionSize(Position.ModeEnum.DUAL_LONG)
                         : queryPositionSize(Position.ModeEnum.DUAL_SHORT);
@@ -2041,6 +2041,8 @@
     }
 
     private void handleLongStopLossTriggered(GridElement gridElement, String orderId) {
+        // 本网格全部止损单覆盖张数(含刚触发的这一单),用于追单时修正 REST 持仓的时序竞态
+        int gridStopLossSize = gridElement.getLongStopLossTotalSize();
         gridElement.removeLongStopLossOrderId(orderId);
 
         int gridId = gridElement.getId();
@@ -2073,7 +2075,7 @@
 
         // 止损追单:同一网格可有多笔挂单,不判断 isHasLongOrder,直接挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(true, true, Integer.parseInt(config.getQuantity()));
+        String size = calculateEntryQuantity(true, true, gridStopLossSize);
         log.info("[Gate] 多仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
                 getEffectiveStopLossCount(true), getEffectiveAddCount(true), config.getAddPositionStopThreshold(),
                 config.getBaseQuantity(), size, config.getStopLossCountMode());
@@ -2109,6 +2111,8 @@
     }
 
     private void handleShortStopLossTriggered(GridElement gridElement, String orderId) {
+        // 本网格全部止损单覆盖张数(含刚触发的这一单),用于追单时修正 REST 持仓的时序竞态
+        int gridStopLossSize = gridElement.getShortStopLossTotalSize();
         gridElement.removeShortStopLossOrderId(orderId);
 
         int gridId = gridElement.getId();
@@ -2139,7 +2143,7 @@
 
         // 止损追单:同一网格可有多笔挂单,不判断 isHasShortOrder,直接挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(false, true, Integer.parseInt(config.getQuantity()));
+        String size = calculateEntryQuantity(false, true, gridStopLossSize);
         log.info("[Gate] 空仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
                 getEffectiveStopLossCount(false), getEffectiveAddCount(false), config.getAddPositionStopThreshold(),
                 config.getBaseQuantity(), size, config.getStopLossCountMode());
@@ -2623,9 +2627,9 @@
                     triggerPrice, rule, orderType, size,
                     profitId -> {
                         if (isLong) {
-                            elem.addLongStopLossOrderId(profitId);
+                            elem.addLongStopLossOrderId(profitId, Integer.parseInt(qtyStr));
                         } else {
-                            elem.addShortStopLossOrderId(profitId);
+                            elem.addShortStopLossOrderId(profitId, Integer.parseInt(qtyStr));
                         }
                         GridElement.refreshIndices();
                         log.info("[Gate] {}止损追加, gridId:{}, 触发价:{}, 第{}单, stopLossId:{}",

--
Gitblit v1.9.1