From 5226ba9bc3d5a35736d975ca732d139329ac27de Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 23 Sep 2026 17:57:02 +0800
Subject: [PATCH] fix(gateApi): 修复止损订单数量计算逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 84 +++++++++++++++++++++++++++---------------
1 files changed, 54 insertions(+), 30 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 0d0b369..fb124ab 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -529,7 +529,7 @@
config.isPriceDriveEnabled() &&
longActive == false &&
longPositionSize.compareTo(BigDecimal.ZERO) == 0){
- processShortGrid(closePrice);
+ processLongGrid(closePrice);
}
@@ -537,7 +537,7 @@
config.isPriceDriveEnabled() &&
shortActive == false &&
shortPositionSize.compareTo(BigDecimal.ZERO) == 0){
- processLongGrid(closePrice);
+ processShortGrid(closePrice);
}
}
@@ -948,12 +948,6 @@
generateShortQueue();
generateLongQueue();
updateGridElements();
-
- GridElement baseGridElement = GridElement.findById(0);
- baseGridElement.setLongOrderId(baseLongTraderParam.getEntryOrderId());
- baseGridElement.setHasLongOrder(true);
- baseGridElement.setShortOrderId(baseShortTraderParam.getEntryOrderId());
- baseGridElement.setHasShortOrder(true);
// // 空仓止损对应的多仓止盈:多仓止盈挂在ID=3
// int tpGridIdLong = 3;
@@ -1449,7 +1443,8 @@
}
- private void processShortGrid(BigDecimal currentPrice) {
+ /** 多仓仓位归零后,在当前价上方最近网格重新挂多单(遍历 totalLongPriceQueue 升序)。 */
+ private void processLongGrid(BigDecimal currentPrice) {
BigDecimal matched = BigDecimal.ZERO;
synchronized (totalLongPriceQueue) {
for (BigDecimal p : totalLongPriceQueue) {
@@ -1475,7 +1470,7 @@
// String quantity = String.valueOf(config.getBaseQuantity());
- String quantity = calculateEntryQuantity(true);
+ String quantity = calculateEntryQuantity(true, false);
// 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单)
boolean hasLongOrderBelow = false;
GridElement checkDownCursor = GridElement.findById(newEntryGrid.getDownId());
@@ -1522,7 +1517,8 @@
}
- private void processLongGrid(BigDecimal currentPrice) {
+ /** 空仓仓位归零后,在当前价下方最近网格重新挂空单(遍历 totalShortPriceQueue 降序)。 */
+ private void processShortGrid(BigDecimal currentPrice) {
BigDecimal matched = BigDecimal.ZERO;
synchronized (totalShortPriceQueue) {
for (BigDecimal p : totalShortPriceQueue) {
@@ -1548,7 +1544,7 @@
// String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()) + Integer.parseInt(config.getQuantity()));
// String quantity = String.valueOf(config.getBaseQuantity());
- String quantity = calculateEntryQuantity(false);
+ String quantity = calculateEntryQuantity(false, false);
// 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单)
boolean hasShortOrderAbove = false;
GridElement checkUpCursor = GridElement.findById(newEntryGrid.getUpId());
@@ -1571,7 +1567,7 @@
GridElement finalCancelCursor = cancelCursor;
executor.cancelConditionalOrder(shortOrderId, oid -> {
shortEntryTraderIdParam(finalCancelCursor, shortOrderId, false);
- log.info("[Gate] 空仓仓位归零, 取消gridId:{}的多单{}", finalCancelCursor.getId(), shortOrderId);
+ log.info("[Gate] 空仓仓位归零, 取消gridId:{}的空单{}", finalCancelCursor.getId(), shortOrderId);
});
}
}
@@ -1582,7 +1578,7 @@
if (!newEntryGrid.isHasShortOrder() && !hasShortOrderAbove){
BigDecimal triggerPrice = newEntryGrid.getGridPrice();
String size = quantity;
- log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张多单",
+ log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张空单",
newEntryGrid.getId(), size);
newEntryGrid.getShortTraderParam().setQuantity(size);
placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
@@ -1616,19 +1612,27 @@
}
/**
- * 根据加仓配置计算止损追单时的实际下单量。
+ * 根据加仓配置计算入场/追单时的实际下单量。
* <p>公式:</p>
* <pre>
* divisor = addPositionInterval + 1
* addMultiplier = floor(effectiveStopLossCount / divisor)
* addQty = addMultiplier × addPositionQuantity
- * finalQty = min(baseQuantity + addQty, maxPositionPerSide > 0 ? maxPositionPerSide : ∞)
+ * finalQty = min(baseQuantity + addQty, upperLimit)
* </pre>
+ * <p>上限 upperLimit:</p>
+ * <ul>
+ * <li>{@code respectCurrentPosition=false}:{@code upperLimit = maxPositionPerSide}
+ * (仓位归零重新入场,仅限制单次下单量)</li>
+ * <li>{@code respectCurrentPosition=true}:{@code upperLimit = maxPositionPerSide - 当前持仓}
+ * (止损追单,保证单边总持仓 ≤ maxPositionPerSide)</li>
+ * </ul>
*
- * @param isLong {@code true}=多仓方向,{@code false}=空仓方向
+ * @param isLong {@code true}=多仓方向,{@code false}=空仓方向
+ * @param respectCurrentPosition {@code true}=按「单边总持仓 ≤ maxPositionPerSide」扣减当前持仓;{@code false}=仅限制单次下单量
* @return 实际下单张数(字符串)
*/
- private String calculateEntryQuantity(boolean isLong) {
+ private String calculateEntryQuantity(boolean isLong, boolean respectCurrentPosition) {
int baseQty = Integer.parseInt(config.getBaseQuantity());
int interval = config.getAddPositionInterval();
int addQtyPerUnit = config.getAddPositionQuantity();
@@ -1645,8 +1649,20 @@
int addQty = addMultiplier * addQtyPerUnit;
int totalQty = baseQty + addQty;
- if (maxPerSide > 0 && totalQty > maxPerSide) {
- totalQty = maxPerSide;
+ if (maxPerSide > 0) {
+ int upperLimit = maxPerSide;
+ if (respectCurrentPosition) {
+ // 止损追单:单边总持仓不得超过 maxPerSide,追单量 = maxPerSide - 当前持仓
+ int currentPos = isLong
+ ? Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue())
+ : Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
+ upperLimit = maxPerSide - currentPos;
+ }
+ if (upperLimit <= 0) {
+ totalQty = 0;
+ } else if (totalQty > upperLimit) {
+ totalQty = upperLimit;
+ }
}
return String.valueOf(totalQty);
@@ -1685,12 +1701,16 @@
// 止损追单:同一网格可有多笔挂单,不判断 isHasLongOrder,直接挂单
BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = calculateEntryQuantity(true);
+ String size = calculateEntryQuantity(true, true);
log.info("[Gate] 多仓止损追单 有效次数:{}, 基础:{}张 → 实际:{}张, 模式:{}",
getEffectiveStopLossCount(true), config.getBaseQuantity(), size, config.getStopLossCountMode());
- newEntryGrid.getLongTraderParam().setQuantity(size);
- placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
+ if (Integer.parseInt(size) <= 0) {
+ log.warn("[Gate] 多仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide());
+ } else {
+ newEntryGrid.getLongTraderParam().setQuantity(size);
+ placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
+ }
// 取消 gridId+2 位置的所有多单
int cancelGridId = gridId + 2;
@@ -1744,12 +1764,16 @@
// 止损追单:同一网格可有多笔挂单,不判断 isHasShortOrder,直接挂单
BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = calculateEntryQuantity(false);
+ String size = calculateEntryQuantity(false, true);
log.info("[Gate] 空仓止损追单 有效次数:{}, 基础:{}张 → 实际:{}张, 模式:{}",
getEffectiveStopLossCount(false), config.getBaseQuantity(), size, config.getStopLossCountMode());
- newEntryGrid.getShortTraderParam().setQuantity(size);
- placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+ if (Integer.parseInt(size) <= 0) {
+ log.warn("[Gate] 空仓止损追单 当前持仓已达单边上限({}), 跳过挂单", config.getMaxPositionPerSide());
+ } else {
+ newEntryGrid.getShortTraderParam().setQuantity(size);
+ placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+ }
// 取消 gridId-2 位置的所有空单
int cancelGridId = gridId - 2;
@@ -1995,7 +2019,7 @@
}
// [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈
- int totalOrders = baseQty / qty;
+ int totalOrders = filledQty / qty;
int[] orders = new int[gridCount];
if (gridCount == 1) {
@@ -2058,7 +2082,7 @@
}
// [Gate] 止损只针对初始开仓仓位(baseQuantity),加仓部分只做止盈
- int totalOrders = baseQty / qty;
+ int totalOrders = filledQty / qty;
int[] orders = new int[gridCount];
if (gridCount == 1) {
--
Gitblit v1.9.1