Administrator
2026-06-09 3d9550bc49d269d0721cc0df039048049800c830
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -136,6 +136,11 @@
    /** 多头是否活跃(有仓位) */
    private volatile boolean longActive = false;
    /** 价格驱动 空头是否挂单 */
    private volatile boolean shortEntryActive = false;
    /** 价格驱动 多头是否挂单 */
    private volatile boolean longEntryActive = false;
    private volatile BigDecimal lastKlinePrice;
    private volatile BigDecimal markPrice = BigDecimal.ZERO;
    private volatile BigDecimal cumulativePnl = BigDecimal.ZERO;
@@ -411,14 +416,18 @@
        checkProfitAndReset();
        if (longActive == false &&
                        longPositionSize.compareTo(BigDecimal.ZERO) == 0){
        if (state == StrategyState.ACTIVE &&
                longActive == false &&
                    longPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
                        longEntryActive){
            processShortGrid(closePrice);
        }
        if (shortActive == false &&
                        shortPositionSize.compareTo(BigDecimal.ZERO) == 0){
        if (state == StrategyState.ACTIVE &&
                shortActive == false &&
                        shortPositionSize.compareTo(BigDecimal.ZERO) == 0 &&
                             shortEntryActive){
            processLongGrid(closePrice);
        }
    }
@@ -476,6 +485,7 @@
//                    log.info("[Gate] 多仓持仓归零,重置策略");
//                    handlePositionZeroAndReset("多仓");
                }
                longEntryActive = true;
                longActive = false;
                longPositionSize = BigDecimal.ZERO;
            }
@@ -499,6 +509,7 @@
//                    log.info("[Gate] 空仓持仓归零,重置策略");
//                    handlePositionZeroAndReset("空仓");
                }
                shortEntryActive = true;
                shortActive = false;
                shortPositionSize = BigDecimal.ZERO;
            }
@@ -1197,7 +1208,7 @@
        if (BigDecimal.ZERO.compareTo( matched) == 0) {
            return;
        }
        log.info("[Gate] 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
        log.info("[Gate] 多仓仓位归零 空仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
        GridElement matchedUpGridElement = GridElement.findByPrice(matched);
@@ -1216,6 +1227,7 @@
                placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
                        FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
                longEntryActive = false;
                /**
                 * 看是否有多仓挂单,有就取消
@@ -1245,7 +1257,7 @@
            return;
        }
        log.info("[Gate] 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
        log.info("[Gate] 空仓仓位归零 多仓队列触发, 匹配:{},当前价:{}", matched, currentPrice);
        GridElement matchedUpGridElement = GridElement.findByPrice(matched);
@@ -1263,6 +1275,8 @@
                newEntryGrid.getShortTraderParam().setQuantity(size);
                placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
                        FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
                shortEntryActive = false;
                /**
                 * 看是否有空仓挂单,有就取消
@@ -1307,6 +1321,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);
@@ -1346,6 +1378,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);