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 io.gate.gateapi.models.Position;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
/**
* 仓位频道处理器(futures.positions)。
*
*
数据用途
* 监控仓位数量(size)和入场价(entry_price)。
* 有仓位时(size.abs > 0):标记方向活跃,记录入场价 → 基底首次成交记录基底入场价并等待生成网格队列,
* 非基底成交立即设止盈条件单。无仓位时(size=0):标记方向不活跃。
*
* 完整推送字段(共20个,日志全部输出)
*
* | 字段 | 类型 | 描述 |
* | contract | String | 合约名称 |
* | mode | String | 持仓模式:dual_long / dual_short |
* | size | String/Integer | 合约张数(正=多头,负=空头) |
* | entry_price | Float | 开仓均价 |
* | cross_leverage_limit | Float | 全仓模式下的杠杆倍数上限 |
* | history_pnl | Float | 已平仓的仓位总盈亏 |
* | history_point | Float | 已平仓的点卡总盈亏 |
* | last_close_pnl | Float | 最近一次平仓的盈亏 |
* | leverage | Integer | 杠杆倍数(0=全仓,正数=逐仓) |
* | leverage_max | Integer | 当前风险限额下允许的最大杠杆倍数 |
* | liq_price | Float | 爆仓价格 |
* | maintenance_rate | Float | 当前风险限额下维持保证金比例 |
* | margin | Float | 保证金 |
* | realised_pnl | Float | 已实现盈亏 |
* | realised_point | Float | 点卡已实现盈亏 |
* | risk_limit | Integer | 风险限额 |
* | time | Integer | 更新 unix 时间戳(秒) |
* | time_ms | Integer | 更新 unix 时间戳(毫秒) |
* | user | String | 用户 ID |
* | update_id | Integer | 消息序列号,每次推送 order 后自增1 |
*
*
* 回调数据(传给 GateGridTradeService)
* mode (Position.ModeEnum), size (BigDecimal), entry_price (BigDecimal)
*
* 注意
* 双向持仓模式下空头 size 为负数,使用 {@code size.abs()} 判断是否有仓位。
* 累计盈亏不由本频道计算,而是由 {@link PositionClosesChannelHandler} 独立处理。
* 止盈条件单由服务端自动触发平仓,本频道不负责开仓操作。
*
* @author Administrator
*/
@Slf4j
public class PositionsChannelHandler extends AbstractPrivateChannelHandler {
private static final String CHANNEL_NAME = "futures.positions";
/**
* @param apiKey Gate API v4 密钥,用于签名认证
* @param apiSecret Gate API v4 签名密钥
* @param contract 合约名称(如 ETH_USDT)
* @param gridTradeService 网格交易策略服务实例
*/
public PositionsChannelHandler(String apiKey, String apiSecret,
String contract,
GateGridTradeService gridTradeService) {
super(CHANNEL_NAME, apiKey, apiSecret, contract, gridTradeService);
}
@Override
public boolean handleMessage(JSONObject response) {
if (!CHANNEL_NAME.equals(response.getString("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 pos = resultArray.getJSONObject(i);
if (!getContract().equals(pos.getString("contract"))) {
continue;
}
String modeStr = pos.getString("mode");
Position.ModeEnum mode = Position.ModeEnum.fromValue(modeStr);
BigDecimal size = new BigDecimal(pos.getString("size"));
BigDecimal entryPrice = new BigDecimal(pos.getString("entry_price"));
log.info("[{}] 持仓更新, 合约:{}, 模式:{}, 数量:{}, 入场价:{}, 全仓杠杆上限:{}, 历史盈亏:{}, 历史点卡:{}, 最近平仓盈亏:{}, 杠杆:{}, 最大杠杆:{}, 爆仓价:{}, 维持保证金率:{}, 保证金:{}, 已实现盈亏:{}, 点卡已实现盈亏:{}, 风险限额:{}, 时间:{}, 时间ms:{}, 用户:{}, 更新ID:{}",
CHANNEL_NAME, pos.getString("contract"), modeStr, size, entryPrice,
pos.get("cross_leverage_limit"), pos.get("history_pnl"), pos.get("history_point"),
pos.get("last_close_pnl"), pos.get("leverage"), pos.get("leverage_max"),
pos.get("liq_price"), pos.get("maintenance_rate"), pos.get("margin"),
pos.get("realised_pnl"), pos.get("realised_point"), pos.get("risk_limit"),
pos.get("time"), pos.get("time_ms"), pos.get("user"), pos.get("update_id"));
if (getGridTradeService() != null) {
getGridTradeService().onPositionUpdate(getContract(), mode, size, entryPrice);
}
}
} catch (Exception e) { log.error("[{}] 处理数据失败", CHANNEL_NAME, e); }
return true;
}
}