| | |
| | | @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; |
| | | /** 统一配置 */ |
| | |
| | | .apiKey(primaryAccount.getApiKey()) |
| | | .secretKey(primaryAccount.getSecretKey()) |
| | | .passphrase(primaryAccount.getPassphrase()) |
| | | .instId("ETH-USDT-SWAP") |
| | | .instId("BTC-USDT-SWAP") |
| | | .leverage("100") |
| | | .tdMode("cross") |
| | | .gridRate(new BigDecimal("0.0025")) |
| | |
| | | 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 + orders-algo(K线 + 条件单) |
| | | gridWsClientPublic = new OkxGridWsClient(primaryAccount, true); |
| | | gridWsClientPublic.addChannelHandler(new OkxKlineChannelHandler(okxConfig.getInstId(), gridTradeService)); |
| | | gridWsClientPublic.addChannelHandler(new OkxAlgoOrdersChannelHandler(okxConfig.getInstId(), gridTradeService)); |
| | | gridWsClientPublic.init(); |
| | | |
| | | // 私有 WS(/v5/private):positions(持仓) |
| | | gridWsClientPrivate = new OkxGridWsClient(primaryAccount, false); |
| | | gridWsClientPrivate.addChannelHandler(new OkxPositionsChannelHandler(okxConfig.getInstId(), gridTradeService)); |
| | | gridWsClientPrivate.init(); |
| | | |
| | | log.info("[OKX-Manager] WS已连接, business: candle1m/orders-algo, private: positions"); |
| | | |
| | | // 5. 激活策略,等待首根 K 线触发基底双开 |
| | | gridTradeService.startGrid(); |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | /** @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; } |
| | | } |