Skip to content

Commit

Permalink
Merge pull request #4899 from rizer1980/PR/bybit-cancel-order
Browse files Browse the repository at this point in the history
[bybit] cancel order
  • Loading branch information
timmolter authored Jun 11, 2024
2 parents 13244ef + 69ef48d commit 6352da0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,6 +107,19 @@ BybitResult<BybitOrderResponse> placeLimitOrder(
BybitPlaceOrderPayload payload)
throws IOException,BybitException;

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/v5/order/cancel-order">API</a>
*/
@POST
@Path("/order/cancel")
@Consumes(MediaType.APPLICATION_JSON)
BybitResult<BybitOrderResponse> cancelOrder(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
BybitCancelOrderPayload payload)
throws IOException,BybitException;

/**
* @apiSpec <https://bybit-exchange.github.io/docs/v5/order/amend-order">API</a>
*/
Expand All @@ -118,4 +132,5 @@ BybitResult<BybitOrderResponse> amendOrder(
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
BybitAmendOrderPayload payload)
throws IOException,BybitException;

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<BybitOrderResponse> response = cancelOrder(category,
convertToBybitSymbol(order.getInstrument()), order.getId(), order.getUserReference());
if (response != null) {
return response.getResult().getOrderId();
} else
return "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,4 +89,19 @@ public BybitResult<BybitOrderResponse> amendOrder(BybitCategory category, String
return amendOrder;
}


public BybitResult<BybitOrderResponse> cancelOrder(BybitCategory category,String symbol,
String orderId, String orderLinkId) throws IOException {
BybitCancelOrderPayload payload = new BybitCancelOrderPayload(category, symbol, orderId, orderLinkId);
BybitResult<BybitOrderResponse> cancelOrder =
bybitAuthenticated.cancelOrder(
apiKey,
signatureCreator,
nonceFactory,
payload);
if (!cancelOrder.isSuccess()) {
throw createBybitExceptionFromResult(cancelOrder);
}
return cancelOrder;
}
}

0 comments on commit 6352da0

Please sign in to comment.