Skip to content

Commit

Permalink
Merge pull request #4998 from rizer1980/PR/bybit-work
Browse files Browse the repository at this point in the history
[BYBIT]
  • Loading branch information
timmolter authored Feb 5, 2025
2 parents 1f1edcc + fa5bc8c commit e9438b0
Show file tree
Hide file tree
Showing 36 changed files with 944 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import static org.knowm.xchange.bybit.dto.BybitCategory.INVERSE;
import static org.knowm.xchange.bybit.dto.BybitCategory.OPTION;
import static org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType.CALL;
import static org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType.PUT;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -26,7 +23,6 @@
import org.knowm.xchange.bybit.dto.marketdata.instruments.BybitInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.instruments.linear.BybitLinearInverseInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType;
import org.knowm.xchange.bybit.dto.marketdata.instruments.spot.BybitSpotInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTicker;
import org.knowm.xchange.bybit.dto.marketdata.tickers.linear.BybitLinearInverseTicker;
Expand Down Expand Up @@ -65,11 +61,12 @@ public class BybitAdapters {
public static Wallet adaptBybitBalances(List<BybitCoinWalletBalance> coinWalletBalances) {
List<Balance> balances = new ArrayList<>(coinWalletBalances.size());
for (BybitCoinWalletBalance bybitCoinBalance : coinWalletBalances) {
BigDecimal availableToWithdraw = bybitCoinBalance.getAvailableToWithdraw().isEmpty() ? BigDecimal.ZERO : new BigDecimal(bybitCoinBalance.getAvailableToWithdraw());
balances.add(
new Balance(
new Currency(bybitCoinBalance.getCoin()),
new BigDecimal(bybitCoinBalance.getEquity()),
new BigDecimal(bybitCoinBalance.getAvailableToWithdraw())));
availableToWithdraw));
}
return Wallet.Builder.from(balances).build();
}
Expand Down Expand Up @@ -242,19 +239,19 @@ public static InstrumentMetaData symbolToCurrencyPairMetaData(
.build();
}

