From aaa9b2d954e3dc3c1588ecbb749b46b413c5fc5b Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Sat, 06 Jun 2026 11:40:02 +0800
Subject: [PATCH] refactor(gateApi): 优化网格交易服务中的变量作用域
---
src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java | 52 ++++++++++++++++++++++++++++++++++++----------------
1 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java
index 78f6c14..c58ea39 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/PositionClosesChannelHandler.java
@@ -9,17 +9,7 @@
import java.math.BigDecimal;
/**
- * 平仓频道处理器。
- *
- * <h3>数据用途</h3>
- * 每笔平仓发生时推送 pnl(盈亏金额),累加到 {@code cumulativePnl} 用于判断策略停止条件:
- * cumulativePnl ≥ overallTp(达到止盈目标)或 ≤ -maxLoss(超过亏损上限)。
- *
- * <h3>推送字段</h3>
- * contract, side(long / short), pnl(该次平仓的盈亏,如 "+0.2" 或 "-0.1")
- *
- * <h3>注意</h3>
- * 平仓盈亏来自服务器端,不受本地计算误差影响。这是策略停止的唯一盈亏判断来源。
+ * 平仓频道处理器(futures.position_closes),接收平仓盈亏推送并回调 {@link GateGridTradeService#onPositionClose}。
*
* @author Administrator
*/
@@ -28,29 +18,59 @@
private static final String CHANNEL_NAME = "futures.position_closes";
+ /**
+ * @param apiKey Gate API v4 密钥,用于签名认证
+ * @param apiSecret Gate API v4 签名密钥
+ * @param contract 合约名称(如 ETH_USDT)
+ * @param gridTradeService 网格交易策略服务实例
+ */
public PositionClosesChannelHandler(String apiKey, String apiSecret,
String contract,
GateGridTradeService gridTradeService) {
super(CHANNEL_NAME, apiKey, apiSecret, contract, gridTradeService);
}
+ /**
+ * 处理平仓推送消息。
+ *
+ * <h3>数据提取</h3>
+ * result 数组中每个元素包含:
+ * <ul>
+ * <li>contract:合约名称</li>
+ * <li>side:平仓方向("long" / "short")</li>
+ * <li>pnl:本次平仓的盈亏金额(字符串格式,如 "+0.2" / "-0.1")</li>
+ * </ul>
+ *
+ * <h3>数据处理</h3>
+ * 按合约名称过滤 → 提取 pnl 和 side → 调用 gridTradeService.onPositionClose() 累加盈亏。
+ * pnl 来自服务端,不受本地计算误差影响。
+ *
+ * @param response WebSocket 推送的完整 JSON
+ * @return true 表示已处理(匹配成功)
+ */
@Override
public boolean handleMessage(JSONObject response) {
- if (!CHANNEL_NAME.equals(response.getString("channel"))) return false;
+ if (!CHANNEL_NAME.equals(response.getString("channel"))) {
+ return false;
+ }
try {
JSONArray resultArray = response.getJSONArray("result");
- if (resultArray == null || resultArray.isEmpty()) return true;
+ if (resultArray == null || resultArray.isEmpty()) {
+ return true;
+ }
for (int i = 0; i < resultArray.size(); i++) {
JSONObject item = resultArray.getJSONObject(i);
- if (!getContract().equals(item.getString("contract"))) continue;
+ if (!getContract().equals(item.getString("contract"))) {
+ continue;
+ }
BigDecimal pnl = new BigDecimal(item.getString("pnl"));
String side = item.getString("side");
- log.info("[{}] side:{}, pnl:{}", CHANNEL_NAME, side, pnl);
+ log.info("[{}] 平仓更新, 方向:{}, 盈亏:{}", CHANNEL_NAME, side, pnl);
if (getGridTradeService() != null) {
getGridTradeService().onPositionClose(getContract(), side, pnl);
}
}
- } catch (Exception e) { log.error("[{}] handle fail", CHANNEL_NAME, e); }
+ } catch (Exception e) { log.error("[{}] 处理数据失败", CHANNEL_NAME, e); }
return true;
}
}
--
Gitblit v1.9.1