From d22108cd8800df3f8d4ab9f7b125ece79639faf3 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 11 May 2026 10:21:51 +0800
Subject: [PATCH] fix(trade): 优化网格交易日志输出并实现市价止盈机制
---
src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java | 21 ++++++++++++++-------
src/main/java/com/xcong/excoin/modules/gateApi/celue.out | 0
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java | 5 +----
src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java | 23 ++++++++++++++++++++++-
4 files changed, 37 insertions(+), 12 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 d9c2310..b51f95d 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -453,8 +453,10 @@
for (int i = 0; i < matched.size(); i++) {
min = min.multiply(BigDecimal.ONE.subtract(step)).setScale(1, RoundingMode.HALF_UP);
shortPriceQueue.add(min);
+ log.info("[Gate] 空队列增加:{}", min);
}
- shortPriceQueue.sort(BigDecimal::compareTo);
+
+ shortPriceQueue.sort((a, b) -> b.compareTo(a));
log.info("[Gate] 现空队列:{}", shortPriceQueue);
}
@@ -462,21 +464,22 @@
synchronized (longPriceQueue) {
BigDecimal first = longPriceQueue.isEmpty() ? matched.get(matched.size() - 1) : longPriceQueue.get(0);
BigDecimal step = config.getGridRate();
- int added = 0;
for (int i = 1; i <= matched.size(); i++) {
BigDecimal elem = first.multiply(BigDecimal.ONE.subtract(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
if (longEntryPrice.compareTo(BigDecimal.ZERO) > 0
&& elem.subtract(longEntryPrice).abs().compareTo(longEntryPrice.multiply(step)) < 0) {
+ log.info("[Gate] 多队列跳过:{}", elem);
continue;
}
longPriceQueue.add(elem);
- added++;
+
+ log.info("[Gate] 多队列增加:{}", elem);
}
longPriceQueue.sort(BigDecimal::compareTo);
while (longPriceQueue.size() > config.getGridQueueSize()) {
longPriceQueue.remove(longPriceQueue.size() - 1);
}
- log.info("[Gate] 现多队列:{}, 跳过{}个(贴近多仓均价)", longPriceQueue, matched.size() - added);
+ log.info("[Gate] 现多队列:{}", longPriceQueue);
}
}
@@ -538,6 +541,8 @@
for (int i = 0; i < matched.size(); i++) {
max = max.multiply(BigDecimal.ONE.add(step)).setScale(1, RoundingMode.HALF_UP);
longPriceQueue.add(max);
+
+ log.info("[Gate] 多队列增加:{}", max);
}
longPriceQueue.sort(BigDecimal::compareTo);
@@ -547,21 +552,23 @@
synchronized (shortPriceQueue) {
BigDecimal first = shortPriceQueue.isEmpty() ? matched.get(0) : shortPriceQueue.get(0);
BigDecimal step = config.getGridRate();
- int added = 0;
for (int i = 1; i <= matched.size(); i++) {
BigDecimal elem = first.multiply(BigDecimal.ONE.add(step.multiply(BigDecimal.valueOf(i)))).setScale(1, RoundingMode.HALF_UP);
if (shortEntryPrice.compareTo(BigDecimal.ZERO) > 0
&& elem.subtract(shortEntryPrice).abs().compareTo(shortEntryPrice.multiply(step)) < 0) {
+
+ log.info("[Gate] 空队列跳过:{}", elem);
continue;
}
shortPriceQueue.add(elem);
- added++;
+
+ log.info("[Gate] 空队列增加:{}", elem);
}
shortPriceQueue.sort((a, b) -> b.compareTo(a));
while (shortPriceQueue.size() > config.getGridQueueSize()) {
shortPriceQueue.remove(shortPriceQueue.size() - 1);
}
- log.info("[Gate] 现空队列:{}, 跳过{}个(贴近空仓均价)", shortPriceQueue, matched.size() - added);
+ log.info("[Gate] 现空队列:{}", shortPriceQueue);
}
}
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java
index 4e695c3..eac6c1e 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateTradeExecutor.java
@@ -150,12 +150,33 @@
log.info("[TradeExec] 止盈单已创建, 触发价:{}, 类型:{}, size:{}, id:{}",
triggerPrice, orderType, size, response.getId());
} catch (Exception e) {
- log.error("[TradeExec] 止盈单创建失败, 触发价:{}, size:{}", triggerPrice, size, e);
+ log.error("[TradeExec] 止盈单创建失败, 触发价:{}, size:{}, 立即市价止盈", triggerPrice, size, e);
+ marketClose(size);
}
});
}
/**
+ * 市价止盈:在止盈条件单创建失败时立即市价平仓。
+ * size 与止盈单保持一致(负=平多,正=平空)。
+ */
+ private void marketClose(String size) {
+ try {
+ FuturesOrder order = new FuturesOrder();
+ order.setContract(contract);
+ order.setSize(size);
+ order.setPrice("0");
+ order.setTif(FuturesOrder.TifEnum.IOC);
+ order.setReduceOnly(true);
+ order.setText("t-grid-mkt-close");
+ FuturesOrder result = futuresApi.createFuturesOrder(SETTLE, order, null);
+ log.info("[TradeExec] 市价止盈成功, 价格:{}, size:{}, id:{}", result.getFillPrice(), size, result.getId());
+ } catch (Exception e) {
+ log.error("[TradeExec] 市价止盈也失败, size:{}", size, e);
+ }
+ }
+
+ /**
* 异步清除指定合约的所有止盈止损条件单。
*/
public void cancelAllPriceTriggeredOrders() {
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/celue.out b/src/main/java/com/xcong/excoin/modules/gateApi/celue.out
new file mode 100644
index 0000000..7d06d00
--- /dev/null
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/celue.out
Binary files differ
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
index 949f64e..9384359 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/CandlestickChannelHandler.java
@@ -87,10 +87,7 @@
log.info("========== Gate K线数据 ==========");
log.info("名称: {} 时间: {}", data.getString("n"), DateUtil.TimeStampToDateTime(data.getLong("t")));
- log.info("开盘: {} 最高: {} 最低: {} 收盘: {} 成交量: {} 成交额: {} 已完结: {}",
- data.getString("o"), data.getString("h"), data.getString("l"),
- data.getString("c"), data.getString("v"), data.getString("a"),
- data.getBooleanValue("w"));
+ log.info("收盘: {} 已完结: {}",data.getString("c"),data.getBooleanValue("w"));
log.info("==================================");
if (gridTradeService != null) {
--
Gitblit v1.9.1