From dad85ad8a84902a41b14f4e71aa317d598facbb1 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 25 Jun 2026 23:34:02 +0800
Subject: [PATCH] fix(gateApi): 解决交易所持仓查询延迟导致的持仓数量错误问题
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 24 +++++++++++++++++-------
1 files changed, 17 insertions(+), 7 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 b473114..cdee426 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -638,7 +638,8 @@
shortEntryTraderIdParam(shortGridElement, null, false);
// [Gate-需求2] 加仓后先撤空仓所有止盈+止损,再查交易所持仓后重挂
cancelAllShortTakeProfitsAndStopLosses();
- int posSize = queryPositionSize(Position.ModeEnum.DUAL_SHORT);
+ // REST 查询可能因交易所延迟返回旧值,与 WS 本地缓存取最大值兜底
+ int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
extendShortStopLoss(posSize, shortGridElement.getId());
accumulatedShortLossCount = 0; // 加仓订单成交,重置止损累计
log.info("[Gate] 空单成交 gridId:{}, 当前持仓:{}张", filledQty, posSize);
@@ -650,7 +651,7 @@
BigDecimal shortExcess = BigDecimal.valueOf(posSize).subtract(shortBaseQty);
int shortExcessCount = shortExcess.divide(shortGridQty, 0, RoundingMode.DOWN).intValue();
for (int i = 0; i < shortExcessCount; i++) {
- int tpGridId = shortGridElement.getId() - 2 - i;
+ int tpGridId = shortGridElement.getId() - 2 * (i + 1);
GridElement tpElem = GridElement.findById(tpGridId);
if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) {
continue;
@@ -680,7 +681,8 @@
longEntryTraderIdParam(longGridElement, null, false);
// [Gate-需求2] 加仓后先撤多仓所有止盈+止损,再查交易所持仓后重挂
cancelAllLongTakeProfitsAndStopLosses();
- int posSize = queryPositionSize(Position.ModeEnum.DUAL_LONG);
+ // REST 查询可能因交易所延迟返回旧值,与 WS 本地缓存取最大值兜底
+ int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
extendLongStopLoss(posSize, longGridElement.getId());
accumulatedLongLossCount = 0; // 加仓订单成交,重置止损累计
log.info("[Gate] 多单成交 gridId:{}, 当前持仓:{}张", filledQty, posSize);
@@ -692,7 +694,7 @@
BigDecimal longExcess = BigDecimal.valueOf(posSize).subtract(longBaseQty);
int longExcessCount = longExcess.divide(longGridQty, 0, RoundingMode.DOWN).intValue();
for (int i = 0; i < longExcessCount; i++) {
- int tpGridId = longGridElement.getId() + 2 + i;
+ int tpGridId = longGridElement.getId() + 2 * (i + 1);
GridElement tpElem = GridElement.findById(tpGridId);
if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) {
continue;
@@ -1331,6 +1333,14 @@
if (span <= 0) {
return;
}
+
+ // 检查是否还有剩余止盈单,只有多空止盈全部清空才继续
+ if (GridElement.getLongTakeProfitCount() > 0 || GridElement.getShortTakeProfitCount() > 0) {
+ log.info("[Gate] 尚有未触发止盈单, 暂不检查跨度重启 longTpCount:{}, shortTpCount:{}",
+ GridElement.getLongTakeProfitCount(), GridElement.getShortTakeProfitCount());
+ return;
+ }
+
BigDecimal step = config.getStep();
if (step == null || step.compareTo(BigDecimal.ZERO) == 0) {
return;
@@ -1356,11 +1366,11 @@
String reason = "";
if (hasLong && hasShort) {
- // 多空双边持仓:多均价 − 空均价 > span × step
- BigDecimal gap = longAvgPrice.subtract(shortAvgPrice);
+ // 多空双边持仓:|多均价 − 空均价| > span × step
+ BigDecimal gap = shortAvgPrice.subtract(longAvgPrice);
if (gap.compareTo(threshold) >= 0) {
shouldRestart = true;
- reason = StrUtil.format("双边跨度 多均价:{} − 空均价:{} = {} > {} (span:{}×step:{})",
+ reason = StrUtil.format("双边跨度 |多均价:{} − 空均价:{}| = {} >= {} (span:{}×step:{})",
longAvgPrice, shortAvgPrice, gap, threshold, span, step);
}
} else if (hasLong) {
--
Gitblit v1.9.1