From b7a2fa0ea40b15bd5b60d737cb96764884dcaaaa Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 28 May 2026 11:07:12 +0800
Subject: [PATCH] config(gateApi): 更新任意仓位变化,检查多空两边那些需要同时取消挂单

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java |  154 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 85 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 71dbafd..cf1c956 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -1,6 +1,8 @@
 package com.xcong.excoin.modules.gateApi;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.StrUtil;
+import com.xcong.excoin.utils.dingtalk.DingTalkUtils;
 import io.gate.gateapi.ApiClient;
 import io.gate.gateapi.ApiException;
 import io.gate.gateapi.GateApiException;
@@ -426,40 +428,8 @@
                     tryGenerateQueues();
                 }else {
                     longPositionSize = size;
-                    List<GridElement> allShortOrders = GridElement.findAllLongOrders(longEntryPrice);
-                    if (CollUtil.isNotEmpty(allShortOrders)){
-                        GridElement keep = allShortOrders.stream()
-                                .max((a, b) -> a.getGridPrice().compareTo(b.getGridPrice()))
-                                .orElse(null);
-                        for (GridElement e : allShortOrders) {
-                            if (e == keep) {
-                                continue;
-                            }
-                            executor.cancelConditionalOrder(
-                                    e.getLongOrderId(),
-                                    orderId -> {
-                                        longEntryTraderIdParam(
-                                                e,
-                                                null,
-                                                false
-                                        );
-                                    }
-                            );
-
-                            if (e.getLongTakeProfitOrderId() != null){
-                                executor.cancelConditionalOrder(
-                                        e.getLongTakeProfitOrderId(),
-                                        orderId -> {
-                                            longTakeProfitTraderIdParam(
-                                                    e,
-                                                    null,
-                                                    false
-                                            );
-                                        }
-                                );
-                            }
-                        }
-                    }
+                    checkShortEntryOrderToCancel();
+                    checkLongEntryOrderToCancel();
                 }
             } else {
                 longActive = false;
@@ -477,43 +447,85 @@
                     tryGenerateQueues();
                 }else {
                     shortPositionSize = size.abs();
-                    List<GridElement> allLongOrders = GridElement.findAllShortOrders(shortEntryPrice);
-                    if (CollUtil.isNotEmpty(allLongOrders)){
-                        GridElement keep = allLongOrders.stream()
-                                .min((a, b) -> a.getGridPrice().compareTo(b.getGridPrice()))
-                                .orElse(null);
-                        for (GridElement e : allLongOrders) {
-                            if (e == keep) {
-                                continue;
-                            }
-                            executor.cancelConditionalOrder(
-                                    e.getShortOrderId(),
-                                    orderId -> {
-                                        shortEntryTraderIdParam(
-                                                e,
-                                                null,
-                                                false
-                                        );
-                                    }
-                            );
-                            if (e.getShortTakeProfitOrderId() != null){
-                                executor.cancelConditionalOrder(
-                                        e.getShortTakeProfitOrderId(),
-                                        orderId -> {
-                                            shortTakeProfitTraderIdParam(
-                                                    e,
-                                                    null,
-                                                    false
-                                            );
-                                        }
-                                );
-                            }
-                        }
-                    }
+                    checkShortEntryOrderToCancel();
+                    checkLongEntryOrderToCancel();
                 }
             } else {
                 shortActive = false;
                 shortPositionSize = BigDecimal.ZERO;
+            }
+        }
+    }
+
+    private void checkShortEntryOrderToCancel() {
+        List<GridElement> allLongOrders = GridElement.findAllShortOrders(shortEntryPrice);
+        if (CollUtil.isNotEmpty(allLongOrders)){
+            GridElement keep = allLongOrders.stream()
+                    .min((a, b) -> a.getGridPrice().compareTo(b.getGridPrice()))
+                    .orElse(null);
+            for (GridElement e : allLongOrders) {
+                if (e == keep) {
+                    continue;
+                }
+                executor.cancelConditionalOrder(
+                        e.getShortOrderId(),
+                        orderId -> {
+                            shortEntryTraderIdParam(
+                                    e,
+                                    null,
+                                    false
+                            );
+                        }
+                );
+                if (e.getShortTakeProfitOrderId() != null){
+                    executor.cancelConditionalOrder(
+                            e.getShortTakeProfitOrderId(),
+                            orderId -> {
+                                shortTakeProfitTraderIdParam(
+                                        e,
+                                        null,
+                                        false
+                                );
+                            }
+                    );
+                }
+            }
+        }
+    }
+
+    private void checkLongEntryOrderToCancel() {
+        List<GridElement> allShortOrders = GridElement.findAllLongOrders(longEntryPrice);
+        if (CollUtil.isNotEmpty(allShortOrders)){
+            GridElement keep = allShortOrders.stream()
+                    .max((a, b) -> a.getGridPrice().compareTo(b.getGridPrice()))
+                    .orElse(null);
+            for (GridElement e : allShortOrders) {
+                if (e == keep) {
+                    continue;
+                }
+                executor.cancelConditionalOrder(
+                        e.getLongOrderId(),
+                        orderId -> {
+                            longEntryTraderIdParam(
+                                    e,
+                                    null,
+                                    false
+                            );
+                        }
+                );
+
+                if (e.getLongTakeProfitOrderId() != null){
+                    executor.cancelConditionalOrder(
+                            e.getLongTakeProfitOrderId(),
+                            orderId -> {
+                                longTakeProfitTraderIdParam(
+                                        e,
+                                        null,
+                                        false
+                                );
+                            }
+                    );
+                }
             }
         }
     }
@@ -546,9 +558,13 @@
                     totalPnl, cumulativePnl, unrealizedPnl);
             state = StrategyState.STOPPED;
         } else if (totalPnl.compareTo(config.getMaxLoss().negate()) <= 0) {
-            log.info("[Gate] 已达亏损上限(合计{})→已停止, 已实现:{}, 未实现:{}",
+            String logMessage = StrUtil.format("[Gate] 已达亏损风险值(合计{}), 已实现:{}, 未实现:{}",
                     totalPnl, cumulativePnl, unrealizedPnl);
-            state = StrategyState.STOPPED;
+            log.info(logMessage);
+
+
+            DingTalkUtils.getDefault().sendActionCard("风险提醒", logMessage, config.getApiKey(), "");
+//            state = StrategyState.STOPPED;
         }
     }
 

--
Gitblit v1.9.1