| | |
| | | |
| | | 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; |
| | |
| | | @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")) |
| | | .expectedProfit(new BigDecimal("2")) |
| | | .maxLoss(new BigDecimal("15")) |
| | | .gridRate(new BigDecimal("0.001")) |
| | | .expectedProfit(new BigDecimal("20")) |
| | | .maxLoss(new BigDecimal("30")) |
| | | .quantity("1") |
| | | .baseQuantity("10") |
| | | .priceScale(2) |
| | | .ctVal(new BigDecimal("0.1")) |
| | | .ctVal(new BigDecimal("0.01")) |
| | | .isSimulate(!primaryAccount.isAccountType()) |
| | | .gridQueueSize(300) |
| | | .marginRatioLimit(new BigDecimal("0.2")) |
| | |
| | | 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(); |
| | |
| | | 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; } |
| | | } |