From e70d64cf80e3526c3ec46ba8f97d9b6d95d9d851 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 24 Jun 2026 15:04:17 +0800
Subject: [PATCH] fix(okx): 修复本金计算逻辑和条件单取消功能

---
 src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java |   33 ++++++++++++----
 src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java    |   54 +++++++++++++++++++++------
 2 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java b/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
index 6db2ab3..99fdd44 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxGridTradeService.java
@@ -133,11 +133,19 @@
     public void init() {
         try {
             JSONObject account = executorGet("/api/v5/account/balance");
-            JSONArray details = account.getJSONArray("data");
-            if (details != null && !details.isEmpty()) {
-                JSONObject total = details.getJSONObject(0);
-                this.initialPrincipal = total.getBigDecimal("totalEq");
-                log.info("[OKX] 初始本金: {} USDT", initialPrincipal);
+            JSONArray dataArr = account.getJSONArray("data");
+            if (dataArr != null && !dataArr.isEmpty()) {
+                JSONArray details = dataArr.getJSONObject(0).getJSONArray("details");
+                if (details != null) {
+                    for (int i = 0; i < details.size(); i++) {
+                        JSONObject currency = details.getJSONObject(i);
+                        if ("USDT".equals(currency.getString("ccy"))) {
+                            this.initialPrincipal = currency.getBigDecimal("eq");
+                            log.info("[OKX] 初始本金(USDT余额): {}", initialPrincipal);
+                            break;
+                        }
+                    }
+                }
             }
 
             // 设置双向持仓模式
@@ -237,9 +245,18 @@
     private void refreshInitialPrincipal() {
         try {
             JSONObject account = executorGet("/api/v5/account/balance");
-            JSONArray details = account.getJSONArray("data");
-            if (details != null && !details.isEmpty()) {
-                this.initialPrincipal = details.getJSONObject(0).getBigDecimal("totalEq");
+            JSONArray dataArr = account.getJSONArray("data");
+            if (dataArr != null && !dataArr.isEmpty()) {
+                JSONArray details = dataArr.getJSONObject(0).getJSONArray("details");
+                if (details != null) {
+                    for (int i = 0; i < details.size(); i++) {
+                        JSONObject currency = details.getJSONObject(i);
+                        if ("USDT".equals(currency.getString("ccy"))) {
+                            this.initialPrincipal = currency.getBigDecimal("eq");
+                            break;
+                        }
+                    }
+                }
             }
         } catch (Exception e) {
             log.warn("[OKX] 获取初始化本金失败,使用旧值: {}", initialPrincipal);
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 0b8cb40..2eb5ed0 100644
--- a/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
+++ b/src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
@@ -440,25 +440,55 @@
     }
 
     /**
-     * 异步清除指定合约的所有算法订单(条件单)。
-     * 发送不含 algoId 的取消请求,OKX 会取消该合约下所有待触发算法单。
+     * 异步清除指定合约的所有算法订单(条件单/止盈止损单)。
+     *
+     * <p>OKX 的 cancel-algos 接口要求必须传 algoId 或 algoClOrdId,
+     * 不能仅凭 instId 批量取消。因此先查询待处理列表,再逐个取消。
      */
     public void cancelAllPriceTriggeredOrders() {
         executor.execute(() -> {
             try {
-                JSONArray bodyArr = new JSONArray();
-                JSONObject item = new JSONObject();
-                item.put("instId", contract);
-                bodyArr.add(item);
-
-                JSONObject resp = okPost("/api/v5/trade/cancel-algos", bodyArr.toJSONString());
-                String code = resp.getString("code");
+                // 1. 查询所有待处理的算法订单
+                String queryPath = "/api/v5/trade/orders-algo-pending?instId=" + contract;
+                JSONObject queryResp = okGet(queryPath);
+                String code = queryResp.getString("code");
                 if (!"0".equals(code)) {
-                    log.warn("[TradeExec-OKX] 清除所有条件单失败, code:{}, msg:{}",
-                            code, resp.getString("msg"));
+                    log.warn("[TradeExec-OKX] 查询待处理条件单失败, code:{}, msg:{}",
+                            code, queryResp.getString("msg"));
                     return;
                 }
-                log.info("[TradeExec-OKX] 已清除所有条件单");
+
+                JSONArray data = queryResp.getJSONArray("data");
+                if (data == null || data.isEmpty()) {
+                    log.info("[TradeExec-OKX] 无待处理条件单");
+                    return;
+                }
+
+                // 2. 收集所有 algoId
+                JSONArray cancelBody = new JSONArray();
+                for (int i = 0; i < data.size(); i++) {
+                    JSONObject order = data.getJSONObject(i);
+                    String algoId = order.getString("algoId");
+                    if (algoId == null) continue;
+                    JSONObject item = new JSONObject();
+                    item.put("algoId", algoId);
+                    item.put("instId", contract);
+                    cancelBody.add(item);
+                }
+
+                if (cancelBody.isEmpty()) {
+                    return;
+                }
+
+                // 3. 批量取消
+                JSONObject cancelResp = okPost("/api/v5/trade/cancel-algos", cancelBody.toJSONString());
+                String cancelCode = cancelResp.getString("code");
+                if (!"0".equals(cancelCode)) {
+                    log.warn("[TradeExec-OKX] 清除条件单部分失败, code:{}, msg:{}",
+                            cancelCode, cancelResp.getString("msg"));
+                    return;
+                }
+                log.info("[TradeExec-OKX] 已清除{}个条件单", cancelBody.size());
             } catch (Exception e) {
                 log.error("[TradeExec-OKX] 清除条件单失败", e);
             }

--
Gitblit v1.9.1