package com.xcong.excoin.modules.gateApi.wsHandler.handler; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.xcong.excoin.modules.gateApi.GateGridTradeService; import com.xcong.excoin.modules.gateApi.wsHandler.AbstractPrivateChannelHandler; import lombok.extern.slf4j.Slf4j; import java.math.BigDecimal; /** * 平仓频道处理器。 * 私有频道,需认证。解析平仓推送后回调 {@link GateGridTradeService#onPositionClose}。 * * @author Administrator */ @Slf4j public class PositionClosesChannelHandler extends AbstractPrivateChannelHandler { private static final String CHANNEL_NAME = "futures.position_closes"; public PositionClosesChannelHandler(String apiKey, String apiSecret, String contract, GateGridTradeService gridTradeService) { super(CHANNEL_NAME, apiKey, apiSecret, contract, gridTradeService); } @Override public boolean handleMessage(JSONObject response) { String channel = response.getString("channel"); if (!CHANNEL_NAME.equals(channel)) { return false; } try { JSONArray resultArray = response.getJSONArray("result"); if (resultArray == null || resultArray.isEmpty()) { return true; } for (int i = 0; i < resultArray.size(); i++) { JSONObject item = resultArray.getJSONObject(i); if (!getContract().equals(item.getString("contract"))) { continue; } BigDecimal pnl = new BigDecimal(item.getString("pnl")); String side = item.getString("side"); log.info("[{}] 推送: contract={}, side={}, pnl={}", CHANNEL_NAME, getContract(), side, pnl); if (getGridTradeService() != null) { getGridTradeService().onPositionClose(getContract(), side, pnl); } } } catch (Exception e) { log.error("[{}] 处理数据失败", CHANNEL_NAME, e); } return true; } }