Administrator
2026-11-13 a7e7e2b4b91d79d0c65a726fc43282faf0f02b52
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -3732,31 +3732,35 @@
    }
    /**
     * 基础止盈:对基础仓位(baseQuantity)也挂止盈单,与超额止盈并行。
     * 基础止盈:对基础仓位挂止盈单,与超额止盈并行(原始仓位止盈挂单优化)。
     *
     * <p>规则:从入场网格下一格(entryGridId ± 1)开始,每格 baseTakeProfitPerGrid 张,
     * 最多挂 baseTakeProfitGridCount 格,挂到止损位前 2 格截止(即对手盘仓位线)。
     * <p>规则:
     * <ol>
     *   <li>总数量 = {@code baseTakeProfitTotalQty}(原始仓位止盈总数量);</li>
     *   <li>网格范围:从入场网格下一格(entryGridId ± 1,起始位置不变)
     *       一直挂到<b>对手盘止损位的前一个格子</b>;</li>
     *   <li>每格数量 = 总数量 ÷ 网格数平均分配,余数挂在最后一个格子;
     *       每格最少 1 张,总量不够网格数时从起始格开始各挂 1 张(挂到哪算哪)。</li>
     * </ol>
     *
     * <p>边界:累计张数到 min(baseQuantity, 当前持仓) 即停(张数不足则最后一格挂剩余 = "挂几个算几个");
     * 网格不足时挂到停止位置结束。
     * <p>张数硬上限 = min(设置总量, baseQuantity, 当前持仓),避免持仓不足时超挂。
     *
     * @param posSize     当前总持仓张数
     * @param isLong      true=多仓止盈,false=空仓止盈
     * @param entryGridId 入场网格 ID
     */
    private void placeBaseTakeProfit(int posSize, boolean isLong, int entryGridId) {
        BigDecimal perGrid = config.getBaseTakeProfitPerGrid();
        int gridCount = config.getBaseTakeProfitGridCount();
        if (perGrid == null || perGrid.compareTo(BigDecimal.ZERO) <= 0 || gridCount <= 0) {
        int totalQty = config.getBaseTakeProfitTotalQty();
        if (totalQty <= 0) {
            return;
        }
        int baseQty = Integer.parseInt(config.getBaseQuantity());
        if (baseQty <= 0) {
            return;
        }
        // 张数硬上限 = min(基础张数, 当前持仓),避免持仓不足 baseQuantity 时超挂
        int maxQty = Math.min(baseQty, posSize);
        if (maxQty <= 0) {
        // 张数硬上限 = min(设置总量, 基础张数, 当前持仓),避免持仓不足 baseQuantity 时超挂
        totalQty = Math.min(totalQty, Math.min(baseQty, posSize));
        if (totalQty <= 0) {
            return;
        }
@@ -3767,13 +3771,12 @@
        }
        int dir = isLong ? 1 : -1;
        int startId = entryGridId + dir;
        // 分配范围到止损位前 2 格截止(即对手盘仓位线)
        int endId = endElem.getId() - 2 * dir;
        // 一直挂到对手盘止损位的前一个格子
        int endId = endElem.getId() - dir;
        BigDecimal remaining = BigDecimal.valueOf(maxQty);
        int placed = 0;
        int id = startId;
        while (placed < gridCount && remaining.compareTo(BigDecimal.ZERO) > 0) {
        // 收集范围内所有网格(安全上限防异常数据长循环)
        List<GridElement> gridList = new ArrayList<>();
        for (int id = startId; ; id += dir) {
            if (isLong ? id > endId : id < endId) {
                break;
            }
@@ -3781,12 +3784,36 @@
            if (elem == null) {
                break;
            }
            BigDecimal qty = perGrid.min(remaining);
            final int fqty = qty.intValue();
            gridList.add(elem);
            if (gridList.size() > config.getGridQueueSize() * 2 + 4) {
                break;
            }
        }
        if (gridList.isEmpty()) {
            return;
        }
        int n = gridList.size();
        int perGrid = totalQty / n;
        int remainder = totalQty % n;
        for (int i = 0; i < n; i++) {
            int qty;
            if (totalQty < n) {
                // 总量不够铺满:每格至少 1 张,从起始格开始挂 totalQty 格,挂到哪算哪
                qty = (i < totalQty) ? 1 : 0;
            } else {
                // 平均分配,余数挂在最后一个格子(最靠近对手盘止损位的那格)
                qty = perGrid + (i == n - 1 ? remainder : 0);
            }
            if (qty <= 0) {
                continue;
            }
            GridElement elem = gridList.get(i);
            final int fqty = qty;
            final GridElement fe = elem;
            final int gid = id;
            final int gid = elem.getId();
            final BigDecimal triggerPrice = elem.getGridPrice();
            final String size = isLong ? negate(qty.toPlainString()) : qty.toPlainString();
            final String size = isLong ? negate(String.valueOf(qty)) : String.valueOf(qty);
            final String orderType = isLong ? ORDER_TYPE_CLOSE_LONG : ORDER_TYPE_CLOSE_SHORT;
            final FuturesPriceTrigger.RuleEnum rule = isLong ? FuturesPriceTrigger.RuleEnum.NUMBER_1
                    : FuturesPriceTrigger.RuleEnum.NUMBER_2;
@@ -3801,12 +3828,9 @@
                                isLong ? "多仓" : "空仓", gid, triggerPrice, size, profitId);
                    }
            );
            remaining = remaining.subtract(qty);
            placed++;
            id += dir;
        }
        log.info("[Gate] 基础止盈 {} 挂单完成, 入场gridId:{}, 挂{}格, 剩余{}张",
                isLong ? "多仓" : "空仓", entryGridId, placed, remaining);
        log.info("[Gate] 基础止盈 {} 挂单完成, 入场gridId:{}, 网格数:{}, 总量:{}张, 每格:{}张, 余数:{}张",
                isLong ? "多仓" : "空仓", entryGridId, n, totalQty, perGrid, remainder);
    }
    /**