| | |
| | | * @param onSuccess 成交成功回调(可为 null) |
| | | * @param onFailure 成交失败回调(可为 null) |
| | | */ |
| | | public void openLong(String quantity, Runnable onSuccess, Runnable onFailure) { |
| | | public void openLong(String quantity, Consumer<String> onSuccess, Runnable onFailure) { |
| | | openPosition(quantity, "t-grid-long", "开多", onSuccess, onFailure); |
| | | } |
| | | |
| | |
| | | * @param onSuccess 成交成功回调(可为 null) |
| | | * @param onFailure 成交失败回调(可为 null) |
| | | */ |
| | | public void openShort(String quantity, Runnable onSuccess, Runnable onFailure) { |
| | | public void openShort(String quantity, Consumer<String> onSuccess, Runnable onFailure) { |
| | | openPosition(quantity, "t-grid-short", "开空", onSuccess, onFailure); |
| | | } |
| | | |
| | |
| | | * @param onSuccess 成功回调 |
| | | * @param onFailure 失败回调 |
| | | */ |
| | | private void openPosition(String size, String text, String label, Runnable onSuccess, Runnable onFailure) { |
| | | private void openPosition(String size, String text, String label, Consumer<String> onSuccess, Runnable onFailure) { |
| | | executor.execute(() -> { |
| | | try { |
| | | FuturesOrder order = new FuturesOrder(); |
| | |
| | | order.setText(text); |
| | | FuturesOrder result = futuresApi.createFuturesOrder(SETTLE, order, null); |
| | | log.info("[TradeExec] {}成功, 价格:{}, id:{}", label, result.getFillPrice(), result.getId()); |
| | | String orderId = String.valueOf(result.getId()); |
| | | if (onSuccess != null) { |
| | | onSuccess.run(); |
| | | onSuccess.accept(orderId); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("[TradeExec] {}失败", label, e); |
| | |
| | | public void placeTakeProfit(BigDecimal triggerPrice, |
| | | FuturesPriceTrigger.RuleEnum rule, |
| | | String orderType, |
| | | String size) { |
| | | String size, |
| | | Consumer<String> onSuccess) { |
| | | executor.execute(() -> { |
| | | FuturesPriceTriggeredOrder order = buildTriggeredOrder(triggerPrice, rule, orderType, size); |
| | | try { |
| | | TriggerOrderResponse response = futuresApi.createPriceTriggeredOrder(SETTLE, order); |
| | | log.info("[TradeExec] 止盈单已创建, 触发价:{}, 类型:{}, size:{}, id:{}", |
| | | triggerPrice, orderType, size, response.getId()); |
| | | String orderId = String.valueOf(response.getId()); |
| | | if (onSuccess != null) { |
| | | onSuccess.accept(orderId); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("[TradeExec] 止盈单创建失败, 触发价:{}, size:{}, 立即市价止盈", triggerPrice, size, e); |
| | | marketClose(size); |
| | |
| | | |
| | | TriggerOrderResponse response = futuresApi.createPriceTriggeredOrder(SETTLE, order); |
| | | String orderId = String.valueOf(response.getId()); |
| | | String orderIdStr = response.getIdString(); |
| | | log.info("[TradeExec] 条件开仓单已创建, trigger:{}, rule:{}, size:{}, id:{}", |
| | | triggerPrice, rule, size, orderId); |
| | | if (onSuccess != null) { |