From 3585a390cd327f3e60438e84674c08bd24359096 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Fri, 22 May 2026 17:52:12 +0800
Subject: [PATCH] feat(gateApi): 添加网格交易的多空仓位触发逻辑

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |  248 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 230 insertions(+), 18 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 b3354d1..befbab7 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -14,7 +14,6 @@
 import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -342,6 +341,16 @@
         lastKlinePrice = closePrice;
         updateUnrealizedPnl();
         if (state == StrategyState.STOPPED) {
+            try {
+                futuresApi.cancelPriceTriggeredOrderList(SETTLE, config.getContract());
+            } catch (ApiException e) {
+                e.printStackTrace();
+            }
+            closeExistingPositions();
+
+            BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
+            log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}",
+                    cumulativePnl, unrealizedPnl, totalPnl);
             return;
         }
 
@@ -517,13 +526,17 @@
             return;
         }
         cumulativePnl = cumulativePnl.add(pnl);
-        log.info("[Gate] 盈亏累加:{}, 方向:{}, 累计:{}", pnl, side, cumulativePnl);
+        BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
+        log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}",
+                cumulativePnl, unrealizedPnl, totalPnl);
 
-        if (cumulativePnl.compareTo(config.getOverallTp()) >= 0) {
-            log.info("[Gate] 已达止盈目标 {}→已停止", cumulativePnl);
+        if (totalPnl.compareTo(config.getOverallTp()) >= 0) {
+            log.info("[Gate] 已达止盈目标(合计{})→已停止, 已实现:{}, 未实现:{}",
+                    totalPnl, cumulativePnl, unrealizedPnl);
             state = StrategyState.STOPPED;
-        } else if (cumulativePnl.compareTo(config.getMaxLoss().negate()) <= 0) {
-            log.info("[Gate] 已达亏损上限 {}→已停止", cumulativePnl);
+        } else if (totalPnl.compareTo(config.getMaxLoss().negate()) <= 0) {
+            log.info("[Gate] 已达亏损上限(合计{})→已停止, 已实现:{}, 未实现:{}",
+                    totalPnl, cumulativePnl, unrealizedPnl);
             state = StrategyState.STOPPED;
         }
     }
@@ -557,11 +570,11 @@
                     null,
                     false
             );
-            longEntryTraderIdParam(
-                    byLongTakeProfitOrderId,
-                    null,
-                    false
-            );
+//            longEntryTraderIdParam(
+//                    byLongTakeProfitOrderId,
+//                    null,
+//                    false
+//            );
         }
         GridElement byShortTakeProfitOrderId = GridElement.findByShortTakeProfitOrderId(orderId);
         if (byShortTakeProfitOrderId != null){
@@ -570,11 +583,11 @@
                     null,
                     false
             );
