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.OrdersChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionClosesChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionsChannelHandler;
import com.xcong.excoin.modules.gateApi.wsHandler.handler.UserTradesChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.math.BigDecimal;
/**
* Gate 模块 Spring 容器入口 — 组件组装 + 生命周期管理。
*
*
组装顺序({@code @PostConstruct})
*
* - {@link GateConfig} — 构建配置(API 密钥、合约、策略参数)
* - {@link GateGridTradeService} — init():获取用户 ID → 切双向持仓 → 清旧条件单 → 平仓 → 设杠杆
* - {@link GateKlineWebSocketClient} — 注册 6 个频道处理器 → init():建立 WS 连接并订阅
* - {@code gridTradeService.startGrid()} — 状态重置,等待首根 K 线
*
*
* 6 个频道处理器
*
* - CandlestickChannelHandler — 公开频道,K线 → onKline()
* - PositionsChannelHandler — 私有频道,仓位 → onPositionUpdate()
* - PositionClosesChannelHandler — 私有频道,平仓 → onPositionClose()
* - OrdersChannelHandler — 私有频道,订单成交 → onOrderUpdate()
* - UserTradesChannelHandler — 私有频道,用户成交 → onUserTrade()
* - AutoOrdersChannelHandler — 私有频道,条件单状态 → onAutoOrder()
*
*
* 销毁顺序({@code @PreDestroy})
*
* - gridTradeService.stopGrid():取消所有条件单 → 关闭交易线程池
* - wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池
*
*
* @author Administrator
*/
@Slf4j
@Component
public class GateWebSocketClientManager {
/** WebSocket 连接管理器 */
private GateKlineWebSocketClient wsClient;
/** 网格交易策略服务 */
private GateGridTradeService gridTradeService;
/** 统一配置 */
private GateConfig config;
@PostConstruct
public void init() {
log.info("[管理器] 开始初始化...");
try {
//实盘
config = GateConfig.builder()
.apiKey("08d6108fc29378fe64d6f0e89745b3fb")
.apiSecret("4d16e40117477a2344f6fd5872c5b075dc9664a599e65422d3e6791ff64379f1")
.contract("ETH_USDT")
.leverage("100")
.marginMode("CROSS")
.positionMode("dual")
.gridRate(new BigDecimal("0.0025"))
.expectedProfit(new BigDecimal("0.25"))
.maxLoss(new BigDecimal("1.5"))
.baseQuantity("3")
.quantity("1")
.maxPositionSize(9)
.priceScale(2)
.contractMultiplier(new BigDecimal("0.01"))
.unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
.isProduction(true)
.reopenMaxRetries(3)
.build();
// //测试盘
// config = GateConfig.builder()
// .apiKey("d90ca272391992b8e74f8f92cedb21ec")
// .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
// .contract("ETH_USDT")
// .leverage("100")
// .marginMode("CROSS")
// .positionMode("dual")
// .gridRate(new BigDecimal("0.002"))
// .expectedProfit(new BigDecimal("1"))
// .maxLoss(new BigDecimal("15"))
// .quantity("1")
// .priceScale(1)
// .contractMultiplier(new BigDecimal("0.01"))
// .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
// .isProduction(false)
// .reopenMaxRetries(3)
// .build();
// 1. 初始化交易服务:查用户ID → 切持仓模式 → 清条件单 → 平已有仓位 → 设杠杆
gridTradeService = new GateGridTradeService(config);
gridTradeService.init();
// 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 OrdersChannelHandler(
config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
wsClient.addChannelHandler(new UserTradesChannelHandler(
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已连接, 已注册 6 个频道处理器");
// 3. 激活策略,等待首根 K 线触发基底双开
gridTradeService.startGrid();
} catch (Exception e) {
log.error("[管理器] 初始化失败", e);
}
}
/**
* 销毁:停止策略 → 关闭交易线程池 → 取消 WS 订阅 → 断开连接 → 关闭 WS 线程池。
*/
@PreDestroy
public void destroy() {
log.info("[管理器] 开始销毁...");
if (gridTradeService != null) {
gridTradeService.stopGrid();
}
if (wsClient != null) {
wsClient.destroy();
}
log.info("[管理器] 销毁完成");
}
/**
* @return WebSocket 连接管理器实例
*/
public GateKlineWebSocketClient getKlineWebSocketClient() { return wsClient; }
/**
* @return 网格交易策略服务实例
*/
public GateGridTradeService getGridTradeService() { return gridTradeService; }
}