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 ++++++++------
src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java | 72 ++++++++++++++++++++++++++++++------
2 files changed, 76 insertions(+), 24 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:{}",
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 3d80889..2ee0a7a 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
@@ -91,6 +91,10 @@
private List<String> longStopLossOrderIds = new ArrayList<>();
/** 空仓止损订单 ID 列表(支持一个网格多个止损单) */
private List<String> shortStopLossOrderIds = new ArrayList<>();
+ /** 多仓止损订单 ID → 张数(止损触发时统计该网格全部止损覆盖张数,修正追单的 REST 持仓时序竞态) */
+ private final Map<String, Integer> longStopLossOrderSizes = new ConcurrentHashMap<>();
+ /** 空仓止损订单 ID → 张数 */
+ private final Map<String, Integer> shortStopLossOrderSizes = new ConcurrentHashMap<>();
/**
* 止损追单是否正在进行中(防重入标记)。
@@ -525,27 +529,71 @@
/** @return 多仓止损订单 ID 列表 */
public List<String> getLongStopLossOrderIds() { return longStopLossOrderIds; }
- /** 添加一个多仓止损订单 ID */
- public void addLongStopLossOrderId(String orderId) { longStopLossOrderIds.add(orderId); }
- /** 移除一个多仓止损订单 ID */
- public boolean removeLongStopLossOrderId(String orderId) { return longStopLossOrderIds.remove(orderId); }
- /** 清空所有多仓止损订单 ID */
- public void clearLongStopLossOrderIds() { longStopLossOrderIds.clear(); }
+ /** 添加一个多仓止损订单 ID(不记录张数,仅兼容旧调用方) */
+ public void addLongStopLossOrderId(String orderId) { addLongStopLossOrderId(orderId, 0); }
+ /** 添加一个多仓止损订单 ID 并记录该单张数 */
+ public void addLongStopLossOrderId(String orderId, int size) {
+ if (orderId == null) {
+ return;
+ }
+ longStopLossOrderIds.add(orderId);
+ longStopLossOrderSizes.put(orderId, size);
+ }
+ /** 移除一个多仓止损订单 ID(同步清理张数记录) */
+ public boolean removeLongStopLossOrderId(String orderId) {
+ longStopLossOrderSizes.remove(orderId);
+ return longStopLossOrderIds.remove(orderId);
+ }
+ /** 清空所有多仓止损订单 ID(同步清理张数记录) */
+ public void clearLongStopLossOrderIds() {
+ longStopLossOrderIds.clear();
+ longStopLossOrderSizes.clear();
+ }
/** @return 是否有止损单 */
public boolean hasLongStopLossOrders() { return !longStopLossOrderIds.isEmpty(); }
+ /** @return 该网格全部多仓止损单覆盖张数之和(用于止损触发时的追单量修正) */
+ public int getLongStopLossTotalSize() {
+ int total = 0;
+ for (Integer size : longStopLossOrderSizes.values()) {
+ total += size;
+ }
+ return total;
+ }
// ==================== 空仓止损订单 ID 列表 ====================
/** @return 空仓止损订单 ID 列表 */
public List<String> getShortStopLossOrderIds() { return shortStopLossOrderIds; }
- /** 添加一个空仓止损订单 ID */
- public void addShortStopLossOrderId(String orderId) { shortStopLossOrderIds.add(orderId); }
- /** 移除一个空仓止损订单 ID */
- public boolean removeShortStopLossOrderId(String orderId) { return shortStopLossOrderIds.remove(orderId); }
- /** 清空所有空仓止损订单 ID */
- public void clearShortStopLossOrderIds() { shortStopLossOrderIds.clear(); }
+ /** 添加一个空仓止损订单 ID(不记录张数,仅兼容旧调用方) */
+ public void addShortStopLossOrderId(String orderId) { addShortStopLossOrderId(orderId, 0); }
+ /** 添加一个空仓止损订单 ID 并记录该单张数 */
+ public void addShortStopLossOrderId(String orderId, int size) {
+ if (orderId == null) {
+ return;
+ }
+ shortStopLossOrderIds.add(orderId);
+ shortStopLossOrderSizes.put(orderId, size);
+ }
+ /** 移除一个空仓止损订单 ID(同步清理张数记录) */
+ public boolean removeShortStopLossOrderId(String orderId) {
+ shortStopLossOrderSizes.remove(orderId);
+ return shortStopLossOrderIds.remove(orderId);
+ }
+ /** 清空所有空仓止损订单 ID(同步清理张数记录) */
+ public void clearShortStopLossOrderIds() {
+ shortStopLossOrderIds.clear();
+ shortStopLossOrderSizes.clear();
+ }
/** @return 是否有止损单 */
public boolean hasShortStopLossOrders() { return !shortStopLossOrderIds.isEmpty(); }
+ /** @return 该网格全部空仓止损单覆盖张数之和(用于止损触发时的追单量修正) */
+ public int getShortStopLossTotalSize() {
+ int total = 0;
+ for (Integer size : shortStopLossOrderSizes.values()) {
+ total += size;
+ }
+ return total;
+ }
// ==================== 止损追单防重入标记 ====================
--
Gitblit v1.9.1