Administrator
2026-07-14 66e08b5ac27b04748f6083883210c9af95791bee
refactor(gateApi): 优化网格交易止盈逻辑实现

- 简化空仓止盈计算逻辑,移除多余的基础数量比较和复杂计算
- 将多仓和空仓的止盈数量计算统一为整数除法运算
- 移除注释掉的冗余代码和无用的BigDecimal操作
- 统一止盈订单挂单位置计算方式,提高代码可读性
- 简化网格元素查找和订单检查流程
- 优化代码结构,减少嵌套循环和条件判断
1 files modified
112 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 112 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -666,43 +666,29 @@
//                        }
//                );
                // 空仓持仓超过baseQuantity时,先找多仓第一个止损位置,从该位置向下挂止盈(间隔=1)
                BigDecimal shortBaseQty = new BigDecimal(config.getBaseQuantity());
                BigDecimal shortGridQty = new BigDecimal(config.getQuantity());
                if (BigDecimal.valueOf(posSize).compareTo(shortBaseQty) > 0) {
                    BigDecimal shortExcess = BigDecimal.valueOf(posSize).subtract(shortBaseQty);
                    int shortExcessCount = shortExcess.divide(shortGridQty, 0, RoundingMode.DOWN).intValue();
                // 空仓止盈:每1张持仓对应1个止盈位,从entry网格向下逐个挂
                int shortGridQty = Integer.parseInt(config.getQuantity());
                int shortTpCount = posSize / shortGridQty;
//                    // 找多仓第一个(最近的)止损位置
//                    int firstLongSlId = 0;
//                    for (GridElement e : config.getGridElements()) {
//                        if (e.hasLongStopLossOrders()) {
//                            if (firstLongSlId == 0 || e.getId() > firstLongSlId) {
//                                firstLongSlId = e.getId();
//                            }
//                        }
//                    }
                    for (int i = 0; i < shortExcessCount; i++) {
                        int tpGridId = shortGridElement.getId() - 2 * (i + 1);
                        GridElement tpElem = GridElement.findById(tpGridId);
                        if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) {
                            continue;
                        }
                        BigDecimal tpPrice = tpElem.getGridPrice();
                        int finalTpGridId = tpGridId;
                        executor.placeTakeProfit(
                                tpPrice,
                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
                                ORDER_TYPE_CLOSE_SHORT,
                                config.getQuantity(),
                                profitId -> {
                                    shortTakeProfitTraderIdParam(tpElem, profitId, true);
                                    log.info("[Gate] 空仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}",
                                            finalTpGridId, tpPrice, profitId);
                                }
                        );
                for (int i = 0; i < shortTpCount; i++) {
                    int tpGridId = shortGridElement.getId() - 2 * (i + 1);
                    GridElement tpElem = GridElement.findById(tpGridId);
                    if (tpElem == null || tpElem.getShortTakeProfitOrderId() != null) {
                        continue;
                    }
                    BigDecimal tpPrice = tpElem.getGridPrice();
                    int finalTpGridId = tpGridId;
                    executor.placeTakeProfit(
                            tpPrice,
                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
                            ORDER_TYPE_CLOSE_SHORT,
                            config.getQuantity(),
                            profitId -> {
                                shortTakeProfitTraderIdParam(tpElem, profitId, true);
                                log.info("[Gate] 空仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}",
                                        finalTpGridId, tpPrice, profitId);
                            }
                    );
                }
            }
        }
@@ -745,43 +731,29 @@
//                        }
//                );
                // 多仓持仓超过baseQuantity时,先找空仓第一个止损位置,从该位置向上挂止盈(间隔=1)
                BigDecimal longBaseQty = new BigDecimal(config.getBaseQuantity());
                BigDecimal longGridQty = new BigDecimal(config.getQuantity());
                if (BigDecimal.valueOf(posSize).compareTo(longBaseQty) > 0) {
                    BigDecimal longExcess = BigDecimal.valueOf(posSize).subtract(longBaseQty);
                    int longExcessCount = longExcess.divide(longGridQty, 0, RoundingMode.DOWN).intValue();
                // 多仓止盈:每1张持仓对应1个止盈位,从entry网格向上逐个挂
                int longGridQty = Integer.parseInt(config.getQuantity());
                int longTpCount = posSize / longGridQty;
//                    // 找空仓第一个(最近的)止损位置
//                    int firstShortSlId = 0;
//                    for (GridElement e : config.getGridElements()) {
//                        if (e.hasShortStopLossOrders()) {
//                            if (firstShortSlId == 0 || e.getId() < firstShortSlId) {
//                                firstShortSlId = e.getId();
//                            }
//                        }
//                    }
                    for (int i = 0; i < longExcessCount; i++) {
                        int tpGridId = longGridElement.getId() + 2 * (i + 1);
                        GridElement tpElem = GridElement.findById(tpGridId);
                        if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) {
                            continue;
                        }
                        BigDecimal tpPrice = tpElem.getGridPrice();
                        int finalTpGridId = tpGridId;
                        executor.placeTakeProfit(
                                tpPrice,
                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
                                ORDER_TYPE_CLOSE_LONG,
                                negate(config.getQuantity()),
                                profitId -> {
                                    longTakeProfitTraderIdParam(tpElem, profitId, true);
                                    log.info("[Gate] 多仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}",
                                            finalTpGridId, tpPrice, profitId);
                                }
                        );
                for (int i = 0; i < longTpCount; i++) {
                    int tpGridId = longGridElement.getId() + 2 * (i + 1);
                    GridElement tpElem = GridElement.findById(tpGridId);
                    if (tpElem == null || tpElem.getLongTakeProfitOrderId() != null) {
                        continue;
                    }
                    BigDecimal tpPrice = tpElem.getGridPrice();
                    int finalTpGridId = tpGridId;
                    executor.placeTakeProfit(
                            tpPrice,
                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
                            ORDER_TYPE_CLOSE_LONG,
                            negate(config.getQuantity()),
                            profitId -> {
                                longTakeProfitTraderIdParam(tpElem, profitId, true);
                                log.info("[Gate] 多仓止盈挂单, gridId:{}, 触发价:{}, takeProfitId:{}",
                                        finalTpGridId, tpPrice, profitId);
                            }
                    );
                }
            }
        }