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 69909b9c65..c1f3efe483 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 @@ -14,6 +14,7 @@ import jakarta.ws.rs.core.MediaType; import java.io.IOException; import org.knowm.xchange.bybit.dto.trade.BybitAmendOrderPayload; +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; @@ -106,6 +107,19 @@ BybitResult placeLimitOrder( BybitPlaceOrderPayload payload) 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; + /** * @apiSpec API */ @@ -118,4 +132,5 @@ BybitResult amendOrder( @HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory timestamp, BybitAmendOrderPayload 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 10fe62986f..f515b9c2c8 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 @@ -11,15 +11,12 @@ import org.knowm.xchange.bybit.BybitAdapters; import org.knowm.xchange.bybit.dto.BybitCategory; import org.knowm.xchange.bybit.dto.BybitResult; -import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTicker; -import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTickers; import org.knowm.xchange.bybit.dto.trade.BybitOrderResponse; import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetail; import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails; import org.knowm.xchange.dto.Order; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.dto.trade.MarketOrder; -import org.knowm.xchange.dto.trade.StopOrder; import org.knowm.xchange.service.trade.TradeService; public class BybitTradeService extends BybitTradeServiceRaw implements TradeService { @@ -95,4 +92,15 @@ public String amendOrder(Order order) throws IOException { return ""; } + + 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 7c6dc2b848..4f691befe3 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.BybitAmendOrderPayload; import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload; import org.knowm.xchange.bybit.dto.BybitResult; @@ -88,4 +89,19 @@ public BybitResult amendOrder(BybitCategory category, String return amendOrder; } + + 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; + } }