From 6fa621f3faa69a0ef6597e27f047e314fae3564c Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 22 Jun 2026 10:55:20 +0800
Subject: [PATCH] feat(gateApi): 添加网格策略重启跨度阈值功能
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 81 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 79 insertions(+), 2 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 01bea3a..aae6ebc 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -597,20 +597,22 @@
return;
}
- // [Gate-需求1] 多仓止盈触发:清空止盈状态 + 取消最远多仓止损
+ // [Gate-需求1] 多仓止盈触发:清空止盈状态 + 取消最远多仓止损 + 检查是否最后一个止盈
GridElement longTpElem = GridElement.findByLongTakeProfitOrderId(orderId);
if (longTpElem != null && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
longTakeProfitTraderIdParam(longTpElem, null, false);
log.info("[Gate] 多仓止盈触发 gridId:{}, orderId:{}", longTpElem.getId(), orderId);
cancelFarthestLongStopLoss();
+ checkLastTakeProfitAndRestart();
return;
}
- // [Gate-需求1] 空仓止盈触发:清空止盈状态 + 取消最远空仓止损
+ // [Gate-需求1] 空仓止盈触发:清空止盈状态 + 取消最远空仓止损 + 检查是否最后一个止盈
GridElement shortTpElem = GridElement.findByShortTakeProfitOrderId(orderId);
if (shortTpElem != null && StrUtil.isNotEmpty(tradeId) && !tradeId.equals("0")) {
shortTakeProfitTraderIdParam(shortTpElem, null, false);
log.info("[Gate] 空仓止盈触发 gridId:{}, orderId:{}", shortTpElem.getId(), orderId);
cancelFarthestShortStopLoss();
+ checkLastTakeProfitAndRestart();
return;
}
@@ -1257,6 +1259,81 @@
// ========== 止盈/止损取消辅助方法 ==========
/**
+ * 止盈触发后检查跨度是否达至要求,满足条件则重启策略。
+ *
+ * <h3>跨度定义</h3>
+ * {@code restartGridSpan} 表示多少倍的绝对步长 step(= 短基价 × gridRate)。
+ *
+ * <h3>判断逻辑</h3>
+ * <ol>
+ * <li>多空双边均有持仓:longEntryPrice − shortEntryPrice > span × step</li>
+ * <li>仅持多仓:currentPrice − longEntryPrice > span × step</li>
+ * <li>仅持空仓:shortEntryPrice − currentPrice > span × step</li>
+ * </ol>
+ * restartGridSpan=0 时禁用此功能。重启复用仓位归零模式:取消全部条件单 → 平仓 → 延迟启动。
+ */
+ private void checkLastTakeProfitAndRestart() {
+ int span = config.getRestartGridSpan();
+ if (span <= 0) {
+ return;
+ }
+ BigDecimal step = config.getStep();
+ if (step == null || step.compareTo(BigDecimal.ZERO) == 0) {
+ return;
+ }
+ BigDecimal threshold = step.multiply(new BigDecimal(span));
+
+ BigDecimal currentPrice = lastKlinePrice;
+ if (currentPrice == null || currentPrice.compareTo(BigDecimal.ZERO) == 0) {
+ return;
+ }
+
+ boolean shouldRestart = false;
+ String reason = "";
+
+ if (longActive && shortActive) {
+ // 多空双边持仓:多均价 − 空均价 > span × step
+ BigDecimal gap = longEntryPrice.subtract(shortEntryPrice);
+ if (gap.compareTo(threshold) > 0) {
+ shouldRestart = true;
+ reason = StrUtil.format("双边跨度 多均价:{} − 空均价:{} = {} > {} (span:{}×step:{})",
+ longEntryPrice, shortEntryPrice, gap, threshold, span, step);
+ }
+ } else if (longActive) {
+ // 仅持多仓:当前价 − 多均价 > span × step
+ BigDecimal gap = currentPrice.subtract(longEntryPrice);
+ if (gap.compareTo(threshold) > 0) {
+ shouldRestart = true;
+ reason = StrUtil.format("多仓跨度 当前价:{} − 多均价:{} = {} > {} (span:{}×step:{})",
+ currentPrice, longEntryPrice, gap, threshold, span, step);
+ }
+ } else if (shortActive) {
+ // 仅持空仓:空均价 − 当前价 > span × step
+ BigDecimal gap = shortEntryPrice.subtract(currentPrice);
+ if (gap.compareTo(threshold) > 0) {
+ shouldRestart = true;
+ reason = StrUtil.format("空仓跨度 空均价:{} − 当前价:{} = {} > {} (span:{}×step:{})",
+ shortEntryPrice, currentPrice, gap, threshold, span, step);
+ }
+ }
+
+ if (shouldRestart) {
+ log.info("[Gate] 跨度已达要求 → {},最后一个止盈触发策略重启", reason);
+ try {
+ futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract());
+ } catch (ApiException ex) {
+ log.warn("[Gate] 重启前清理条件单失败", ex);
+ }
+ closeExistingPositions();
+ state = StrategyState.STOPPED;
+ executor.submitTask(() -> {
+ try { Thread.sleep(3000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); }
+ startGrid();
+ });
+ }
+ }
+
+ /**
* 取消最远的多仓止损订单。
* 多仓止损在 gridId 负方向,最远 = id 最小。
*/
--
Gitblit v1.9.1