From ec9954eda0e8fe210beaa3b60f49eb9abdd021c4 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 23 Nov 2026 10:24:57 +0800
Subject: [PATCH] feat(config): 添加原始仓位减仓递减步长配置

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java         |  261 +++++++++++++++++++++++++++++++++++++++++++++++----
 src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java                |    5 +
 src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java |    1 
 src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java                   |    9 +
 4 files changed, 254 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
index a7230dd..9c9162c 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -146,6 +146,8 @@
     private final boolean tpPositionReplenishEnabled;
     /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,余数挂最后一格,每格至少1张。默认 0 */
     private final int baseTakeProfitTotalQty;
+    /** 原始仓位减仓递减步长:0=关闭,>0=每次止损后追单量递减的步长,默认 0 */
+    private final int originalPositionReduce;
     /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */
     private BigDecimal step;
     /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */
@@ -195,6 +197,7 @@
         this.tpTriggerOppositeAddQuantity = builder.tpTriggerOppositeAddQuantity;
         this.tpPositionReplenishEnabled = builder.tpPositionReplenishEnabled;
         this.baseTakeProfitTotalQty = builder.baseTakeProfitTotalQty;
+        this.originalPositionReduce = builder.originalPositionReduce;
     }
 
     // ==================== REST/WS 地址 ====================
@@ -318,6 +321,8 @@
     public boolean isTpPositionReplenishEnabled() { return tpPositionReplenishEnabled; }
     /** @return 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格 */
     public int getBaseTakeProfitTotalQty() { return baseTakeProfitTotalQty; }
+    /** @return 原始仓位减仓递减步长:0=关闭,>0=每次止损后追单量递减的步长 */
+    public int getOriginalPositionReduce() { return originalPositionReduce; }
 
     // ==================== 运行时参数 ====================
 
@@ -446,6 +451,8 @@
         private boolean tpPositionReplenishEnabled = false;
         /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,默认 0 */
         private int baseTakeProfitTotalQty = 0;
+        /** 原始仓位减仓递减步长:0=关闭,>0=每次止损后追单量递减的步长,默认 0 */
+        private int originalPositionReduce = 0;
 
         /** 设置 API Key */
         public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; }
@@ -525,6 +532,8 @@
         public Builder tpPositionReplenishEnabled(boolean tpPositionReplenishEnabled) { this.tpPositionReplenishEnabled = tpPositionReplenishEnabled; return this; }
         /** 设置原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格 */
         public Builder baseTakeProfitTotalQty(int baseTakeProfitTotalQty) { this.baseTakeProfitTotalQty = baseTakeProfitTotalQty; return this; }
