src/main/java/com/xcong/excoin/modules/gateApi/GateConfig.java
@@ -142,6 +142,8 @@ private final boolean tpTriggerOppositeAddEnabled; /** 对向加仓挂单数量:止盈成交后在对向挂出的开仓张数,0=不启用。默认 0 */ private final int tpTriggerOppositeAddQuantity; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单,false=保持现有逻辑。默认 false */ private final boolean tpPositionReplenishEnabled; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,支持小数。默认 0 */ private final BigDecimal baseTakeProfitPerGrid; /** 止盈网格数量:基础仓位止盈挂单的网格数量。默认 0 */ @@ -193,6 +195,7 @@ this.shortStopLossPrice = builder.shortStopLossPrice; this.tpTriggerOppositeAddEnabled = builder.tpTriggerOppositeAddEnabled; this.tpTriggerOppositeAddQuantity = builder.tpTriggerOppositeAddQuantity; this.tpPositionReplenishEnabled = builder.tpPositionReplenishEnabled; this.baseTakeProfitPerGrid = builder.baseTakeProfitPerGrid; this.baseTakeProfitGridCount = builder.baseTakeProfitGridCount; } @@ -314,6 +317,8 @@ public boolean isTpTriggerOppositeAddEnabled() { return tpTriggerOppositeAddEnabled; } /** @return 对向加仓挂单数量:止盈成交后在对向挂出的开仓张数 */ public int getTpTriggerOppositeAddQuantity() { return tpTriggerOppositeAddQuantity; } /** @return 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单 */ public boolean isTpPositionReplenishEnabled() { return tpPositionReplenishEnabled; } /** @return 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数 */ public BigDecimal getBaseTakeProfitPerGrid() { return baseTakeProfitPerGrid; } /** @return 止盈网格数量:基础仓位止盈挂单的网格数量 */ @@ -442,6 +447,8 @@ private boolean tpTriggerOppositeAddEnabled = false; /** 对向加仓挂单数量:止盈成交后在对向挂出的开仓张数,默认 0 */ private int tpTriggerOppositeAddQuantity = 0; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单。默认 false */ private boolean tpPositionReplenishEnabled = false; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,默认 0 */ private BigDecimal baseTakeProfitPerGrid = BigDecimal.ZERO; /** 止盈网格数量:基础仓位止盈挂单的网格数量,默认 0 */ @@ -521,6 +528,8 @@ public Builder tpTriggerOppositeAddEnabled(boolean tpTriggerOppositeAddEnabled) { this.tpTriggerOppositeAddEnabled = tpTriggerOppositeAddEnabled; return this; } /** 设置对向加仓挂单数量:止盈成交后在对向挂出的开仓张数 */ 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; } /** 设置止盈网格数量:基础仓位止盈挂单的网格数量 */ src/main/java/com/xcong/excoin/modules/gateApi/GateConfigDTO.java
@@ -83,6 +83,8 @@ private Boolean tpTriggerOppositeAddEnabled; /** 对向加仓挂单数量:止盈成交后在对向挂出的开仓张数,0=不启用 */ private Integer tpTriggerOppositeAddQuantity; /** 仓位止盈补仓开关:true=止盈触发时在对侧止损位挂同方向同张数开仓单,false=保持现有逻辑。null=未设置(默认false) */ private Boolean tpPositionReplenishEnabled; /** 单格止盈张数:基础仓位止盈挂单时每个网格挂的止盈张数,支持小数,0=不启用 */ private BigDecimal baseTakeProfitPerGrid; /** 止盈网格数量:基础仓位止盈挂单的网格数量,0=不启用 */ @@ -126,6 +128,7 @@ .shortStopLossPrice(config.getShortStopLossPrice()) .tpTriggerOppositeAddEnabled(config.isTpTriggerOppositeAddEnabled()) .tpTriggerOppositeAddQuantity(config.getTpTriggerOppositeAddQuantity()) .tpPositionReplenishEnabled(config.isTpPositionReplenishEnabled()) .baseTakeProfitPerGrid(config.getBaseTakeProfitPerGrid()) .baseTakeProfitGridCount(config.getBaseTakeProfitGridCount()) .build(); @@ -161,6 +164,7 @@ m.put("shortStopLossPrice", shortStopLossPrice); m.put("tpTriggerOppositeAddEnabled", tpTriggerOppositeAddEnabled); m.put("tpTriggerOppositeAddQuantity", tpTriggerOppositeAddQuantity); m.put("tpPositionReplenishEnabled", tpPositionReplenishEnabled); m.put("baseTakeProfitPerGrid", baseTakeProfitPerGrid); m.put("baseTakeProfitGridCount", baseTakeProfitGridCount); return m; @@ -203,6 +207,7 @@ .shortStopLossPrice(null) .tpTriggerOppositeAddEnabled(false) .tpTriggerOppositeAddQuantity(0) .tpPositionReplenishEnabled(false) .baseTakeProfitPerGrid(BigDecimal.ZERO) .baseTakeProfitGridCount(0) .build(); src/main/java/com/xcong/excoin/modules/gateApi/GateConfigPersistenceService.java
@@ -118,6 +118,7 @@ .shortStopLossPrice(dto.getShortStopLossPrice()) .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) .build(); src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -921,6 +921,8 @@ * @param orderId 止盈单 ID */ private void handleLongTakeProfitTriggered(GridElement tpElem, String orderId) { // 清理前读取本次止盈单张数(仓位止盈补仓用;清理后该记录会被移除) Integer tpSize = tpElem.getLongTakeProfitOrderSize(orderId); // 清理前判断:当前是否为该方向最后一个止盈单(最后一个不触发对向加仓) boolean isLastTp = GridElement.getLongTakeProfitCount() <= 1; // 清理止盈单状态(移除已成交的这笔止盈单) @@ -930,6 +932,11 @@ // 止盈依次上移:先取消之前挂的对向空开仓单,再挂新单 cancelTpOppositeShortEntries(); // 仓位止盈补仓:在对侧(空仓)止损位挂同方向(多仓)开仓单,张数 = 本次止盈单张数 if (config.isTpPositionReplenishEnabled()) { replenishAfterTp(true, tpSize); } if (!config.isTpTriggerOppositeAddEnabled()) { return; @@ -959,6 +966,8 @@ * @param orderId 止盈单 ID */ private void handleShortTakeProfitTriggered(GridElement tpElem, String orderId) { // 清理前读取本次止盈单张数(仓位止盈补仓用;清理后该记录会被移除) Integer tpSize = tpElem.getShortTakeProfitOrderSize(orderId); // 清理前判断:当前是否为该方向最后一个止盈单(最后一个不触发对向加仓) boolean isLastTp = GridElement.getShortTakeProfitCount() <= 1; // 清理止盈单状态(移除已成交的这笔止盈单) @@ -968,6 +977,11 @@ // 止盈依次下移:先取消之前挂的对向多开仓单,再挂新单 cancelTpOppositeLongEntries(); // 仓位止盈补仓:在对侧(多仓)止损位挂同方向(空仓)开仓单,张数 = 本次止盈单张数 if (config.isTpPositionReplenishEnabled()) { replenishAfterTp(false, tpSize); } if (!config.isTpTriggerOppositeAddEnabled()) { return; @@ -988,6 +1002,41 @@ return; } placeOppositeAddLongEntry(target, tpElem, qty); } /** * 仓位止盈补仓:任意方向止盈触发时,在对侧止损位挂与止盈方向一致、张数 = 本次止盈单张数的条件开仓单。 * <p>开仓单走标准入场流程(placeEntryOrderWithPreFlag 预置标记),成交后进入 ENTRY_FILLED 常规分支, * extend 会按最新全持仓重挂止损,保证补仓仓位有完整止损覆盖。</p> * * @param isLong true=多仓止盈(挂多仓开仓单),false=空仓止盈(挂空仓开仓单) * @param tpSize 本次触发止盈单的张数(null 或 <=0 则跳过) */ private void replenishAfterTp(boolean isLong, Integer tpSize) { if (tpSize == null || tpSize <= 0) { log.warn("[Gate] 仓位止盈补仓跳过: 止盈单张数无效, isLong:{}, tpSize:{}", isLong, tpSize); return; } // 对侧止损位:多仓止盈 → 空仓第一止损位;空仓止盈 → 多仓第一止损位 GridElement slElem = isLong ? findFirstShortStopLossGrid() : findFirstLongStopLossGrid(); if (slElem == null) { log.warn("[Gate] 仓位止盈补仓: 未找到对侧止损位, isLong:{}", isLong); return; } BigDecimal triggerPrice = slElem.getGridPrice(); // 方向与止盈方向一致:多仓止盈 → 多开仓(价格≥触发价 NUMBER_1);空仓止盈 → 空开仓(价格≤触发价 NUMBER_2) FuturesPriceTrigger.RuleEnum rule = isLong ? FuturesPriceTrigger.RuleEnum.NUMBER_1 : FuturesPriceTrigger.RuleEnum.NUMBER_2; String size = isLong ? String.valueOf(tpSize) : negate(String.valueOf(tpSize)); // 记录本网格该方向的开仓张数(供 ENTRY_FILLED 分支读取成交张数) if (isLong) { slElem.getLongTraderParam().setQuantity(String.valueOf(tpSize)); } else { slElem.getShortTraderParam().setQuantity(String.valueOf(tpSize)); } placeEntryOrderWithPreFlag(slElem, isLong, triggerPrice, rule, size); log.info("[Gate] 仓位止盈补仓: {}止盈触发, 对侧止损位 gridId:{}, 挂{}开仓{}张", isLong ? "多仓" : "空仓", slElem.getId(), isLong ? "多" : "空", tpSize); } /** @@ -1478,12 +1527,17 @@ private void longTakeProfitTraderIdParam( GridElement baseElement,String profitId, boolean flag ) { longTakeProfitTraderIdParam(baseElement, profitId, flag, 0); } private void longTakeProfitTraderIdParam( GridElement baseElement,String profitId, boolean flag, int size ) { TraderParam tp = baseElement.getLongTraderParam(); if (flag && profitId != null) { // 挂单成功:追加到止盈单列表(支持基础止盈 + 超额止盈并行挂单) // 挂单成功:追加到止盈单列表(支持基础止盈 + 超额止盈并行挂单),并记录该单张数 tp.setTakeProfitOrderId(profitId); tp.setTakeProfitPlaced(true); baseElement.addLongTakeProfitOrderId(profitId); baseElement.addLongTakeProfitOrderId(profitId, size); } else { // 清理:profitId 非空移除指定止盈单,为空清空该网格全部止盈单 tp.setTakeProfitPlaced(false); @@ -1502,12 +1556,17 @@ private void shortTakeProfitTraderIdParam( GridElement baseElement,String profitId, boolean flag ) { shortTakeProfitTraderIdParam(baseElement, profitId, flag, 0); } private void shortTakeProfitTraderIdParam( GridElement baseElement,String profitId, boolean flag, int size ) { TraderParam tp = baseElement.getShortTraderParam(); if (flag && profitId != null) { // 挂单成功:追加到止盈单列表(支持基础止盈 + 超额止盈并行挂单) // 挂单成功:追加到止盈单列表(支持基础止盈 + 超额止盈并行挂单),并记录该单张数 tp.setTakeProfitOrderId(profitId); tp.setTakeProfitPlaced(true); baseElement.addShortTakeProfitOrderId(profitId); baseElement.addShortTakeProfitOrderId(profitId, size); } else { // 清理:profitId 非空移除指定止盈单,为空清空该网格全部止盈单 tp.setTakeProfitPlaced(false); @@ -3049,9 +3108,9 @@ executor.placeTakeProfit(triggerPrice, rule, orderType, size, profitId -> { if (isLong) { longTakeProfitTraderIdParam(tpElem, profitId, true); longTakeProfitTraderIdParam(tpElem, profitId, true, excessQty); } else { shortTakeProfitTraderIdParam(tpElem, profitId, true); shortTakeProfitTraderIdParam(tpElem, profitId, true, excessQty); } log.info("[Gate] {}止盈挂单, gridId:{}, 触发价:{}, 数量:{}, takeProfitId:{}", isLong ? "多仓" : "空仓", finalTpGridId, triggerPrice, finalSize, profitId); @@ -3139,6 +3198,7 @@ if (qty <= 0) { continue; } final int fqty = qty; final GridElement elem = gridList.get(i); final int gid = elem.getId(); final BigDecimal triggerPrice = elem.getGridPrice(); @@ -3149,9 +3209,9 @@ executor.placeTakeProfit(triggerPrice, rule, orderType, size, profitId -> { if (isLong) { longTakeProfitTraderIdParam(elem, profitId, true); longTakeProfitTraderIdParam(elem, profitId, true, fqty); } else { shortTakeProfitTraderIdParam(elem, profitId, true); shortTakeProfitTraderIdParam(elem, profitId, true, fqty); } log.info("[Gate] 分散止盈 {} gridId:{}, 触发价:{}, 数量:{}, tpId:{}", isLong ? "多仓" : "空仓", gid, triggerPrice, size, profitId); @@ -3211,6 +3271,7 @@ break; } BigDecimal qty = perGrid.min(remaining); final int fqty = qty.intValue(); final GridElement fe = elem; final int gid = id; final BigDecimal triggerPrice = elem.getGridPrice(); @@ -3221,9 +3282,9 @@ executor.placeTakeProfit(triggerPrice, rule, orderType, size, profitId -> { if (isLong) { longTakeProfitTraderIdParam(fe, profitId, true); longTakeProfitTraderIdParam(fe, profitId, true, fqty); } else { shortTakeProfitTraderIdParam(fe, profitId, true); shortTakeProfitTraderIdParam(fe, profitId, true, fqty); } log.info("[Gate] 基础止盈 {} gridId:{}, 触发价:{}, 数量:{}, tpId:{}", isLong ? "多仓" : "空仓", gid, triggerPrice, size, profitId); @@ -3250,9 +3311,9 @@ executor.placeTakeProfit(triggerPrice, rule, orderType, size, profitId -> { if (isLong) { longTakeProfitTraderIdParam(tpElem, profitId, true); longTakeProfitTraderIdParam(tpElem, profitId, true, qty); } else { shortTakeProfitTraderIdParam(tpElem, profitId, true); shortTakeProfitTraderIdParam(tpElem, profitId, true, qty); } log.info("[Gate] 止损{}→对手超额止盈 gridId:{}, 量:{}, tpId:{}", times, gridId, qty, profitId); } @@ -3277,8 +3338,8 @@ String size = isLong ? negate(String.valueOf(tpQty)) : String.valueOf(tpQty); executor.placeTakeProfit(triggerPrice, rule, orderType, size, profitId -> { if (isLong) longTakeProfitTraderIdParam(tpElem, profitId, true); else shortTakeProfitTraderIdParam(tpElem, profitId, true); if (isLong) longTakeProfitTraderIdParam(tpElem, profitId, true, tpQty); else shortTakeProfitTraderIdParam(tpElem, profitId, true, tpQty); log.info("[Gate] 止损次数{}→对手{}止盈 gridId:{}, 量:{}, tpId:{}", times, isLong ? "多仓" : "空仓", tpGridId, size, profitId); } src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
@@ -87,6 +87,10 @@ private List<String> longTakeProfitOrderIds = new ArrayList<>(); /** 空仓止盈订单 ID 列表(支持一网格多笔止盈单:基础止盈 + 超额止盈并行挂单) */ private List<String> shortTakeProfitOrderIds = new ArrayList<>(); /** 多仓止盈订单 ID → 张数(仓位止盈补仓时取本次止盈单张数) */ private final Map<String, Integer> longTakeProfitOrderSizes = new ConcurrentHashMap<>(); /** 空仓止盈订单 ID → 张数 */ private final Map<String, Integer> shortTakeProfitOrderSizes = new ConcurrentHashMap<>(); /** 多仓止损订单 ID 列表(支持一个网格多个止损单) */ private List<String> longStopLossOrderIds = new ArrayList<>(); /** 空仓止损订单 ID 列表(支持一个网格多个止损单) */ @@ -489,14 +493,30 @@ /** @return 多仓止盈订单 ID 列表 */ public List<String> getLongTakeProfitOrderIds() { return longTakeProfitOrderIds; } /** 添加多仓止盈订单 ID */ public void addLongTakeProfitOrderId(String orderId) { if (orderId != null) longTakeProfitOrderIds.add(orderId); } /** 移除多仓止盈订单 ID */ public boolean removeLongTakeProfitOrderId(String orderId) { return longTakeProfitOrderIds.remove(orderId); } /** 清空多仓止盈订单 ID */ public void clearLongTakeProfitOrderIds() { longTakeProfitOrderIds.clear(); } /** 添加多仓止盈订单 ID(不记录张数,仅兼容旧调用方) */ public void addLongTakeProfitOrderId(String orderId) { addLongTakeProfitOrderId(orderId, 0); } /** 添加多仓止盈订单 ID 并记录该单张数 */ public void addLongTakeProfitOrderId(String orderId, int size) { if (orderId == null) { return; } longTakeProfitOrderIds.add(orderId); longTakeProfitOrderSizes.put(orderId, size); } /** 移除多仓止盈订单 ID(同步清理张数记录) */ public boolean removeLongTakeProfitOrderId(String orderId) { longTakeProfitOrderSizes.remove(orderId); return longTakeProfitOrderIds.remove(orderId); } /** 清空多仓止盈订单 ID(同步清理张数记录) */ public void clearLongTakeProfitOrderIds() { longTakeProfitOrderIds.clear(); longTakeProfitOrderSizes.clear(); } /** @return 是否有止盈单 */ public boolean hasLongTakeProfitOrders() { return !longTakeProfitOrderIds.isEmpty(); } /** @return 指定多仓止盈单的张数,未记录返回 null */ public Integer getLongTakeProfitOrderSize(String orderId) { return longTakeProfitOrderSizes.get(orderId); } /** @deprecated 返回最后一个止盈订单ID,用于单订单场景的兼容 */ @Deprecated public String getLongTakeProfitOrderId() { return longTakeProfitOrderIds.isEmpty() ? null : longTakeProfitOrderIds.get(longTakeProfitOrderIds.size() - 1); } @@ -508,14 +528,30 @@ /** @return 空仓止盈订单 ID 列表 */ public List<String> getShortTakeProfitOrderIds() { return shortTakeProfitOrderIds; } /** 添加空仓止盈订单 ID */ public void addShortTakeProfitOrderId(String orderId) { if (orderId != null) shortTakeProfitOrderIds.add(orderId); } /** 移除空仓止盈订单 ID */ public boolean removeShortTakeProfitOrderId(String orderId) { return shortTakeProfitOrderIds.remove(orderId); } /** 清空空仓止盈订单 ID */ public void clearShortTakeProfitOrderIds() { shortTakeProfitOrderIds.clear(); } /** 添加空仓止盈订单 ID(不记录张数,仅兼容旧调用方) */ public void addShortTakeProfitOrderId(String orderId) { addShortTakeProfitOrderId(orderId, 0); } /** 添加空仓止盈订单 ID 并记录该单张数 */ public void addShortTakeProfitOrderId(String orderId, int size) { if (orderId == null) { return; } shortTakeProfitOrderIds.add(orderId); shortTakeProfitOrderSizes.put(orderId, size); } /** 移除空仓止盈订单 ID(同步清理张数记录) */ public boolean removeShortTakeProfitOrderId(String orderId) { shortTakeProfitOrderSizes.remove(orderId); return shortTakeProfitOrderIds.remove(orderId); } /** 清空空仓止盈订单 ID(同步清理张数记录) */ public void clearShortTakeProfitOrderIds() { shortTakeProfitOrderIds.clear(); shortTakeProfitOrderSizes.clear(); } /** @return 是否有止盈单 */ public boolean hasShortTakeProfitOrders() { return !shortTakeProfitOrderIds.isEmpty(); } /** @return 指定空仓止盈单的张数,未记录返回 null */ public Integer getShortTakeProfitOrderSize(String orderId) { return shortTakeProfitOrderSizes.get(orderId); } /** @deprecated 返回最后一个止盈订单ID,用于单订单场景的兼容 */ @Deprecated public String getShortTakeProfitOrderId() { return shortTakeProfitOrderIds.isEmpty() ? null : shortTakeProfitOrderIds.get(shortTakeProfitOrderIds.size() - 1); }