From 3d1aed8344db0708106769a05965994fc366e11b Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 16 Jul 2026 10:34:13 +0800
Subject: [PATCH] fix(gateApi): 修复网格交易中订单数量计算逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 67 +++++++++++++++++----------------
1 files changed, 35 insertions(+), 32 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 1b93e1e..1dda170 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -666,9 +666,10 @@
// }
// );
- // 空仓止盈:每1张持仓对应1个止盈位,从entry网格向下逐个挂
+ // 空仓止盈:超额部分挂止盈,从entry网格向下逐个挂
+ int shortBaseQty = Integer.parseInt(config.getBaseQuantity());
int shortGridQty = Integer.parseInt(config.getQuantity());
- int shortTpCount = posSize / shortGridQty;
+ int shortTpCount = posSize > shortBaseQty ? (posSize - shortBaseQty) / shortGridQty : 0;
for (int i = 0; i < shortTpCount; i++) {
int tpGridId = shortGridElement.getId() - 2 * (i + 1);
@@ -731,9 +732,10 @@
// }
// );
- // 多仓止盈:每1张持仓对应1个止盈位,从entry网格向上逐个挂
+ // 多仓止盈:超额部分挂止盈,从entry网格向上逐个挂
+ int longBaseQty = Integer.parseInt(config.getBaseQuantity());
int longGridQty = Integer.parseInt(config.getQuantity());
- int longTpCount = posSize / longGridQty;
+ int longTpCount = posSize > longBaseQty ? (posSize - longBaseQty) / longGridQty : 0;
for (int i = 0; i < longTpCount; i++) {
int tpGridId = longGridElement.getId() + 2 * (i + 1);
@@ -1202,9 +1204,7 @@
if (newEntryGrid != null) {
GridElement cancelGridElement = GridElement.findById(newEntryGrid.getUpId());
- String quantity = cancelGridElement != null
- ? cancelGridElement.getLongTraderParam().getQuantity()
- : config.getBaseQuantity();
+ String quantity = String.valueOf((accumulatedLongLossCount + 1) * Integer.parseInt(config.getQuantity()));
if (cancelGridElement != null && cancelGridElement.isHasLongOrder()) {
for (String longOrderId : new ArrayList<>(cancelGridElement.getLongOrderIds())) {
executor.cancelConditionalOrder(longOrderId, oid -> {
@@ -1254,9 +1254,7 @@
GridElement cancelGridElement = GridElement.findById(newEntryGrid.getDownId());
- String quantity = cancelGridElement != null
- ? cancelGridElement.getShortTraderParam().getQuantity()
- : config.getBaseQuantity();
+ String quantity = String.valueOf((accumulatedShortLossCount + 1) * Integer.parseInt(config.getQuantity()));
/**
* 看是否有空仓挂单,有就取消
*/
@@ -1307,7 +1305,7 @@
BigDecimal triggerPrice = newEntryGrid.getGridPrice();
int posSize = Math.min(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
int maxPos = config.getMaxPositionSize();
- int targetAmount = sameGrid ? Integer.parseInt(config.getQuantity()) : accumulatedLongLossCount + 1;
+ int targetAmount = sameGrid ? Integer.parseInt(config.getQuantity()) : (accumulatedLongLossCount + 1) * Integer.parseInt(config.getQuantity());
int addSize;
if (maxPos > 0) {
int remainingRoom = maxPos - posSize;
@@ -1383,7 +1381,7 @@
BigDecimal triggerPrice = newEntryGrid.getGridPrice();
int posSize = Math.min(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
int maxPos = config.getMaxPositionSize();
- int targetAmount = sameGrid ? Integer.parseInt(config.getQuantity()) : accumulatedShortLossCount + 1;
+ int targetAmount = sameGrid ? Integer.parseInt(config.getQuantity()) : (accumulatedShortLossCount + 1) * Integer.parseInt(config.getQuantity());
int addSize;
if (maxPos > 0) {
int remainingRoom = maxPos - posSize;
@@ -1653,41 +1651,46 @@
private void extendLongStopLoss(int filledQty, int gridId) {
int qty = Integer.parseInt(config.getQuantity());
int aCount = Math.max(0, filledQty / qty - 2);
- log.info("[Gate] 多仓追挂止损, 成交{}张, gridId:{}, A({})x{} B({})x1 C({})x1",
- filledQty, gridId, gridId - 2, aCount, gridId - 3, gridId - 4);
+ if (aCount < 0) return;
- // A 位置:挂 aCount 个独立止损单
- placeStopLossOrders(gridId - 2, aCount, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ int a, b, c;
+ if (aCount == 0) { a = 2; b = 0; c = 0; }
+ else if (aCount == 1) { a = 2; b = 1; c = 0; }
+ else { a = aCount; b = 1; c = 1; }
+
+ log.info("[Gate] 多仓追挂止损, 成交{}张, gridId:{}, A({})x{} B({})x{} C({})x{}",
+ filledQty, gridId, gridId - 2, a, gridId - 3, b, gridId - 4, c);
+
+ if (a > 0) placeStopLossOrders(gridId - 2, a, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
ORDER_TYPE_CLOSE_LONG, true);
- // B 位置:挂 1 个
- placeStopLossOrders(gridId - 3, 1, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ if (b > 0) placeStopLossOrders(gridId - 3, b, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
ORDER_TYPE_CLOSE_LONG, true);
- // C 位置:挂 1 个
- placeStopLossOrders(gridId - 4, 1, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ if (c > 0) placeStopLossOrders(gridId - 4, c, qty, FuturesPriceTrigger.RuleEnum.NUMBER_2,
ORDER_TYPE_CLOSE_LONG, true);
}
/**
* 空仓止损追单 — 3 个固定位置,从近到远分配合约。
- * A(gridId+2): (filledQty/qty - 2) 个独立止损单,每个 qty 张
- * B(gridId+3): 1 个止损单,qty 张
- * C(gridId+4): 1 个止损单,qty 张
- * 注意:空仓止损在 gridId 正方向,触发价 ≥ 网格价。
+ * aCount<0跳过, aCount=0→A=2, aCount=1→A=2,B=1, aCount>=2→A=aCount,B=1,C=1
*/
private void extendShortStopLoss(int filledQty, int gridId) {
int qty = Integer.parseInt(config.getQuantity());
int aCount = Math.max(0, filledQty / qty - 2);
- log.info("[Gate] 空仓追挂止损, 成交{}张, gridId:{}, A({})x{} B({})x1 C({})x1",
- filledQty, gridId, gridId + 2, aCount, gridId + 3, gridId + 4);
+ if (aCount < 0) return;
- // A 位置:挂 aCount 个独立止损单
- placeStopLossOrders(gridId + 2, aCount, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
+ int a, b, c;
+ if (aCount == 0) { a = 2; b = 0; c = 0; }
+ else if (aCount == 1) { a = 2; b = 1; c = 0; }
+ else { a = aCount; b = 1; c = 1; }
+
+ log.info("[Gate] 空仓追挂止损, 成交{}张, gridId:{}, A({})x{} B({})x{} C({})x{}",
+ filledQty, gridId, gridId + 2, a, gridId + 3, b, gridId + 4, c);
+
+ if (a > 0) placeStopLossOrders(gridId + 2, a, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
ORDER_TYPE_CLOSE_SHORT, false);
- // B 位置:挂 1 个
- placeStopLossOrders(gridId + 3, 1, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
+ if (b > 0) placeStopLossOrders(gridId + 3, b, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
ORDER_TYPE_CLOSE_SHORT, false);
- // C 位置:挂 1 个
- placeStopLossOrders(gridId + 4, 1, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
+ if (c > 0) placeStopLossOrders(gridId + 4, c, qty, FuturesPriceTrigger.RuleEnum.NUMBER_1,
ORDER_TYPE_CLOSE_SHORT, false);
}
--
Gitblit v1.9.1