| | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.PreDestroy; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Gate 模块 Spring 入口,组装所有组件并管理生命周期。 |
| | | * Gate 模块 Spring 入口,管理多个账号实例的生命周期。 |
| | | * |
| | | * <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 #buildAccountConfigs()} 构建每个账号的 {@link GateConfig}</li> |
| | | * <li>每个账号创建独立的 {@link AccountInstance}(含 service + wsClient)</li> |
| | | * <li>逐个 init → startGrid</li> |
| | | * </ol> |
| | | * |
| | | * <h3>销毁流程 ({@code @PreDestroy})</h3> |
| | | * <ol> |
| | | * <li>gridTradeService.stopGrid():取消条件单 → 关闭交易线程池</li> |
| | | * <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li> |
| | | * <li>遍历 instances:stopGrid + wsClient.destroy()</li> |
| | | * </ol> |
| | | * |
| | | * <h3>配置</h3> |
| | | * 当前在代码中硬编码测试网参数。切换到生产网只需改为 {@code .isProduction(true)}。 |
| | | * <h3>添加新账号</h3> |
| | | * 在 {@link #buildAccountConfigs()} 方法中添加新的 {@link GateConfig} 即可。 |
| | | * 通过 {@link GateConfig#getLabel()} 区分日志输出, |
| | | * 通过 {@link GateConfig#isProduction()} 控制模拟盘/实盘环境。 |
| | | * |
| | | * @author Administrator |
| | | */ |
| | |
| | | @Component |
| | | public class GateWebSocketClientManager { |
| | | |
| | | /** WebSocket 连接管理器 */ |
| | | private GateKlineWebSocketClient wsClient; |
| | | /** 网格交易策略服务 */ |
| | | private GateGridTradeService gridTradeService; |
| | | /** 统一配置 */ |
| | | private GateConfig config; |
| | | /** 所有账号实例列表 */ |
| | | private final List<AccountInstance> instances = new ArrayList<>(); |
| | | |
| | | /** |
| | | * 封装一个账号的完整运行时组件。 |
| | | */ |
| | | private static class AccountInstance { |
| | | final String label; |
| | | final GateConfig config; |
| | | final GateGridTradeService service; |
| | | final GateKlineWebSocketClient wsClient; |
| | | |
| | | AccountInstance(GateConfig config) { |
| | | this.label = config.getLabel(); |
| | | this.config = config; |
| | | this.service = new GateGridTradeService(config); |
| | | this.wsClient = new GateKlineWebSocketClient(config.getWsUrl()); |
| | | } |
| | | |
| | | void init() { |
| | | service.init(); |
| | | wsClient.addChannelHandler(new CandlestickChannelHandler(config.getContract(), service)); |
| | | wsClient.addChannelHandler(new PositionsChannelHandler( |
| | | config.getApiKey(), config.getApiSecret(), config.getContract(), service)); |
| | | wsClient.addChannelHandler(new PositionClosesChannelHandler( |
| | | config.getApiKey(), config.getApiSecret(), config.getContract(), service)); |
| | | wsClient.init(); |
| | | log.info("[{}] WS已连接, 已注册 3 个频道处理器", label); |
| | | service.startGrid(); |
| | | } |
| | | |
| | | void destroy() { |
| | | log.info("[{}] 开始销毁...", label); |
| | | service.stopGrid(); |
| | | wsClient.destroy(); |
| | | log.info("[{}] 销毁完成", label); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 在此方法中配置所有账号。 |
| | | * <p>每个 {@link GateConfig} 对应一个账号,label 用于日志区分, |
| | | * isProduction 控制连接测试网(true=实盘)还是测试网(false=模拟盘)。 |
| | | */ |
| | | private static GateConfig[] buildAccountConfigs() { |
| | | return new GateConfig[]{ |
| | | GateConfig.builder() |
| | | .label("模拟盘1") |
| | | .apiKey("d90ca272391992b8e74f8f92cedb21ec") |
| | | .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274") |
| | | .contract("ETH_USDT") |
| | | .leverage("100") |
| | | .marginMode("CROSS") |
| | | .positionMode("dual") |
| | | .gridRate(new BigDecimal("0.0015")) |
| | | .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(), |
| | | }; |
| | | } |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | log.info("[管理器] 开始初始化..."); |
| | | |
| | | try { |
| | | 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(); |
| | | |
| | | 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("[管理器] 初始化失败", e); |
| | | GateConfig[] configs = buildAccountConfigs(); |
| | | log.info("[管理器] 开始初始化 {} 个账号...", configs.length); |
| | | for (GateConfig config : configs) { |
| | | try { |
| | | AccountInstance instance = new AccountInstance(config); |
| | | instance.init(); |
| | | instances.add(instance); |
| | | } catch (Exception e) { |
| | | log.error("[管理器] 账号 {} 初始化失败", config.getLabel(), e); |
| | | } |
| | | } |
| | | |
| | | log.info("[管理器] 初始化完成, 成功启动 {}/{} 个账号", instances.size(), configs.length); |
| | | } |
| | | |
| | | @PreDestroy |
| | | public void destroy() { |
| | | log.info("[管理器] 开始销毁..."); |
| | | if (gridTradeService != null) { |
| | | gridTradeService.stopGrid(); |
| | | } |
| | | if (wsClient != null) { |
| | | wsClient.destroy(); |
| | | log.info("[管理器] 开始销毁 {} 个账号...", instances.size()); |
| | | for (AccountInstance instance : instances) { |
| | | try { |
| | | instance.destroy(); |
| | | } catch (Exception e) { |
| | | log.error("[管理器] 账号 {} 销毁失败", instance.label, e); |
| | | } |
| | | } |
| | | log.info("[管理器] 销毁完成"); |
| | | } |
| | | |
| | | public GateKlineWebSocketClient getKlineWebSocketClient() { return wsClient; } |
| | | public GateGridTradeService getGridTradeService() { return gridTradeService; } |
| | | /** 获取账号数量 */ |
| | | public int getInstanceCount() { |
| | | return instances.size(); |
| | | } |
| | | |
| | | /** 获取指定索引的 GridTradeService(用于外部监控/查询) */ |
| | | public GateGridTradeService getGridTradeService(int index) { |
| | | return instances.get(index).service; |
| | | } |
| | | } |