Administrator
4 days ago e4934031411acd1a86f3e99922cf857763934023
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
package com.xcong.excoin.modules.gateApi;
 
import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.math.BigDecimal;
 
/**
 * Gate 模块入口管理类,作为 Spring Bean 负责组件的生命周期。
 *
 * <h3>启动流程</h3>
 * <pre>
 *   @PostConstruct init():
 *     1. new GateGridTradeService(参数) → init()     // 设杠杆、查余额、切双向持仓
 *     2. new GateKlineWebSocketClient → init()        // 连接 WebSocket,订阅 K 线 + 仓位
 *     3. gridTradeService.startGrid()                 // 激活策略,等待 K 线触发首次双开
 * </pre>
 *
 * <h3>销毁流程</h3>
 * <pre>
 *   @PreDestroy destroy():
 *     1. gridTradeService.stopGrid()                  // 停止策略
 *     2. klinePriceClient.destroy()                   // 取消订阅 → 关闭 WS → 关闭线程池
 * </pre>
 *
 * <h3>配置参数</h3>
 * 当前配置在代码中硬编码:
 * <ul>
 *   <li>合约: XAU_USDT, 杠杆: 30x, 全仓, 双向持仓</li>
 *   <li>网格间距: 0.35%, 整体止盈: 0.5 USDT, 最大亏损: 7.5 USDT</li>
 *   <li>数量: 10 张</li>
 * </ul>
 *
 * @author Administrator
 */
@Slf4j
@Component
public class GateWebSocketClientManager {
    @Autowired
    private CaoZuoService caoZuoService;
    @Autowired
    private WangGeListService wangGeListService;
 
    /** K 线 WebSocket 客户端 */
    private GateKlineWebSocketClient klinePriceClient;
    /** 网格交易服务 */
    private GateGridTradeService gridTradeService;
 
    /** Gate 测试网 API Key */
    private static final String API_KEY = "d90ca272391992b8e74f8f92cedb21ec";
    /** Gate 测试网 API Secret */
    private static final String API_SECRET = "1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274";
 
    /**
     * Spring 容器启动后自动调用。初始化网格交易服务和 WebSocket 客户端。
     */
    @PostConstruct
    public void init() {
        log.info("开始初始化GateWebSocketClientManager");
 
        try {
            gridTradeService = new GateGridTradeService(
                    API_KEY, API_SECRET,
                    "XAUT_USDT",
                    "30",
                    "cross",
                    "dual",
                    new BigDecimal("0.0035"),
                    new BigDecimal("0.5"),
                    3,
                    new BigDecimal("7.5"),
                    "10"
            );
            gridTradeService.init();
 
            klinePriceClient = new GateKlineWebSocketClient(caoZuoService, this, wangGeListService, gridTradeService, API_KEY, API_SECRET);
            klinePriceClient.init();
            log.info("已初始化GateKlineWebSocketClient");
 
            gridTradeService.startGrid();
        } catch (Exception e) {
            log.error("初始化GateWebSocketClientManager失败", e);
        }
    }
 
    /**
     * Spring 容器销毁前自动调用。停止策略并关闭所有连接。
     */
    @PreDestroy
    public void destroy() {
        log.info("开始销毁GateWebSocketClientManager");
 
        if (gridTradeService != null) {
            gridTradeService.stopGrid();
        }
        if (klinePriceClient != null) {
            try {
                klinePriceClient.destroy();
                log.info("已销毁GateKlineWebSocketClient");
            } catch (Exception e) {
                log.error("销毁GateKlineWebSocketClient失败", e);
            }
        }
 
        log.info("GateWebSocketClientManager销毁完成");
    }
 
    public GateKlineWebSocketClient getKlineWebSocketClient() {
        return klinePriceClient;
    }
 
    public GateGridTradeService getGridTradeService() {
        return gridTradeService;
    }
}