+        /** 设置原始仓位减仓递减步长:0=关闭,>0=每次止损后追单量递减的步长 */
+        public Builder originalPositionReduce(int originalPositionReduce) { this.originalPositionReduce = originalPositionReduce; return this; }
 
         public GateConfig build() {
             return new GateConfig(this);
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
index ba67910..b89d608 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
@@ -87,6 +87,8 @@
     private Boolean tpPositionReplenishEnabled;
     /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,余数挂最后一格,每格至少1张,0=不启用 */
     private Integer baseTakeProfitTotalQty;
+    /** 原始仓位减仓递减步长:0=关闭,>0=每次止损后追单量递减的步长,默认 0 */
+    private Integer originalPositionReduce;
 
     /**
      * 从 GateConfig 构建 DTO(不暴露 apiSecret)。
@@ -128,6 +130,7 @@
                 .tpTriggerOppositeAddQuantity(config.getTpTriggerOppositeAddQuantity())
                 .tpPositionReplenishEnabled(config.isTpPositionReplenishEnabled())
                 .baseTakeProfitTotalQty(config.getBaseTakeProfitTotalQty())
+                .originalPositionReduce(config.getOriginalPositionReduce())
                 .build();
     }
 
@@ -163,6 +166,7 @@
         m.put("tpTriggerOppositeAddQuantity", tpTriggerOppositeAddQuantity);
         m.put("tpPositionReplenishEnabled", tpPositionReplenishEnabled);
         m.put("baseTakeProfitTotalQty", baseTakeProfitTotalQty);
+        m.put("originalPositionReduce", originalPositionReduce);
         return m;
     }
 
@@ -205,6 +209,7 @@
                 .tpTriggerOppositeAddQuantity(0)
                 .tpPositionReplenishEnabled(false)
                 .baseTakeProfitTotalQty(0)
+                .originalPositionReduce(0)
                 .build();
     }
 }
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
index 597f3d6..5d37c30 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
@@ -120,6 +120,7 @@
                 .tpTriggerOppositeAddQuantity(dto.getTpTriggerOppositeAddQuantity() != null && dto.getTpTriggerOppositeAddQuantity() > 0 ? dto.getTpTriggerOppositeAddQuantity() : 0)
                 .tpPositionReplenishEnabled(nvl(dto.getTpPositionReplenishEnabled(), false))
                 .baseTakeProfitTotalQty(dto.getBaseTakeProfitTotalQty() != null && dto.getBaseTakeProfitTotalQty() > 0 ? dto.getBaseTakeProfitTotalQty() : 0)
+                .originalPositionReduce(dto.getOriginalPositionReduce() != null && dto.getOriginalPositionReduce() > 0 ? dto.getOriginalPositionReduce() : 0)
                 .build();
     }
 
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 4ead873..c5f7f64 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -181,6 +181,18 @@
     private final Map<String, Integer> tpReplenishLongOrderMap = new ConcurrentHashMap<>();
     /** 仓位止盈补仓:空仓止盈→空仓开仓单ID → 张数(成交后按原空仓止损位重挂全持仓止损) */
     private final Map<String, Integer> tpReplenishShortOrderMap = new ConcurrentHashMap<>();
+    /** 原始仓位减仓:本轮多头是否已发生首次止损(startGrid 重置) */
+    private volatile boolean longHasStopped = false;
+    /** 原始仓位减仓:本轮空头是否已发生首次止损(startGrid 重置) */
+    private volatile boolean shortHasStopped = false;
+    /** 原始仓位减仓:当前多头递减追单数量(价格驱动移动时保持) */
+    private volatile int currentLongChaseQty = 0;
+    /** 原始仓位减仓:当前空头递减追单数量(价格驱动移动时保持) */
+    private volatile int currentShortChaseQty = 0;
+    /** 原始仓位减仓:多头补仓单 orderId → 张数(补仓单挂在空头第一止损位) */
+    private final Map<String, Integer> reduceReplenishLongOrderMap = new ConcurrentHashMap<>();
+    /** 原始仓位减仓:空头补仓单 orderId → 张数(补仓单挂在多头第一止损位) */
+    private final Map<String, Integer> reduceReplenishShortOrderMap = new ConcurrentHashMap<>();
 
     private volatile BigDecimal lastKlinePrice;
     private volatile BigDecimal markPrice = BigDecimal.ZERO;
@@ -449,6 +461,12 @@
         dualStopLossModeActive.set(false);
         tpReplenishLongOrderMap.clear();
         tpReplenishShortOrderMap.clear();
+        longHasStopped = false;
+        shortHasStopped = false;
+        currentLongChaseQty = 0;
+        currentShortChaseQty = 0;
+        reduceReplenishLongOrderMap.clear();
+        reduceReplenishShortOrderMap.clear();
         shortPriceQueue.clear();
         longPriceQueue.clear();
         totalShortPriceQueue.clear();
@@ -864,6 +882,12 @@
                     handleReplenishShortAddFilled(shortGridElement, orderId, replenishQty);
                     return;
                 }
+                // 原始仓位减仓:空头补仓单成交 → 在空头现有止损位追加该数量止损
+                Integer reduceReplenishQty = reduceReplenishShortOrderMap.remove(orderId);
+                if (reduceReplenishQty != null) {
+                    handleReduceReplenishShortFilled(shortGridElement, orderId, reduceReplenishQty);
+                    return;
+                }
                 // 加仓执行计数:含加仓量的开仓单真正成交时才 +1(挂单后被取消的不计入)
                 if (pendingAddEntryOrderIds.remove(orderId)) {
                     accumulatedShortAddCount++;
@@ -904,6 +928,10 @@
                 }
                 // [Gate] 基础仓位止盈挂单:与超额止盈并行,独立于止盈模式
                 placeBaseTakeProfit(posSize, false, shortGridElement.getId());
+                // 原始仓位减仓:追单成交后,在多头第一止损位挂空头补仓单(数量 = 原始仓位 - 当前仓位)
+                if (config.getOriginalPositionReduce() > 0) {
+                    placeReduceReplenish(false, posSize);
+                }
                 log.info("[Gate] 空单成交 {}张, 当前持仓:{}张", filledQty, posSize);
 
 
