From cee19e0b747112a8e023b8dcbb4b38fb643082cf Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 06 Nov 2026 11:28:04 +0800
Subject: [PATCH] fix(grid): 修复网格并发修改异常与日志变量错误
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 254 +++++++++++++++++++++++++++-----------------------
1 files changed, 135 insertions(+), 119 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 d36dbaa..9a7bed8 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -201,7 +201,7 @@
public GateGridTradeService(GateConfig config, StatsEventProducer statsProducer) {
this.config = config;
this.statsProducer = statsProducer;
- ApiClient apiClient = new ApiClient();
+ ApiClient apiClient = new GateClosingApiClient();
apiClient.setBasePath(config.getRestBasePath());
apiClient.setApiKeySecret(config.getApiKey(), config.getApiSecret());
// 修复 gate-api SDK finishAs 枚举不完整导致的反序列化失败(见 buildLenientJson)
@@ -268,7 +268,7 @@
*/
public void init() {
try {
- ApiClient detailClient = new ApiClient();
+ ApiClient detailClient = new GateClosingApiClient();
detailClient.setBasePath(config.getRestBasePath());
detailClient.setApiKeySecret(config.getApiKey(), config.getApiSecret());
AccountDetail detail = new AccountApi(detailClient).getAccountDetail();
@@ -2302,166 +2302,182 @@
/** 多仓仓位归零后,在当前价上方最近网格重新挂多单(遍历 totalLongPriceQueue 升序)。 */
private void processLongGrid(BigDecimal currentPrice) {
- BigDecimal matched = BigDecimal.ZERO;
+ GridElement newEntryGrid = null;
+ boolean hasLongOrderBelow = false;
+ // 阶段1(队列锁内):匹配价格 + 网格定位 + 本地挂单检查 + 提交撤单,全程无阻塞 IO
synchronized (totalLongPriceQueue) {
+ BigDecimal matched = BigDecimal.ZERO;
for (BigDecimal p : totalLongPriceQueue) {
if (p.compareTo(currentPrice) >= 0) {
- matched = p;
+ matched = p;
break;
}
}
-// log.info("[Gate-1] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
- if (BigDecimal.ZERO.compareTo( matched) == 0) {
+ if (BigDecimal.ZERO.compareTo(matched) == 0) {
return;
}
GridElement matchedUpGridElement = GridElement.findByPrice(matched);
- if (matchedUpGridElement != null){
- if (!matchedUpGridElement.isHasLongOrder()){
- Integer upId = matchedUpGridElement.getUpId();
- GridElement newEntryGrid = GridElement.findById(upId);
+ if (matchedUpGridElement != null && !matchedUpGridElement.isHasLongOrder()) {
+ Integer upId = matchedUpGridElement.getUpId();
+ newEntryGrid = GridElement.findById(upId);
- if (newEntryGrid != null) {
+ if (newEntryGrid != null) {
+ // 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单)
+ GridElement checkDownCursor = GridElement.findById(newEntryGrid.getDownId());
+ while (checkDownCursor != null) {
+ if (checkDownCursor.isHasLongOrder()) {
+ hasLongOrderBelow = true;
+ log.debug("[Gate] 多仓仓位归零 gridId:{}, 下方gridId:{}已有多单, 跳过本次挂单",
+ newEntryGrid.getId(), checkDownCursor.getId());
+ break;
+ }
+ Integer nextDownId = checkDownCursor.getDownId();
+ checkDownCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
+ }
- String quantity = calculateEntryQuantity(true, false, 0);
- // 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单)
- boolean hasLongOrderBelow = false;
- GridElement checkDownCursor = GridElement.findById(newEntryGrid.getDownId());
- while (checkDownCursor != null) {
- if (checkDownCursor.isHasLongOrder()) {
- hasLongOrderBelow = true;
- log.debug("[Gate] 多仓仓位归零 gridId:{}, 下方gridId:{}已有多单, 跳过本次挂单",
- newEntryGrid.getId(), checkDownCursor.getId());
- break;
+ // 向上遍历取消所有遗留多单(跳过基础入场网格0)
+ GridElement cancelCursor = GridElement.findById(newEntryGrid.getUpId());
+ while (cancelCursor != null) {
+ if (cancelCursor.isHasLongOrder()) {
+ for (String longOrderId : new ArrayList<>(cancelCursor.getLongOrderIds())) {
+ GridElement finalCancelCursor = cancelCursor;
+ executor.cancelConditionalOrder(longOrderId, oid -> {
+ longEntryTraderIdParam(finalCancelCursor, longOrderId, false);
+ log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单,{}", finalCancelCursor.getId(), longOrderId);
+ });
}
- Integer nextDownId = checkDownCursor.getDownId();
- checkDownCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
}
-
- // 向上遍历取消所有遗留多单(跳过基础入场网格0)
- GridElement cancelCursor = GridElement.findById(newEntryGrid.getUpId());
- while (cancelCursor != null) {
- if (cancelCursor.isHasLongOrder()) {
- for (String longOrderId : new ArrayList<>(cancelCursor.getLongOrderIds())) {
- GridElement finalCancelCursor = cancelCursor;
- executor.cancelConditionalOrder(longOrderId, oid -> {
- longEntryTraderIdParam(finalCancelCursor, longOrderId, false);
- log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单,{}", finalCancelCursor.getId(), longOrderId);
- });
- }
- }
- Integer nextUpId = cancelCursor.getUpId();
- cancelCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
- }
-// log.info("[Gate-2] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
- if (!newEntryGrid.isHasLongOrder() && !hasLongOrderBelow) {
- // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
- // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍(历史出现过 68→136 张)
- int realPos = queryPositionSize(Position.ModeEnum.DUAL_LONG);
- if (realPos > 0) {
- longActive = true;
- longPositionSize = BigDecimal.valueOf(realPos);
- log.warn("[Gate] 多仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
- return;
- }
- BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = quantity;
- log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}张多单",
- newEntryGrid.getId(), size);
- newEntryGrid.getLongTraderParam().setQuantity(size);
- // 含加仓量的单打标记:真正成交后才计入加仓执行次数
- boolean hasAdd = computeAddQty(true) > 0;
- placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_1, size,
- hasAdd ? pendingAddEntryOrderIds::add : null);
- }
+ Integer nextUpId = cancelCursor.getUpId();
+ cancelCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
}
}
}
}
+ // 阶段2(队列锁外):REST 持仓核实,阻塞 IO 不持锁
+ if (newEntryGrid == null || newEntryGrid.isHasLongOrder() || hasLongOrderBelow) {
+ return;
+ }
+ // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
+ // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍(历史出现过 68→136 张);
+ // 顺带同步 entryPrice(此前核实只修 size/active,均价仍是旧值)
+ Position longPos = queryPosition(Position.ModeEnum.DUAL_LONG);
+ int realPos = longPos != null ? Math.abs(new BigDecimal(longPos.getSize()).intValue()) : 0;
+ if (realPos > 0) {
+ longActive = true;
+ longPositionSize = BigDecimal.valueOf(realPos);
+ if (longPos.getEntryPrice() != null) {
+ try {
+ longEntryPrice = new BigDecimal(longPos.getEntryPrice());
+ } catch (NumberFormatException ignored) {
+ }
+ }
+ log.warn("[Gate] 多仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
+ return;
+ }
+
+ // 阶段3:挂单
+ BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+ String size = calculateEntryQuantity(true, false, 0);
+ log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}张多单",
+ newEntryGrid.getId(), size);
+ newEntryGrid.getLongTraderParam().setQuantity(size);
+ // 含加仓量的单打标记:真正成交后才计入加仓执行次数
+ boolean hasAdd = computeAddQty(true) > 0;
+ placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_1, size,
+ hasAdd ? pendingAddEntryOrderIds::add : null);
}
/** 空仓仓位归零后,在当前价下方最近网格重新挂空单(遍历 totalShortPriceQueue 降序)。 */
private void processShortGrid(BigDecimal currentPrice) {
- BigDecimal matched = BigDecimal.ZERO;
+ GridElement newEntryGrid = null;
+ boolean hasShortOrderAbove = false;
+ // 阶段1(队列锁内):匹配价格 + 网格定位 + 本地挂单检查 + 提交撤单,全程无阻塞 IO
synchronized (totalShortPriceQueue) {
+ BigDecimal matched = BigDecimal.ZERO;
for (BigDecimal p : totalShortPriceQueue) {
if (p.compareTo(currentPrice) <= 0) {
matched = p;
break;
}
}
-// log.info("[Gate-3] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
- if (BigDecimal.ZERO.compareTo( matched) == 0) {
+ if (BigDecimal.ZERO.compareTo(matched) == 0) {
return;
}
GridElement matchedUpGridElement = GridElement.findByPrice(matched);
- if (matchedUpGridElement != null){
- if(!matchedUpGridElement.isHasShortOrder()){
- Integer downId = matchedUpGridElement.getDownId();
- GridElement newEntryGrid = GridElement.findById(downId);
+ if (matchedUpGridElement != null && !matchedUpGridElement.isHasShortOrder()) {
+ Integer downId = matchedUpGridElement.getDownId();
+ newEntryGrid = GridElement.findById(downId);
- if (newEntryGrid != null) {
-
- String quantity = calculateEntryQuantity(false, false, 0);
- // 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单)
- boolean hasShortOrderAbove = false;
- GridElement checkUpCursor = GridElement.findById(newEntryGrid.getUpId());
- while (checkUpCursor != null) {
- if (checkUpCursor.isHasShortOrder()) {
- hasShortOrderAbove = true;
- log.debug("[Gate] 空仓仓位归零 gridId:{}, 上方gridId:{}已有空单, 跳过本次挂单",
- newEntryGrid.getId(), checkUpCursor.getId());
- break;
- }
- Integer nextUpId = checkUpCursor.getUpId();
- checkUpCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
+ if (newEntryGrid != null) {
+ // 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单)
+ GridElement checkUpCursor = GridElement.findById(newEntryGrid.getUpId());
+ while (checkUpCursor != null) {
+ if (checkUpCursor.isHasShortOrder()) {
+ hasShortOrderAbove = true;
+ log.debug("[Gate] 空仓仓位归零 gridId:{}, 上方gridId:{}已有空单, 跳过本次挂单",
+ newEntryGrid.getId(), checkUpCursor.getId());
+ break;
}
+ Integer nextUpId = checkUpCursor.getUpId();
+ checkUpCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
+ }
- // 向下遍历取消所有遗留空单(跳过基础入场网格0)
- GridElement cancelCursor = GridElement.findById(newEntryGrid.getDownId());
- while (cancelCursor != null) {
- if (cancelCursor.isHasShortOrder()) {
- for (String shortOrderId : new ArrayList<>(cancelCursor.getShortOrderIds())) {
- GridElement finalCancelCursor = cancelCursor;
- executor.cancelConditionalOrder(shortOrderId, oid -> {
- shortEntryTraderIdParam(finalCancelCursor, shortOrderId, false);
- log.info("[Gate] 空仓仓位归零, 取消gridId:{}的空单{}", finalCancelCursor.getId(), shortOrderId);
- });
- }
+ // 向下遍历取消所有遗留空单(跳过基础入场网格0)
+ GridElement cancelCursor = GridElement.findById(newEntryGrid.getDownId());
+ while (cancelCursor != null) {
+ if (cancelCursor.isHasShortOrder()) {
+ for (String shortOrderId : new ArrayList<>(cancelCursor.getShortOrderIds())) {
+ GridElement finalCancelCursor = cancelCursor;
+ executor.cancelConditionalOrder(shortOrderId, oid -> {
+ shortEntryTraderIdParam(finalCancelCursor, shortOrderId, false);
+ log.info("[Gate] 空仓仓位归零, 取消gridId:{}的空单{}", finalCancelCursor.getId(), shortOrderId);
+ });
}
- Integer nextDownId = cancelCursor.getDownId();
- cancelCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
}
-// log.info("[Gate-4] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
- if (!newEntryGrid.isHasShortOrder() && !hasShortOrderAbove){
- // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
- // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍
- int realPos = queryPositionSize(Position.ModeEnum.DUAL_SHORT);
- if (realPos > 0) {
- shortActive = true;
- shortPositionSize = BigDecimal.valueOf(realPos);
- log.warn("[Gate] 空仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
- return;
- }
- BigDecimal triggerPrice = newEntryGrid.getGridPrice();
- String size = quantity;
- log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}张空单",
- newEntryGrid.getId(), size);
- newEntryGrid.getShortTraderParam().setQuantity(size);
- // 含加仓量的单打标记:真正成交后才计入加仓执行次数
- boolean hasAdd = computeAddQty(false) > 0;
- placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
- FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size),
- hasAdd ? pendingAddEntryOrderIds::add : null);
- }
-
+ Integer nextDownId = cancelCursor.getDownId();
+ cancelCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
}
}
}
}
+
+ // 阶段2(队列锁外):REST 持仓核实,阻塞 IO 不持锁
+ if (newEntryGrid == null || newEntryGrid.isHasShortOrder() || hasShortOrderAbove) {
+ return;
+ }
+ // 挂单前核实:本地持仓可能滞后(成交推送延迟/被快照风暴阻塞),
+ // REST 确认该方向真实持仓为 0 才挂单,避免重复建仓导致持仓翻倍;
+ // 顺带同步 entryPrice(此前核实只修 size/active,均价仍是旧值)
+ Position shortPos = queryPosition(Position.ModeEnum.DUAL_SHORT);
+ int realPos = shortPos != null ? Math.abs(new BigDecimal(shortPos.getSize()).intValue()) : 0;
+ if (realPos > 0) {
+ shortActive = true;
+ shortPositionSize = BigDecimal.valueOf(realPos);
+ if (shortPos.getEntryPrice() != null) {
+ try {
+ shortEntryPrice = new BigDecimal(shortPos.getEntryPrice());
+ } catch (NumberFormatException ignored) {
+ }
+ }
+ log.warn("[Gate] 空仓仓位归零挂单前核实: 实际持仓{}张>0, 跳过挂单(本地状态已滞后)", realPos);
+ return;
+ }
+
+ // 阶段3:挂单
+ BigDecimal triggerPrice = newEntryGrid.getGridPrice();
+ String size = calculateEntryQuantity(false, false, 0);
+ log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}张空单",
+ newEntryGrid.getId(), size);
+ newEntryGrid.getShortTraderParam().setQuantity(size);
+ // 含加仓量的单打标记:真正成交后才计入加仓执行次数
+ boolean hasAdd = computeAddQty(false) > 0;
+ placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
+ FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size),
+ hasAdd ? pendingAddEntryOrderIds::add : null);
}
// ========== 加仓计算 ==========
--
Gitblit v1.9.1