From afc858458cbdde1d5f3f2cfce6d056656bf75c16 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 13 May 2026 21:39:39 +0800
Subject: [PATCH] refactor(okxNewPrice): 账户配置

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java |   73 +++++++++++++++++++++++++++++-------
 1 files changed, 59 insertions(+), 14 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
index 600f00f..6dc10ac 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java
@@ -26,29 +26,27 @@
 
         try {
             config = OkxConfig.builder()
-                    .apiKey("d90ca272391992b8e74f8f92cedb21ec")
-                    .secretKey("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
-                    .passphrase("Aa123123@")
-                    .contract("BTC-USDT-SWAP")
+                    .apiKey("ac76252d-e717-4459-a6f9-80512aed5ea0")
+                    .secretKey("A8168543EF4F08A6DBFE27AB23956898")
+                    .passphrase("Aa12345678@")
+                    .contract("ETH-USDT-SWAP")
                     .leverage("100")
                     .marginMode("cross")
+                    .posMode("long_short_mode")
                     .gridRate(new BigDecimal("0.0015"))
-                    .overallTp(new BigDecimal("5"))
-                    .maxLoss(new BigDecimal("15"))
+                    .overallTp(new BigDecimal("15"))
+                    .maxLoss(new BigDecimal("20"))
                     .quantity("1")
-                    .contractMultiplier(new BigDecimal("1"))
+                    .contractMultiplier(new BigDecimal("0.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(),
@@ -58,10 +56,22 @@
             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 个频道处理器");
 
+            wsPrivateClient.setOnLoginSuccess(() -> {
+                log.info("[管理器] 私有WS登录完成,启动K线订阅");
+                try {
+                    wsKlineClient = new OkxKlineWebSocketClient(config.getWsKlineUrl());
+                    wsKlineClient.addChannelHandler(new OkxCandlestickChannelHandler(config.getContract(), gridTradeService));
+                    wsKlineClient.init();
+                    log.info("[管理器] K线WS已连接, 已注册K线频道处理器");
+                } catch (Exception e) {
+                    log.error("[管理器] K线WS初始化失败", e);
+                }
+            });
+
+            wsPrivateClient.init();
             gridTradeService.setWebSocketClient(wsPrivateClient.getWebSocketClient());
+            log.info("[管理器] 私有WS已连接, 等待登录完成...");
 
         } catch (Exception e) {
             log.error("[管理器] 初始化失败", e);
@@ -85,6 +95,41 @@
         log.info("[管理器] 销毁完成");
     }
 
+    private void setupAccount() {
+        log.info("[管理器] 开始配置账户...");
+        OkxRestClient restClient = new OkxRestClient(
+                config.getRestBaseUrl(),
+                config.getApiKey(),
+                config.getSecretKey(),
+                config.getPassphrase(),
+                !config.isProduction());
+
+        log.info("[管理器] 清理原有持仓和委托...");
+        restClient.cancelAllOrders(config.getContract());
+        restClient.closeAllPositions(config.getContract(), config.getMarginMode());
+
+        boolean posModeOk = restClient.setPositionMode(config.getPosMode());
+        if (!posModeOk) {
+            log.error("[管理器] 设置持仓方式失败,策略可能无法正常运作");
+        }
+
+        boolean leverOk = restClient.setLeverage(
+                config.getContract(), config.getLeverage(), config.getMarginMode());
+        if (!leverOk) {
+            log.error("[管理器] 设置杠杆倍数失败,策略可能无法正常运作");
+        }
+
+        Long instIdCode = restClient.fetchInstIdCode("SWAP", config.getContract());
+        if (instIdCode != null) {
+            config.setInstIdCode(instIdCode);
+        } else {
+            log.error("[管理器] 获取instIdCode失败,WS下单将无法正常工作");
+        }
+
+        log.info("[管理器] 账户配置完成, posMode:{}, leverage:{}, marginMode:{}, instIdCode:{}",
+                config.getPosMode(), config.getLeverage(), config.getMarginMode(), config.getInstIdCode());
+    }
+
     public OkxKlineWebSocketClient getKlineWebSocketClient() { return wsKlineClient; }
     public OkxKlineWebSocketClient getPrivateWebSocketClient() { return wsPrivateClient; }
     public OkxGridTradeService getGridTradeService() { return gridTradeService; }

--
Gitblit v1.9.1