From 8beede41ea527872eee2572f2271110ac7a14d9a Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 19 May 2026 10:11:37 +0800
Subject: [PATCH] 第二个版本

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |   25 ++++++++++++++++++++++---
 1 files changed, 22 insertions(+), 3 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 4b4e83c..5150891 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1,5 +1,6 @@
 package com.xcong.excoin.modules.gateApi;
 
+import cn.hutool.core.collection.CollUtil;
 import io.gate.gateapi.ApiClient;
 import io.gate.gateapi.ApiException;
 import io.gate.gateapi.GateApiException;
@@ -432,6 +433,13 @@
                     tryGenerateQueues();
                 }else {
                     longPositionSize = size;
+                    //取消多仓位线以上的开空仓挂单
+                    List<GridElement> allShortOrders = GridElement.findAllShortOrders(longEntryPrice);
+                    if (CollUtil.isNotEmpty(allShortOrders)){
+                        for (GridElement e : allShortOrders) {
+                            executor.cancelOrder(e.getShortOrderId());
+                        }
+                    }
                 }
             } else {
                 longActive = false;
@@ -449,6 +457,13 @@
                     tryGenerateQueues();
                 }else {
                     shortPositionSize = size.abs();
+                    //取消多仓位线以上的开空仓挂单
+                    List<GridElement> allLongOrders = GridElement.findAllLongOrders(shortEntryPrice);
+                    if (CollUtil.isNotEmpty(allLongOrders)){
+                        for (GridElement e : allLongOrders) {
+                            executor.cancelOrder(e.getShortOrderId());
+                        }
+                    }
                 }
             } else {
                 shortActive = false;
@@ -616,6 +631,7 @@
             GridElement baseGridElement = GridElement.findById(0);
             TraderParam baseLongTraderParam = config.getBaseLongTraderParam();
             baseGridElement.setLongOrderId(baseLongTraderParam.getEntryOrderId());
+            baseGridElement.setHasLongOrder(true);
             //0位置的网格的多单止盈
             BigDecimal upTakeProfitPrice = baseGridElement.getLongTraderParam().getTakeProfitPrice();
             executor.placeTakeProfit(
@@ -634,6 +650,7 @@
             //0位置的网格的空单止盈
             TraderParam baseShortTraderParam = config.getBaseShortTraderParam();
             baseGridElement.setShortOrderId(baseShortTraderParam.getEntryOrderId());
+            baseGridElement.setHasShortOrder(true);
             BigDecimal downTakeProfitPrice = baseGridElement.getShortTraderParam().getTakeProfitPrice();
             executor.placeTakeProfit(
                     downTakeProfitPrice,
@@ -911,7 +928,7 @@
         List<BigDecimal> matched = new ArrayList<>();
         synchronized (shortPriceQueue) {
             for (BigDecimal p : shortPriceQueue) {
-                if (p.compareTo(currentPrice) > 0) {
+                if (p.compareTo(currentPrice) >= 0) {
                     matched.add(p);
                 } else {
                     break;
@@ -993,7 +1010,7 @@
             if (downGridElement != null){
 
                 TraderParam downLongTraderParam = downGridElement.getLongTraderParam();
-                if (!downGridElement.isHasShortOrder()){
+                if (!downGridElement.isHasLongOrder()){
                     executor.placeConditionalEntryOrder(
                             downLongTraderParam.getEntryPrice(),
                             FuturesPriceTrigger.RuleEnum.NUMBER_1,
@@ -1014,6 +1031,7 @@
                 BigDecimal downGridPrice = downGridElement.getGridPrice();
                 if (
                         !downGridElement.isHasShortOrder() &&
+                                downGridPrice.compareTo(currentPrice) < 0 &&
                                 downGridPrice.compareTo(longEntryPrice) <= 0 &&
                                 downGridPrice.compareTo(shortEntryPrice) >= 0
                 ){
@@ -1068,7 +1086,7 @@
         List<BigDecimal> matched = new ArrayList<>();
         synchronized (longPriceQueue) {
             for (BigDecimal p : longPriceQueue) {
-                if (p.compareTo(currentPrice) < 0) {
+                if (p.compareTo(currentPrice) <= 0) {
                     matched.add(p);
                 } else {
                     break;
@@ -1175,6 +1193,7 @@
                 BigDecimal downGridPrice = downGridElement.getGridPrice();
                 if (
                         !downGridElement.isHasLongOrder() &&
+                                downGridPrice.compareTo(currentPrice) > 0 &&
                                 downGridPrice.compareTo(longEntryPrice) <= 0 &&
                                 downGridPrice.compareTo(shortEntryPrice) >= 0
                 ){

--
Gitblit v1.9.1