From df818cff8343abd94068c226845ac207a72f8e70 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 19 May 2026 14:23:14 +0800
Subject: [PATCH] ``` fix(gate): 修复网格交易中订单状态更新处理逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 232 ++++++++++++++++++++++-----------------------------------
1 files changed, 91 insertions(+), 141 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 a1ecfc3..b1d7814 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -457,7 +457,7 @@
tryGenerateQueues();
}else {
shortPositionSize = size.abs();
- //取消多仓位线以上的开空仓挂单
+ //取消空仓仓位线以下的开多仓挂单
List<GridElement> allLongOrders = GridElement.findAllLongOrders(shortEntryPrice);
if (CollUtil.isNotEmpty(allLongOrders)){
for (GridElement e : allLongOrders) {
@@ -1018,30 +1018,6 @@
log.info("[Gate] 网格元素列表已构建, 共{}个元素 (空仓:{} 位置:0 多仓:{})", elements.size(), shortSize, longSize);
}
- /**
- * 空仓网格处理(当前价跌破空仓队列元素)。
- *
- * <h3>匹配规则</h3>
- * 遍历空仓队列(降序排列,大→小),收集所有大于当前价的元素为 matched。
- * 降序排列保证一旦遇到 price ≤ currentPrice 即可停止遍历。
- *
- * <h3>执行流程</h3>
- * <ol>
- * <li>匹配队列元素 → 为空则直接返回,不触发</li>
- * <li>空仓队列:移除 matched 元素,从尾部递减 step 补充等量新元素,重新降序排序</li>
- * <li>多仓队列:<b>不再更新</b>(队列转移逻辑已移除)</li>
- * <li>保证金检查 → 不安全则跳过挂单(队列照常更新),安全则继续</li>
- * <li>挂新空仓条件单(触发价 = newShortFirst,rule=NUMBER_2,止盈 = newShortFirst − step,
- * orderId → 止盈价存入 currentShortOrderIds)</li>
- * <li>多仓条件单守卫:newLongFirst = newShortFirst + step × 2,
- * 若 newLongFirst < longEntryPrice → 挂多仓条件单(止盈 = newLongFirst + step,
- * orderId → 止盈价存入 currentLongOrderIds)</li>
- * </ol>
- * 条件单成交后由 {@link #onOrderUpdate} 匹配止盈价并挂止盈条件单。
- * 反向条件单不再在此处理,改为在 {@link #onPositionUpdate} 仓位净减少时触发。
- *
- * @param currentPrice 当前 K 线收盘价(最新成交价)
- */
private void processShortGrid(BigDecimal currentPrice) {
int prec = config.getPriceScale();
List<BigDecimal> matched = new ArrayList<>();
@@ -1120,86 +1096,60 @@
null
);
}
- }
+ int i = UpGridElement.getId() + 2;
+ GridElement downGridElement = GridElement.findById(i);
+ if (downGridElement != null){
+ BigDecimal downGridPrice = downGridElement.getGridPrice();
+ TraderParam downShortTraderParam = downGridElement.getShortTraderParam();
+ if (
+ !downGridElement.isHasShortOrder() &&
+ downGridPrice.compareTo(longEntryPrice) <= 0 &&
+ downGridPrice.compareTo(shortEntryPrice) >= 0
+ ){
+ executor.placeConditionalEntryOrder(
+ downShortTraderParam.getEntryPrice(),
+ FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ negate(downShortTraderParam.getQuantity()),
+ orderId ->
+ {
+ shortEntryTraderIdParam(
+ downGridElement,
+ orderId,
+ true
+ );
+ },
+ null
+ );
- int i = UpGridElement.getId() + 2;
- GridElement downGridElement = GridElement.findById(i);
- if (downGridElement != null){
+ }
- TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
- if (!downGridElement.isHasLongOrder()){
- executor.placeConditionalEntryOrder(
- downLongTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_1,
- downLongTraderParam.getQuantity(),
- orderId ->
- {
- longEntryTraderIdParam(
- downGridElement,
- orderId,
- true
- );
- },
- null
- );
- }
-
- TraderParam downShortTraderParam = downGridElement.getShortTraderParam();
- BigDecimal downGridPrice = downGridElement.getGridPrice();
- if (
- !downGridElement.isHasShortOrder() &&
- downGridPrice.compareTo(currentPrice) < 0 &&
- downGridPrice.compareTo(longEntryPrice) <= 0 &&
- downGridPrice.compareTo(shortEntryPrice) >= 0
- ){
- executor.placeConditionalEntryOrder(
- downShortTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_2,
- negate(downShortTraderParam.getQuantity()),
- orderId ->
- {
- shortEntryTraderIdParam(
- downGridElement,
- orderId,
- true
- );
- },
- null
- );
-
+ TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
+ if (
+ !downGridElement.isHasLongOrder() &&
+ downGridPrice.compareTo(longEntryPrice) <= 0
+ ){
+ executor.placeConditionalEntryOrder(
+ downLongTraderParam.getEntryPrice(),
+ FuturesPriceTrigger.RuleEnum.NUMBER_1,
+ downLongTraderParam.getQuantity(),
+ orderId ->
+ {
+ longEntryTraderIdParam(
+ downGridElement,
+ orderId,
+ true
+ );
+ },
+ null
+ );
+ }
}
}
-
}
-
}
- /**
- * 多仓网格处理(当前价涨破多仓队列元素)。
- *
- * <h3>匹配规则</h3>
- * 遍历多仓队列(升序排列,小→大),收集所有小于当前价的元素为 matched。
- * 升序排列保证一旦遇到 price ≥ currentPrice 即可停止遍历。
- *
- * <h3>执行流程</h3>
- * <ol>
- * <li>匹配队列元素 → 为空则直接返回,不触发</li>
- * <li>多仓队列:移除 matched 元素,从尾部递增 step 补充等量新元素,重新升序排序</li>
- * <li>空仓队列:<b>不再更新</b>(队列转移逻辑已移除)</li>
- * <li>保证金检查 → 不安全则跳过挂单(队列照常更新),安全则继续</li>
- * <li>挂新多仓条件单(触发价 = newLongFirst,rule=NUMBER_1,止盈 = newLongFirst + step,
- * orderId → 止盈价存入 currentLongOrderIds)</li>
- * <li>空仓条件单守卫:newShortFirst = newLongFirst − step × 2,
- * 若 newShortFirst > shortEntryPrice → 挂空仓条件单(止盈 = newShortFirst − step,
- * orderId → 止盈价存入 currentShortOrderIds)</li>
- * </ol>
- * 条件单成交后由 {@link #onOrderUpdate} 匹配止盈价并挂止盈条件单。
- * 反向条件单不再在此处理,改为在 {@link #onPositionUpdate} 仓位净减少时触发。
- *
- * @param currentPrice 当前 K 线收盘价(最新成交价)
- */
private void processLongGrid(BigDecimal currentPrice) {
int prec = config.getPriceScale();
List<BigDecimal> matched = new ArrayList<>();
@@ -1282,60 +1232,60 @@
null
);
}
- }
+ int i = UpGridElement.getId() - 2;
+ GridElement downGridElement = GridElement.findById(i);
+ if (downGridElement != null){
+ BigDecimal downGridPrice = downGridElement.getGridPrice();
- int i = UpGridElement.getId() - 2;
- GridElement downGridElement = GridElement.findById(i);
- if (downGridElement != null){
+ TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
+ if (
+ !downGridElement.isHasLongOrder() &&
+ downGridPrice.compareTo(shortEntryPrice) >= 0 &&
+ downGridPrice.compareTo(longEntryPrice) <= 0
+ ){
+ executor.placeConditionalEntryOrder(
+ downLongTraderParam.getEntryPrice(),
+ FuturesPriceTrigger.RuleEnum.NUMBER_1,
+ config.getQuantity(),
+ orderId ->
+ {
+ longEntryTraderIdParam(
+ downGridElement,
+ orderId,
+ true
+ );
+ },
+ null
+ );
- TraderParam shortTraderParam = downGridElement.getShortTraderParam();
- if (!downGridElement.isHasShortOrder()){
- executor.placeConditionalEntryOrder(
- shortTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_2,
- negate(config.getQuantity()),
- orderId ->
- {
- shortEntryTraderIdParam(
- downGridElement,
- orderId,
- true
- );
- },
- null
- );
- }
+ }
- TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
- BigDecimal downGridPrice = downGridElement.getGridPrice();
- if (
- !downGridElement.isHasLongOrder() &&
- downGridPrice.compareTo(currentPrice) > 0 &&
- downGridPrice.compareTo(longEntryPrice) <= 0 &&
- downGridPrice.compareTo(shortEntryPrice) >= 0
- ){
- executor.placeConditionalEntryOrder(
- downLongTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_1,
- config.getQuantity(),
- orderId ->
- {
- longEntryTraderIdParam(
- downGridElement,
- orderId,
- true
- );
- },
- null
- );
+ TraderParam shortTraderParam = downGridElement.getShortTraderParam();
+ if (
+ !downGridElement.isHasShortOrder() &&
+ downGridPrice.compareTo(shortEntryPrice) >= 0
+ ){
+ executor.placeConditionalEntryOrder(
+ shortTraderParam.getEntryPrice(),
+ FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ negate(config.getQuantity()),
+ orderId ->
+ {
+ shortEntryTraderIdParam(
+ downGridElement,
+ orderId,
+ true
+ );
+ },
+ null
+ );
+ }
}
}
-
}
-
}
// ---- 保证金安全阀 ----
--
Gitblit v1.9.1