| | |
| | | import java.util.concurrent.LinkedBlockingQueue; |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Consumer; |
| | | |
| | | /** |
| | | * Gate REST API 执行器。 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 异步挂限价单(GTC),用于网格限价开仓。 |
| | | * |
| | | * @param price 限价价格 |
| | | * @param size 下单张数(正=多 / 负=空) |
| | | * @param onSuccess 成功回调,接收 orderId(可为 null) |
| | | * @param onFailure 失败回调(可为 null) |
| | | */ |
| | | public void placeGridLimitOrder(BigDecimal price, String size, Consumer<String> onSuccess, Runnable onFailure) { |
| | | executor.execute(() -> { |
| | | try { |
| | | FuturesOrder order = new FuturesOrder(); |
| | | order.setContract(contract); |
| | | order.setSize(size); |
| | | order.setPrice(price.toString()); |
| | | order.setTif(FuturesOrder.TifEnum.GTC); |
| | | order.setText(size.startsWith("-") ? "t-grid-limit-short" : "t-grid-limit-long"); |
| | | FuturesOrder result = futuresApi.createFuturesOrder(SETTLE, order, null); |
| | | log.info("[TradeExec] 限价单已挂, price:{}, size:{}, id:{}, status:{}", price, size, result.getId(), result.getStatus()); |
| | | if (onSuccess != null) { |
| | | onSuccess.accept(String.valueOf(result.getId())); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("[TradeExec] 限价单挂单失败, price:{}, size:{}", price, size, e); |
| | | if (onFailure != null) { |
| | | onFailure.run(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 异步取消指定订单。 |
| | | * |
| | | * @param orderId 订单 ID,为 null 时跳过 |
| | | */ |
| | | public void cancelOrder(String orderId) { |
| | | if (orderId == null) { |
| | | return; |
| | | } |
| | | executor.execute(() -> { |
| | | try { |
| | | FuturesOrder cancelled = futuresApi.cancelFuturesOrder(SETTLE, orderId, null); |
| | | log.info("[TradeExec] 订单已取消, id:{}, status:{}", orderId, cancelled.getStatus()); |
| | | } catch (Exception e) { |
| | | log.warn("[TradeExec] 取消订单失败(可能已成交), id:{}", orderId); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 构建 FuturesPriceTriggeredOrder 对象。 |
| | | * |
| | | * <p>策略=0(价格触发),price_type=0(最新价),expiration=0(永不过期), |