From 1b5c1005f3b46e60cbf08d5ae7924b23765084eb Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 26 May 2026 10:08:02 +0800
Subject: [PATCH] fix(gateApi): 修复网格交易中多空仓位订单取消逻辑错误

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |  140 +++++++++++++++++++++++-----------------------
 1 files changed, 71 insertions(+), 69 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 f953866..4e0776f 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -377,8 +377,8 @@
         if (state != StrategyState.ACTIVE) {
             return;
         }
-//        processLongGrid(closePrice);
-//        processShortGrid(closePrice);
+        processLongGrid(closePrice);
+        processShortGrid(closePrice);
     }
 
     // ---- 仓位推送回调 ----
@@ -426,14 +426,14 @@
                     tryGenerateQueues();
                 }else {
                     longPositionSize = size;
-                    //取消多仓位线以上的开空仓挂单
+                    //取消多仓位线以上的开多仓挂单
                     List<GridElement> allShortOrders = GridElement.findAllShortOrders(longEntryPrice);
                     if (CollUtil.isNotEmpty(allShortOrders)){
                         for (GridElement e : allShortOrders) {
                             executor.cancelConditionalOrder(
-                                    e.getShortOrderId(),
+                                    e.getLongOrderId(),
                                     orderId -> {
-                                        shortEntryTraderIdParam(
+                                        longEntryTraderIdParam(
                                                 e,
                                                 null,
                                                 false
@@ -441,11 +441,11 @@
                                     }
                             );
 
-                            if (e.getShortTakeProfitOrderId() != null){
+                            if (e.getLongTakeProfitOrderId() != null){
                                 executor.cancelConditionalOrder(
-                                        e.getShortTakeProfitOrderId(),
+                                        e.getLongTakeProfitOrderId(),
                                         orderId -> {
-                                            shortTakeProfitTraderIdParam(
+                                            longTakeProfitTraderIdParam(
                                                     e,
                                                     null,
                                                     false
@@ -472,25 +472,25 @@
                     tryGenerateQueues();
                 }else {
                     shortPositionSize = size.abs();
-                    //取消空仓仓位线以下的开多仓挂单
+                    //取消空仓仓位线以下的开空仓挂单
                     List<GridElement> allLongOrders = GridElement.findAllLongOrders(shortEntryPrice);
                     if (CollUtil.isNotEmpty(allLongOrders)){
                         for (GridElement e : allLongOrders) {
                             executor.cancelConditionalOrder(
-                                    e.getLongOrderId(),
+                                    e.getShortOrderId(),
                                     orderId -> {
-                                        longEntryTraderIdParam(
+                                        shortEntryTraderIdParam(
                                                 e,
                                                 null,
                                                 false
                                         );
                                     }
                             );
-                            if (e.getLongTakeProfitOrderId() != null){
+                            if (e.getShortTakeProfitOrderId() != null){
                                 executor.cancelConditionalOrder(
-                                        e.getLongTakeProfitOrderId(),
+                                        e.getShortTakeProfitOrderId(),
                                         orderId -> {
-                                            longTakeProfitTraderIdParam(
+                                            shortTakeProfitTraderIdParam(
                                                     e,
                                                     null,
                                                     false
@@ -526,6 +526,7 @@
             return;
         }
         cumulativePnl = cumulativePnl.add(pnl);
+        updateUnrealizedPnl();
         BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
         log.info("[Gate] 已实现:{}, 未实现:{}, 合计:{}",
                 cumulativePnl, unrealizedPnl, totalPnl);
@@ -704,7 +705,7 @@
                     null,
                     false
             );
-            TPonUserTradeShortEntry(byShortTakeProfitOrderId);
+//            TPonUserTradeShortEntry(byShortTakeProfitOrderId);
         }
         GridElement byLongTakeProfitOrderId = GridElement.findByLongTakeProfitOrderId(orderId);
         if (byLongTakeProfitOrderId != null){
@@ -718,7 +719,7 @@
                     null,
                     false
             );
-            TPonUserTradeLongEntry(byLongTakeProfitOrderId);
+//            TPonUserTradeLongEntry(byLongTakeProfitOrderId);
         }
 
         /**
@@ -829,18 +830,18 @@
             // 判断网格是否能开多空仓,如果不能则跳过
             if (gridElement != null) {
 
-                TraderParam downLongTraderParam = gridElement.getLongTraderParam();
-                if (
-                        !gridElement.isHasLongOrder() &&
-                                newLongFirst.compareTo(shortEntryPrice) >= 0 &&
-                                newLongFirst.compareTo(longEntryPrice) <= 0
-                ){
-                    placeEntryOrderWithPreFlag(gridElement, true,
-                            downLongTraderParam.getEntryPrice(),
-                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
-                            config.getQuantity());
-
-                }
+//                TraderParam downLongTraderParam = gridElement.getLongTraderParam();
+//                if (
+//                        !gridElement.isHasLongOrder() &&
+//                                newLongFirst.compareTo(shortEntryPrice) >= 0 &&
+//                                newLongFirst.compareTo(longEntryPrice) <= 0
+//                ){
+//                    placeEntryOrderWithPreFlag(gridElement, true,
+//                            downLongTraderParam.getEntryPrice(),
+//                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
+//                            config.getQuantity());
+//
+//                }
 
                 TraderParam shortTraderParam = gridElement.getShortTraderParam();
                 if (
@@ -1096,8 +1097,9 @@
         int longSize = longPriceQueue.size();
         //根据精度转换成小数
         int prec = config.getPriceScale();
-        BigDecimal minTick = BigDecimal.ONE.scaleByPowerOfTen(-prec);
-        BigDecimal step = config.getStep().subtract(minTick);
+//        BigDecimal minTick = BigDecimal.ONE.scaleByPowerOfTen(-prec);
+//        BigDecimal step = config.getStep().subtract(minTick);
+        BigDecimal step = config.getStep();
         String qty = config.getQuantity();
 
         // 空仓队列:id 从 -1 自减, shortPriceQueue[i] → id=-(i+1)
@@ -1244,32 +1246,32 @@
             // 判断网格是否能开空仓,如果不能则跳过
             if (UpGridElement != null) {
 
-                if (!UpGridElement.isHasShortOrder() && shortEntryPrice.compareTo(newLongFirst) > 0) {
-
-                    TraderParam upShortTraderParam = UpGridElement.getShortTraderParam();
-                    placeEntryOrderWithPreFlag(UpGridElement, false,
-                            upShortTraderParam.getEntryPrice(),
-                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
-                            negate(upShortTraderParam.getQuantity()));
-                }
+//                if (!UpGridElement.isHasShortOrder() && shortEntryPrice.compareTo(newLongFirst) > 0) {
+//
+//                    TraderParam upShortTraderParam = UpGridElement.getShortTraderParam();
+//                    placeEntryOrderWithPreFlag(UpGridElement, false,
+//                            upShortTraderParam.getEntryPrice(),
+//                            FuturesPriceTrigger.RuleEnum.NUMBER_2,
+//                            negate(upShortTraderParam.getQuantity()));
+//                }
                 int i = UpGridElement.getId() + 2;
                 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
-                    ){
-                        placeEntryOrderWithPreFlag(downGridElement, false,
-                                downShortTraderParam.getEntryPrice(),
-                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
-                                negate(downShortTraderParam.getQuantity()));
-
-                    }
+//                    TraderParam downShortTraderParam = downGridElement.getShortTraderParam();
+//                    if (
+//                            !downGridElement.isHasShortOrder() &&
+//                                    downGridPrice.compareTo(longEntryPrice) <= 0 &&
+//                                    downGridPrice.compareTo(shortEntryPrice) >= 0
+//                    ){
+//                        placeEntryOrderWithPreFlag(downGridElement, false,
+//                                downShortTraderParam.getEntryPrice(),
+//                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
+//                                negate(downShortTraderParam.getQuantity()));
+//
+//                    }
 
                     TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
                     if (
@@ -1350,13 +1352,13 @@
             // 判断网格是否能开多仓,如果不能则跳过
             if (UpGridElement != null) {
 
-                if (!UpGridElement.isHasLongOrder() && longEntryPrice.compareTo(newLongFirst) < 0) {
-                    TraderParam upLongTraderParam = UpGridElement.getLongTraderParam();
-                    placeEntryOrderWithPreFlag(UpGridElement, true,
-                            upLongTraderParam.getEntryPrice(),
-                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
-                            config.getQuantity());
-                }
+//                if (!UpGridElement.isHasLongOrder() && longEntryPrice.compareTo(newLongFirst) < 0) {
+//                    TraderParam upLongTraderParam = UpGridElement.getLongTraderParam();
+//                    placeEntryOrderWithPreFlag(UpGridElement, true,
+//                            upLongTraderParam.getEntryPrice(),
+//                            FuturesPriceTrigger.RuleEnum.NUMBER_1,
+//                            config.getQuantity());
+//                }
 
                 int i = UpGridElement.getId() - 2;
                 GridElement downGridElement = GridElement.findById(i);
@@ -1364,18 +1366,18 @@
 
                     BigDecimal downGridPrice = downGridElement.getGridPrice();
 
-                    TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
-                    if (
-                            !downGridElement.isHasLongOrder() &&
-                                    downGridPrice.compareTo(shortEntryPrice) >= 0 &&
-                                    downGridPrice.compareTo(longEntryPrice) <= 0
-                    ){
-                        placeEntryOrderWithPreFlag(downGridElement, true,
-                                downLongTraderParam.getEntryPrice(),
-                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
-                                config.getQuantity());
-
-                    }
+//                    TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
+//                    if (
+//                            !downGridElement.isHasLongOrder() &&
+//                                    downGridPrice.compareTo(shortEntryPrice) >= 0 &&
+//                                    downGridPrice.compareTo(longEntryPrice) <= 0
+//                    ){
+//                        placeEntryOrderWithPreFlag(downGridElement, true,
+//                                downLongTraderParam.getEntryPrice(),
+//                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
+//                                config.getQuantity());
+//
+//                    }
 
                     TraderParam shortTraderParam = downGridElement.getShortTraderParam();
                     if (

--
Gitblit v1.9.1