src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -144,10 +144,8 @@ private final int tpTriggerOppositeAddQuantity; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单,false=保持现有逻辑。默认 false */ private final boolean tpPositionReplenishEnabled; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,支持小数。默认 0 */ private final BigDecimal baseTakeProfitPerGrid; /** 止盈网格数量:基础仓位止盈挂单的网格数量。默认 0 */ private final int baseTakeProfitGridCount; /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,余数挂最后一格,每格至少1张。默认 0 */ private final int baseTakeProfitTotalQty; /** 网格绝对步长(shortBaseEntryPrice × gridRate),运行时由队列生成逻辑设置 */ private BigDecimal step; /** 网格元素列表,由队列初始化时同步填充,包含完整的多空仓挂单状态 */ @@ -196,8 +194,7 @@ this.tpTriggerOppositeAddEnabled = builder.tpTriggerOppositeAddEnabled; this.tpTriggerOppositeAddQuantity = builder.tpTriggerOppositeAddQuantity; this.tpPositionReplenishEnabled = builder.tpPositionReplenishEnabled; this.baseTakeProfitPerGrid = builder.baseTakeProfitPerGrid; this.baseTakeProfitGridCount = builder.baseTakeProfitGridCount; this.baseTakeProfitTotalQty = builder.baseTakeProfitTotalQty; } // ==================== REST/WS 地址 ==================== @@ -319,10 +316,8 @@ public int getTpTriggerOppositeAddQuantity() { return tpTriggerOppositeAddQuantity; } /** @return 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单 */ public boolean isTpPositionReplenishEnabled() { return tpPositionReplenishEnabled; } /** @return 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数 */ public BigDecimal getBaseTakeProfitPerGrid() { return baseTakeProfitPerGrid; } /** @return 止盈网格数量:基础仓位止盈挂单的网格数量 */ public int getBaseTakeProfitGridCount() { return baseTakeProfitGridCount; } /** @return 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格 */ public int getBaseTakeProfitTotalQty() { return baseTakeProfitTotalQty; } // ==================== 运行时参数 ==================== @@ -449,10 +444,8 @@ private int tpTriggerOppositeAddQuantity = 0; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单。默认 false */ private boolean tpPositionReplenishEnabled = false; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,默认 0 */ private BigDecimal baseTakeProfitPerGrid = BigDecimal.ZERO; /** 止盈网格数量:基础仓位止盈挂单的网格数量,默认 0 */ private int baseTakeProfitGridCount = 0; /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,默认 0 */ private int baseTakeProfitTotalQty = 0; /** 设置 API Key */ public Builder apiKey(String apiKey) { this.apiKey = apiKey; return this; } @@ -530,10 +523,8 @@ public Builder tpTriggerOppositeAddQuantity(int tpTriggerOppositeAddQuantity) { this.tpTriggerOppositeAddQuantity = tpTriggerOppositeAddQuantity; return this; } /** 设置仓位止盈补仓开关:true=启用,false=禁用 */ public Builder tpPositionReplenishEnabled(boolean tpPositionReplenishEnabled) { this.tpPositionReplenishEnabled = tpPositionReplenishEnabled; return this; } /** 设置单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数 */ public Builder baseTakeProfitPerGrid(BigDecimal baseTakeProfitPerGrid) { this.baseTakeProfitPerGrid = baseTakeProfitPerGrid; return this; } /** 设置止盈网格数量:基础仓位止盈挂单的网格数量 */ public Builder baseTakeProfitGridCount(int baseTakeProfitGridCount) { this.baseTakeProfitGridCount = baseTakeProfitGridCount; return this; } /** 设置原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格 */ public Builder baseTakeProfitTotalQty(int baseTakeProfitTotalQty) { this.baseTakeProfitTotalQty = baseTakeProfitTotalQty; return this; } public GateConfig build() { return new GateConfig(this); src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
@@ -85,10 +85,8 @@ private Integer tpTriggerOppositeAddQuantity; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单,false=保持现有逻辑。null=未设置(默认false) */ private Boolean tpPositionReplenishEnabled; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,支持小数,0=不启用 */ private BigDecimal baseTakeProfitPerGrid; /** 止盈网格数量:基础仓位止盈挂单的网格数量,0=不启用 */ private Integer baseTakeProfitGridCount; /** 原始仓位止盈总数量:平均分配到对手盘止损位前的所有网格,余数挂最后一格,每格至少1张,0=不启用 */ private Integer baseTakeProfitTotalQty; /** * 从 GateConfig 构建 DTO(不暴露 apiSecret)。 @@ -129,8 +127,7 @@ .tpTriggerOppositeAddEnabled(config.isTpTriggerOppositeAddEnabled()) .tpTriggerOppositeAddQuantity(config.getTpTriggerOppositeAddQuantity()) .tpPositionReplenishEnabled(config.isTpPositionReplenishEnabled()) .baseTakeProfitPerGrid(config.getBaseTakeProfitPerGrid()) .baseTakeProfitGridCount(config.getBaseTakeProfitGridCount()) .baseTakeProfitTotalQty(config.getBaseTakeProfitTotalQty()) .build(); } @@ -165,8 +162,7 @@ m.put("tpTriggerOppositeAddEnabled", tpTriggerOppositeAddEnabled); m.put("tpTriggerOppositeAddQuantity", tpTriggerOppositeAddQuantity); m.put("tpPositionReplenishEnabled", tpPositionReplenishEnabled); m.put("baseTakeProfitPerGrid", baseTakeProfitPerGrid); m.put("baseTakeProfitGridCount", baseTakeProfitGridCount); m.put("baseTakeProfitTotalQty", baseTakeProfitTotalQty); return m; } @@ -208,8 +204,7 @@ .tpTriggerOppositeAddEnabled(false) .tpTriggerOppositeAddQuantity(0) .tpPositionReplenishEnabled(false) .baseTakeProfitPerGrid(BigDecimal.ZERO) .baseTakeProfitGridCount(0) .baseTakeProfitTotalQty(0) .build(); } } src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
@@ -119,8 +119,7 @@ .tpTriggerOppositeAddEnabled(nvl(dto.getTpTriggerOppositeAddEnabled(), false)) .tpTriggerOppositeAddQuantity(dto.getTpTriggerOppositeAddQuantity() != null && dto.getTpTriggerOppositeAddQuantity() > 0 ? dto.getTpTriggerOppositeAddQuantity() : 0) .tpPositionReplenishEnabled(nvl(dto.getTpPositionReplenishEnabled(), false)) .baseTakeProfitPerGrid(dto.getBaseTakeProfitPerGrid() != null && dto.getBaseTakeProfitPerGrid().compareTo(BigDecimal.ZERO) > 0 ? dto.getBaseTakeProfitPerGrid() : BigDecimal.ZERO) .baseTakeProfitGridCount(dto.getBaseTakeProfitGridCount() != null && dto.getBaseTakeProfitGridCount() > 0 ? dto.getBaseTakeProfitGridCount() : 0) .baseTakeProfitTotalQty(dto.getBaseTakeProfitTotalQty() != null && dto.getBaseTakeProfitTotalQty() > 0 ? dto.getBaseTakeProfitTotalQty() : 0) .build(); } 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); } /**