From 526dc9bd7e7e71e5e18c4dd966d3996e628ae6fa Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 21 May 2026 10:14:33 +0800
Subject: [PATCH] fix(gateApi): 更新网格交易盈亏计算逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 55 ++++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 42 insertions(+), 13 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 02c2304..4fd0216 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -421,7 +421,7 @@
List<GridElement> allShortOrders = GridElement.findAllShortOrders(longEntryPrice);
if (CollUtil.isNotEmpty(allShortOrders)){
for (GridElement e : allShortOrders) {
- executor.cancelOrder(
+ executor.cancelConditionalOrder(
e.getShortOrderId(),
orderId -> {
shortEntryTraderIdParam(
@@ -431,6 +431,19 @@
);
}
);
+
+ if (e.getShortTakeProfitOrderId() != null){
+ executor.cancelConditionalOrder(
+ e.getShortTakeProfitOrderId(),
+ orderId -> {
+ shortTakeProfitTraderIdParam(
+ e,
+ null,
+ false
+ );
+ }
+ );
+ }
}
}
}
@@ -454,16 +467,28 @@
List<GridElement> allLongOrders = GridElement.findAllLongOrders(shortEntryPrice);
if (CollUtil.isNotEmpty(allLongOrders)){
for (GridElement e : allLongOrders) {
- executor.cancelOrder(
+ executor.cancelConditionalOrder(
e.getLongOrderId(),
orderId -> {
- shortEntryTraderIdParam(
+ longEntryTraderIdParam(
e,
null,
false
);
}
);
+ if (e.getLongTakeProfitOrderId() != null){
+ executor.cancelConditionalOrder(
+ e.getLongTakeProfitOrderId(),
+ orderId -> {
+ longTakeProfitTraderIdParam(
+ e,
+ null,
+ false
+ );
+ }
+ );
+ }
}
}
}
@@ -492,13 +517,17 @@
return;
}
cumulativePnl = cumulativePnl.add(pnl);
- log.info("[Gate] 盈亏累加:{}, 方向:{}, 累计:{}", pnl, side, cumulativePnl);
+ BigDecimal totalPnl = cumulativePnl.add(unrealizedPnl);
+ log.info("[Gate] 盈亏累加:{}, 方向:{}, 已实现:{}, 未实现:{}, 合计:{}",
+ pnl, side, 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;
}
}
@@ -631,7 +660,7 @@
* @param reason 变更原因
* @param orderType 订单类型(plan-close-long-position 等)
*/
- public void onAutoOrder(String orderId, String status, String reason, String orderType) {
+ public void onAutoOrder(String orderId, String status, String reason, String orderType, String tradeId) {
if (state == StrategyState.STOPPED) {
return;
}
@@ -676,7 +705,7 @@
*/
GridElement longGridElement = GridElement.findByLongOrderId(orderId);
if (longGridElement != null) {
- if (longGridElement.isHasLongOrder()){
+ if (longGridElement.isHasLongOrder() && !tradeId.equals("0")){
if (longGridElement.getLongTakeProfitOrderId() == null){
BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice();
if (longTp != null) {
@@ -699,7 +728,7 @@
}
GridElement shortGridElement = GridElement.findByShortOrderId(orderId);
if (shortGridElement != null) {
- if (shortGridElement.isHasShortOrder()){
+ if (shortGridElement.isHasShortOrder() && !tradeId.equals("0")){
if (shortGridElement.getShortTakeProfitOrderId() == null){
BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice();
if (shortTp != null) {
@@ -1112,7 +1141,7 @@
){
executor.placeConditionalEntryOrder(
downShortTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ FuturesPriceTrigger.RuleEnum.NUMBER_1,
negate(downShortTraderParam.getQuantity()),
orderId ->
{
@@ -1134,7 +1163,7 @@
){
executor.placeConditionalEntryOrder(
downLongTraderParam.getEntryPrice(),
- FuturesPriceTrigger.RuleEnum.NUMBER_2,
+ FuturesPriceTrigger.RuleEnum.NUMBER_1,
downLongTraderParam.getQuantity(),
orderId ->
{
--
Gitblit v1.9.1