From c0d51fba7f4fffff503354fd84b246e884d7c685 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 01 Jun 2026 13:48:52 +0800
Subject: [PATCH] fix(gateApi): 修复网格交易策略状态管理问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   34 ++++++++++++++++++++++------------
 1 files changed, 22 insertions(+), 12 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 5df541b..aafeba4 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -427,8 +427,8 @@
                     tryGenerateQueues();
                 }else {
                     longPositionSize = size;
-                    checkShortEntryOrderToCancel();
-                    checkLongEntryOrderToCancel();
+//                    checkShortEntryOrderToCancel();
+//                    checkLongEntryOrderToCancel();
                 }
             } else {
                 if (longActive && state == StrategyState.ACTIVE) {
@@ -450,8 +450,8 @@
                     tryGenerateQueues();
                 }else {
                     shortPositionSize = size.abs();
-                    checkShortEntryOrderToCancel();
-                    checkLongEntryOrderToCancel();
+//                    checkShortEntryOrderToCancel();
+//                    checkLongEntryOrderToCancel();
                 }
             } else {
                 if (shortActive && state == StrategyState.ACTIVE) {
@@ -1357,7 +1357,6 @@
         log.info("[Gate] 多仓止损触发 gridId:{}, 开始追单", gridId);
 
         int newEntryGridId = -(N - 1);
-        int entryQty = N - 1;
 
         GridElement newEntryGrid = GridElement.findById(newEntryGridId);
         if (newEntryGrid == null) {
@@ -1377,12 +1376,16 @@
             }
         }
 
-        String size = String.valueOf(entryQty);
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}张多单", gridId, newEntryGridId, entryQty);
+        BigDecimal priceDiff = longEntryPrice.subtract(triggerPrice).abs();
+        int entryQty = priceDiff.divide(config.getStep(), 0, RoundingMode.DOWN).intValue();
+        entryQty = Math.max(1, entryQty);
+        String size = String.valueOf(entryQty);
+        log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}张多单, 均价:{}, 价差:{}, 步长:{}",
+                gridId, newEntryGridId, entryQty, longEntryPrice, priceDiff, config.getStep());
         newEntryGrid.getLongTraderParam().setQuantity(size);
         placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
-                FuturesPriceTrigger.RuleEnum.NUMBER_2, size);
+                FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
     }
 
     private void handleShortStopLossTriggered(GridElement gridElement) {
@@ -1392,7 +1395,6 @@
         log.info("[Gate] 空仓止损触发 gridId:{}, 开始追单", gridId);
 
         int newEntryGridId = N - 1;
-        int entryQty = N - 1;
 
         GridElement newEntryGrid = GridElement.findById(newEntryGridId);
         if (newEntryGrid == null) {
@@ -1412,12 +1414,18 @@
             }
         }
 
-        String size = String.valueOf(entryQty);
+
+
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
-        log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}张空单", gridId, newEntryGridId, entryQty);
+        BigDecimal priceDiff = shortEntryPrice.subtract(triggerPrice).abs();
+        int entryQty = priceDiff.divide(config.getStep(), 0, RoundingMode.DOWN).intValue();
+        entryQty = Math.max(1, entryQty);
+        String size = String.valueOf(entryQty);
+        log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}张空单, 均价:{}, 价差:{}, 步长:{}",
+                gridId, newEntryGridId, entryQty, shortEntryPrice, priceDiff, config.getStep());
         newEntryGrid.getShortTraderParam().setQuantity(size);
         placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
-                FuturesPriceTrigger.RuleEnum.NUMBER_1, negate(size));
+                FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
     }
 
     private void extendLongStopLoss(int filledQty) {
@@ -1497,6 +1505,7 @@
                     unrealisedPnl, available, totalEquity, target);
             if (totalEquity.compareTo(target) > 0) {
                 log.info("[Gate] 盈亏达标({}>{}),重置策略", totalEquity, target);
+                state = StrategyState.STOPPED;
                 closeExistingPositions();
                 futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract());
                 startGrid();
@@ -1507,6 +1516,7 @@
     }
 
     private void handlePositionZeroAndReset(String direction) {
+        state = StrategyState.STOPPED;
         try {
             futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract());
         } catch (Exception e) {

--
Gitblit v1.9.1