Skip to content

Commit

Permalink
[bybit] cancel order
Browse files Browse the repository at this point in the history
  • Loading branch information
rizer1980 committed May 30, 2024
1 parent 8dad542 commit 4e9dc7c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,7 +104,19 @@ BybitResult<BybitOrderResponse> placeLimitOrder(
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
BybitPlaceOrderPayload payload)
// @FormParam("positionIdx")
// @FormParam("reduceOnly")
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;

}
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
@@ -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;
Expand Down Expand Up @@ -72,4 +73,15 @@ public Collection<Order> getOrder(String... orderIds) throws IOException {

return results;
}

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.BybitPlaceOrderPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.trade.BybitOrderResponse;
Expand Down Expand Up @@ -65,4 +66,19 @@ public BybitResult<BybitOrderResponse> placeLimitOrder(
}
return placeOrder;
}

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 4e9dc7c

Please sign in to comment.