-            shortEntryTraderIdParam(
-                    byShortTakeProfitOrderId,
-                    null,
-                    false
-            );
+//            shortEntryTraderIdParam(
+//                    byShortTakeProfitOrderId,
+//                    null,
+//                    false
+//            );
         }
 
         /**
@@ -583,6 +596,11 @@
         GridElement longGridElement = GridElement.findByLongOrderId(orderId);
         if (longGridElement != null) {
             if (longGridElement.isHasLongOrder()){
+                longEntryTraderIdParam(
+                        longGridElement,
+                        null,
+                        false
+                );
                 if (longGridElement.getLongTakeProfitOrderId() == null){
                     BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice();
                     if (longTp != null) {
@@ -606,6 +624,11 @@
         GridElement shortGridElement = GridElement.findByShortOrderId(orderId);
         if (shortGridElement != null) {
             if (shortGridElement.isHasShortOrder()){
+                shortEntryTraderIdParam(
+                        shortGridElement,
+                        null,
+                        false
+                );
                 if (shortGridElement.getShortTakeProfitOrderId() == null){
                     BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice();
                     if (shortTp != null) {
@@ -702,6 +725,8 @@
         GridElement longGridElement = GridElement.findByLongOrderId(orderId);
         if (longGridElement != null) {
             if (longGridElement.isHasLongOrder() && !tradeId.equals("0")){
+
+                onUserTradeLongEntry(longGridElement);
                 if (longGridElement.getLongTakeProfitOrderId() == null){
                     BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice();
                     if (longTp != null) {
@@ -725,6 +750,8 @@
         GridElement shortGridElement = GridElement.findByShortOrderId(orderId);
         if (shortGridElement != null) {
             if (shortGridElement.isHasShortOrder() && !tradeId.equals("0")){
+
+                onUserTradeShortEntry(shortGridElement);
                 if (shortGridElement.getShortTakeProfitOrderId() == null){
                     BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice();
                     if (shortTp != null) {
@@ -740,6 +767,191 @@
                                     );
                                 });
                         log.info("[Gate] 空单成交匹配止盈, orderId:{}, 止盈价:{}, size:{}", orderId, shortTp, config.getQuantity());
+                    }
+                }
+            }
+        }
+    }
+
+    private void onUserTradeShortEntry(GridElement gridElement) {
+        if (!isMarginSafe()) {
+            log.warn("[Gate] 保证金超限,跳过挂条件单");
+        } else {
+
+            /**
+             * 下一个开仓位置
+             *      获取队列第一个元素的价格对应的网格
+             *      判断网格是否能开空仓,如果不能则跳过
+             *      前进方向挂空仓条件单
+             *      后置方向挂多空条件单
+             */
+            //下一个开仓位置
+            GridElement UpGridElement = GridElement.findById(gridElement.getDownId());
+            BigDecimal newLongFirst = UpGridElement.getGridPrice();
+
+            // 判断网格是否能开空仓,如果不能则跳过
+            if (UpGridElement != null) {
+
+                if (!UpGridElement.isHasShortOrder() && shortEntryPrice.compareTo(newLongFirst) > 0) {
+
+                    //挂空仓条件单
+                    TraderParam upShortTraderParam = UpGridElement.getShortTraderParam();
+                    executor.placeConditionalEntryOrder(
+                            upShortTraderParam.getEntryPrice(),
+                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
+                            negate(upShortTraderParam.getQuantity()),
+                            orderId ->
+                            {
+                                shortEntryTraderIdParam(
+                                        UpGridElement,
+                                        orderId,
+                                        true
+                                );
+                            },
+                            null
+                    );
+                }
+                int i = gridElement.getUpId();
+                GridElement downGridElement = GridElement.findById(i);
+                if (downGridElement != null){
+
+                    BigDecimal downGridPrice = downGridElement.getGridPrice();
+
+                    TraderParam downShortTraderParam = downGridElement.getShortTraderParam();
+                    if (
+                            !downGridElement.isHasShortOrder() &&
+                                    downGridPrice.compareTo(longEntryPrice) <= 0 &&
+                                    downGridPrice.compareTo(shortEntryPrice) >= 0
+                    ){
+                        executor.placeConditionalEntryOrder(
+                                downShortTraderParam.getEntryPrice(),
+                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
+                                negate(downShortTraderParam.getQuantity()),
+                                orderId ->
+                                {
+                                    shortEntryTraderIdParam(
+                                            downGridElement,
+                                            orderId,
+                                            true
+                                    );
+                                },
+                                null
+                        );
+
+                    }
+
+                    TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
+                    if (
+                            !downGridElement.isHasLongOrder() &&
+                                    downGridPrice.compareTo(longEntryPrice) <= 0
+                    ){
+                        executor.placeConditionalEntryOrder(
+                                downLongTraderParam.getEntryPrice(),
+                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
+                                downLongTraderParam.getQuantity(),
+                                orderId ->
+                                {
+                                    longEntryTraderIdParam(
+                                            downGridElement,
+                                            orderId,
+                                            true
+                                    );
+                                },
+                                null
+                        );
+                    }
+                }
+            }
+        }
+    }
+
+    private void onUserTradeLongEntry(GridElement gridElement) {
+        if (!isMarginSafe()) {
+            log.warn("[Gate] 保证金超限,跳过挂条件单");
+        } else {
+
+            /**
+             * 下一个开仓位置
+             *      获取队列第一个元素的价格对应的网格
+             *      判断网格是否能开多仓,如果不能则跳过
+             *      前进方向挂多仓条件单
+             *      后置方向挂多空条件单
+             */
+            //下一个开仓位置
+            GridElement UpGridElement = GridElement.findById(gridElement.getUpId());
+            BigDecimal newLongFirst = UpGridElement.getGridPrice() ;
+
+            // 判断网格是否能开多仓,如果不能则跳过
+            if (UpGridElement != null) {
+
+                if (!UpGridElement.isHasLongOrder() && longEntryPrice.compareTo(newLongFirst) < 0) {
+                    //挂多仓条件单
+                    TraderParam upLongTraderParam = UpGridElement.getLongTraderParam();
+                    executor.placeConditionalEntryOrder(
+                            upLongTraderParam.getEntryPrice(),
+                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
+                            config.getQuantity(),
+                            orderId ->
+                            {
+                                longEntryTraderIdParam(
+                                        UpGridElement,
+                                        orderId,
+                                        true
+                                );
+                            },
+                            null
+                    );
+                }
+
+                int i = gridElement.getDownId();
+                GridElement downGridElement = GridElement.findById(i);
+                if (downGridElement != null){
+
+                    BigDecimal downGridPrice = downGridElement.getGridPrice();
+
+                    TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
+                    if (
+                            !downGridElement.isHasLongOrder() &&
+                                    downGridPrice.compareTo(shortEntryPrice) >= 0 &&
+                                    downGridPrice.compareTo(longEntryPrice) <= 0
+                    ){
+                        executor.placeConditionalEntryOrder(
+                                downLongTraderParam.getEntryPrice(),
+                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
+                                config.getQuantity(),
+                                orderId ->
+                                {
+                                    longEntryTraderIdParam(
+                                            downGridElement,
+                                            orderId,
+                                            true
+                                    );
+                                },
+                                null
+                        );
+
+                    }
+
+                    TraderParam shortTraderParam = downGridElement.getShortTraderParam();
+                    if (
+                            !downGridElement.isHasShortOrder() &&
+                                    downGridPrice.compareTo(shortEntryPrice) >= 0
+                    ){
+
+                        executor.placeConditionalEntryOrder(
+                                shortTraderParam.getEntryPrice(),
+                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
+                                negate(config.getQuantity()),
+                                orderId ->
+                                {
+                                    shortEntryTraderIdParam(
+                                            downGridElement,
+                                            orderId,
+                                            true
+                                    );
+                                },
+                                null
+                        );
                     }
                 }
             }
@@ -1104,7 +1316,7 @@
             // 判断网格是否能开空仓,如果不能则跳过
             if (UpGridElement != null) {
 
-                if (!UpGridElement.isHasShortOrder()) {
+                if (!UpGridElement.isHasShortOrder() && shortEntryPrice.compareTo(newLongFirst) > 0) {
 
                     //挂空仓条件单
                     TraderParam upShortTraderParam = UpGridElement.getShortTraderParam();
@@ -1241,7 +1453,7 @@
             // 判断网格是否能开多仓,如果不能则跳过
             if (UpGridElement != null) {
 
-                if (!UpGridElement.isHasLongOrder()) {
+                if (!UpGridElement.isHasLongOrder() && longEntryPrice.compareTo(newLongFirst) < 0) {
                     //挂多仓条件单
                     TraderParam upLongTraderParam = UpGridElement.getLongTraderParam();
                     executor.placeConditionalEntryOrder(

--
Gitblit v1.9.1