From 04063bcb7b9e9d8e0242c1313f54ccc1b71f0b6e Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 25 Jun 2026 22:46:56 +0800
Subject: [PATCH] fix(gateApi): 调整网格交易参数配置

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java |   50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 49 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java b/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
index a7ee107..9f1d7c2 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
@@ -330,6 +330,50 @@
     }
 
     /**
+     * 对外暴露的市价平仓接口(含回调),供策略层直接调用。
+     *
+     * @param size      平仓张数(正数)
+     * @param posSide   持仓方向(long=平多 / short=平空)
+     * @param onSuccess 成功回调(可为 null)
+     * @param onFailure 失败回调(可为 null)
+     */
+    public void marketClosePosition(String size, String posSide,
+                                     Runnable onSuccess, Runnable onFailure) {
+        executor.execute(() -> {
+            String side = "long".equals(posSide) ? "sell" : "buy";
+            try {
+                JSONObject body = new JSONObject();
+                body.put("instId", contract);
+                body.put("tdMode", "cross");
+                body.put("side", side);
+                body.put("posSide", posSide);
+                body.put("ordType", "market");
+                body.put("sz", size);
+
+                JSONObject resp = okPost("/api/v5/trade/order", body.toJSONString());
+                String code = resp.getString("code");
+                if (!"0".equals(code)) {
+                    log.error("[TradeExec-OKX] 市价平仓失败, side:{}, posSide:{}, sz:{}, code:{}, msg:{}",
+                            side, posSide, size, code, resp.getString("msg"));
+                    if (onFailure != null) {
+                        onFailure.run();
+                    }
+                    return;
+                }
+                log.info("[TradeExec-OKX] 市价平仓成功, side:{}, posSide:{}, sz:{}", side, posSide, size);
+                if (onSuccess != null) {
+                    onSuccess.run();
+                }
+            } catch (Exception e) {
+                log.error("[TradeExec-OKX] 市价平仓失败, side:{}, posSide:{}, sz:{}", side, posSide, size, e);
+                if (onFailure != null) {
+                    onFailure.run();
+                }
+            }
+        });
+    }
+
+    /**
      * 指定方向的市价平仓。
      *
      * @param sz      平仓张数
@@ -392,13 +436,17 @@
         executor.execute(() -> {
             try {
                 String side = isLong ? "buy" : "sell";
+                String posSide = isLong ? "long" : "short";
+                // OKX sz 必须为正数,strategy 层传入的负数需转正
+                String absSz = size.startsWith("-") ? size.substring(1) : size;
 
                 JSONObject body = new JSONObject();
                 body.put("instId", contract);
                 body.put("tdMode", "cross");
                 body.put("side", side);
+                body.put("posSide", posSide);            // 双向持仓模式必须指定
                 body.put("ordType", "trigger");          // 计划委托 = 触发后开仓
-                body.put("sz", size);
+                body.put("sz", absSz);
                 body.put("triggerPx", triggerPrice.stripTrailingZeros().toPlainString());
                 body.put("triggerPxType", "last");
                 body.put("orderPx", "-1");               // OKX 使用 orderPx,非 ordPx

--
Gitblit v1.9.1