package com.xcong.excoin.modules.okxApi.wsHandler.handler;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.xcong.excoin.modules.okxApi.Direction;
|
import com.xcong.excoin.modules.okxApi.IOkxStrategy;
|
import com.xcong.excoin.modules.okxApi.OkxConfig;
|
import com.xcong.excoin.modules.okxApi.wsHandler.AbstractOkxPrivateChannelHandler;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.math.BigDecimal;
|
|
/**
|
* OKX 仓位频道处理器(positions),接收仓位更新推送并回调
|
* {@link IOkxStrategy#onPositionUpdate(String, Direction, BigDecimal, BigDecimal)}。
|
*
|
* <h3>订阅格式</h3>
|
* 私有频道,需要先登录认证。订阅 arg 使用 instType: "SWAP"(不指定具体 instId,
|
* 会在 handleMessage 中通过 config.getContract() 过滤)。
|
* <pre>
|
* {"op":"subscribe","args":[{"channel":"positions","instType":"SWAP"}]}
|
* </pre>
|
*
|
* <h3>数据映射</h3>
|
* <ul>
|
* <li>posSide "long" → {@link Direction#LONG}</li>
|
* <li>posSide "short" → {@link Direction#SHORT}</li>
|
* <li>pos → 持仓张数(绝对值)</li>
|
* <li>avgPx → 开仓均价</li>
|
* </ul>
|
*
|
* @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 strategy OKX 交易策略服务实例
|
*/
|
public PositionsOkxChannelHandler(OkxConfig config, IOkxStrategy strategy) {
|
super(CHANNEL_NAME,
|
config.getApiKey(), config.getApiSecret(), config.getPassphrase(),
|
config.getContract(),
|
strategy);
|
this.config = config;
|
}
|
|
/**
|
* 处理仓位推送消息。
|
*
|
* <h3>处理流程</h3>
|
* <ol>
|
* <li>检查 arg.channel 是否匹配 "positions"</li>
|
* <li>遍历 data 数组,按 instId 过滤出 config.getContract() 对应的仓位</li>
|
* <li>映射 posSide → Direction(long=DualLong, short=DualShort)</li>
|
* <li>提取 pos(张数)、avgPx(均价)</li>
|
* <li>调用 gridTradeService.onPositionUpdate(contract, direction, size, entryPrice)</li>
|
* </ol>
|
*
|
* @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");
|
Direction direction;
|
if ("long".equals(posSide)) {
|
direction = Direction.LONG;
|
} else if ("short".equals(posSide)) {
|
direction = 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 (getStrategy() != null) {
|
getStrategy().onPositionUpdate(contract, direction, size, entryPrice);
|
}
|
}
|
} catch (Exception e) {
|
log.error("[OKX-WS] 处理 positions 数据失败", e);
|
}
|
return true;
|
}
|
}
|