Administrator
2026-11-07 fdb6650b5fde2d39fa06568e4e234d8264c053e2
refactor(grid): 重构止损挂单逻辑为单位置全量止损

- 移除默认空仓和多仓止损的阶梯式循环挂单逻辑
- 将默认止损改为在固定网格位置挂载单张全量止损单
- 重构多仓和空仓止损追单逻辑,取消多网格拆分挂单
- 将追单止损合并为一张单,挂在入场格相邻的第二格
- 更新相关方法的注释与日志以匹配单位置全量止损逻辑
- 优化止损单数量计算,直接使用基础仓位或成交量的最大值
2 files modified
90 ■■■■■ changed files
docs/策略运转逻辑.md 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 88 ●●●●● patch | view | raw | blame | history
docs/策略运转逻辑.md
@@ -153,7 +153,7 @@
| OPENING 基底 | 同上(OPENING 态) | 双基底 REST 已成交 → 补 baseOpened/entryPrice + 触发队列生成 |
| 条件单幽灵清理 | 同上(ACTIVE 态) | 本地索引中不在交易所 open 列表的单 → 移除 + 修正标志 |
| 累计盈亏 | 同上 | REST 校准(数据源待修,见 §9) |
| 止损覆盖兜底 | 同上 | 有持仓但止损覆盖 < 持仓 → 按原位置重挂全持仓止损 |
| 止损覆盖兜底 | 同上 | 有持仓但止损覆盖 < 持仓 → 按原位置重挂;止损位全部丢失 → 按默认位(±2)整补止损+止盈 |
| 挂单前核实 | 价格驱动挂单瞬间 | REST 确认该方向持仓为 0 才挂(防翻倍) |
---
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);
    }
    /**