| | |
| | | import java.math.BigDecimal; |
| | | |
| | | /** |
| | | * Gate 模块入口管理类,作为 Spring Bean 负责组件的生命周期。 |
| | | * Gate 模块 Spring 入口,组装所有组件并管理生命周期。 |
| | | * |
| | | * <h3>启动流程</h3> |
| | | * <pre> |
| | | * @PostConstruct init(): |
| | | * 1. new GateGridTradeService(参数) → init() // 设杠杆、查余额、切双向持仓 |
| | | * 2. new GateKlineWebSocketClient → init() // 连接 WebSocket,订阅 K 线 + 仓位 |
| | | * 3. gridTradeService.startGrid() // 激活策略,等待 K 线触发首次双开 |
| | | * </pre> |
| | | * <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> |
| | | * </ol> |
| | | * |
| | | * <h3>销毁流程</h3> |
| | | * <pre> |
| | | * @PreDestroy destroy(): |
| | | * 1. gridTradeService.stopGrid() // 停止策略 |
| | | * 2. klinePriceClient.destroy() // 取消订阅 → 关闭 WS → 关闭线程池 |
| | | * </pre> |
| | | * <h3>销毁流程 ({@code @PreDestroy})</h3> |
| | | * <ol> |
| | | * <li>gridTradeService.stopGrid():取消条件单 → 关闭交易线程池</li> |
| | | * <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li> |
| | | * </ol> |
| | | * |
| | | * <h3>配置参数</h3> |
| | | * 当前配置在代码中硬编码: |
| | | * <ul> |
| | | * <li>合约: XAU_USDT, 杠杆: 30x, 全仓, 双向持仓</li> |
| | | * <li>网格间距: 0.35%, 整体止盈: 0.5 USDT, 最大亏损: 7.5 USDT</li> |
| | | * <li>数量: 10 张</li> |
| | | * </ul> |
| | | * <h3>配置</h3> |
| | | * 当前在代码中硬编码测试网参数。切换到生产网只需改为 {@code .isProduction(true)}。 |
| | | * |
| | | * @author Administrator |
| | | */ |
| | |
| | | @Component |
| | | public class GateWebSocketClientManager { |
| | | |
| | | /** K 线 WebSocket 客户端 */ |
| | | private GateKlineWebSocketClient klinePriceClient; |
| | | /** 网格交易服务 */ |
| | | /** WebSocket 连接管理器 */ |
| | | private GateKlineWebSocketClient wsClient; |
| | | /** 网格交易策略服务 */ |
| | | private GateGridTradeService gridTradeService; |
| | | /** 统一配置 */ |
| | | private GateConfig config; |
| | | |
| | | /** Gate 测试网 API Key */ |
| | | private static final String API_KEY = "d90ca272391992b8e74f8f92cedb21ec"; |
| | | /** Gate 测试网 API Secret */ |
| | | private static final String API_SECRET = "1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274"; |
| | | /** 合约 */ |
| | | private static final String CONTRACT = "XAUT_USDT"; |
| | | |
| | | /** |
| | | * Spring 容器启动后自动调用。初始化网格交易服务和 WebSocket 客户端。 |
| | | */ |
| | | @PostConstruct |
| | | public void init() { |
| | | log.info("开始初始化GateWebSocketClientManager"); |
| | | log.info("[管理器] 开始初始化..."); |
| | | |
| | | try { |
| | | gridTradeService = new GateGridTradeService( |
| | | API_KEY, API_SECRET, |
| | | CONTRACT, |
| | | "30", |
| | | "cross", |
| | | "dual", |
| | | new BigDecimal("0.0035"), |
| | | new BigDecimal("0.5"), |
| | | new BigDecimal("7.5"), |
| | | "10" |
| | | ); |
| | | config = GateConfig.builder() |
| | | .apiKey("d90ca272391992b8e74f8f92cedb21ec") |
| | | .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274") |
| | | .contract("ETH_USDT") |
| | | .leverage("100") |
| | | .marginMode("cross") |
| | | .positionMode("dual") |
| | | .gridRate(new BigDecimal("0.002")) |
| | | .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(); |
| | | |
| | | gridTradeService = new GateGridTradeService(config); |
| | | gridTradeService.init(); |
| | | |
| | | klinePriceClient = new GateKlineWebSocketClient(); |
| | | klinePriceClient.addChannelHandler(new CandlestickChannelHandler(CONTRACT, gridTradeService)); |
| | | klinePriceClient.addChannelHandler(new PositionsChannelHandler(API_KEY, API_SECRET, CONTRACT, gridTradeService)); |
| | | klinePriceClient.addChannelHandler(new PositionClosesChannelHandler(API_KEY, API_SECRET, CONTRACT, gridTradeService)); |
| | | klinePriceClient.init(); |
| | | log.info("已初始化GateKlineWebSocketClient及3个频道Handler"); |
| | | 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.init(); |
| | | log.info("[管理器] WS已连接, 已注册 3 个频道处理器"); |
| | | |
| | | gridTradeService.startGrid(); |
| | | } catch (Exception e) { |
| | | log.error("初始化GateWebSocketClientManager失败", e); |
| | | log.error("[管理器] 初始化失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Spring 容器销毁前自动调用。停止策略并关闭所有连接。 |
| | | */ |
| | | @PreDestroy |
| | | public void destroy() { |
| | | log.info("开始销毁GateWebSocketClientManager"); |
| | | |
| | | log.info("[管理器] 开始销毁..."); |
| | | if (gridTradeService != null) { |
| | | gridTradeService.stopGrid(); |
| | | } |
| | | if (klinePriceClient != null) { |
| | | try { |
| | | klinePriceClient.destroy(); |
| | | log.info("已销毁GateKlineWebSocketClient"); |
| | | } catch (Exception e) { |
| | | log.error("销毁GateKlineWebSocketClient失败", e); |
| | | } |
| | | if (wsClient != null) { |
| | | wsClient.destroy(); |
| | | } |
| | | |
| | | log.info("GateWebSocketClientManager销毁完成"); |
| | | log.info("[管理器] 销毁完成"); |
| | | } |
| | | |
| | | public GateKlineWebSocketClient getKlineWebSocketClient() { |
| | | return klinePriceClient; |
| | | } |
| | | |
| | | public GateGridTradeService getGridTradeService() { |
| | | return gridTradeService; |
| | | } |
| | | public GateKlineWebSocketClient getKlineWebSocketClient() { return wsClient; } |
| | | public GateGridTradeService getGridTradeService() { return gridTradeService; } |
| | | } |