From eabbff4732628956fa6ea628d063b162472cf786 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 24 Jun 2026 17:02:55 +0800
Subject: [PATCH] fix(okxApi): 修复网格交易队列生成逻辑

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java |   49 +++++++------------------------------------------
 1 files changed, 7 insertions(+), 42 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 f9e7ddd..7d5f6b0 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
@@ -298,11 +298,7 @@
             return;
         }
 
-        // OPENING 状态下若 WS 仓位已确认但 REST 回调尚未完成,等标记价格推送时重试队列生成
-        if (state == StrategyState.OPENING &&
-                baseLongOpened && baseShortOpened) {
-            tryGenerateQueues();
-        }
+
 
         if (state == StrategyState.ACTIVE &&
                 !longActive &&
@@ -595,7 +591,8 @@
     // ---- 网格队列处理 ----
 
     private void tryGenerateQueues() {
-        if (baseLongOpened && baseShortOpened) {
+        // OPENING 状态下若 WS 仓位已确认但 REST 回调尚未完成,等标记价格推送时重试队列生成
+        if (state == StrategyState.OPENING && baseLongOpened && baseShortOpened) {
             // 确保 openLong/openShort 的 REST 回调已完成(WS 推送可能比回调更快到达)
             if (config.getBaseLongTraderParam() == null || config.getBaseShortTraderParam() == null) {
                 log.warn("[OKX] 基底REST回调尚未完成, 延后队列生成");
@@ -614,24 +611,6 @@
             baseGridElement.setShortOrderId(baseShortTraderParam.getEntryOrderId());
             baseGridElement.setHasShortOrder(true);
 
-            // 挂基座止盈
-            {
-                BigDecimal tpPrice = baseGridElement.getGridPrice().add(config.getStep());
-                executor.placeTakeProfit(tpPrice, "close_long", config.getBaseQuantity(),
-                        profitId -> {
-                            longTakeProfitTraderIdParam(baseGridElement, profitId, true);
-                            log.info("[OKX] 基座多仓止盈已挂, gridId:0, 触发价:{}, tpId:{}", tpPrice, profitId);
-                        });
-            }
-            {
-                BigDecimal tpPrice = baseGridElement.getGridPrice().subtract(config.getStep());
-                executor.placeTakeProfit(tpPrice, "close_short", config.getBaseQuantity(),
-                        profitId -> {
-                            shortTakeProfitTraderIdParam(baseGridElement, profitId, true);
-                            log.info("[OKX] 基座空仓止盈已挂, gridId:0, 触发价:{}, tpId:{}", tpPrice, profitId);
-                        });
-            }
-
             // 挂初始止损
             int stopCount = Integer.parseInt(config.getBaseQuantity()) / Integer.parseInt(config.getQuantity()) + 1;
             for (int id = 2; id <= stopCount; id++) {
@@ -641,7 +620,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();
@@ -655,7 +634,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();
@@ -663,20 +642,6 @@
                         });
             }
             log.info("[OKX] 止损单已全部挂完, 空仓止损: 2~{}, 多仓止损: -2~-{}", stopCount, stopCount);
-
-            // 挂初始条件开仓单
-            GridElement longFirst = GridElement.findById(1);
-            if (longFirst != null && !longFirst.isHasLongOrder()) {
-                BigDecimal triggerPrice = longFirst.getGridPrice();
-                log.info("[OKX] 挂初始多仓条件单, gridId:1, trigger:{}", triggerPrice);
-                placeEntryOrderWithPreFlag(longFirst, true, triggerPrice, config.getBaseQuantity());
-            }
-            GridElement shortFirst = GridElement.findById(-1);
-            if (shortFirst != null && !shortFirst.isHasShortOrder()) {
-                BigDecimal triggerPrice = shortFirst.getGridPrice();
-                log.info("[OKX] 挂初始空仓条件单, gridId:-1, trigger:{}", triggerPrice);
-                placeEntryOrderWithPreFlag(shortFirst, false, triggerPrice, negate(config.getBaseQuantity()));
-            }
 
             state = StrategyState.ACTIVE;
         }
@@ -1144,7 +1109,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();
@@ -1172,7 +1137,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