Administrator
2 hours ago bb272e1e2c0d4637d6d9ebfefb635662b804b459
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
131
132
package com.xcong.excoin.modules.okxApi;
 
import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxAccountChannelHandler;
import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxCandlestickChannelHandler;
import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxOrderInfoChannelHandler;
import com.xcong.excoin.modules.okxApi.wsHandler.handler.OkxPositionsChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.math.BigDecimal;
 
@Slf4j
@Component
public class OkxWebSocketClientManager {
 
    private OkxKlineWebSocketClient wsKlineClient;
    private OkxKlineWebSocketClient wsPrivateClient;
    private OkxGridTradeService gridTradeService;
    private OkxConfig config;
 
    @PostConstruct
    public void init() {
        log.info("[管理器] 开始初始化...");
 
        try {
            config = OkxConfig.builder()
                    .apiKey("ac76252d-e717-4459-a6f9-80512aed5ea0")
                    .secretKey("A8168543EF4F08A6DBFE27AB23956898")
                    .passphrase("Aa12345678@")
                    .contract("ETH-USDT-SWAP")
                    .leverage("100")
                    .marginMode("cross")
                    .posMode("long_short_mode")
                    .gridRate(new BigDecimal("0.0015"))
                    .overallTp(new BigDecimal("15"))
                    .maxLoss(new BigDecimal("20"))
                    .quantity("1")
                    .contractMultiplier(new BigDecimal("0.1"))
                    .unrealizedPnlPriceMode(OkxConfig.PnLPriceMode.LAST_PRICE)
                    .isProduction(false)
                    .build();
 
            setupAccount();
 
            String accountName = "OKX_API";
            gridTradeService = new OkxGridTradeService(config, accountName);
            gridTradeService.startGrid();
 
            wsPrivateClient = new OkxKlineWebSocketClient(
                    config.getWsPrivateUrl(),
                    config.getApiKey(),
                    config.getSecretKey(),
                    config.getPassphrase());
            wsPrivateClient.addChannelHandler(new OkxPositionsChannelHandler(config.getContract(), gridTradeService));
            wsPrivateClient.addChannelHandler(new OkxAccountChannelHandler());
            wsPrivateClient.addChannelHandler(new OkxOrderInfoChannelHandler(config.getContract(), gridTradeService, config));
 
            wsPrivateClient.setOnLoginSuccess(() -> {
                log.info("[管理器] 私有WS登录完成,启动K线订阅");
                try {
                    wsKlineClient = new OkxKlineWebSocketClient(config.getWsKlineUrl());
                    wsKlineClient.addChannelHandler(new OkxCandlestickChannelHandler(config.getContract(), gridTradeService));
                    wsKlineClient.init();
                    log.info("[管理器] K线WS已连接, 已注册K线频道处理器");
                } catch (Exception e) {
                    log.error("[管理器] K线WS初始化失败", e);
                }
            });
 
            wsPrivateClient.init();
            gridTradeService.setWebSocketClient(wsPrivateClient.getWebSocketClient());
            log.info("[管理器] 私有WS已连接, 等待登录完成...");
 
        } catch (Exception e) {
            log.error("[管理器] 初始化失败", e);
        }
    }
 
    @PreDestroy
    public void destroy() {
        log.info("[管理器] 开始销毁...");
 
        if (gridTradeService != null) {
            gridTradeService.stopGrid();
        }
        if (wsKlineClient != null) {
            wsKlineClient.destroy();
        }
        if (wsPrivateClient != null) {
            wsPrivateClient.destroy();
        }
 
        log.info("[管理器] 销毁完成");
    }
 
    private void setupAccount() {
        log.info("[管理器] 开始配置账户...");
        OkxRestClient restClient = new OkxRestClient(
                config.getRestBaseUrl(),
                config.getApiKey(),
                config.getSecretKey(),
                config.getPassphrase(),
                !config.isProduction());
 
        boolean posModeOk = restClient.setPositionMode(config.getPosMode());
        if (!posModeOk) {
            log.error("[管理器] 设置持仓方式失败,策略可能无法正常运作");
        }
 
        boolean leverOk = restClient.setLeverage(
                config.getContract(), config.getLeverage(), config.getMarginMode());
        if (!leverOk) {
            log.error("[管理器] 设置杠杆倍数失败,策略可能无法正常运作");
        }
 
        String instIdCode = restClient.fetchInstIdCode("SWAP", config.getContract());
        if (instIdCode != null) {
            config.setInstIdCode(instIdCode);
        } else {
            log.error("[管理器] 获取instIdCode失败,WS下单将无法正常工作");
        }
 
        log.info("[管理器] 账户配置完成, posMode:{}, leverage:{}, marginMode:{}, instIdCode:{}",
                config.getPosMode(), config.getLeverage(), config.getMarginMode(), config.getInstIdCode());
    }
 
    public OkxKlineWebSocketClient getKlineWebSocketClient() { return wsKlineClient; }
    public OkxKlineWebSocketClient getPrivateWebSocketClient() { return wsPrivateClient; }
    public OkxGridTradeService getGridTradeService() { return gridTradeService; }
}