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

- 简化空仓止盈计算逻辑,移除多余的基础数量比较和复杂计算
- 将多仓和空仓的止盈数量计算统一为整数除法运算
- 移除注释掉的冗余代码和无用的BigDecimal操作
- 统一止盈订单挂单位置计算方式,提高代码可读性
- 简化网格元素查找和订单检查流程
- 优化代码结构,减少嵌套循环和条件判断
1 files modified
44 ■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 44 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -666,24 +666,11 @@
//                        }
//                );
                // 空仓持仓超过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++) {
                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) {
@@ -702,7 +689,6 @@
                                            finalTpGridId, tpPrice, profitId);
                                }
                        );
                    }
                }
            }
        }
@@ -745,24 +731,11 @@
//                        }
//                );
                // 多仓持仓超过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++) {
                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) {
@@ -781,7 +754,6 @@
                                            finalTpGridId, tpPrice, profitId);
                                }
                        );
                    }
                }
            }
        }