Administrator
yesterday 025c66091b6b6903b5e830c5bde981fdbacbc744
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
package com.xcong.excoin.modules.okxApi.wsHandler.handler;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxApi.OkxConfig;
import com.xcong.excoin.modules.okxApi.OkxGridTradeService;
import com.xcong.excoin.modules.okxApi.wsHandler.OkxChannelHandler;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
 
import java.math.BigDecimal;
 
/**
 * OKX 订单信息频道处理器(orders)。
 *
 * <h3>数据用途</h3>
 * 监控订单成交(filled)状态,跟踪已实现盈亏(fillPnl)。
 * 成交后通过 batch-orders 频道设置止盈止损限价单。
 *
 * <h3>推送字段</h3>
 * instId, ordId, clOrdId, side, posSide, state, accFillSz, avgPx, fillPnl, fillFee
 *
 * <h3>数据处理</h3>
 * state=filled 且 accFillSz>0 → 成交 → 调用 gridTradeService.onOrderFilled() 跟踪盈亏
 *
 * @author Administrator
 */
@Slf4j
public class OkxOrderInfoChannelHandler implements OkxChannelHandler {
 
    private static final String CHANNEL_NAME = "orders";
    private static final String CHANNEL = "orders";
 
    private final String instId;
    private final OkxGridTradeService gridTradeService;
    private final OkxConfig config;
 
    public OkxOrderInfoChannelHandler(String instId, OkxGridTradeService gridTradeService, OkxConfig config) {
        this.instId = instId;
        this.gridTradeService = gridTradeService;
        this.config = config;
    }
 
    @Override
    public String getChannelName() {
        return CHANNEL_NAME;
    }
 
    @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", CHANNEL);
        arg.put("instType", "SWAP");
        arg.put("instId", instId);
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[{}] 订阅成功, 合约:{}", CHANNEL_NAME, 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", CHANNEL);
        arg.put("instType", "SWAP");
        arg.put("instId", instId);
        args.add(arg);
        msg.put("args", args);
        ws.send(msg.toJSONString());
        log.info("[{}] 取消订阅成功", CHANNEL_NAME);
    }
 
    @Override
    public boolean handleMessage(JSONObject response) {
        JSONObject argObj = response.getJSONObject("arg");
        if (argObj == null) {
            return false;
        }
        String channel = argObj.getString("channel");
        if (!CHANNEL.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 detail = dataArray.getJSONObject(i);
                if (!instId.equals(detail.getString("instId"))) {
                    continue;
                }
                String state = detail.getString("state");
                String accFillSz = detail.getString("accFillSz");
                String fillPnl = detail.getString("fillPnl");
                String posSide = detail.getString("posSide");
                String avgPx = detail.getString("avgPx");
                String clOrdId = detail.getString("clOrdId");
 
                log.info("[{}] 订单, 方向:{}, 状态:{}, 成交量:{}, 均价:{}, 盈亏:{}, 编号:{}",
                        CHANNEL_NAME, posSide, state, accFillSz, avgPx, fillPnl, clOrdId);
 
                if ("filled".equals(state) && accFillSz != null && new BigDecimal(accFillSz).compareTo(BigDecimal.ZERO) > 0) {
                    if (gridTradeService != null) {
                        BigDecimal pnl = fillPnl != null ? new BigDecimal(fillPnl) : BigDecimal.ZERO;
                        gridTradeService.onOrderFilled(posSide, new BigDecimal(accFillSz), pnl);
                    }
                }
            }
        } catch (Exception e) {
            log.error("[{}] 处理数据失败", CHANNEL_NAME, e);
        }
        return true;
    }
}