From c55a379e46a23958d169446937f0d5adf47a5a2a Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 09 Jun 2026 15:28:17 +0800
Subject: [PATCH] refactor(gateApi): 优化网格交易服务的挂单逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 143 ++++++++++++++++++++++-------------------------
1 files changed, 68 insertions(+), 75 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 bc63eae..4bbfa71 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -136,11 +136,6 @@
/** 多头是否活跃(有仓位) */
private volatile boolean longActive = false;
- /** 价格驱动 空头是否挂单 */
- private volatile boolean shortEntryActive = false;
- /** 价格驱动 多头是否挂单 */
- private volatile boolean longEntryActive = false;
-
private volatile BigDecimal lastKlinePrice;
private volatile BigDecimal markPrice = BigDecimal.ZERO;
private volatile BigDecimal cumulativePnl = BigDecimal.ZERO;
@@ -418,16 +413,14 @@
if (state == StrategyState.ACTIVE &&
longActive == false &&
- longPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
- longEntryActive){
+ longPositionSize.compareTo(BigDecimal.ZERO) == 0){
processShortGrid(closePrice);
}
if (state == StrategyState.ACTIVE &&
shortActive == false &&
- shortPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
- shortEntryActive){
+ shortPositionSize.compareTo(BigDecimal.ZERO) == 0){
processLongGrid(closePrice);
}
}
@@ -485,7 +478,6 @@
// log.info("[Gate] 多仓持仓归零,重置策略");
// handlePositionZeroAndReset("多仓");
}
- longEntryActive = true;
longActive = false;
longPositionSize = BigDecimal.ZERO;
}
@@ -509,7 +501,6 @@
// log.info("[Gate] 空仓持仓归零,重置策略");
// handlePositionZeroAndReset("空仓");
}
- shortEntryActive = true;
shortActive = false;
shortPositionSize = BigDecimal.ZERO;
}
@@ -1204,42 +1195,45 @@
break;
}
}
- }
- if (BigDecimal.ZERO.compareTo( matched) == 0) {
- return;
- }
- log.info("[Gate] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
+ if (BigDecimal.ZERO.compareTo( matched) == 0) {
+ return;
+ }
+ log.info("[Gate] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
- GridElement matchedUpGridElement = GridElement.findByPrice(matched);
+ GridElement matchedUpGridElement = GridElement.findByPrice(matched);
+ if (matchedUpGridElement != null){
+ Integer upId = matchedUpGridElement.getUpId();
+ GridElement newEntryGrid = GridElement.findById(upId);
- Integer upId = matchedUpGridElement.getUpId();
- GridElement newEntryGrid = GridElement.findById(upId);
+ if (newEntryGrid != null) {
+ /**
+ * 看是否有多仓挂单,有就取消
+ */
+ if (!newEntryGrid.isHasLongOrder()) {
+ BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+ String size = config.getBaseQuantity();
+ log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}基础张多单",
+ newEntryGrid.getId(), size);
+ newEntryGrid.getLongTraderParam().setQuantity(size);
+ placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
+ }
- if (newEntryGrid != null) {
- GridElement cancelGridElement = GridElement.findById(newEntryGrid.getUpId());
- if (cancelGridElement != null) {
- BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = cancelGridElement.getLongTraderParam().getQuantity();
- log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}基础张多单",
- newEntryGrid.getId(), size);
- newEntryGrid.getLongTraderParam().setQuantity(size);
- placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
-
- longEntryActive = false;
-
- /**
- * 看是否有多仓挂单,有就取消
- */
- if (cancelGridElement != null && cancelGridElement.isHasLongOrder()) {
- executor.cancelConditionalOrder(cancelGridElement.getLongOrderId(), oid -> {
- longEntryTraderIdParam(cancelGridElement, null, false);
- log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
- });
+ GridElement cancelGridElement = GridElement.findById(newEntryGrid.getUpId());
+ if (cancelGridElement != null && cancelGridElement.isHasLongOrder()) {
+ /**
+ * 看是否有多仓挂单,有就取消
+ */
+ executor.cancelConditionalOrder(cancelGridElement.getLongOrderId(), oid -> {
+ longEntryTraderIdParam(cancelGridElement, null, false);
+ log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
+ });
+ }
}
}
}
+
}
private void processLongGrid(BigDecimal currentPrice) {
@@ -1252,43 +1246,42 @@
break;
}
}
- }
- if (BigDecimal.ZERO.compareTo( matched) == 0) {
- return;
- }
-
- log.info("[Gate] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
-
- GridElement matchedUpGridElement = GridElement.findByPrice(matched);
-
- Integer downId = matchedUpGridElement.getDownId();
- GridElement newEntryGrid = GridElement.findById(downId);
-
- if (newEntryGrid != null) {
- GridElement cancelGridElement = GridElement.findById(newEntryGrid.getDownId());
-
- if (cancelGridElement != null) {
- BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = cancelGridElement.getShortTraderParam().getQuantity();
- log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张多单",
- newEntryGrid.getId(), size);
- newEntryGrid.getShortTraderParam().setQuantity(size);
- placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
-
- shortEntryActive = false;
-
- /**
- * 看是否有空仓挂单,有就取消
- */
- if (cancelGridElement != null && cancelGridElement.isHasShortOrder()) {
- executor.cancelConditionalOrder(cancelGridElement.getShortOrderId(), oid -> {
- shortEntryTraderIdParam(cancelGridElement, null, false);
- log.info("[Gate] 空仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
- });
- }
+ if (BigDecimal.ZERO.compareTo( matched) == 0) {
+ return;
}
+ log.info("[Gate] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
+
+ GridElement matchedUpGridElement = GridElement.findByPrice(matched);
+ if (matchedUpGridElement != null){
+ Integer downId = matchedUpGridElement.getDownId();
+ GridElement newEntryGrid = GridElement.findById(downId);
+
+ if (newEntryGrid != null) {
+
+ if (!newEntryGrid.isHasShortOrder()){
+ BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+ String size = config.getBaseQuantity();
+ log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张多单",
+ newEntryGrid.getId(), size);
+ newEntryGrid.getShortTraderParam().setQuantity(size);
+ placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+ }
+
+ GridElement cancelGridElement = GridElement.findById(newEntryGrid.getDownId());
+ /**
+ * 看是否有空仓挂单,有就取消
+ */
+ if (cancelGridElement != null && cancelGridElement.isHasShortOrder()) {
+ executor.cancelConditionalOrder(cancelGridElement.getShortOrderId(), oid -> {
+ shortEntryTraderIdParam(cancelGridElement, null, false);
+ log.info("[Gate] 空仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
+ });
+ }
+
+ }
+ }
}
}
--
Gitblit v1.9.1