| | |
| | | } |
| | | |
| | | /** |
| | | * 对外暴露的市价平仓接口(含回调),供策略层直接调用。 |
| | | * |
| | | * @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 平仓张数 |
| | |
| | | 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 |