Administrator
2026-06-08 23df5ca18786801917920513c420a8aaa6391b0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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),接收仓位更新推送并回调 {@link GateGridTradeService#onPositionUpdate}。
 *
 * @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;
    }
}