| New file |
| | |
| | | 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; |
| | | |
| | | /** |
| | | * OKX 订单频道处理器 (orders),替代 orders-algo。 |
| | | * 当 algo 订单触发后生成普通市价单,其 fill 数据带 algoId,可匹配到原始条件单。 |
| | | */ |
| | | @Slf4j |
| | | public class OkxOrdersChannelHandler implements OkxGridChannelHandler { |
| | | |
| | | private static final String CHANNEL_NAME = "orders"; |
| | | |
| | | private final String instId; |
| | | private final OkxGridTradeService gridTradeService; |
| | | |
| | | public OkxOrdersChannelHandler(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 order = data.getJSONObject(i); |
| | | if (!instId.equals(order.getString("instId"))) continue; |
| | | |
| | | String algoId = order.getString("algoId"); |
| | | if (algoId == null || algoId.isEmpty()) continue; |
| | | |
| | | String state = order.getString("state"); |
| | | if (!"filled".equals(state)) continue; |
| | | |
| | | String ordType = order.getString("ordType"); |
| | | log.info("[OKX-WS] 订单成交(algo), algoId:{}, ordType:{}, fillPx:{}, fillSz:{}", |
| | | algoId, ordType, |
| | | order.getString("fillPx"), |
| | | order.getString("fillSz")); |
| | | |
| | | if (gridTradeService != null) { |
| | | gridTradeService.onOrderUpdate(algoId, state, ordType); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("[OKX-WS] {} 处理数据失败", CHANNEL_NAME, e); |
| | | } |
| | | return true; |
| | | } |
| | | } |