Administrator
2026-06-05 ed57e750b5e2cf14fe5d447ff318228f8df77d23
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxWebSocketClientManager.java
@@ -1,7 +1,7 @@
package com.xcong.excoin.modules.okxNewPrice;
import com.xcong.excoin.modules.okxNewPrice.gridWs.OkxAlgoOrdersChannelHandler;
import com.xcong.excoin.modules.okxNewPrice.gridWs.OkxKlineChannelHandler;
import com.xcong.excoin.modules.okxNewPrice.gridWs.OkxOrdersChannelHandler;
import com.xcong.excoin.modules.okxNewPrice.gridWs.OkxPositionsChannelHandler;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.OKXAccount;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.DefaultUrls;
@@ -44,8 +44,10 @@
@ConditionalOnProperty(prefix = "app", name = "quant", havingValue = "true")
public class OkxWebSocketClientManager {
    /** 网格交易 WS 客户端 */
    private OkxGridWsClient gridWsClient;
    /** 网格交易公共 WS 客户端(candle1m) */
    private OkxGridWsClient gridWsClientPublic;
    /** 网格交易私有 WS 客户端(positions / orders-algo) */
    private OkxGridWsClient gridWsClientPrivate;
    /** 网格交易策略服务 */
    private OkxGridTradeService gridTradeService;
    /** 统一配置 */
@@ -86,13 +88,13 @@
                    .instId("ETH-USDT-SWAP")
                    .leverage("100")
                    .tdMode("cross")
                    .gridRate(new BigDecimal("0.0025"))
                    .expectedProfit(new BigDecimal("2"))
                    .maxLoss(new BigDecimal("15"))
                    .quantity("1")
                    .baseQuantity("10")
                    .gridRate(new BigDecimal("0.002"))
                    .expectedProfit(new BigDecimal("180"))
                    .maxLoss(new BigDecimal("30"))
                    .quantity("5")
                    .baseQuantity("50")
                    .priceScale(2)
                    .ctVal(new BigDecimal("0.1"))
                    .ctVal(new BigDecimal("0.01"))
                    .isSimulate(!primaryAccount.isAccountType())
                    .gridQueueSize(300)
                    .marginRatioLimit(new BigDecimal("0.2"))
@@ -102,13 +104,19 @@
            gridTradeService = new OkxGridTradeService(okxConfig, okxAccount);
            gridTradeService.init();
            // 4. 创建 WS 客户端并注册 3 个频道处理器
            gridWsClient = new OkxGridWsClient(primaryAccount);
            gridWsClient.addChannelHandler(new OkxKlineChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClient.addChannelHandler(new OkxPositionsChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClient.addChannelHandler(new OkxAlgoOrdersChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClient.init();
            log.info("[OKX-Manager] WS已连接, 已注册 3 个频道处理器: candle1m/positions/orders-algo");
            // 4. 创建 WS 客户端并注册频道处理器
            // 业务 WS(/v5/business):candle1m
            gridWsClientPublic = new OkxGridWsClient(primaryAccount, true);
            gridWsClientPublic.addChannelHandler(new OkxKlineChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClientPublic.init();
            // 私有 WS(/v5/private):positions + orders(algo触发后订单fill带algoId可匹配)
            gridWsClientPrivate = new OkxGridWsClient(primaryAccount, false);
            gridWsClientPrivate.addChannelHandler(new OkxPositionsChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClientPrivate.addChannelHandler(new OkxOrdersChannelHandler(okxConfig.getInstId(), gridTradeService));
            gridWsClientPrivate.init();
            log.info("[OKX-Manager] WS已连接, business: candle1m, private: positions/orders");
            // 5. 激活策略,等待首根 K 线触发基底双开
            gridTradeService.startGrid();
@@ -133,11 +141,18 @@
                log.error("[OKX-Manager] 停止策略失败", e);
            }
        }
        if (gridWsClient != null) {
        if (gridWsClientPublic != null) {
            try {
                gridWsClient.destroy();
                gridWsClientPublic.destroy();
            } catch (Exception e) {
                log.error("[OKX-Manager] 销毁WS客户端失败", e);
                log.error("[OKX-Manager] 销毁公共WS客户端失败", e);
            }
        }
        if (gridWsClientPrivate != null) {
            try {
                gridWsClientPrivate.destroy();
            } catch (Exception e) {
                log.error("[OKX-Manager] 销毁私有WS客户端失败", e);
            }
        }
@@ -146,8 +161,9 @@
    /** @return 网格交易策略服务实例 */
    public OkxGridTradeService getGridTradeService() { return gridTradeService; }
    /** @return 网格交易 WS 客户端实例 */
    public OkxGridWsClient getGridWsClient() { return gridWsClient; }
    /** @return 统一配置实例 */
    /** @return 网格交易公共 WS 客户端实例 */
    public OkxGridWsClient getGridWsClientPublic() { return gridWsClientPublic; }
    /** @return 网格交易私有 WS 客户端实例 */
    public OkxGridWsClient getGridWsClientPrivate() { return gridWsClientPrivate; }
    public OkxConfig getOkxConfig() { return okxConfig; }
}