Administrator
119 mins ago e9a397babbbfa9cff8a5ed026447d585e739c37f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.xcong.excoin.modules.gateApi;
 
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;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.math.BigDecimal;
 
/**
 * 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>
 * </ol>
 *
 * <h3>销毁流程 ({@code @PreDestroy})</h3>
 * <ol>
 *   <li>gridTradeService.stopGrid():取消条件单 → 关闭交易线程池</li>
 *   <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li>
 * </ol>
 *
 * <h3>配置</h3>
 * 当前在代码中硬编码测试网参数。切换到生产网只需改为 {@code .isProduction(true)}。
 *
 * @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("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")
                    .contract("ETH_USDT")
                    .leverage("100")
                    .marginMode("CROSS")
                    .positionMode("dual")
                    .gridRate(new BigDecimal("0.005"))
                    .overallTp(new BigDecimal("5"))
                    .maxLoss(new BigDecimal("15"))
                    .quantity("1")
                    .contractMultiplier(new BigDecimal("0.01"))
                    .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
                    .isProduction(true)
                    .reopenMaxRetries(3)
                    .build();
 
            // 1. 初始化交易服务:查用户ID → 切持仓模式 → 清条件单 → 平已有仓位 → 设杠杆
            gridTradeService = new GateGridTradeService(config);
            gridTradeService.init();
 
            // 2. 创建 WS 客户端并注册 3 个频道处理器
            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 个频道处理器");
 
            // 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; }
}