From 6cc2de8e5181d83dc5baa014d528da9e93e5c2a6 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 22 Jun 2026 22:59:42 +0800
Subject: [PATCH] fix(gateApi): 修复双边持仓跨度计算逻辑
---
src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java | 70 +++++++++++++++++------------------
1 files changed, 34 insertions(+), 36 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
index f9c2c35..e0e1de7 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
@@ -1,5 +1,6 @@
package com.xcong.excoin.modules.gateApi;
+import com.xcong.excoin.modules.gateApi.wsHandler.handler.AutoOrdersChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.CandlestickChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionClosesChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionsChannelHandler;
@@ -11,24 +12,31 @@
import java.math.BigDecimal;
/**
- * Gate 模块 Spring 入口,组装所有组件并管理生命周期。
+ * Gate 模块 Spring 容器入口 — 组件组装 + 生命周期管理。
*
- * <h3>启动流程 ({@code @PostConstruct})</h3>
+ * <h3>组装顺序({@code @PostConstruct})</h3>
* <ol>
- * <li>构建 {@link GateConfig}(Builder 模式,含 API 密钥、合约、策略参数)</li>
- * <li>创建 {@link GateGridTradeService} → init():切持仓模式、清旧条件单、设杠杆</li>
- * <li>创建 {@link GateKlineWebSocketClient} → 注册 3 个 Handler → init():建立 WS 连接</li>
- * <li>gridTradeService.startGrid():激活策略,等待 K 线触发首次双开</li>
+ * <li>{@link GateConfig} — 构建配置(API 密钥、合约、策略参数)</li>
+ * <li>{@link GateGridTradeService} — init():获取用户 ID → 切双向持仓 → 清旧条件单 → 平仓 → 设杠杆</li>
+ * <li>{@link GateKlineWebSocketClient} — 注册 6 个频道处理器 → init():建立 WS 连接并订阅</li>
+ * <li>{@code gridTradeService.startGrid()} — 状态重置,等待首根 K 线</li>
* </ol>
*
- * <h3>销毁流程 ({@code @PreDestroy})</h3>
+ * <h3>6 个频道处理器</h3>
* <ol>
- * <li>gridTradeService.stopGrid():取消条件单 → 关闭交易线程池</li>
+ * <li>CandlestickChannelHandler — 公开频道,K线 → onKline()</li>
+ * <li>PositionsChannelHandler — 私有频道,仓位 → onPositionUpdate()</li>
+ * <li>PositionClosesChannelHandler — 私有频道,平仓 → onPositionClose()</li>
+ * <li>OrdersChannelHandler — 私有频道,订单成交 → onOrderUpdate()</li>
+ * <li>UserTradesChannelHandler — 私有频道,用户成交 → onUserTrade()</li>
+ * <li>AutoOrdersChannelHandler — 私有频道,条件单状态 → onAutoOrder()</li>
+ * </ol>
+ *
+ * <h3>销毁顺序({@code @PreDestroy})</h3>
+ * <ol>
+ * <li>gridTradeService.stopGrid():取消所有条件单 → 关闭交易线程池</li>
* <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li>
* </ol>
- *
- * <h3>配置</h3>
- * 当前在代码中硬编码测试网参数。切换到生产网只需改为 {@code .isProduction(true)}。
*
* @author Administrator
*/
@@ -48,35 +56,22 @@
log.info("[管理器] 开始初始化...");
try {
- //测试盘
-// config = GateConfig.builder()
-// .apiKey("d90ca272391992b8e74f8f92cedb21ec")
-// .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
-// .contract("ETH_USDT")
-// .leverage("100")
-// .marginMode("CROSS")
-// .positionMode("dual")
-// .gridRate(new BigDecimal("0.003"))
-// .overallTp(new BigDecimal("5"))
-// .maxLoss(new BigDecimal("15"))
-// .quantity("1")
-// .contractMultiplier(new BigDecimal("0.01"))
-// .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
-// .isProduction(false)
-// .reopenMaxRetries(3)
-// .build();
//实盘
config = GateConfig.builder()
- .apiKey("865371cdaccd5d238aceb06a55f0143a")
- .apiSecret("49589c30dfdc3acba007eed445a94990c4b0aa5faac9843e32defdd7371f5a50")
+ .apiKey("a2338398e00b7935104520e16be96918")
+ .apiSecret("9111d897f2346d5217619f2da76536632715fef4d7eb304c6c61e869a2a74e98")
.contract("ETH_USDT")
.leverage("100")
.marginMode("CROSS")
.positionMode("dual")
- .gridRate(new BigDecimal("0.005"))
- .overallTp(new BigDecimal("5"))
- .maxLoss(new BigDecimal("15"))
- .quantity("1")
+ .gridRate(new BigDecimal("0.0035"))
+ .expectedProfit(new BigDecimal("2.5"))
+ .maxLoss(new BigDecimal("1.5"))
+ .baseQuantity("9")
+ .quantity("3")
+ .restartGridSpan(2)
+ .maxPositionSize(2)
+ .priceScale(2)
.contractMultiplier(new BigDecimal("0.01"))
.unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
.isProduction(true)
@@ -87,15 +82,18 @@
gridTradeService = new GateGridTradeService(config);
gridTradeService.init();
- // 2. 创建 WS 客户端并注册 3 个频道处理器
+ // 2. 创建 WS 客户端并注册频道处理器
wsClient = new GateKlineWebSocketClient(config.getWsUrl());
wsClient.addChannelHandler(new CandlestickChannelHandler(config.getContract(), gridTradeService));
wsClient.addChannelHandler(new PositionsChannelHandler(
config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
wsClient.addChannelHandler(new PositionClosesChannelHandler(
config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
+ wsClient.addChannelHandler(new AutoOrdersChannelHandler(
+ config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
+ gridTradeService.setWsClient(wsClient);
wsClient.init();
- log.info("[管理器] WS已连接, 已注册 3 个频道处理器");
+ log.info("[管理器] WS已连接, 已注册 4 个频道处理器");
// 3. 激活策略,等待首根 K 线触发基底双开
gridTradeService.startGrid();
--
Gitblit v1.9.1