From f9f3ca2abf0690b73018731f097dd6d7e11a9cca Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 09 Nov 2026 11:06:59 +0800
Subject: [PATCH] refactor(grid): 重构止损挂单逻辑为单位置全量止损
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 88 +++++++++++++++++++++++++++-----------------
docs/策略运转逻辑.md | 2
2 files changed, 55 insertions(+), 35 deletions(-)
diff --git "a/docs/\347\255\226\347\225\245\350\277\220\350\275\254\351\200\273\350\276\221.md" "b/docs/\347\255\226\347\225\245\350\277\220\350\275\254\351\200\273\350\276\221.md"
index 7852c6b..17ddf1d 100644
--- "a/docs/\347\255\226\347\225\245\350\277\220\350\275\254\351\200\273\350\276\221.md"
+++ "b/docs/\347\255\226\347\225\245\350\277\220\350\275\254\351\200\273\350\276\221.md"
@@ -153,7 +153,7 @@
| OPENING 基底 | 同上(OPENING 态) | 双基底 REST 已成交 → 补 baseOpened/entryPrice + 触发队列生成 |
| 条件单幽灵清理 | 同上(ACTIVE 态) | 本地索引中不在交易所 open 列表的单 → 移除 + 修正标志 |
| 累计盈亏 | 同上 | REST 校准(数据源待修,见 §9) |
-| 止损覆盖兜底 | 同上 | 有持仓但止损覆盖 < 持仓 → 按原位置重挂全持仓止损 |
+| 止损覆盖兜底 | 同上 | 有持仓但止损覆盖 < 持仓 → 按原位置重挂;止损位全部丢失 → 按默认位(±2)整补止损+止盈 |
| 挂单前核实 | 价格驱动挂单瞬间 | REST 确认该方向持仓为 0 才挂(防翻倍) |
---
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 b47e02d..7da0847 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1641,7 +1641,11 @@
int longPos = longPositionSize.intValue();
if (longPos > 0) {
GridElement anchor = findNearestLongStopLossGrid();
- if (anchor != null) {
+ if (anchor == null) {
+ // 止损位全部丢失(断线期间撤掉未重挂/幽灵被对账清空)→ 按默认位整补止盈+止损
+ log.warn("[Gate] 多仓有持仓{}张但无任何止损位, 对账按默认位补挂止盈止损", longPos);
+ rebuildLongProtection();
+ } else {
int covered = 0;
for (GridElement e : config.getGridElements()) {
covered += e.getLongStopLossTotalSize();
@@ -1658,7 +1662,11 @@
int shortPos = shortPositionSize.intValue();
if (shortPos > 0) {
GridElement anchor = findNearestShortStopLossGrid();
- if (anchor != null) {
+ if (anchor == null) {
+ // 止损位全部丢失(断线期间撤掉未重挂/幽灵被对账清空)→ 按默认位整补止盈+止损
+ log.warn("[Gate] 空仓有持仓{}张但无任何止损位, 对账按默认位补挂止盈止损", shortPos);
+ rebuildShortProtection();
+ } else {
int covered = 0;
for (GridElement e : config.getGridElements()) {
covered += e.getShortStopLossTotalSize();
@@ -1672,6 +1680,48 @@
}
}
}
+ }
+
+ /**
+ * 按默认位(基底网格 ±2)重建多仓止盈+止损:撤旧 → 按最新持仓挂单位置全量止损 + 超额/基础止盈。
+ * 供对账兜底(止损位全部丢失时)与手动重建共用。
+ */
+ private void rebuildLongProtection() {
+ cancelAllLongTakeProfitsAndStopLosses();
+ int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
+ if (posSize <= 0) {
+ log.warn("[Gate] 生成多仓止盈止损失败:无多仓持仓");
+ return;
+ }
+ extendLongStopLoss(posSize, 0);
+ if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
+ placeExcessTakeProfit(posSize, true);
+ } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
+ placeDispersedTakeProfit(posSize, true, 0);
+ }
+ placeBaseTakeProfit(posSize, true, 0);
+ log.info("[Gate] 多仓止盈止损已按默认位重建, 仓位:{}张", posSize);
+ }
+
+ /**
+ * 按默认位(基底网格 ±2)重建空仓止盈+止损:撤旧 → 按最新持仓挂单位置全量止损 + 超额/基础止盈。
+ * 供对账兜底(止损位全部丢失时)与手动重建共用。
+ */
+ private void rebuildShortProtection() {
+ cancelAllShortTakeProfitsAndStopLosses();
+ int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
+ if (posSize <= 0) {
+ log.warn("[Gate] 生成空仓止盈止损失败:无空仓持仓");
+ return;
+ }
+ extendShortStopLoss(posSize, 0);
+ if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
+ placeExcessTakeProfit(posSize, false);
+ } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
+ placeDispersedTakeProfit(posSize, false, 0);
+ }
+ placeBaseTakeProfit(posSize, false, 0);
+ log.info("[Gate] 空仓止盈止损已按默认位重建, 仓位:{}张", posSize);
}
/**
@@ -3270,22 +3320,7 @@
* 先撤多仓所有止盈止损,再按交易所最新持仓重挂单位置全量止损,超额部分挂止盈到空仓第一止损位。
*/
public void rebuildLongTakeProfitAndStopLoss() {
- submitManualTask(() -> {
- cancelAllLongTakeProfitsAndStopLosses();
- int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue());
- if (posSize <= 0) {
- log.warn("[Gate] 生成多仓止盈止损失败:无多仓持仓");
- return;
- }
- extendLongStopLoss(posSize, 0);
- if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
- placeExcessTakeProfit(posSize, true);
- } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
- placeDispersedTakeProfit(posSize, true, 0);
- }
- placeBaseTakeProfit(posSize, true, 0);
- log.info("[Gate] 生成多仓止盈止损完成, 仓位:{}张", posSize);
- });
+ submitManualTask(this::rebuildLongProtection);
}
/**
@@ -3293,22 +3328,7 @@
* 先撤空仓所有止盈止损,再按交易所最新持仓重挂单位置全量止损,超额部分挂止盈到多仓第一止损位。
*/
public void rebuildShortTakeProfitAndStopLoss() {
- submitManualTask(() -> {
- cancelAllShortTakeProfitsAndStopLosses();
- int posSize = Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue());
- if (posSize <= 0) {
- log.warn("[Gate] 生成空仓止盈止损失败:无空仓持仓");
- return;
- }
- extendShortStopLoss(posSize, 0);
- if (GateConfig.EXCESS_TP_CONCENTRATED.equals(config.getPlaceExcessTakeProfit())) {
- placeExcessTakeProfit(posSize, false);
- } else if (GateConfig.EXCESS_TP_DISPERSED.equals(config.getPlaceExcessTakeProfit())) {
- placeDispersedTakeProfit(posSize, false, 0);
- }
- placeBaseTakeProfit(posSize, false, 0);
- log.info("[Gate] 生成空仓止盈止损完成, 仓位:{}张", posSize);
- });
+ submitManualTask(this::rebuildShortProtection);
}
/**
--
Gitblit v1.9.1