public static Order adaptBybitOrderDetails(BybitOrderDetail bybitOrderResult) {
public static Order adaptBybitOrderDetails(BybitOrderDetail bybitOrderResult, BybitCategory category) {
Order.Builder builder;

switch (bybitOrderResult.getOrderType()) {
case MARKET:
builder =
new MarketOrder.Builder(
adaptOrderType(bybitOrderResult), guessSymbol(bybitOrderResult.getSymbol()));
adaptOrderType(bybitOrderResult), convertBybitSymbolToInstrument(bybitOrderResult.getSymbol(),category));
break;
case LIMIT:
builder =
new LimitOrder.Builder(
adaptOrderType(bybitOrderResult), guessSymbol(bybitOrderResult.getSymbol()))
adaptOrderType(bybitOrderResult), convertBybitSymbolToInstrument(bybitOrderResult.getSymbol(),category))
.limitPrice(bybitOrderResult.getPrice());
break;
default:
Expand Down
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 javax.annotation.Nonnull;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.account.BybitAccountInfoResponse;
import org.knowm.xchange.bybit.dto.account.BybitCancelAllOrdersPayload;
Expand Down Expand Up @@ -65,25 +66,26 @@ BybitResult<BybitAllCoinsBalance> getAllCoinsBalance(
*/
@GET
@Path("/account/fee-rate")
BybitResult<BybitFeeRates> getFeeRates(
BybitResult<BybitFeeRates> getFeeRate(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
@QueryParam("category") String category,
@QueryParam("symbol") String symbol)
@Nonnull @QueryParam("symbol") String symbol)
throws IOException, BybitException;

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/v5/order/open-order">API</a>
*/
@GET
@Path("/order/realtime")
BybitResult<BybitOrderDetails<BybitOrderDetail>> getOpenOrders(
BybitResult<BybitOrderDetails<BybitOrderDetail>> getOrders(
@HeaderParam(X_BAPI_API_KEY) String apiKey,
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
@QueryParam("category") String category,
@QueryParam("orderId") String orderId)
@Nonnull @QueryParam("symbol") String symbol,
@Nonnull @QueryParam("orderId") String orderId)
throws IOException, BybitException;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.knowm.xchange.bybit;

import java.io.IOException;
import lombok.Getter;
import org.knowm.xchange.BaseExchange;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeSpecification;
Expand All @@ -15,6 +16,7 @@
import org.knowm.xchange.bybit.service.BybitTradeService;
import org.knowm.xchange.client.ResilienceRegistries;
import org.knowm.xchange.exceptions.ExchangeException;
import si.mazi.rescu.SynchronizedValueFactory;

public class BybitExchange extends BaseExchange implements Exchange{

Expand All @@ -28,6 +30,9 @@ public class BybitExchange extends BaseExchange implements Exchange{

private static ResilienceRegistries RESILIENCE_REGISTRIES;

@Getter
protected SynchronizedValueFactory<Long> timeStampFactory = new BybitTimeStampFactory();

@Override
protected void initServices() {
marketDataService = new BybitMarketDataService(this,getResilienceRegistries());
Expand Down Expand Up @@ -135,4 +140,11 @@ public ResilienceRegistries getResilienceRegistries() {
}
return RESILIENCE_REGISTRIES;
}

@Override
public SynchronizedValueFactory<Long> getNonceFactory() {
throw new UnsupportedOperationException(
"Bybit uses timestamp/recv-window rather than a nonce");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.knowm.xchange.bybit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import si.mazi.rescu.SynchronizedValueFactory;

public class BybitTimeStampFactory implements SynchronizedValueFactory<Long> {
private static final Logger LOG = LoggerFactory.getLogger(BybitTimeStampFactory.class);

@Override
public Long createValue() {
return System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.knowm.xchange.bybit.dto.BybitCategory;

@Getter
@JsonInclude(Include.NON_NULL)
@JsonInclude(Include.NON_EMPTY)
public class BybitAmendOrderPayload {

BybitCategory category;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.knowm.xchange.bybit.dto.trade;

import lombok.Getter;
import org.knowm.xchange.bybit.dto.BybitCategory;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.trade.params.CancelAllOrders;

@Getter
public class BybitCancelAllOrdersParams implements CancelAllOrders {

private final BybitCategory category;
private final Instrument symbol;
private String baseCoin;
private String settleCoin;
private String orderFilter;
private String stopOrderType;

public BybitCancelAllOrdersParams(BybitCategory category, Instrument symbol) {
this.category = category;
this.symbol = symbol;
}

public BybitCancelAllOrdersParams(BybitCategory category, Instrument symbol, String baseCoin,
String settleCoin, String orderFilter, String stopOrderType) {
this.category = category;
this.symbol = symbol;
this.baseCoin = baseCoin;
this.settleCoin = settleCoin;
this.orderFilter = orderFilter;
this.stopOrderType = stopOrderType;
}

@Override
public String toString() {
return "BybitCancelAllOrdersParams{" +
"category=" + category +
", symbol=" + symbol +
", baseCoin='" + baseCoin + '\'' +
", settleCoin='" + settleCoin + '\'' +
", orderFilter='" + orderFilter + '\'' +
", stopOrderType='" + stopOrderType + '\'' +
'}';
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.knowm.xchange.bybit.dto.trade;

import lombok.Getter;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.trade.params.CancelOrderByUserReferenceParams;
import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams;

@Getter
public class BybitCancelOrderParams extends DefaultCancelOrderByInstrumentAndIdParams
implements CancelOrderByUserReferenceParams {

private final String userReference;

public BybitCancelOrderParams(Instrument instrument, String orderId, String userReference) {
super(instrument, orderId);
this.userReference = userReference;
}

@Override
public String toString() {
return "BybitCancelOrderParams{" +
"instrument='" + getInstrument() + '\'' +
", orderId='" + getOrderId() + '\'' +
", userReference=" + getUserReference() +
'}';
}
}
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.dto.BybitCategory;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument;

@Getter
public class BybitOpenOrdersParam extends DefaultOpenOrdersParamInstrument {
private final BybitCategory category;

public BybitOpenOrdersParam(Instrument instrument, BybitCategory category) {
super(instrument);
this.category = category;
}
@Override
public String toString() {
return "BybitOrderQueryParams{" +
"category='" + category + '\'' +
", instrument='" + getInstrument() + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import lombok.Data;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
import org.knowm.xchange.bybit.dto.BybitCategorizedPayload;
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitInverseOrderDetails;
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitLinearOrderDetails;
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitSpotOrderDetails;
import org.knowm.xchange.bybit.dto.trade.details.inverse.BybitInverseOrderDetail;
import org.knowm.xchange.bybit.dto.trade.details.linear.BybitLinearOrderDetail;
import org.knowm.xchange.bybit.dto.trade.details.spot.BybitSpotOrderDetail;

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "category", visible = true)
@JsonTypeInfo(use = Id.NAME, property = "category", visible = true,defaultImpl = BybitOrderDetails.class)
@JsonSubTypes({
@Type(value = BybitLinearOrderDetails.class, name = "linear"),
@Type(value = BybitOrderDetails.class, name = "inverse"),
@Type(value = BybitInverseOrderDetails.class, name = "inverse"),
@Type(value = BybitOrderDetails.class, name = "option"),
@Type(value = BybitSpotOrderDetails.class, name = "spot"),
})

public class BybitOrderDetails<T extends BybitOrderDetail> extends BybitCategorizedPayload<T> {

@JsonProperty("nextPageCursor")
Expand All @@ -33,4 +37,8 @@ public static class BybitLinearOrderDetails extends BybitOrderDetails<BybitLinea
@Jacksonized
@Value
public static class BybitSpotOrderDetails extends BybitOrderDetails<BybitSpotOrderDetail> {}

@Jacksonized
@Value
public static class BybitInverseOrderDetails extends BybitOrderDetails<BybitInverseOrderDetail> {}
}
Loading

0 comments on commit e9438b0

Please sign in to comment.