Administrator
3 days ago 617ff2b79c2a4b6c4ed3f2984115bdd830fa12c0
fix(gateApi): 解决网格交易重复挂单问题

- 添加向下检查逻辑防止多单在更低价格网格重复挂单
- 添加向上检查逻辑防止空单在更高价格网格重复挂单
- 优化挂单条件判断避免价格波动导致的重复操作
- 增加详细的日志记录便于监控挂单状态
- 修复仓位归零时的订单冲突处理机制
1 files modified
34 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 34 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1133,6 +1133,21 @@
                    if (newEntryGrid != null) {
                        String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_SHORT), shortPositionSize.intValue()) + Integer.parseInt(config.getQuantity()));
                        // 向下检查是否已有多单挂在更低价格网格,有则跳过(防止价格回升后重复挂单)
                        boolean hasLongOrderBelow = false;
                        GridElement checkDownCursor = GridElement.findById(newEntryGrid.getDownId());
                        while (checkDownCursor != null) {
                            if (checkDownCursor.isHasLongOrder()) {
                                hasLongOrderBelow = true;
                                log.info("[Gate] 多仓仓位归零 gridId:{}, 下方gridId:{}已有多单, 跳过本次挂单",
                                        newEntryGrid.getId(), checkDownCursor.getId());
                                break;
                            }
                            Integer nextDownId = checkDownCursor.getDownId();
                            checkDownCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
                        }
                        // 向上遍历取消所有遗留多单(跳过基础入场网格0)
                        GridElement cancelCursor = GridElement.findById(newEntryGrid.getUpId());
                        while (cancelCursor != null) {
@@ -1149,7 +1164,7 @@
                            cancelCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
                        }
//                        log.info("[Gate-2] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
                        if (!newEntryGrid.isHasLongOrder()) {
                        if (!newEntryGrid.isHasLongOrder() && !hasLongOrderBelow) {
                            BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                            String size = quantity;
                            log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}基础张多单",
@@ -1189,6 +1204,21 @@
//                        String quantity = String.valueOf((accumulatedShortLossCount + 1) * Integer.parseInt(config.getQuantity()));
                        String quantity = String.valueOf(Math.max(queryPositionSize(Position.ModeEnum.DUAL_LONG), longPositionSize.intValue()) + Integer.parseInt(config.getQuantity()));
                        // 向上检查是否已有空单挂在更高价格网格,有则跳过(防止价格回落后重复挂单)
                        boolean hasShortOrderAbove = false;
                        GridElement checkUpCursor = GridElement.findById(newEntryGrid.getUpId());
                        while (checkUpCursor != null) {
                            if (checkUpCursor.isHasShortOrder()) {
                                hasShortOrderAbove = true;
                                log.info("[Gate] 空仓仓位归零 gridId:{}, 上方gridId:{}已有空单, 跳过本次挂单",
                                        newEntryGrid.getId(), checkUpCursor.getId());
                                break;
                            }
                            Integer nextUpId = checkUpCursor.getUpId();
                            checkUpCursor = nextUpId != null ? GridElement.findById(nextUpId) : null;
                        }
                        // 向下遍历取消所有遗留空单(跳过基础入场网格0)
                        GridElement cancelCursor = GridElement.findById(newEntryGrid.getDownId());
                        while (cancelCursor != null) {
@@ -1205,7 +1235,7 @@
                            cancelCursor = nextDownId != null ? GridElement.findById(nextDownId) : null;
                        }
//                        log.info("[Gate-4] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
                        if (!newEntryGrid.isHasShortOrder()){
                        if (!newEntryGrid.isHasShortOrder() && !hasShortOrderAbove){
                            BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                            String size = quantity;
                            log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张多单",