@@ -924,6 +952,12 @@
                 Integer replenishQty = tpReplenishLongOrderMap.remove(orderId);
                 if (replenishQty != null) {
                     handleReplenishLongAddFilled(longGridElement, orderId, replenishQty);
+                    return;
+                }
+                // 原始仓位减仓:多头补仓单成交 → 在多头现有止损位追加该数量止损
+                Integer reduceReplenishQty = reduceReplenishLongOrderMap.remove(orderId);
+                if (reduceReplenishQty != null) {
+                    handleReduceReplenishLongFilled(longGridElement, orderId, reduceReplenishQty);
                     return;
                 }
                 // 加仓执行计数:含加仓量的开仓单真正成交时才 +1(挂单后被取消的不计入)
@@ -966,6 +1000,10 @@
                 }
                 // [Gate] 基础仓位止盈挂单:与超额止盈并行,独立于止盈模式
                 placeBaseTakeProfit(posSize, true, longGridElement.getId());
+                // 原始仓位减仓:追单成交后,在空头第一止损位挂多头补仓单(数量 = 原始仓位 - 当前仓位)
+                if (config.getOriginalPositionReduce() > 0) {
+                    placeReduceReplenish(true, posSize);
+                }
                 log.info("[Gate] 多单成交 {}张, 当前持仓:{}张", filledQty, posSize);
 
             }
@@ -2558,12 +2596,18 @@
 
         // 阶段3:挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(true, false, 0);
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            size = String.valueOf(currentLongChaseQty > 0 ? currentLongChaseQty : Integer.parseInt(config.getBaseQuantity()));
+            hasAdd = false;
+        } else {
+            size = calculateEntryQuantity(true, false, 0);
+            hasAdd = computeAddQty(true) > 0;
+        }
         log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}张多单",
                 newEntryGrid.getId(), size);
         newEntryGrid.getLongTraderParam().setQuantity(size);
-        // 含加仓量的单打标记:真正成交后才计入加仓执行次数
-        boolean hasAdd = computeAddQty(true) > 0;
         placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
                 FuturesPriceTrigger.RuleEnum.NUMBER_1, size,
                 hasAdd ? pendingAddEntryOrderIds::add : null);
@@ -2589,13 +2633,20 @@
             return;
         }
         BigDecimal triggerPrice = entryGrid.getGridPrice();
-        String size = calculateEntryQuantity(true, false, 0);
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            size = String.valueOf(currentLongChaseQty > 0 ? currentLongChaseQty : Integer.parseInt(config.getBaseQuantity()));
+            hasAdd = false;
+        } else {
+            size = calculateEntryQuantity(true, false, 0);
+            hasAdd = computeAddQty(true) > 0;
+        }
         if (Integer.parseInt(size) <= 0) {
             log.warn("[Gate] 接管挂单: 已达单边上限({}), 跳过", config.getMaxPositionPerSide());
             return;
         }
         entryGrid.getLongTraderParam().setQuantity(size);
-        boolean hasAdd = computeAddQty(true) > 0;
         log.info("[Gate] 价格驱动接管: 多仓挂单 gridId:{}, {}张", entryGrid.getId(), size);
         placeEntryOrderWithPreFlag(entryGrid, true, triggerPrice,
                 FuturesPriceTrigger.RuleEnum.NUMBER_1, size,
@@ -2622,13 +2673,20 @@
             return;
         }
         BigDecimal triggerPrice = entryGrid.getGridPrice();
-        String size = calculateEntryQuantity(false, false, 0);
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            size = String.valueOf(currentShortChaseQty > 0 ? currentShortChaseQty : Integer.parseInt(config.getBaseQuantity()));
+            hasAdd = false;
+        } else {
+            size = calculateEntryQuantity(false, false, 0);
+            hasAdd = computeAddQty(false) > 0;
+        }
         if (Integer.parseInt(size) <= 0) {
             log.warn("[Gate] 接管挂单: 已达单边上限({}), 跳过", config.getMaxPositionPerSide());
             return;
         }
         entryGrid.getShortTraderParam().setQuantity(size);
-        boolean hasAdd = computeAddQty(false) > 0;
         log.info("[Gate] 价格驱动接管: 空仓挂单 gridId:{}, {}张", entryGrid.getId(), size);
         placeEntryOrderWithPreFlag(entryGrid, false, triggerPrice,
                 FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size),
