Administrator
21 hours ago be6ebe0f51aacef4435ee3d81a913bf8e8b48505
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
package com.xcong.excoin.modules.okxApi.wsHandler.handler;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxApi.OkxGridTradeService;
import com.xcong.excoin.modules.okxApi.enums.OkxEnums;
import com.xcong.excoin.modules.okxApi.wsHandler.OkxChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
 
import java.math.BigDecimal;
 
@Slf4j
public class OkxPositionsChannelHandler implements OkxChannelHandler {
 
    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 OkxEnums.CHANNEL_POSITIONS;
    }
 
    @Override
    public void subscribe(WebSocketClient ws) {
        JSONObject msg = new JSONObject();
        msg.put("op", "subscribe");
        JSONArray args = new JSONArray();
        JSONObject arg = new JSONObject();
        arg.put("channel", OkxEnums.CHANNEL_POSITIONS);
        arg.put("instType", OkxEnums.INSTTYPE_SWAP);
        arg.put("instId", instId);
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[{}] 订阅成功, 合约:{}", OkxEnums.CHANNEL_POSITIONS, instId);
    }
 
    @Override
    public void unsubscribe(WebSocketClient ws) {
        JSONObject msg = new JSONObject();
        msg.put("op", "unsubscribe");
        JSONArray args = new JSONArray();
        JSONObject arg = new JSONObject();
        arg.put("channel", OkxEnums.CHANNEL_POSITIONS);
        arg.put("instType", OkxEnums.INSTTYPE_SWAP);
        arg.put("instId", instId);
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[{}] 取消订阅成功", OkxEnums.CHANNEL_POSITIONS);
    }
 
    @Override
    public boolean handleMessage(JSONObject response) {
        JSONObject argObj = response.getJSONObject("arg");
        if (argObj == null) {
            return false;
        }
        String channel = argObj.getString("channel");
        if (!OkxEnums.CHANNEL_POSITIONS.equals(channel)) {
            return false;
        }
        try {
            JSONArray dataArray = response.getJSONArray("data");
            if (dataArray == null || dataArray.isEmpty()) {
                return true;
            }
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject pos = dataArray.getJSONObject(i);
                if (!instId.equals(pos.getString("instId"))) {
                    continue;
                }
                String posSide = pos.getString("posSide");
                BigDecimal size = new BigDecimal(pos.getString("pos"));
                BigDecimal avgPx = pos.containsKey("avgPx") && pos.getString("avgPx") != null
                        ? new BigDecimal(pos.getString("avgPx")) : BigDecimal.ZERO;
 
                log.info("[{}] 持仓更新, 方向:{}, 数量:{}, 均价:{}, 未实现盈亏:{}, 保证金:{}",
                        OkxEnums.CHANNEL_POSITIONS, posSide, size, avgPx,
                        pos.get("upl"), pos.get("imr"));
 
                if (gridTradeService != null) {
                    gridTradeService.onPositionUpdate(posSide, size, avgPx);
                }
            }
        } catch (Exception e) {
            log.error("[{}] 处理数据失败", OkxEnums.CHANNEL_POSITIONS, e);
        }
        return true;
    }
}