From e4c5d36a2da3fabd0f233df15a563d520d08b287 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 24 Jun 2026 16:10:34 +0800
Subject: [PATCH] refactor(okx): 删除K线处理器并重命名止盈单方法为止损单

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java |   53 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java b/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
index 6db2ab3..62fdc13 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
@@ -133,11 +133,19 @@
     public void init() {
         try {
             JSONObject account = executorGet("/api/v5/account/balance");
-            JSONArray details = account.getJSONArray("data");
-            if (details != null && !details.isEmpty()) {
-                JSONObject total = details.getJSONObject(0);
-                this.initialPrincipal = total.getBigDecimal("totalEq");
-                log.info("[OKX] 初始本金: {} USDT", initialPrincipal);
+            JSONArray dataArr = account.getJSONArray("data");
+            if (dataArr != null && !dataArr.isEmpty()) {
+                JSONArray details = dataArr.getJSONObject(0).getJSONArray("details");
+                if (details != null) {
+                    for (int i = 0; i < details.size(); i++) {
+                        JSONObject currency = details.getJSONObject(i);
+                        if ("USDT".equals(currency.getString("ccy"))) {
+                            this.initialPrincipal = currency.getBigDecimal("eq");
+                            log.info("[OKX] 初始本金(USDT余额): {}", initialPrincipal);
+                            break;
+                        }
+                    }
+                }
             }
 
             // 设置双向持仓模式
@@ -237,9 +245,18 @@
     private void refreshInitialPrincipal() {
         try {
             JSONObject account = executorGet("/api/v5/account/balance");
-            JSONArray details = account.getJSONArray("data");
-            if (details != null && !details.isEmpty()) {
-                this.initialPrincipal = details.getJSONObject(0).getBigDecimal("totalEq");
+            JSONArray dataArr = account.getJSONArray("data");
+            if (dataArr != null && !dataArr.isEmpty()) {
+                JSONArray details = dataArr.getJSONObject(0).getJSONArray("details");
+                if (details != null) {
+                    for (int i = 0; i < details.size(); i++) {
+                        JSONObject currency = details.getJSONObject(i);
+                        if ("USDT".equals(currency.getString("ccy"))) {
+                            this.initialPrincipal = currency.getBigDecimal("eq");
+                            break;
+                        }
+                    }
+                }
             }
         } catch (Exception e) {
             log.warn("[OKX] 获取初始化本金失败,使用旧值: {}", initialPrincipal);
@@ -279,6 +296,12 @@
                 config.setBaseShortTraderParam(baseShortTp);
             }, null);
             return;
+        }
+
+        // OPENING 状态下若 WS 仓位已确认但 REST 回调尚未完成,等标记价格推送时重试队列生成
+        if (state == StrategyState.OPENING &&
+                baseLongOpened && baseShortOpened) {
+            tryGenerateQueues();
         }
 
         if (state == StrategyState.ACTIVE &&
@@ -573,6 +596,12 @@
 
     private void tryGenerateQueues() {
         if (baseLongOpened && baseShortOpened) {
+            // 确保 openLong/openShort 的 REST 回调已完成(WS 推送可能比回调更快到达)
+            if (config.getBaseLongTraderParam() == null || config.getBaseShortTraderParam() == null) {
+                log.warn("[OKX] 基底REST回调尚未完成, 延后队列生成");
+                return;
+            }
+
             generateShortQueue();
             generateLongQueue();
             updateGridElements();
@@ -612,7 +641,7 @@
                 }
                 BigDecimal triggerPrice = elem.getGridPrice();
                 int finalId = id;
-                executor.placeTakeProfit(triggerPrice, "close_short", config.getQuantity(),
+                executor.placeStopLoss(triggerPrice, "close_short", config.getQuantity(),
                         profitId -> {
                             elem.setShortStopLossOrderId(profitId);
                             GridElement.refreshIndices();
@@ -626,7 +655,7 @@
                 }
                 BigDecimal triggerPrice = elem.getGridPrice();
                 int finalId = id;
-                executor.placeTakeProfit(triggerPrice, "close_long", config.getQuantity(),
+                executor.placeStopLoss(triggerPrice, "close_long", config.getQuantity(),
                         profitId -> {
                             elem.setLongStopLossOrderId(profitId);
                             GridElement.refreshIndices();
@@ -1115,7 +1144,7 @@
             }
             BigDecimal triggerPrice = elem.getGridPrice();
             int finalSlId = newSlId;
-            executor.placeTakeProfit(triggerPrice, "close_long", config.getQuantity(),
+            executor.placeStopLoss(triggerPrice, "close_long", config.getQuantity(),
                     profitId -> {
                         elem.setLongStopLossOrderId(profitId);
                         GridElement.refreshIndices();
@@ -1143,7 +1172,7 @@
             }
             BigDecimal triggerPrice = elem.getGridPrice();
             int finalSlId = newSlId;
-            executor.placeTakeProfit(triggerPrice, "close_short", config.getQuantity(),
+            executor.placeStopLoss(triggerPrice, "close_short", config.getQuantity(),
                     profitId -> {
                         elem.setShortStopLossOrderId(profitId);
                         GridElement.refreshIndices();

--
Gitblit v1.9.1