@@ -2735,12 +2793,18 @@
 
         // 阶段3:挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(false, false, 0);
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            size = String.valueOf(currentShortChaseQty > 0 ? currentShortChaseQty : Integer.parseInt(config.getBaseQuantity()));
+            hasAdd = false;
+        } else {
+            size = calculateEntryQuantity(false, false, 0);
+            hasAdd = computeAddQty(false) > 0;
+        }
         log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}张空单",
                 newEntryGrid.getId(), size);
         newEntryGrid.getShortTraderParam().setQuantity(size);
-        // 含加仓量的单打标记:真正成交后才计入加仓执行次数
-        boolean hasAdd = computeAddQty(false) > 0;
         placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
                 FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size),
                 hasAdd ? pendingAddEntryOrderIds::add : null);
@@ -2876,6 +2940,136 @@
         return String.valueOf(totalQty);
     }
 
+    // ========== 原始仓位减仓:递减追单 + 对手盘补仓 ==========
+
+    /**
+     * 计算原始仓位减仓模式下的递减追单数量。
+     * 本轮首次止损 → 追单 = 原始仓位;之后 → 追单 = 本次被平数量 - 步长;
+     * 若结果 < 步长 → 重置为原始仓位(最大仓位),形成循环。
+     */
+    private int computeReducedChaseQty(boolean isLong, int stoppedQty) {
+        int baseQty = Integer.parseInt(config.getBaseQuantity());
+        int reduceStep = config.getOriginalPositionReduce();
+        if (isLong) {
+            if (!longHasStopped) {
+                longHasStopped = true;
+                return baseQty;
+            }
+        } else {
+            if (!shortHasStopped) {
+                shortHasStopped = true;
+                return baseQty;
+            }
+        }
+        int chase = stoppedQty - reduceStep;
+        if (chase < reduceStep) {
+            chase = baseQty;
+        }
+        return chase;
+    }
+
+    /**
+     * 原始仓位减仓:追单成交后,在对手盘第一止损位挂同方向补仓单。
+     * 补仓数量 = 原始仓位 - 当前仓位;补仓为条件单,成交后才追加止损。
+     */
+    private void placeReduceReplenish(boolean isLong, int currentPos) {
+        int baseQty = Integer.parseInt(config.getBaseQuantity());
+        int replenishQty = baseQty - currentPos;
+        // 追单成交后重算:先撤销本方向旧补仓单,再按新数量重挂
+        cancelReduceReplenish(isLong);
+        if (replenishQty <= 0) {
+            return;
+        }
+        GridElement opponentSl = isLong ? findFirstShortStopLossGrid() : findFirstLongStopLossGrid();
+        if (opponentSl == null) {
+            log.warn("[Gate] 原始仓位减仓: 未找到{}第一止损位, 跳过补仓", isLong ? "空头" : "多头");
+            return;
+        }
+        BigDecimal triggerPrice = opponentSl.getGridPrice();
+        FuturesPriceTrigger.RuleEnum rule = isLong
+                ? FuturesPriceTrigger.RuleEnum.NUMBER_1
+                : FuturesPriceTrigger.RuleEnum.NUMBER_2;
+        String size = isLong ? String.valueOf(replenishQty) : negate(String.valueOf(replenishQty));
+        executor.placeConditionalEntryOrder(triggerPrice, rule, size,
+                orderId -> {
+                    if (isLong) {
+                        reduceReplenishLongOrderMap.put(orderId, replenishQty);
+                        longEntryTraderIdParam(opponentSl, orderId, true);
+                    } else {
+                        reduceReplenishShortOrderMap.put(orderId, replenishQty);
+                        shortEntryTraderIdParam(opponentSl, orderId, true);
+                    }
+                    log.info("[Gate] 原始仓位减仓: {}补仓{}张已挂, 对手盘止损位 gridId:{}, orderId:{}",
+                            isLong ? "多头" : "空头", replenishQty, opponentSl.getId(), orderId);
+                },
+                () -> {
+                    if (isLong) {
+                        if (!opponentSl.hasLongOrderIds()) {
+                            opponentSl.setHasLongOrder(false);
+                        }
+                    } else {
+                        if (!opponentSl.hasShortOrderIds()) {
+                            opponentSl.setHasShortOrder(false);
+                        }
+                    }
+                    GridElement.refreshIndices();
+                    log.warn("[Gate] 原始仓位减仓: {}补仓挂单失败, 对手盘止损位 gridId:{}",
+                            isLong ? "多头" : "空头", opponentSl.getId());
+                }
+        );
+    }
+
+    /** 撤销本方向已挂未成交的补仓单。 */
+    private void cancelReduceReplenish(boolean isLong) {
+        Map<String, Integer> map = isLong ? reduceReplenishLongOrderMap : reduceReplenishShortOrderMap;
+        if (map.isEmpty()) {
+            return;
+        }
+        List<String> orderIds = new ArrayList<>(map.keySet());
+        map.clear();
+        for (String oid : orderIds) {
+            GridElement elem = isLong ? GridElement.findByLongOrderId(oid) : GridElement.findByShortOrderId(oid);
+            if (elem != null) {
+                if (isLong) {
+                    elem.removeLongOrderId(oid);
+                    elem.setHasLongOrder(elem.hasLongOrderIds());
+                } else {
+                    elem.removeShortOrderId(oid);
+                    elem.setHasShortOrder(elem.hasShortOrderIds());
+                }
+            }
+            executor.cancelConditionalOrder(oid, o -> {});
+        }
+        GridElement.refreshIndices();
+        log.info("[Gate] 原始仓位减仓: 已撤销{}补仓单{}张", isLong ? "多头" : "空头", orderIds.size());
+    }
+
+    /** 原始仓位减仓:多头补仓单成交后,在多头现有止损位追加该数量止损(不撤旧止损)。 */
+    private void handleReduceReplenishLongFilled(GridElement slElem, String orderId, int replenishQty) {
+        longEntryTraderIdParam(slElem, orderId, false);
+        GridElement longSl = findFirstLongStopLossGrid();
+        if (longSl == null) {
+            log.warn("[Gate] 原始仓位减仓: 多头补仓成交但未找到多头止损位, 跳过追加止损");
+            return;
+        }
+        placeStopLossOrders(longSl.getId(), 1, String.valueOf(replenishQty),
+                FuturesPriceTrigger.RuleEnum.NUMBER_2, ORDER_TYPE_CLOSE_LONG, true, null, null);
+        log.info("[Gate] 原始仓位减仓: 多头补仓{}张成交, 已在多头止损位 gridId:{} 追加止损", replenishQty, longSl.getId());
+    }
+
+    /** 原始仓位减仓:空头补仓单成交后,在空头现有止损位追加该数量止损(不撤旧止损)。 */
+    private void handleReduceReplenishShortFilled(GridElement slElem, String orderId, int replenishQty) {
+        shortEntryTraderIdParam(slElem, orderId, false);
+        GridElement shortSl = findFirstShortStopLossGrid();
+        if (shortSl == null) {
+            log.warn("[Gate] 原始仓位减仓: 空头补仓成交但未找到空头止损位, 跳过追加止损");
+            return;
+        }
+        placeStopLossOrders(shortSl.getId(), 1, String.valueOf(replenishQty),
+                FuturesPriceTrigger.RuleEnum.NUMBER_1, ORDER_TYPE_CLOSE_SHORT, false, null, null);
+        log.info("[Gate] 原始仓位减仓: 空头补仓{}张成交, 已在空头止损位 gridId:{} 追加止损", replenishQty, shortSl.getId());
+    }
+
     private void handleLongStopLossTriggered(GridElement gridElement, String orderId) {
         // 本网格全部止损单覆盖张数(含刚触发的这一单),用于追单时修正 REST 持仓的时序竞态
         int gridStopLossSize = gridElement.getLongStopLossTotalSize();
@@ -2923,10 +3117,23 @@
 
         // 止损追单:同一网格可有多笔挂单,不判断 isHasLongOrder,直接挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(true, true, gridStopLossSize);
-        log.info("[Gate] 多仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
-                getEffectiveStopLossCount(true), getEffectiveAddCount(true), config.getAddPositionStopThreshold(),
-                config.getBaseQuantity(), size, config.getStopLossCountMode());
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            // 原始仓位减仓:递减追单(首次=原始仓位,之后=本次被平数量-步长,≤步长时重置为原始仓位)
+            int chaseQty = computeReducedChaseQty(true, gridStopLossSize);
+            currentLongChaseQty = chaseQty;
+            size = String.valueOf(chaseQty);
+            hasAdd = false;
+            log.info("[Gate] 多仓原始仓位减仓追单 被平:{}张, 步长:{}, 追单:{}张",
+                    gridStopLossSize, config.getOriginalPositionReduce(), size);
+        } else {
+            size = calculateEntryQuantity(true, true, gridStopLossSize);
+            hasAdd = computeAddQty(true) > 0;
+            log.info("[Gate] 多仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
+                    getEffectiveStopLossCount(true), getEffectiveAddCount(true), config.getAddPositionStopThreshold(),
+                    config.getBaseQuantity(), size, config.getStopLossCountMode());
+        }
         if (Integer.parseInt(size) <= 0) {
             log.warn("[Gate] 多仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide());
             // 清理占坑标记与追单登记(未挂单)
@@ -2935,8 +3142,6 @@
             GridElement.refreshIndices();
         } else {
             newEntryGrid.getLongTraderParam().setQuantity(size);
-            // 含加仓量的单打标记:真正成交后才计入加仓执行次数(挂单后被取消的不算)
-            boolean hasAdd = computeAddQty(true) > 0;
             placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
                     FuturesPriceTrigger.RuleEnum.NUMBER_1, size,
                     hasAdd ? pendingAddEntryOrderIds::add : null);
@@ -2996,10 +3201,22 @@
 
         // 止损追单:同一网格可有多笔挂单,不判断 isHasShortOrder,直接挂单
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        String size = calculateEntryQuantity(false, true, gridStopLossSize);
-        log.info("[Gate] 空仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
-                getEffectiveStopLossCount(false), getEffectiveAddCount(false), config.getAddPositionStopThreshold(),
-                config.getBaseQuantity(), size, config.getStopLossCountMode());
+        String size;
+        boolean hasAdd;
+        if (config.getOriginalPositionReduce() > 0) {
+            int chaseQty = computeReducedChaseQty(false, gridStopLossSize);
+            currentShortChaseQty = chaseQty;
+            size = String.valueOf(chaseQty);
+            hasAdd = false;
+            log.info("[Gate] 空仓原始仓位减仓追单 被平:{}张, 步长:{}, 追单:{}张",
+                    gridStopLossSize, config.getOriginalPositionReduce(), size);
+        } else {
+            size = calculateEntryQuantity(false, true, gridStopLossSize);
+            hasAdd = computeAddQty(false) > 0;
+            log.info("[Gate] 空仓止损追单 有效次数:{}, 加仓执行:{}, 停止阈值:{}, 基础:{}张 → 实际:{}张, 模式:{}",
+                    getEffectiveStopLossCount(false), getEffectiveAddCount(false), config.getAddPositionStopThreshold(),
+                    config.getBaseQuantity(), size, config.getStopLossCountMode());
+        }
         if (Integer.parseInt(size) <= 0) {
             log.warn("[Gate] 空仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide());
             // 清理占坑标记与追单登记(未挂单)
@@ -3008,8 +3225,6 @@
             GridElement.refreshIndices();
         } else {
             newEntryGrid.getShortTraderParam().setQuantity(size);
-            // 含加仓量的单打标记:真正成交后才计入加仓执行次数(挂单后被取消的不算)
-            boolean hasAdd = computeAddQty(false) > 0;
             placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
                     FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size),
                     hasAdd ? pendingAddEntryOrderIds::add : null);
@@ -3703,6 +3918,7 @@
                 // 同步清理对向加仓/补仓/加仓标记登记表,避免残留失效记录
                 tpOppositeLongOrderGridMap.remove(oid);
                 tpReplenishLongOrderMap.remove(oid);
+                reduceReplenishLongOrderMap.remove(oid);
                 pendingAddEntryOrderIds.remove(oid);
                 executor.cancelConditionalOrder(oid, o -> {});
             }
@@ -3728,6 +3944,7 @@
                 // 同步清理对向加仓/补仓/加仓标记登记表,避免残留失效记录
                 tpOppositeShortOrderGridMap.remove(oid);
                 tpReplenishShortOrderMap.remove(oid);
+                reduceReplenishShortOrderMap.remove(oid);
                 pendingAddEntryOrderIds.remove(oid);
                 executor.cancelConditionalOrder(oid, o -> {});
             }

--
Gitblit v1.9.1