From a80451b53728e62db65bafe7c4e7a43b5e3259cd Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 10 Dec 2025 14:11:20 +0800
Subject: [PATCH] feat(okxNewPrice): 新增止损冷静机制并优化订单状态控制

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/okxWs/TradeOrderWs.java |   65 +++++++++++++++++++++++++++++---
 1 files changed, 58 insertions(+), 7 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..093ac5e 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
  */
@@ -21,23 +23,44 @@
 
     public static void orderEvent(WebSocketClient webSocketClient, RedisUtils redisUtils, String side) {
 
-        String buyCnt = null;
+        log.info("开始执行TradeOrderWs......");
+        if (StrUtil.isBlank( side)){
+            log.warn("止损了,下次再战...");
+            return;
+        }
 
+        String buyCnt = null;
+        String ctval = getRedisValue(redisUtils, InstrumentsWs.INSTRUMENTSWS_CHANNEL, ":ctVal");
+        String buyCntNormal = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":buyCnt");
+        String pos = getRedisValue(redisUtils, PositionsWs.POSITIONSWS_CHANNEL, ":pos");
         if (OrderParamEnums.ORDERING.getValue().equals(side)) {
             return;
         } else if (OrderParamEnums.HOLDING.getValue().equals(side)) {
             return;
         } else if (OrderParamEnums.INIT.getValue().equals(side)) {
             side = OrderParamEnums.BUY.getValue();
-            buyCnt = getRedisValue(redisUtils, InstrumentsWs.INSTRUMENTSWS_CHANNEL, ":ctVal");
+            if (StrUtil.isNotBlank(buyCntNormal) && BigDecimal.ZERO.compareTo(new BigDecimal(buyCntNormal)) > 0) {
+                buyCnt = buyCntNormal;
+            }else{
+                buyCnt = ctval;
+            }
         } else if (OrderParamEnums.OUT.getValue().equals(side)) {
+            log.info(OrderParamEnums.getNameByValue(OrderParamEnums.OUT.getValue()));
+            side = OrderParamEnums.SELL.getValue();
+            buyCnt = pos;
+        } else if (OrderParamEnums.BUY.getValue().equals(side)){
+            side = OrderParamEnums.BUY.getValue();
+            if (StrUtil.isNotBlank(buyCntNormal) && BigDecimal.ZERO.compareTo(new BigDecimal(buyCntNormal)) > 0) {
+                buyCnt = buyCntNormal;
+            }else{
+                buyCnt = ctval;
+            }
+        }else if (OrderParamEnums.SELL.getValue().equals(side)){
             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;
-            }
+        }else{
+            log.warn("操作信号异常,请检查下单操作...");
+            return;
         }
 
         // 校验必要参数
@@ -80,6 +103,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