From d22108cd8800df3f8d4ab9f7b125ece79639faf3 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 11 May 2026 10:21:51 +0800
Subject: [PATCH] fix(trade): 优化网格交易日志输出并实现市价止盈机制

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   38 ++++++++++++++++++++++----------------
 1 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
index fc47591..b51f95d 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -166,6 +166,14 @@
                 }
             }
 
+            try {
+                futuresApi.updateDualModePositionLeverageCall(
+                        SETTLE, config.getContract(), config.getLeverage(),
+                        null, null).execute();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
             if (!config.getMarginMode().equals(account.getMarginMode())) {
 
                 UpdateDualCompPositionCrossModeRequest updateDualCompPositionCrossModeRequest = new UpdateDualCompPositionCrossModeRequest();
@@ -178,15 +186,6 @@
                 }
             }
             log.info("[Gate] 持仓模式: {} 余额: {}", config.getPositionMode(), account.getAvailable());
-
-
-            try {
-                futuresApi.updateDualModePositionLeverageCall(
-                        SETTLE, config.getContract(), config.getLeverage(),
-                        config.getMarginMode(), null).execute();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
             log.info("[Gate] 杠杆: {}x {}", config.getLeverage(), config.getMarginMode());
         } catch (GateApiException e) {
             log.error("[Gate] 初始化失败, label:{}, msg:{}", e.getErrorLabel(), e.getMessage());
@@ -454,8 +453,10 @@
             for (int i = 0; i < matched.size(); i++) {
                 min = min.multiply(BigDecimal.ONE.subtract(step)).setScale(1, RoundingMode.HALF_UP);
                 shortPriceQueue.add(min);
+                log.info("[Gate] 空队列增加:{}", min);
             }
-            shortPriceQueue.sort(BigDecimal::compareTo);
+
+            shortPriceQueue.sort((a, b) -> b.compareTo(a));
 
             log.info("[Gate] 现空队列:{}", shortPriceQueue);
         }
@@ -463,21 +464,22 @@
         synchronized (longPriceQueue) {
             BigDecimal first = longPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : longPriceQueue.get(0);
             BigDecimal step = config.getGridRate();
-            int added = 0;
             for (int i = 1; i <= matched.size(); i++) {
                 BigDecimal elem = first.multiply(BigDecimal.ONE.subtract(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                 if (longEntryPrice.compareTo(BigDecimal.ZERO) > 0
                         && elem.subtract(longEntryPrice).abs().compareTo(longEntryPrice.multiply(step)) < 0) {
+                    log.info("[Gate] 多队列跳过:{}", elem);
                     continue;
                 }
                 longPriceQueue.add(elem);
-                added++;
+
+                log.info("[Gate] 多队列增加:{}", elem);
             }
             longPriceQueue.sort(BigDecimal::compareTo);
             while (longPriceQueue.size() > config.getGridQueueSize()) {
                 longPriceQueue.remove(longPriceQueue.size() - 1);
             }
-            log.info("[Gate] 现多队列:{}, 跳过{}个(贴近多仓均价)", longPriceQueue, matched.size() - added);
+            log.info("[Gate] 现多队列:{}", longPriceQueue);
         }
     }
 
@@ -539,6 +541,8 @@
             for (int i = 0; i < matched.size(); i++) {
                 max = max.multiply(BigDecimal.ONE.add(step)).setScale(1, RoundingMode.HALF_UP);
                 longPriceQueue.add(max);
+
+                log.info("[Gate] 多队列增加:{}", max);
             }
             longPriceQueue.sort(BigDecimal::compareTo);
 
@@ -548,21 +552,23 @@
         synchronized (shortPriceQueue) {
             BigDecimal first = shortPriceQueue.isEmpty() ? matched.get(0) : shortPriceQueue.get(0);
             BigDecimal step = config.getGridRate();
-            int added = 0;
             for (int i = 1; i <= matched.size(); i++) {
                 BigDecimal elem = first.multiply(BigDecimal.ONE.add(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
                 if (shortEntryPrice.compareTo(BigDecimal.ZERO) > 0
                         && elem.subtract(shortEntryPrice).abs().compareTo(shortEntryPrice.multiply(step)) < 0) {
+
+                    log.info("[Gate] 空队列跳过:{}", elem);
                     continue;
                 }
                 shortPriceQueue.add(elem);
-                added++;
+
+                log.info("[Gate] 空队列增加:{}", elem);
             }
             shortPriceQueue.sort((a, b) -> b.compareTo(a));
             while (shortPriceQueue.size() > config.getGridQueueSize()) {
                 shortPriceQueue.remove(shortPriceQueue.size() - 1);
             }
-            log.info("[Gate] 现空队列:{}, 跳过{}个(贴近空仓均价)", shortPriceQueue, matched.size() - added);
+            log.info("[Gate] 现空队列:{}", shortPriceQueue);
         }
     }
 

--
Gitblit v1.9.1