From 4e9dc7cf811d66907d41b3c61a8c57a536f17c55 Mon Sep 17 00:00:00 2001 From: rizer1980 <4340180@gmail.com> Date: Wed, 29 May 2024 19:57:38 +0300 Subject: [PATCH] [bybit] cancel order --- .../xchange/bybit/BybitAuthenticated.java | 17 ++++++++++++-- .../dto/trade/BybitCancelOrderPayload.java | 23 +++++++++++++++++++ .../bybit/service/BybitTradeService.java | 12 ++++++++++ .../bybit/service/BybitTradeServiceRaw.java | 16 +++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitCancelOrderPayload.java diff --git a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAuthenticated.java b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAuthenticated.java index 16fb0dea3d..5615594183 100644 --- a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAuthenticated.java +++ b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAuthenticated.java @@ -13,6 +13,7 @@ import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.MediaType; import java.io.IOException; +import org.knowm.xchange.bybit.dto.trade.BybitCancelOrderPayload; import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload; import org.knowm.xchange.bybit.dto.BybitResult; import org.knowm.xchange.bybit.dto.account.allcoins.BybitAllCoinsBalance; @@ -103,7 +104,19 @@ BybitResult placeLimitOrder( @HeaderParam(X_BAPI_SIGN) ParamsDigest signature, @HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory timestamp, BybitPlaceOrderPayload payload) -// @FormParam("positionIdx") -// @FormParam("reduceOnly") throws IOException,BybitException; + + /** + * @apiSpec API + */ + @POST + @Path("/order/cancel") + @Consumes(MediaType.APPLICATION_JSON) + BybitResult cancelOrder( + @HeaderParam(X_BAPI_API_KEY) String apiKey, + @HeaderParam(X_BAPI_SIGN) ParamsDigest signature, + @HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory timestamp, + BybitCancelOrderPayload payload) + throws IOException,BybitException; + } diff --git a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitCancelOrderPayload.java b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitCancelOrderPayload.java new file mode 100644 index 0000000000..d67c9e6489 --- /dev/null +++ b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitCancelOrderPayload.java @@ -0,0 +1,23 @@ +package org.knowm.xchange.bybit.dto.trade; + +import lombok.Getter; +import org.knowm.xchange.bybit.Bybit; +import org.knowm.xchange.bybit.dto.BybitCategory; + +@Getter +public class BybitCancelOrderPayload { + + private BybitCategory category; + private String symbol; + private String orderId; + private String orderLinkId; + private String orderFilter; + + public BybitCancelOrderPayload(BybitCategory category, String symbol, String orderId, String orderLinkId) { + this.category = category; + this.symbol = symbol; + this.orderId = orderId; + this.orderLinkId = orderLinkId; + } + +} diff --git a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeService.java b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeService.java index 27ef297dc4..3f9e154c0b 100644 --- a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeService.java +++ b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeService.java @@ -1,6 +1,7 @@ package org.knowm.xchange.bybit.service; import static org.knowm.xchange.bybit.BybitAdapters.adaptBybitOrderDetails; +import static org.knowm.xchange.bybit.BybitAdapters.convertToBybitSymbol; import java.io.IOException; import java.util.ArrayList; @@ -72,4 +73,15 @@ public Collection getOrder(String... orderIds) throws IOException { return results; } + + public String cancelOrder(Order order) throws IOException { + BybitCategory category = BybitAdapters.getCategory(order.getInstrument()); + BybitResult response = cancelOrder(category, + convertToBybitSymbol(order.getInstrument()), order.getId(), order.getUserReference()); + if (response != null) { + return response.getResult().getOrderId(); + } else + return ""; + } + } diff --git a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeServiceRaw.java b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeServiceRaw.java index 37e2f4f430..75c6e9611f 100644 --- a/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeServiceRaw.java +++ b/xchange-bybit/src/main/java/org/knowm/xchange/bybit/service/BybitTradeServiceRaw.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import org.knowm.xchange.Exchange; import org.knowm.xchange.bybit.dto.BybitCategory; +import org.knowm.xchange.bybit.dto.trade.BybitCancelOrderPayload; import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload; import org.knowm.xchange.bybit.dto.BybitResult; import org.knowm.xchange.bybit.dto.trade.BybitOrderResponse; @@ -65,4 +66,19 @@ public BybitResult placeLimitOrder( } return placeOrder; } + + public BybitResult cancelOrder(BybitCategory category,String symbol, + String orderId, String orderLinkId) throws IOException { + BybitCancelOrderPayload payload = new BybitCancelOrderPayload(category, symbol, orderId, orderLinkId); + BybitResult cancelOrder = + bybitAuthenticated.cancelOrder( + apiKey, + signatureCreator, + nonceFactory, + payload); + if (!cancelOrder.isSuccess()) { + throw createBybitExceptionFromResult(cancelOrder); + } + return cancelOrder; + } }