Skip to content

Commit

Permalink
Merge pull request knowm#4833 from rizer1980/bybit-fix
Browse files Browse the repository at this point in the history
[bybit] fix futures error "symbol invalid"
  • Loading branch information
timmolter authored and duplessisc committed May 28, 2024
2 parents c1acbfd + 3901134 commit 827db6d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.knowm.xchange.bybit.service.BybitDigest.X_BAPI_SIGN;
import static org.knowm.xchange.bybit.service.BybitDigest.X_BAPI_TIMESTAMP;

import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
Expand All @@ -13,7 +13,7 @@
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.math.BigDecimal;
import org.knowm.xchange.bybit.dto.trade.BybitPlaceOrderPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.account.allcoins.BybitAllCoinsBalance;
import org.knowm.xchange.bybit.dto.account.feerates.BybitFeeRates;
Expand Down Expand Up @@ -84,35 +84,26 @@ BybitResult<BybitOrderDetails<BybitOrderDetail>> getOpenOrders(
*/
@POST
@Path("/order/create")
@Consumes(MediaType.APPLICATION_JSON)
BybitResult<BybitOrderResponse> placeMarketOrder(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
@FormParam("category") String category,
@FormParam("symbol") String symbol,
@FormParam("side") String side,
@FormParam("orderType") String orderType,
@FormParam("qty") BigDecimal qty,
@FormParam("orderLinkId") String orderLinkId)
BybitPlaceOrderPayload payload)
throws IOException, BybitException;

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/v5/order/create-order">API</a>
*/
@POST
@Path("/order/create")
@Consumes(MediaType.APPLICATION_JSON)
BybitResult<BybitOrderResponse> placeLimitOrder(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
@FormParam("category") String category,
@FormParam("symbol") String symbol,
@FormParam("side") String side,
@FormParam("orderType") String orderType,
@FormParam("qty") BigDecimal qty,
@FormParam("price") BigDecimal price,
@FormParam("positionIdx") Integer positionIdx,
@FormParam("orderLinkId") String orderLinkId,
@FormParam("reduceOnly") Boolean reduceOnly)
throws IOException, BybitException;
BybitPlaceOrderPayload payload)
// @FormParam("positionIdx")
// @FormParam("reduceOnly")
throws IOException,BybitException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.knowm.xchange.bybit.dto.trade;

import java.math.BigDecimal;
import lombok.Getter;

@Getter
public class BybitPlaceOrderPayload {

private String category;
private String symbol;
private String side;
private String orderType;
private String qty;
private String orderLinkId;
private String price;

public BybitPlaceOrderPayload(String category, String symbol, String side, String orderType,
BigDecimal qty,
String orderLinkId) {
this.category = category;
this.symbol = symbol;
this.side = side;
this.orderType = orderType;
this.qty = qty.toString();
this.orderLinkId = orderLinkId;
}
public BybitPlaceOrderPayload(String category, String symbol, String side, String orderType,
BigDecimal qty, String orderLinkId, BigDecimal price) {
this.category = category;
this.symbol = symbol;
this.side = side;
this.orderType = orderType;
this.qty = qty.toString();
this.orderLinkId = orderLinkId;
this.price = price.toString();
}
}
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.BybitPlaceOrderPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.trade.BybitOrderResponse;
import org.knowm.xchange.bybit.dto.trade.BybitOrderType;
Expand Down Expand Up @@ -33,45 +34,32 @@ public BybitResult<BybitOrderDetails<BybitOrderDetail>> getBybitOrder(
public BybitResult<BybitOrderResponse> placeMarketOrder(
BybitCategory category, String symbol, BybitSide side, BigDecimal qty, String orderLinkId)
throws IOException {
BybitPlaceOrderPayload payload = new BybitPlaceOrderPayload(category.getValue(),
symbol, side.getValue(), BybitOrderType.MARKET.getValue(), qty, orderLinkId);
BybitResult<BybitOrderResponse> placeOrder =
bybitAuthenticated.placeMarketOrder(
apiKey,
signatureCreator,
nonceFactory,
category.getValue(),
symbol,
side.getValue(),
BybitOrderType.MARKET.getValue(),
qty,
orderLinkId);
payload);
if (!placeOrder.isSuccess()) {
throw createBybitExceptionFromResult(placeOrder);
}
return placeOrder;
}

