From 7e213a5e5a233ba19eaa148cc65c9f3cfa96986e Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 09 Dec 2025 14:45:57 +0800
Subject: [PATCH] feat(okx): 优化持仓策略与交易逻辑

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java |   41 ++++++++++++++++++++++++++++++++++++++---
 1 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
index 7712098..0c9eae3 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java
@@ -11,6 +11,8 @@
 import lombok.extern.slf4j.Slf4j;
 import org.java_websocket.client.WebSocketClient;
 
+import java.math.BigDecimal;
+
 /**
  * @author Administrator
  */
@@ -34,9 +36,14 @@
             side = OrderParamEnums.SELL.getValue();
             buyCnt = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
         } else {
-            String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
-            if (StrUtil.isNotBlank(buyCntNormal)) {
-                buyCnt = buyCntNormal;
+            if (OrderParamEnums.BUY.getValue().equals(side)){
+                String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
+                if (StrUtil.isNotBlank(buyCntNormal)) {
+                    buyCnt = buyCntNormal;
+                }
+            }else{
+                side = OrderParamEnums.SELL.getValue();
+                buyCnt = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
             }
         }
 
@@ -80,6 +87,34 @@
         }
     }
 
+
+
+    /**
+     * 计算盈亏金额。
+     *
+     * @param faceValue 面值
+     * @param position 持仓数量
+     * @param markPrice 标记价格
+     * @param openPrice 开仓价格
+     * @param isLong 是否为多头仓位
+     * @param minTickSz 最小变动单位精度
+     * @return 盈亏金额,保留指定精度的小数位
+     */
+    public BigDecimal profit(BigDecimal faceValue, BigDecimal position,
+                             BigDecimal markPrice, BigDecimal openPrice, boolean isLong, int minTickSz) {
+        BigDecimal profit = BigDecimal.ZERO;
+        if (isLong) {
+            profit = markPrice.subtract(openPrice)
+                    .multiply(faceValue)
+                    .multiply(position);
+        } else {
+            profit = openPrice.subtract(markPrice)
+                    .multiply(faceValue)
+                    .multiply(position);
+        }
+        return profit.setScale(minTickSz, BigDecimal.ROUND_DOWN);
+    }
+
     /**
      * 统一封装 Redis Key 构建逻辑
      *

--
Gitblit v1.9.1