From 05c48c8c5fd2570ed078d99c2231e2cf7cf28a4f Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 09 Jun 2026 10:08:59 +0800
Subject: [PATCH] ``` feat(gateApi): 添加网格交易最大持仓限制功能

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   96 +++++++++++++++++++++++++++++++----------------
 1 files changed, 63 insertions(+), 33 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 fede298..df29b87 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -411,22 +411,16 @@
         checkProfitAndReset();
 
 
-        if (
+        if (state == StrategyState.ACTIVE &&
                 longActive == false &&
                         longPositionSize.compareTo(BigDecimal.ZERO) == 0){
-
-        }{
-
             processShortGrid(closePrice);
         }
 
 
-        if (
+        if (state == StrategyState.ACTIVE &&
                 shortActive == false &&
                         shortPositionSize.compareTo(BigDecimal.ZERO) == 0){
-
-        }{
-
             processLongGrid(closePrice);
         }
     }
@@ -1211,11 +1205,11 @@
 
         Integer upId = matchedUpGridElement.getUpId();
         GridElement newEntryGrid = GridElement.findById(upId);
-        GridElement cancelGridElement = GridElement.findById(newEntryGrid.getUpId());
 
-        if (cancelGridElement != null) {
+        if (newEntryGrid != null) {
+            GridElement cancelGridElement = GridElement.findById(newEntryGrid.getUpId());
 
-            if (newEntryGrid != null) {
+            if (cancelGridElement != null) {
                 BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                 String size = cancelGridElement.getLongTraderParam().getQuantity();
                 log.info("[Gate] 多仓仓位归零 gridId:{}, 挂{}基础张多单",
@@ -1224,19 +1218,18 @@
                 placeEntryOrderWithPreFlag(newEntryGrid, true, triggerPrice,
                         FuturesPriceTrigger.RuleEnum.NUMBER_1, size);
 
-            }
 
-            /**
-             * 看是否有多仓挂单,有就取消
-             */
-            if (cancelGridElement != null && cancelGridElement.isHasLongOrder()) {
-                executor.cancelConditionalOrder(cancelGridElement.getLongOrderId(), oid -> {
-                    longEntryTraderIdParam(cancelGridElement, null, false);
-                    log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
-                });
+                /**
+                 * 看是否有多仓挂单,有就取消
+                 */
+                if (cancelGridElement != null && cancelGridElement.isHasLongOrder()) {
+                    executor.cancelConditionalOrder(cancelGridElement.getLongOrderId(), oid -> {
+                        longEntryTraderIdParam(cancelGridElement, null, false);
+                        log.info("[Gate] 多仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
+                    });
+                }
             }
         }
-
     }
 
     private void processLongGrid(BigDecimal currentPrice) {
@@ -1260,11 +1253,11 @@
 
         Integer downId = matchedUpGridElement.getDownId();
         GridElement newEntryGrid = GridElement.findById(downId);
-        GridElement cancelGridElement = GridElement.findById(newEntryGrid.getDownId());
 
-        if (cancelGridElement != null) {
+        if (newEntryGrid != null) {
+            GridElement cancelGridElement = GridElement.findById(newEntryGrid.getDownId());
 
-            if (newEntryGrid != null) {
+            if (cancelGridElement != null) {
                 BigDecimal triggerPrice = newEntryGrid.getGridPrice();
                 String size = cancelGridElement.getShortTraderParam().getQuantity();
                 log.info("[Gate] 空仓仓位归零 gridId:{}, 挂{}基础张多单",
@@ -1272,17 +1265,18 @@
                 newEntryGrid.getShortTraderParam().setQuantity(size);
                 placeEntryOrderWithPreFlag(newEntryGrid, false, triggerPrice,
                         FuturesPriceTrigger.RuleEnum.NUMBER_2, negate(size));
+
+                /**
+                 * 看是否有空仓挂单,有就取消
+                 */
+                if (cancelGridElement != null && cancelGridElement.isHasShortOrder()) {
+                    executor.cancelConditionalOrder(cancelGridElement.getShortOrderId(), oid -> {
+                        shortEntryTraderIdParam(cancelGridElement, null, false);
+                        log.info("[Gate] 空仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
+                    });
+                }
             }
 
-            /**
-             * 看是否有空仓挂单,有就取消
-             */
-            if (cancelGridElement != null && cancelGridElement.isHasShortOrder()) {
-                executor.cancelConditionalOrder(cancelGridElement.getShortOrderId(), oid -> {
-                    shortEntryTraderIdParam(cancelGridElement, null, false);
-                    log.info("[Gate] 空仓仓位归零, 取消gridId:{}的多单", cancelGridElement);
-                });
-            }
         }
     }
 
@@ -1315,6 +1309,24 @@
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
         longEntryQty++;
         int entryQty = longEntryQty;
+
+        // 最大持仓限制:已持仓+本次挂单 ≤ maxPositionSize
+        int maxPos = config.getMaxPositionSize();
+        if (maxPos > 0) {
+            int currentPos = longPositionSize.intValue();
+            int maxAllowed = maxPos - currentPos;
+            if (maxAllowed <= 0) {
+                log.warn("[Gate] 多仓止损触发 gridId:{}, 已达最大持仓{},跳过挂单", gridId, maxPos);
+                longEntryQty = 1;
+                return;
+            }
+            if (entryQty > maxAllowed) {
+                log.info("[Gate] 多仓止损触发 gridId:{}, 挂单{}张超限, 截断为{}张", gridId, entryQty, maxAllowed);
+                entryQty = maxAllowed;
+                longEntryQty = 1;
+            }
+        }
+
         String size = new BigDecimal(String.valueOf(entryQty)).multiply(new BigDecimal(config.getQuantity())).toString();
         log.info("[Gate] 多仓止损触发 gridId:{}, 在gridId:{}挂{}基础张多单(计数器:{}, size:{})",
                 gridId, newEntryGridId, entryQty, longEntryQty, size);
@@ -1354,6 +1366,24 @@
         BigDecimal triggerPrice = newEntryGrid.getGridPrice();
         shortEntryQty++;
         int entryQty = shortEntryQty;
+
+        // 最大持仓限制:已持仓+本次挂单 ≤ maxPositionSize
+        int maxPos = config.getMaxPositionSize();
+        if (maxPos > 0) {
+            int currentPos = shortPositionSize.intValue();
+            int maxAllowed = maxPos - currentPos;
+            if (maxAllowed <= 0) {
+                log.warn("[Gate] 空仓止损触发 gridId:{}, 已达最大持仓{},跳过挂单", gridId, maxPos);
+                shortEntryQty = 1;
+                return;
+            }
+            if (entryQty > maxAllowed) {
+                log.info("[Gate] 空仓止损触发 gridId:{}, 挂单{}张超限, 截断为{}张", gridId, entryQty, maxAllowed);
+                entryQty = maxAllowed;
+                shortEntryQty = 1;
+            }
+        }
+
         String size = new BigDecimal(String.valueOf(entryQty)).multiply(new BigDecimal(config.getQuantity())).toString();
         log.info("[Gate] 空仓止损触发 gridId:{}, 在gridId:{}挂{}基础张空单(计数器:{}, size:{})",
                 gridId, newEntryGridId, entryQty, shortEntryQty, size);

--
Gitblit v1.9.1