Administrator
2026-06-09 ae09d21efc6c4ae0802ea9d4af2e419e2e61c2b6
```
feat(gateApi): 添加网格交易最大持仓限制功能

- 实现多仓最大持仓限制检查和订单数量截断逻辑
- 实现空仓最大持仓限制检查和订单数量截断逻辑
- 添加持仓超限时的日志记录和警告信息
- 防止网格交易超出预设的最大持仓规模限制
```
1 files modified
36 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 36 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1309,6 +1309,24 @@
        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
        longEntryQty++;
        int entryQty = longEntryQty;
        // 最大持仓限制:已持仓+本次挂单 ≤ maxPositionSize
        int maxPos = config.getMaxPositionSize();
        if (maxPos > 0) {
            int currentPos = longPositionSize.intValue();
            int maxAllowed = maxPos - currentPos;
            if (maxAllowed <= 0) {
                log.warn("[Gate] 多仓止损触发 gridId:{}, 已达最大持仓{},跳过挂单", gridId, maxPos);
                longEntryQty = 1;
                return;
            }
            if (entryQty > maxAllowed) {
                log.info("[Gate] 多仓止损触发 gridId:{}, 挂单{}张超限, 截断为{}张", gridId, entryQty, maxAllowed);
                entryQty = maxAllowed;
                longEntryQty = 1;
            }
        }
        String size = new BigDecimal(String.valueOf(entryQty)).multiply(new BigDecimal(config.getQuantity())).toString();
        log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}基础张多单(计数器:{}, size:{})",
                gridId, newEntryGridId, entryQty, longEntryQty, size);
@@ -1348,6 +1366,24 @@
        BigDecimal triggerPrice = newEntryGrid.getGridPrice();
        shortEntryQty++;
        int entryQty = shortEntryQty;
        // 最大持仓限制:已持仓+本次挂单 ≤ maxPositionSize
        int maxPos = config.getMaxPositionSize();
        if (maxPos > 0) {
            int currentPos = shortPositionSize.intValue();
            int maxAllowed = maxPos - currentPos;
            if (maxAllowed <= 0) {
                log.warn("[Gate] 空仓止损触发 gridId:{}, 已达最大持仓{},跳过挂单", gridId, maxPos);
                shortEntryQty = 1;
                return;
            }
            if (entryQty > maxAllowed) {
                log.info("[Gate] 空仓止损触发 gridId:{}, 挂单{}张超限, 截断为{}张", gridId, entryQty, maxAllowed);
                entryQty = maxAllowed;
                shortEntryQty = 1;
            }
        }
        String size = new BigDecimal(String.valueOf(entryQty)).multiply(new BigDecimal(config.getQuantity())).toString();
        log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}基础张空单(计数器:{}, size:{})",
                gridId, newEntryGridId, entryQty, shortEntryQty, size);