Administrator
2026-06-05 3c86df8d1170406ca3ca036d1337e8654ab5d41a
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
package com.xcong.excoin.modules.okxNewPrice.gridWs;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.OkxGridTradeService;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
 
import java.math.BigDecimal;
 
/**
 * OKX 持仓频道处理器 (positions)。
 * 接收持仓更新推送并回调 OkxGridTradeService.onPositionUpdate()。
 *
 * @author Administrator
 */
@Slf4j
public class OkxPositionsChannelHandler implements OkxGridChannelHandler {
 
    private static final String CHANNEL_NAME = "positions";
 
    private final String instId;
    private final OkxGridTradeService gridTradeService;
 
    public OkxPositionsChannelHandler(String instId, OkxGridTradeService gridTradeService) {
        this.instId = instId;
        this.gridTradeService = gridTradeService;
    }
 
    @Override
    public String getChannelName() { return CHANNEL_NAME; }
 
    @Override
    public void subscribe(WebSocketClient ws) {
        JSONObject msg = new JSONObject();
        JSONObject arg = new JSONObject();
        arg.put("channel", CHANNEL_NAME);
        arg.put("instType", "SWAP");
        msg.put("op", "subscribe");
        JSONArray args = new JSONArray();
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[OKX-WS] {} 订阅成功", CHANNEL_NAME);
    }
 
    @Override
    public void unsubscribe(WebSocketClient ws) {
        JSONObject msg = new JSONObject();
        JSONObject arg = new JSONObject();
        arg.put("channel", CHANNEL_NAME);
        arg.put("instType", "SWAP");
        msg.put("op", "unsubscribe");
        JSONArray args = new JSONArray();
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[OKX-WS] {} 取消订阅成功", CHANNEL_NAME);
    }
 
    @Override
    public boolean handleMessage(JSONObject response) {
        JSONObject arg = response.getJSONObject("arg");
        if (arg == null || !CHANNEL_NAME.equals(arg.getString("channel"))) {
            return false;
        }
        try {
            JSONArray data = response.getJSONArray("data");
            if (data == null || data.isEmpty()) return true;
            for (int i = 0; i < data.size(); i++) {
                JSONObject pos = data.getJSONObject(i);
                String posInstId = pos.getString("instId");
                if (posInstId == null || !instId.equals(posInstId)) continue;
 
                String posSide = pos.getString("posSide");
                String posStr = pos.getString("pos");
                String avgPxStr = pos.getString("avgPx");
                if (posStr == null || posStr.isEmpty() || avgPxStr == null || avgPxStr.isEmpty()) continue;
 
                BigDecimal posSize = new BigDecimal(posStr);
                BigDecimal avgPx = new BigDecimal(avgPxStr);
                log.info("[OKX-WS] 持仓更新, instId:{}, posSide:{}, pos:{}, avgPx:{}",
                        instId, posSide, posSize, avgPx);
 
                if (gridTradeService != null) {
                    gridTradeService.onPositionUpdate(instId, posSide, posSize, avgPx);
                }
            }
        } catch (Exception e) {
            log.error("[OKX-WS] {} 处理数据失败", CHANNEL_NAME, e);
        }
        return true;
    }
}