Administrator
2026-06-01 56a48823bd0ebf96919fe27201b73ab96e800c33
fix(gateApi): 修复网格交易策略状态管理问题

- 在盈亏达标时正确设置策略状态为STOPPED
- 在持仓归零重置时设置策略状态为STOPPED
- 确保策略在达到目标后正确停止并重新启动
2 files modified
69 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java 11 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java 58 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -353,6 +353,8 @@
            BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
            log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}",
                    cumulativePnl, unrealizedPnl, totalPnl);
            startGrid();
            return;
        }
@@ -559,19 +561,12 @@
        BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
        log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}",
                cumulativePnl, unrealizedPnl, totalPnl);
        if (totalPnl.compareTo(config.getOverallTp()) >= 0) {
            log.info("[Gate] 已达止盈目标(合计{})→已停止, 已实现:{}, 未实现:{}",
                    totalPnl, cumulativePnl, unrealizedPnl);
            state = StrategyState.STOPPED;
        } else if (totalPnl.compareTo(config.getMaxLoss().negate()) <= 0) {
        if(totalPnl.compareTo(config.getMaxLoss().negate()) <= 0) {
            String logMessage = StrUtil.format("[Gate] 已达亏损风险值(合计{}), 已实现:{}, 未实现:{}",
                    totalPnl, cumulativePnl, unrealizedPnl);
            log.info(logMessage);
            DingTalkUtils.getDefault().sendActionCard("风险提醒", logMessage, config.getApiKey(), "");
//            state = StrategyState.STOPPED;
        }
    }
src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
@@ -58,7 +58,25 @@
        log.info("[管理器] 开始初始化...");
        try {
            //测试盘
            //实盘
            config = GateConfig.builder()
                    .apiKey("a2338398e00b7935104520e16be96918")
                    .apiSecret("9111d897f2346d5217619f2da76536632715fef4d7eb304c6c61e869a2a74e98")
                    .contract("ETH_USDT")
                    .leverage("100")
                    .marginMode("CROSS")
                    .positionMode("dual")
                    .gridRate(new BigDecimal("0.0025"))
                    .expectedProfit(new BigDecimal("2"))
                    .maxLoss(new BigDecimal("15"))
                    .quantity("1")
                    .priceScale(2)
                    .contractMultiplier(new BigDecimal("0.01"))
                    .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
                    .isProduction(true)
                    .reopenMaxRetries(3)
                    .build();
//            //测试盘
//            config = GateConfig.builder()
//                    .apiKey("d90ca272391992b8e74f8f92cedb21ec")
//                    .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
@@ -67,7 +85,7 @@
//                    .marginMode("CROSS")
//                    .positionMode("dual")
//                    .gridRate(new BigDecimal("0.002"))
//                    .overallTp(new BigDecimal("5"))
//                    .expectedProfit(new BigDecimal("1"))
//                    .maxLoss(new BigDecimal("15"))
//                    .quantity("1")
//                    .priceScale(1)
@@ -76,42 +94,6 @@
//                    .isProduction(false)
//                    .reopenMaxRetries(3)
//                    .build();
//            //实盘
//            config = GateConfig.builder()
//                    .apiKey("a2338398e00b7935104520e16be96918")
//                    .apiSecret("9111d897f2346d5217619f2da76536632715fef4d7eb304c6c61e869a2a74e98")
//                    .contract("ETH_USDT")
//                    .leverage("100")
//                    .marginMode("CROSS")
//                    .positionMode("dual")
//                    .gridRate(new BigDecimal("0.005"))
//                    .overallTp(new BigDecimal("100"))
//                    .maxLoss(new BigDecimal("15"))
//                    .quantity("5")
//                    .priceScale(2)
//                    .contractMultiplier(new BigDecimal("0.01"))
//                    .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
//                    .isProduction(true)
//                    .reopenMaxRetries(3)
//                    .build();
            //实盘
            config = GateConfig.builder()
                    .apiKey("d90ca272391992b8e74f8f92cedb21ec")
                    .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
                    .contract("ETH_USDT")
                    .leverage("100")
                    .marginMode("CROSS")
                    .positionMode("dual")
                    .gridRate(new BigDecimal("0.002"))
                    .overallTp(new BigDecimal("100"))
                    .maxLoss(new BigDecimal("15"))
                    .quantity("1")
                    .priceScale(1)
                    .contractMultiplier(new BigDecimal("0.01"))
                    .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
                    .isProduction(false)
                    .reopenMaxRetries(3)
                    .build();
            // 1. 初始化交易服务:查用户ID → 切持仓模式 → 清条件单 → 平已有仓位 → 设杠杆
            gridTradeService = new GateGridTradeService(config);