public BybitResult<BybitOrderResponse> placeLimitOrder(
BybitCategory category,
String symbol,
BybitSide side,
BigDecimal qty,
BigDecimal limitPrice,
BybitCategory category, String symbol, BybitSide side, BigDecimal qty, BigDecimal limitPrice,
String orderLinkId)
throws IOException {
BybitPlaceOrderPayload payload = new BybitPlaceOrderPayload(category.getValue(),
symbol, side.getValue(), BybitOrderType.LIMIT.getValue(), qty, orderLinkId, limitPrice);
BybitResult<BybitOrderResponse> placeOrder =
bybitAuthenticated.placeLimitOrder(
apiKey,
signatureCreator,
nonceFactory,
category.getValue(),
symbol,
side.getValue(),
BybitOrderType.LIMIT.getValue(),
qty,
limitPrice,
0,
orderLinkId,
false);
payload);
if (!placeOrder.isSuccess()) {
throw createBybitExceptionFromResult(placeOrder);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.knowm.xchange.bybit;

import static org.knowm.xchange.bybit.BybitExchange.SPECIFIC_PARAM_ACCOUNT_TYPE;

import java.io.IOException;
import java.math.BigDecimal;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.ExchangeSpecification;
import org.knowm.xchange.bybit.dto.account.walletbalance.BybitAccountType;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.derivative.FuturesContract;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.instrument.Instrument;

public class TradeExample {

public static void main(String[] args) throws InterruptedException {
try {
testTrade();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void testTrade() throws IOException {
ExchangeSpecification exchangeSpecification =
new BybitExchange().getDefaultExchangeSpecification();
exchangeSpecification.setApiKey(System.getProperty("test_api_key"));
exchangeSpecification.setSecretKey(System.getProperty("test_secret_key"));
exchangeSpecification.setExchangeSpecificParametersItem(
SPECIFIC_PARAM_ACCOUNT_TYPE, BybitAccountType.UNIFIED); // or FUND
Exchange exchange = ExchangeFactory.INSTANCE.createExchange(
exchangeSpecification);
Instrument ETH_USDT = new CurrencyPair("ETH/USDT");
Instrument BTC_USDT_PERP = new FuturesContract(new CurrencyPair("BTC/USDT"), "PERP");
Instrument ETH_USDT_PERP = new FuturesContract(new CurrencyPair("ETH/USDT"), "PERP");

System.out.printf("Wallets: %n%s%n",
exchange.getAccountService().getAccountInfo().getWallets());
Ticker ticker = exchange
.getMarketDataService()
.getTicker(ETH_USDT_PERP);
System.out.println(ticker.toString());

System.out.printf("Instrument %s:%n %s", ETH_USDT, exchange.getExchangeMetaData()
.getInstruments().get(ETH_USDT));
System.out.printf("Instrument %s:%n %s", ETH_USDT_PERP, exchange.getExchangeMetaData()
.getInstruments().get(ETH_USDT_PERP));

BigDecimal minAmountSpot = exchange.getExchangeMetaData().getInstruments().get(ETH_USDT)
.getMinimumAmount();
BigDecimal minUSDTSpot = minAmountSpot.multiply(ticker.getLast());
//buy
String marketSpotOrderId =
exchange
.getTradeService()
.placeMarketOrder(
new MarketOrder(OrderType.BID, minUSDTSpot, ETH_USDT));
System.out.println("Market Spot order id: " + marketSpotOrderId);
//sell
marketSpotOrderId =
exchange
.getTradeService()
.placeMarketOrder(
new MarketOrder(OrderType.ASK, minAmountSpot, ETH_USDT));

System.out.println("Market Spot order id: " + marketSpotOrderId);

BigDecimal minAmountFuture = exchange.getExchangeMetaData().getInstruments().get(ETH_USDT_PERP)
.getMinimumAmount();

//long
String marketFutureOrderId =
exchange
.getTradeService()
.placeMarketOrder(
new MarketOrder(OrderType.BID, minAmountFuture, ETH_USDT_PERP));
System.out.println("Market Future order id: " + marketFutureOrderId);

//short
String limitFutureOrderId =
exchange
.getTradeService()
.placeLimitOrder(
new LimitOrder(OrderType.ASK, minAmountFuture, ETH_USDT_PERP, "123213", null,
ticker.getLast()));
System.out.println("Limit Future order id: " + limitFutureOrderId);
}

}
2 changes: 1 addition & 1 deletion xchange-bybit/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

<!-- Define logging for organization applications only -->
<!-- <logger name="org.knowm.xchange" level="DEBUG"/>-->
<!--<logger name="si.mazi.rescu" level="TRACE"/>-->
<!-- <logger name="si.mazi.rescu" level="TRACE"/>-->

</configuration>

0 comments on commit 827db6d

Please sign in to comment.