From 4dbaa914142b578be0756608d3cd23c328c3e4cd Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 05 Jan 2026 10:14:24 +0800
Subject: [PATCH] fix(okxWs): 修复订单信息WebSocket中账户名称初始化参数错误

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/PositionsWs.java |   73 ++++++++++++++++++++++--------------
 1 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/PositionsWs.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/PositionsWs.java
index 75e230a..f1a8d12 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/PositionsWs.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/PositionsWs.java
@@ -3,6 +3,7 @@
 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;
@@ -10,6 +11,7 @@
 import org.java_websocket.client.WebSocketClient;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -19,7 +21,13 @@
 @Slf4j
 public class PositionsWs {
 
-    public static  final Map<String,BigDecimal> POSITIONSWSMAP = new ConcurrentHashMap<>();
+    // 使用双层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";
 
@@ -35,28 +43,31 @@
             String connId = MallUtils.getOrderNum(POSITIONSWS_CHANNEL);
             JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, option, argsArray);
             webSocketClient.send(jsonObject.toJSONString());
-//            log.info("发送持仓频道频道:{}", option);
+            log.info("发送持仓频道频道:{}", option);
         } catch (Exception e) {
             log.error("订阅持仓频道频道构建失败", e);
         }
     }
 
-    public static void initEvent(JSONObject response) {
-//        log.info("订阅成功,数据初始化: {}", response.getJSONObject("arg"));
-        JSONObject arg = response.getJSONObject("arg");
-        initParam(arg);
+    public static String initAccountName(String accountName, String posSide) {
+        return accountName+"_"+ posSide;
     }
 
-    public static void handleEvent(JSONObject response) {
+    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......");
+        log.info("开始执行PositionsWs......");
         try {
             JSONArray dataArray = response.getJSONArray("data");
             if (dataArray == null || dataArray.isEmpty()) {
-//                log.info("账户持仓频道数据为空,已当前价买入,并且初始化网格");
-                JSONObject posData = new JSONObject();
-                initParam(posData);
+                log.info("账户持仓频道数据为空,等待更新");
                 return;
             }
 
@@ -64,7 +75,7 @@
                 JSONObject posData = dataArray.getJSONObject(i);
                 String instId = posData.getString("instId");
                 if (CoinEnums.HE_YUE.getCode().equals(instId)) {
-//                    log.info("查询到账户{}持仓数据",CoinEnums.HE_YUE.getCode());
+                    log.info("查询到账户{}持仓数据",CoinEnums.HE_YUE.getCode());
                     String mgnMode = posData.getString("mgnMode");
                     String posSide = posData.getString("posSide");
                     String pos = posData.getString("pos");
@@ -87,19 +98,18 @@
                     String fee = posData.getString("fee");
                     String fundingFee = posData.getString("fundingFee");
                     log.info(
-                            "账户持仓频道-产品类型: {}, 保证金模式: {}, 持仓方向: {}, 持仓数量: {}, 开仓平均价: {}, "
+                            "{}: 账户持仓频道-产品类型: {}, 保证金模式: {}, 持仓方向: {}, 持仓数量: {}, 开仓平均价: {}, "
                                     + "未实现收益: {}, 未实现收益率: {}, 杠杆倍数: {}, 预估强平价: {}, 初始保证金: {}, "
                                     + "维持保证金率: {}, 维持保证金: {}, 以美金价值为单位的持仓数量: {}, 占用保证金的币种: {}, "
                                     + "最新成交价: {}, 最新指数价格: {}, 盈亏平衡价: {}, 已实现收益: {}, 累计已结算收益: {}"
                                     + "最新标记价格: {},累计手续费: {},累计持仓费: {},",
-                            instId, mgnMode, posSide, pos, avgPx,
+                            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);
+                    initParam(posData, accountName,posSide);
                 }
             }
         } catch (Exception e) {
@@ -107,18 +117,25 @@
         }
     }
 
-    private static void initParam(JSONObject posData) {
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "avgPx", WsMapBuild.parseBigDecimalSafe(posData.getString("avgPx")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "pos", WsMapBuild.parseBigDecimalSafe(posData.getString("pos")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "upl", WsMapBuild.parseBigDecimalSafe(posData.getString("upl")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "imr", WsMapBuild.parseBigDecimalSafe(posData.getString("imr")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "mgnRatio", WsMapBuild.parseBigDecimalSafe(posData.getString("mgnRatio")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "markPx", WsMapBuild.parseBigDecimalSafe(posData.getString("markPx")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "bePx", WsMapBuild.parseBigDecimalSafe(posData.getString("bePx")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "realizedPnl", WsMapBuild.parseBigDecimalSafe(posData.getString("realizedPnl")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "fee", WsMapBuild.parseBigDecimalSafe(posData.getString("fee")));
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, "fundingFee", WsMapBuild.parseBigDecimalSafe(posData.getString("fundingFee")));
+    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")));
 
-        WsMapBuild.saveBigDecimalToMap(POSITIONSWSMAP, CoinEnums.READY_STATE.name(), WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_YES.getCode()));
+        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()));
+        }
     }
 }

--
Gitblit v1.9.1