| | |
| | | private static final String CHANNEL_NAME = "positions"; |
| | | |
| | | private final String instId; |
| | | private final String instFamily; |
| | | private final OkxGridTradeService gridTradeService; |
| | | |
| | | public OkxPositionsChannelHandler(String instId, OkxGridTradeService gridTradeService) { |
| | | this.instId = instId; |
| | | this.instFamily = instId.contains("-") ? instId.substring(0, instId.lastIndexOf("-")) : instId; |
| | | this.gridTradeService = gridTradeService; |
| | | } |
| | | |
| | |
| | | JSONObject arg = new JSONObject(); |
| | | arg.put("channel", CHANNEL_NAME); |
| | | arg.put("instType", "SWAP"); |
| | | arg.put("instFamily", instFamily); |
| | | msg.put("op", "subscribe"); |
| | | JSONArray args = new JSONArray(); |
| | | args.add(arg); |
| | | msg.put("args", args); |
| | | ws.send(msg.toJSONString()); |
| | | log.info("[OKX-WS] {} 订阅成功, instFamily:{}", CHANNEL_NAME, instFamily); |
| | | log.info("[OKX-WS] {} 订阅成功", CHANNEL_NAME); |
| | | } |
| | | |
| | | @Override |
| | | public void onSubscribed() { |
| | | log.info("[OKX-WS] {} 订阅确认", CHANNEL_NAME); |
| | | if (gridTradeService != null) { |
| | | gridTradeService.onSubscriptionConfirmed(CHANNEL_NAME); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | JSONObject arg = new JSONObject(); |
| | | arg.put("channel", CHANNEL_NAME); |
| | | arg.put("instType", "SWAP"); |
| | | arg.put("instFamily", instFamily); |
| | | msg.put("op", "unsubscribe"); |
| | | JSONArray args = new JSONArray(); |
| | | args.add(arg); |
| | |
| | | if (data == null || data.isEmpty()) return true; |
| | | for (int i = 0; i < data.size(); i++) { |
| | | JSONObject pos = data.getJSONObject(i); |
| | | if (!instId.equals(pos.getString("instId"))) continue; |
| | | String posInstId = pos.getString("instId"); |
| | | if (posInstId == null || !instId.equals(posInstId)) continue; |
| | | |
| | | String posSide = pos.getString("posSide"); |
| | | BigDecimal posSize = new BigDecimal(pos.getString("pos")); |
| | | BigDecimal avgPx = new BigDecimal(pos.getString("avgPx")); |
| | | 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); |
| | | |