From 6a51f45e6a00b65a9e7b0b0707b453c11311f3ef Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 11 May 2026 22:38:13 +0800
Subject: [PATCH] feat(okxApi): 添加仓位模式配置和REST客户端功能

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java |  118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 118 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java b/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java
new file mode 100644
index 0000000..42dc164
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java
@@ -0,0 +1,118 @@
+package com.xcong.excoin.modules.okxApi;
+
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxAccountChannelHandler;
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxCandlestickChannelHandler;
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxOrderInfoChannelHandler;
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxPositionsChannelHandler;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.math.BigDecimal;
+
+@Slf4j
+@Component
+public class OkxWebSocketClientManager {
+
+    private OkxKlineWebSocketClient wsKlineClient;
+    private OkxKlineWebSocketClient wsPrivateClient;
+    private OkxGridTradeService gridTradeService;
+    private OkxConfig config;
+
+    @PostConstruct
+    public void init() {
+        log.info("[管理器] 开始初始化...");
+
+        try {
+            config = OkxConfig.builder()
+                    .apiKey("d90ca272391992b8e74f8f92cedb21ec")
+                    .secretKey("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
+                    .passphrase("Aa123123@")
+                    .contract("BTC-USDT-SWAP")
+                    .leverage("100")
+                    .marginMode("cross")
+                    .posMode("long_short_mode")
+                    .gridRate(new BigDecimal("0.0015"))
+                    .overallTp(new BigDecimal("5"))
+                    .maxLoss(new BigDecimal("15"))
+                    .quantity("1")
+                    .contractMultiplier(new BigDecimal("1"))
+                    .unrealizedPnlPriceMode(OkxConfig.PnLPriceMode.LAST_PRICE)
+                    .isProduction(false)
+                    .build();
+
+            setupAccount();
+
+            String accountName = "OKX_API";
+            gridTradeService = new OkxGridTradeService(config, accountName);
+            gridTradeService.startGrid();
+
+            wsKlineClient = new OkxKlineWebSocketClient(config.getWsKlineUrl());
+            wsKlineClient.addChannelHandler(new OkxCandlestickChannelHandler(config.getContract(), gridTradeService));
+            wsKlineClient.init();
+            log.info("[管理器] K线WS已连接, 已注册K线频道处理器");
+
+            wsPrivateClient = new OkxKlineWebSocketClient(
+                    config.getWsPrivateUrl(),
+                    config.getApiKey(),
+                    config.getSecretKey(),
+                    config.getPassphrase());
+            wsPrivateClient.addChannelHandler(new OkxPositionsChannelHandler(config.getContract(), gridTradeService));
+            wsPrivateClient.addChannelHandler(new OkxAccountChannelHandler());
+            wsPrivateClient.addChannelHandler(new OkxOrderInfoChannelHandler(config.getContract(), gridTradeService, config));
+            wsPrivateClient.init();
+            log.info("[管理器] 私有WS已连接, 已注册 3 个频道处理器");
+
+            gridTradeService.setWebSocketClient(wsPrivateClient.getWebSocketClient());
+
+        } catch (Exception e) {
+            log.error("[管理器] 初始化失败", e);
+        }
+    }
+
+    @PreDestroy
+    public void destroy() {
+        log.info("[管理器] 开始销毁...");
+
+        if (gridTradeService != null) {
+            gridTradeService.stopGrid();
+        }
+        if (wsKlineClient != null) {
+            wsKlineClient.destroy();
+        }
+        if (wsPrivateClient != null) {
+            wsPrivateClient.destroy();
+        }
+
+        log.info("[管理器] 销毁完成");
+    }
+
+    private void setupAccount() {
+        log.info("[管理器] 开始配置账户...");
+        OkxRestClient restClient = new OkxRestClient(
+                config.getRestBaseUrl(),
+                config.getApiKey(),
+                config.getSecretKey(),
+                config.getPassphrase(),
+                !config.isProduction());
+
+        boolean posModeOk = restClient.setPositionMode(config.getPosMode());
+        if (!posModeOk) {
+            log.error("[管理器] 设置持仓方式失败,策略可能无法正常运作");
+        }
+
+        boolean leverOk = restClient.setLeverage(
+                config.getContract(), config.getLeverage(), config.getMarginMode());
+        if (!leverOk) {
+            log.error("[管理器] 设置杠杆倍数失败,策略可能无法正常运作");
+        }
+
+        log.info("[管理器] 账户配置完成, posMode:{}, leverage:{}, marginMode:{}",
+                config.getPosMode(), config.getLeverage(), config.getMarginMode());
+    }
+
+    public OkxKlineWebSocketClient getKlineWebSocketClient() { return wsKlineClient; }
+    public OkxKlineWebSocketClient getPrivateWebSocketClient() { return wsPrivateClient; }
+    public OkxGridTradeService getGridTradeService() { return gridTradeService; }
+}

--
Gitblit v1.9.1