Administrator
7 days ago b0c4c70716351b4c539c03b0f1f6c4fd516779f2
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.xcong.excoin.modules.okxNewPrice.okxWs;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
import com.xcong.excoin.modules.okxNewPrice.okxpi.MallUtils;
import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild;
import com.xcong.excoin.modules.okxNewPrice.utils.WsParamBuild;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @author Administrator
 */
@Slf4j
public class PositionsWs {
 
    // 使用双层Map,第一层key为账号名称,第二层key为数据key
    public static final Map<String, Map<String, BigDecimal>> POSITIONSWSMAP = new ConcurrentHashMap<>();
    
    // 获取指定账号的Map,如果不存在则创建
    public static Map<String, BigDecimal> getAccountMap(String accountName) {
        return POSITIONSWSMAP.computeIfAbsent(accountName, k -> new ConcurrentHashMap<>());
    }
 
    public static final String POSITIONSWS_CHANNEL = "positions";
 
    public static void subscribePositionChannel(WebSocketClient webSocketClient, String option) {
        try {
            JSONArray argsArray = new JSONArray();
            JSONObject args = new JSONObject();
            args.put("channel", POSITIONSWS_CHANNEL);
            args.put("instType", CoinEnums.INSTTYPE_SWAP.getCode());
            args.put("instId", CoinEnums.HE_YUE.getCode());
            argsArray.add(args);
 
            String connId = MallUtils.getOrderNum(POSITIONSWS_CHANNEL);
            JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, option, argsArray);
            webSocketClient.send(jsonObject.toJSONString());
            log.info("发送持仓频道频道:{}", option);
        } catch (Exception e) {
            log.error("订阅持仓频道频道构建失败", e);
        }
    }
 
    public static String initAccountName(String accountName, String posSide) {
        return accountName+"_"+ posSide;
    }
 
    public static void initEvent(JSONObject response, String accountName) {
        log.info("订阅成功,数据初始化: {}", response.getJSONObject("arg"));
        JSONObject arg = response.getJSONObject("arg");
        initParam(arg, accountName,CoinEnums.POSSIDE_LONG.getCode());
        initParam(arg, accountName,CoinEnums.POSSIDE_SHORT.getCode());
    }
 
    public static void handleEvent(JSONObject response, String accountName) {
 
 
//        log.info("开始执行PositionsWs......");
        try {
            JSONArray dataArray = response.getJSONArray("data");
            if (dataArray == null || dataArray.isEmpty()) {
                log.info("账户持仓频道数据为空,等待更新");
                return;
            }
 
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject posData = dataArray.getJSONObject(i);
                String instId = posData.getString("instId");
                if (CoinEnums.HE_YUE.getCode().equals(instId)) {
//                    log.info("查询到账户{}持仓数据",CoinEnums.HE_YUE.getCode());
                    String mgnMode = posData.getString("mgnMode");
                    String posSide = posData.getString("posSide");
                    String pos = posData.getString("pos");
                    String avgPx = posData.getString("avgPx");
                    String upl = posData.getString("upl");
                    String uplRatio = posData.getString("uplRatio");
                    String lever = posData.getString("lever");
                    String liqPx = posData.getString("liqPx");
                    String markPx = posData.getString("markPx");
                    String imr = posData.getString("imr");
                    String mgnRatio = posData.getString("mgnRatio");
                    String mmr = posData.getString("mmr");
                    String notionalUsd = posData.getString("notionalUsd");
                    String ccy = posData.getString("ccy");
                    String last = posData.getString("last");
                    String idxPx = posData.getString("idxPx");
                    String bePx = posData.getString("bePx");
                    String realizedPnl = posData.getString("realizedPnl");
                    String settledPnl = posData.getString("settledPnl");
                    String fee = posData.getString("fee");
                    String fundingFee = posData.getString("fundingFee");
//                    log.info(
//                            "{}: 账户持仓频道-产品类型: {}, 保证金模式: {}, 持仓方向: {}, 持仓数量: {}, 开仓平均价: {}, "
//                                    + "未实现收益: {}, 未实现收益率: {}, 杠杆倍数: {}, 预估强平价: {}, 初始保证金: {}, "
//                                    + "维持保证金率: {}, 维持保证金: {}, 以美金价值为单位的持仓数量: {}, 占用保证金的币种: {}, "
//                                    + "最新成交价: {}, 最新指数价格: {}, 盈亏平衡价: {}, 已实现收益: {}, 累计已结算收益: {}"
//                                    + "最新标记价格: {},累计手续费: {},累计持仓费: {},",
//                            initAccountName(accountName, posSide), instId, mgnMode, posSide, pos, avgPx,
//                            upl, uplRatio, lever, liqPx, imr,
//                            mgnRatio, mmr, notionalUsd, ccy,
//                            last, idxPx, bePx, realizedPnl, settledPnl,
//                            markPx,fee,fundingFee
//                    );
                    initParam(posData, accountName,posSide);
                }
            }
        } catch (Exception e) {
            log.error("处理持仓频道推送数据失败", e);
        }
    }
 
    private static void initParam(JSONObject posData, String accountName,String posSide) {
        String accountNamePositons = initAccountName(accountName, posSide);
        Map<String, BigDecimal> accountMap = getAccountMap(accountNamePositons);
        WsMapBuild.saveBigDecimalToMap(accountMap, "avgPx", WsMapBuild.parseBigDecimalSafe(posData.getString("avgPx")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "pos", WsMapBuild.parseBigDecimalSafe(posData.getString("pos")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "upl", WsMapBuild.parseBigDecimalSafe(posData.getString("upl")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "imr", WsMapBuild.parseBigDecimalSafe(posData.getString("imr")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "mgnRatio", WsMapBuild.parseBigDecimalSafe(posData.getString("mgnRatio")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "markPx", WsMapBuild.parseBigDecimalSafe(posData.getString("markPx")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "bePx", WsMapBuild.parseBigDecimalSafe(posData.getString("bePx")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "realizedPnl", WsMapBuild.parseBigDecimalSafe(posData.getString("realizedPnl")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "fee", WsMapBuild.parseBigDecimalSafe(posData.getString("fee")));
        WsMapBuild.saveBigDecimalToMap(accountMap, "fundingFee", WsMapBuild.parseBigDecimalSafe(posData.getString("fundingFee")));
 
        BigDecimal ordFrozImr = PositionsWs.getAccountMap(accountNamePositons).get("imr");
        BigDecimal totalOrderUsdt = WsMapBuild.parseBigDecimalSafe(AccountWs.getAccountMap(accountName).get(CoinEnums.TOTAL_ORDER_USDT.name()))
                .divide(new BigDecimal("2"), RoundingMode.DOWN);
        if (ordFrozImr.compareTo(totalOrderUsdt) <= 0){
            WsMapBuild.saveBigDecimalToMap(accountMap, CoinEnums.READY_STATE.name(), WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_YES.getCode()));
        }
    }
}