From 7a49fb32bd836a2ed6f9879e26dd98a817c7c1ee Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 22 Jul 2026 16:18:16 +0800
Subject: [PATCH] feat(gate): 添加策略运行轮数限制功能

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 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 c640610..08f369b 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -135,6 +135,9 @@
     /** 多头是否活跃(有仓位) */
     private volatile boolean longActive = false;
 
+    /** 当前已完成轮数,每次盈利重启时+1 */
+    private volatile int currentRound = 0;
+
     /** 多头累计止损次数(始终递增,加仓订单成交后归零) */
     private volatile int accumulatedLongLossCount = 0;
     /** 空头累计止损次数(始终递增,加仓订单成交后归零) */
@@ -322,6 +325,7 @@
         currentShortOrderIds.clear();
         // 每次重启重新获取当前本金
         refreshInitialPrincipal();
+        currentRound = 0;
 
         log.info("[Gate] 网格策略已启动, 当前本金: {} USDT", initialPrincipal);
     }
@@ -432,7 +436,17 @@
             BigDecimal totalEquity = new BigDecimal(account.getTotal()).add(new BigDecimal(account.getUnrealisedPnl()));
 
             if (totalEquity.compareTo(target) > 0) {
-                log.info("[Gate] 盈亏达标(净权益{}>目标{}),重置策略", totalEquity, target);
+                currentRound++;
+                int maxRounds = config.getRounds();
+                log.info("[Gate] 盈亏达标(净权益{}>目标{}),第{}轮完成", totalEquity, target, currentRound);
+
+                if (maxRounds > 0 && currentRound >= maxRounds) {
+                    log.info("[Gate] 已达到运行轮数上限({}),策略停止", maxRounds);
+                    stopGrid();
+                    return;
+                }
+
+                log.info("[Gate] 重置策略,开始第{}轮...", currentRound);
                 state = StrategyState.STOPPED;
                 try {
                     futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract());
@@ -443,7 +457,10 @@
                 // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置
                 executor.submitTask(() -> {
                     try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
+                    // 注意:startGrid() 会将 currentRound 归零,这里需要保留
+                    int savedRound = currentRound;
                     startGrid();
+                    currentRound = savedRound;
                 });
             }
         } catch (Exception e) {
@@ -542,7 +559,9 @@
             // 提交到 executor 末尾:单线程FIFO保证前面所有平仓/取消任务完成后才重置
             executor.submitTask(() -> {
                 try { Thread.sleep(3000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
+                int savedRound = currentRound;
                 startGrid();
+                currentRound = savedRound;
             });
             log.info("[Gate] 重置策略");
             return;

--
Gitblit v1.9.1