From e45e705c22df5bc979e72db6014dd1ff9637be42 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 24 Jun 2026 22:21:58 +0800
Subject: [PATCH] fix(okx): 修复网格交易成交日志记录问题

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 117 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..0ea9325
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxWebSocketClientManager.java
@@ -0,0 +1,117 @@
+package com.xcong.excoin.modules.okxApi;
+
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.MarkPriceOkxChannelHandler;
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.OrdersOkxChannelHandler;
+import com.xcong.excoin.modules.okxApi.wsHandler.handler.PositionsOkxChannelHandler;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.math.BigDecimal;
+
+/**
+ * OKX 模块 Spring 容器入口 — 组件组装 + 生命周期管理。
+ *
+ * <h3>组装顺序({@code @PostConstruct})</h3>
+ * <ol>
+ *   <li>{@link OkxConfig} — 构建配置(API 密钥、合约、策略参数)</li>
+ *   <li>{@link OkxGridTradeService} — init():切双向持仓 → 清旧条件单 → 平仓 → 设杠杆</li>
+ *   <li>{@link OkxKlineWebSocketClient} — 注册 3 个频道处理器 → init():建立 WS 连接并订阅</li>
+ *   <li>{@code gridTradeService.startGrid()} — 状态重置,等待首根 K 线</li>
+ * </ol>
+ *
+ * <h3>3 个频道处理器</h3>
+ * <ol>
+ *   <li>MarkPriceOkxChannelHandler — 公开频道,标记价格 → onKline() + setMarkPrice()</li>
+ *   <li>PositionsOkxChannelHandler — 私有频道,仓位 → onPositionUpdate()</li>
+ *   <li>OrdersOkxChannelHandler — 私有频道,订单成交(含algoId) → onAutoOrder()</li>
+ * </ol>
+ *
+ * <h3>销毁顺序({@code @PreDestroy})</h3>
+ * <ol>
+ *   <li>gridTradeService.stopGrid():取消所有条件单 → 关闭交易线程池</li>
+ *   <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li>
+ * </ol>
+ *
+ * @author Administrator
+ */
+@Slf4j
+@Component
+public class OkxWebSocketClientManager {
+
+    private OkxKlineWebSocketClient wsClient;
+    private OkxGridTradeService gridTradeService;
+    private OkxConfig config;
+
+    @PostConstruct
+    public void init() {
+        log.info("[OKX管理器] 开始初始化...");
+
+        try {
+            // TODO: 替换为实际 API 密钥
+            config = OkxConfig.builder()
+                    .apiKey("ac76252d-e717-4459-a6f9-80512aed5ea0")
+                    .apiSecret("A8168543EF4F08A6DBFE27AB23956898")
+                    .passphrase("Aa12345678@")
+                    .contract("ETH-USDT-SWAP")
+                    .leverage("100")
+                    .marginMode("cross")
+                    .positionMode("long_short_mode")
+                    .gridRate(new BigDecimal("0.0025"))
+                    .expectedProfit(new BigDecimal("25"))
+                    .maxLoss(new BigDecimal("15"))
+                    .baseQuantity("1")
+                    .quantity("1")
+                    .restartGridSpan(6)
+                    .maxPositionSize(2)
+                    .priceScale(2)
+                    .contractMultiplier(new BigDecimal("0.01"))
+                    .unrealizedPnlPriceMode(OkxConfig.PnLPriceMode.LAST_PRICE)
+                    .isProduction(false)
+                    .reopenMaxRetries(3)
+                    .build();
+
+            // 1. 初始化交易服务
+            gridTradeService = new OkxGridTradeService(config);
+            gridTradeService.init();
+
+            // 2. 创建 WS 客户端并注册频道处理器
+            wsClient = new OkxKlineWebSocketClient(config);
+
+            // 公开频道:标记价格(替代 K 线,同时驱动策略 onKline 和 PnL 计算)
+            wsClient.addPublicHandler(new MarkPriceOkxChannelHandler(
+                    config.getContract(), gridTradeService));
+            // 私有频道:仓位
+            wsClient.addPrivateHandler(new PositionsOkxChannelHandler(
+                    config, gridTradeService));
+            // 私有频道:条件单(orders 频道含 algoId,可追溯到源条件单)
+            wsClient.addPrivateHandler(new OrdersOkxChannelHandler(
+                    config, gridTradeService));
+
+            gridTradeService.setWsClient(wsClient);
+            wsClient.init();
+            log.info("[OKX管理器] WS已连接, 已注册 3 个频道处理器");
+
+            // 3. 激活策略
+            gridTradeService.startGrid();
+        } catch (Exception e) {
+            log.error("[OKX管理器] 初始化失败", e);
+        }
+    }
+
+    @PreDestroy
+    public void destroy() {
+        log.info("[OKX管理器] 开始销毁...");
+        if (gridTradeService != null) {
+            gridTradeService.stopGrid();
+        }
+        if (wsClient != null) {
+            wsClient.destroy();
+        }
+        log.info("[OKX管理器] 销毁完成");
+    }
+
+    public OkxKlineWebSocketClient getKlineWebSocketClient() { return wsClient; }
+    public OkxGridTradeService getGridTradeService() { return gridTradeService; }
+}

--
Gitblit v1.9.1