Administrator
7 days ago 951009a204ec2d9e9caa813e67ea9adb1fb03818
src/main/java/com/xcong/excoin/modules/okxApi/OkxTradeExecutor.java
@@ -440,25 +440,57 @@
    }
    /**
     * 异步清除指定合约的所有算法订单(条件单)。
     * 发送不含 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);
                // ordType 是 orders-algo-pending 的必填参数,需分别查询 conditional 和 trigger
                JSONArray cancelBody = new JSONArray();
                for (String ordType : new String[]{"conditional", "trigger"}) {
                    String queryPath = "/api/v5/trade/orders-algo-pending?instId=" + contract
                            + "&ordType=" + ordType;
                    try {
                        JSONObject queryResp = okGet(queryPath);
                        if (!"0".equals(queryResp.getString("code"))) {
                            log.warn("[TradeExec-OKX] 查询 pending ordType={} 失败, code:{}, msg:{}",
                                    ordType, queryResp.getString("code"), queryResp.getString("msg"));
                            continue;
                        }
                        JSONArray data = queryResp.getJSONArray("data");
                        if (data != null) {
                            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);
                            }
                        }
                    } catch (Exception e) {
                        log.warn("[TradeExec-OKX] 查询待处理条件单失败, ordType:{}", ordType, e);
                    }
                }
                JSONObject resp = okPost("/api/v5/trade/cancel-algos", bodyArr.toJSONString());
                String code = resp.getString("code");
                if (!"0".equals(code)) {
                    log.warn("[TradeExec-OKX] 清除所有条件单失败, code:{}, msg:{}",
                            code, resp.getString("msg"));
                if (cancelBody.isEmpty()) {
                    log.info("[TradeExec-OKX] 无待处理条件单");
                    return;
                }
                log.info("[TradeExec-OKX] 已清除所有条件单");
                // 批量取消
                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);
            }