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.AbstractOkxPrivateChannelHandler; import com.xcong.excoin.modules.okxApi.TraderParam; import lombok.extern.slf4j.Slf4j; import java.math.BigDecimal; /** * OKX 仓位频道处理器(positions),接收仓位更新推送并回调 * {@link OkxGridTradeService#onPositionUpdate(String, TraderParam.Direction, BigDecimal, BigDecimal)}。 * *

订阅格式

* 私有频道,需要先登录认证。订阅 arg 使用 instType: "SWAP"(不指定具体 instId, * 会在 handleMessage 中通过 config.getContract() 过滤)。 *
 * {"op":"subscribe","args":[{"channel":"positions","instType":"SWAP"}]}
 * 
* *

数据推送格式

*
 * {
 *   "arg": {"channel":"positions","instType":"SWAP"},
 *   "data": [{
 *     "instId": "ETH-USDT-SWAP",
 *     "posSide": "long",        // "long" 或 "short"
 *     "pos": "1",               // 持仓张数
 *     "avgPx": "3000",          // 开仓均价
 *     ...
 *   }]
 * }
 * 
* *

数据映射

* * * @author Administrator */ @Slf4j public class PositionsOkxChannelHandler extends AbstractOkxPrivateChannelHandler { /** OKX 仓位频道名称 */ private static final String CHANNEL_NAME = "positions"; /** OKX 配置,用于获取合约名称进行过滤 */ private final OkxConfig config; /** * 构造仓位频道处理器。 * * @param config OKX 配置实例(提供合约名称等) * @param gridTradeService OKX 网格交易策略服务实例 */ public PositionsOkxChannelHandler(OkxConfig config, OkxGridTradeService gridTradeService) { super(CHANNEL_NAME, config.getApiKey(), config.getApiSecret(), config.getPassphrase(), config.getContract(), gridTradeService); this.config = config; } /** * 处理仓位推送消息。 * *

处理流程

*
    *
  1. 检查 arg.channel 是否匹配 "positions"
  2. *
  3. 遍历 data 数组,按 instId 过滤出 config.getContract() 对应的仓位
  4. *
  5. 映射 posSide → Direction(long=DualLong, short=DualShort)
  6. *
  7. 提取 pos(张数)、avgPx(均价)
  8. *
  9. 调用 gridTradeService.onPositionUpdate(contract, direction, size, entryPrice)
  10. *
* * @param response WebSocket 推送的完整 JSON * @return true 表示已处理(匹配成功) */ @Override public boolean handleMessage(JSONObject response) { JSONObject arg = response.getJSONObject("arg"); if (arg == null || !CHANNEL_NAME.equals(arg.getString("channel"))) { return false; } try { JSONArray dataArray = response.getJSONArray("data"); if (dataArray == null || dataArray.isEmpty()) { return true; } String contract = config.getContract(); // e.g., "ETH-USDT" for (int i = 0; i < dataArray.size(); i++) { JSONObject posData = dataArray.getJSONObject(i); // 按 instId 精确过滤,只处理当前合约的仓位(避免误匹配交割合约) String dataInstId = posData.getString("instId"); if (dataInstId == null || !dataInstId.equals(contract)) { continue; } // 解析持仓方向:OKX 的 posSide 可以是 "long" 或 "short" String posSide = posData.getString("posSide"); TraderParam.Direction direction; if ("long".equals(posSide)) { direction = TraderParam.Direction.LONG; } else if ("short".equals(posSide)) { direction = TraderParam.Direction.SHORT; } else { log.debug("[OKX-WS] positions 忽略 net 方向: {}", posSide); continue; } String posStr = posData.getString("pos"); String avgPxStr = posData.getString("avgPx"); // 仓位归零时 OKX 推送 avgPx: ""(空串),需做防护 BigDecimal size = (posStr != null && !posStr.isEmpty()) ? new BigDecimal(posStr) : BigDecimal.ZERO; BigDecimal entryPrice = (avgPxStr != null && !avgPxStr.isEmpty()) ? new BigDecimal(avgPxStr) : BigDecimal.ZERO; log.info("[OKX-WS] positions 持仓更新, instId:{}, posSide:{}, pos:{}, avgPx:{}", dataInstId, posSide, size, entryPrice); if (getGridTradeService() != null) { getGridTradeService().onPositionUpdate(contract, direction, size, entryPrice); } } } catch (Exception e) { log.error("[OKX-WS] 处理 positions 数据失败", e); } return true; } }