Skip to content

Commit e9438b0

Browse files
authored
Merge pull request #4998 from rizer1980/PR/bybit-work
[BYBIT]
2 parents 1f1edcc + fa5bc8c commit e9438b0

36 files changed

+944
-310
lines changed

xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAdapters.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import static org.knowm.xchange.bybit.dto.BybitCategory.INVERSE;
44
import static org.knowm.xchange.bybit.dto.BybitCategory.OPTION;
55
import static org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType.CALL;
6-
import static org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType.PUT;
76

87
import java.math.BigDecimal;
9-
import java.text.DateFormat;
108
import java.text.ParseException;
119
import java.text.SimpleDateFormat;
1210
import java.time.LocalDate;
1311
import java.time.format.DateTimeFormatter;
1412
import java.time.format.DateTimeFormatterBuilder;
15-
import java.time.temporal.TemporalAccessor;
1613
import java.util.ArrayList;
1714
import java.util.Arrays;
1815
import java.util.Date;
@@ -26,7 +23,6 @@
2623
import org.knowm.xchange.bybit.dto.marketdata.instruments.BybitInstrumentInfo;
2724
import org.knowm.xchange.bybit.dto.marketdata.instruments.linear.BybitLinearInverseInstrumentInfo;
2825
import org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo;
29-
import org.knowm.xchange.bybit.dto.marketdata.instruments.option.BybitOptionInstrumentInfo.OptionType;
3026
import org.knowm.xchange.bybit.dto.marketdata.instruments.spot.BybitSpotInstrumentInfo;
3127
import org.knowm.xchange.bybit.dto.marketdata.tickers.BybitTicker;
3228
import org.knowm.xchange.bybit.dto.marketdata.tickers.linear.BybitLinearInverseTicker;
@@ -65,11 +61,12 @@ public class BybitAdapters {
6561
public static Wallet adaptBybitBalances(List<BybitCoinWalletBalance> coinWalletBalances) {
6662
List<Balance> balances = new ArrayList<>(coinWalletBalances.size());
6763
for (BybitCoinWalletBalance bybitCoinBalance : coinWalletBalances) {
64+
BigDecimal availableToWithdraw = bybitCoinBalance.getAvailableToWithdraw().isEmpty() ? BigDecimal.ZERO : new BigDecimal(bybitCoinBalance.getAvailableToWithdraw());
6865
balances.add(
6966
new Balance(
7067
new Currency(bybitCoinBalance.getCoin()),
7168
new BigDecimal(bybitCoinBalance.getEquity()),
72-
new BigDecimal(bybitCoinBalance.getAvailableToWithdraw())));
69+
availableToWithdraw));
7370
}
7471
return Wallet.Builder.from(balances).build();
7572
}
@@ -242,19 +239,19 @@ public static InstrumentMetaData symbolToCurrencyPairMetaData(
242239
.build();
243240
}
244241

245-
public static Order adaptBybitOrderDetails(BybitOrderDetail bybitOrderResult) {
242+
public static Order adaptBybitOrderDetails(BybitOrderDetail bybitOrderResult, BybitCategory category) {
246243
Order.Builder builder;
247244

248245
switch (bybitOrderResult.getOrderType()) {
249246
case MARKET:
250247
builder =
251248
new MarketOrder.Builder(
252-
adaptOrderType(bybitOrderResult), guessSymbol(bybitOrderResult.getSymbol()));
249+
adaptOrderType(bybitOrderResult), convertBybitSymbolToInstrument(bybitOrderResult.getSymbol(),category));
253250
break;
254251
case LIMIT:
255252
builder =
256253
new LimitOrder.Builder(
257-
adaptOrderType(bybitOrderResult), guessSymbol(bybitOrderResult.getSymbol()))
254+
adaptOrderType(bybitOrderResult), convertBybitSymbolToInstrument(bybitOrderResult.getSymbol(),category))
258255
.limitPrice(bybitOrderResult.getPrice());
259256
break;
260257
default:

xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitAuthenticated.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.ws.rs.QueryParam;
1414
import jakarta.ws.rs.core.MediaType;
1515
import java.io.IOException;
16+
import javax.annotation.Nonnull;
1617
import org.knowm.xchange.bybit.dto.BybitResult;
1718
import org.knowm.xchange.bybit.dto.account.BybitAccountInfoResponse;
1819
import org.knowm.xchange.bybit.dto.account.BybitCancelAllOrdersPayload;
@@ -65,25 +66,26 @@ BybitResult<BybitAllCoinsBalance> getAllCoinsBalance(
6566
*/
6667
@GET
6768
@Path("/account/fee-rate")
68-
BybitResult<BybitFeeRates> getFeeRates(
69+
BybitResult<BybitFeeRates> getFeeRate(
6970
@HeaderParam(X_BAPI_API_KEY) String apiKey,
7071
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
7172
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
7273
@QueryParam("category") String category,
73-
@QueryParam("symbol") String symbol)
74+
@Nonnull @QueryParam("symbol") String symbol)
7475
throws IOException, BybitException;
7576

7677
/**
7778
* @apiSpec <a href="https://bybit-exchange.github.io/docs/v5/order/open-order">API</a>
7879
*/
7980
@GET
8081
@Path("/order/realtime")
81-
BybitResult<BybitOrderDetails<BybitOrderDetail>> getOpenOrders(
82+
BybitResult<BybitOrderDetails<BybitOrderDetail>> getOrders(
8283
@HeaderParam(X_BAPI_API_KEY) String apiKey,
8384
@HeaderParam(X_BAPI_SIGN) ParamsDigest signature,
8485
@HeaderParam(X_BAPI_TIMESTAMP) SynchronizedValueFactory<Long> timestamp,
8586
@QueryParam("category") String category,
86-
@QueryParam("orderId") String orderId)
87+
@Nonnull @QueryParam("symbol") String symbol,
88+
@Nonnull @QueryParam("orderId") String orderId)
8789
throws IOException, BybitException;
8890

8991
/**

xchange-bybit/src/main/java/org/knowm/xchange/bybit/BybitExchange.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.knowm.xchange.bybit;
22

33
import java.io.IOException;
4+
import lombok.Getter;
45
import org.knowm.xchange.BaseExchange;
56
import org.knowm.xchange.Exchange;
67
import org.knowm.xchange.ExchangeSpecification;
@@ -15,6 +16,7 @@
1516
import org.knowm.xchange.bybit.service.BybitTradeService;
1617
import org.knowm.xchange.client.ResilienceRegistries;
1718
import org.knowm.xchange.exceptions.ExchangeException;
19+
import si.mazi.rescu.SynchronizedValueFactory;
1820

1921
public class BybitExchange extends BaseExchange implements Exchange{
2022

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

2931
private static ResilienceRegistries RESILIENCE_REGISTRIES;
3032

33+
@Getter
34+
protected SynchronizedValueFactory<Long> timeStampFactory = new BybitTimeStampFactory();
35+
3136
@Override
3237
protected void initServices() {
3338
marketDataService = new BybitMarketDataService(this,getResilienceRegistries());
@@ -135,4 +140,11 @@ public ResilienceRegistries getResilienceRegistries() {
135140
}
136141
return RESILIENCE_REGISTRIES;
137142
}
143+
144+
@Override
145+
public SynchronizedValueFactory<Long> getNonceFactory() {
146+
throw new UnsupportedOperationException(
147+
"Bybit uses timestamp/recv-window rather than a nonce");
148+
}
149+
138150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.knowm.xchange.bybit;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import si.mazi.rescu.SynchronizedValueFactory;
6+
7+
public class BybitTimeStampFactory implements SynchronizedValueFactory<Long> {
8+
private static final Logger LOG = LoggerFactory.getLogger(BybitTimeStampFactory.class);
9+
10+
@Override
11+
public Long createValue() {
12+
return System.currentTimeMillis();
13+
}
14+
}

xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/BybitAmendOrderPayload.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.knowm.xchange.bybit.dto.BybitCategory;
77

88
@Getter
9-
@JsonInclude(Include.NON_NULL)
9+
@JsonInclude(Include.NON_EMPTY)
1010
public class BybitAmendOrderPayload {
1111

1212
BybitCategory category;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.knowm.xchange.bybit.dto.trade;
2+
3+
import lombok.Getter;
4+
import org.knowm.xchange.bybit.dto.BybitCategory;
5+
import org.knowm.xchange.instrument.Instrument;
6+
import org.knowm.xchange.service.trade.params.CancelAllOrders;
7+
8+
@Getter
9+
public class BybitCancelAllOrdersParams implements CancelAllOrders {
10+
11+
private final BybitCategory category;
12+
private final Instrument symbol;
13+
private String baseCoin;
14+
private String settleCoin;
15+
private String orderFilter;
16+
private String stopOrderType;
17+
18+
public BybitCancelAllOrdersParams(BybitCategory category, Instrument symbol) {
19+
this.category = category;
20+
this.symbol = symbol;
21+
}
22+
23+
public BybitCancelAllOrdersParams(BybitCategory category, Instrument symbol, String baseCoin,
24+
String settleCoin, String orderFilter, String stopOrderType) {
25+
this.category = category;
26+
this.symbol = symbol;
27+
this.baseCoin = baseCoin;
28+
this.settleCoin = settleCoin;
29+
this.orderFilter = orderFilter;
30+
this.stopOrderType = stopOrderType;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "BybitCancelAllOrdersParams{" +
36+
"category=" + category +
37+
", symbol=" + symbol +
38+
", baseCoin='" + baseCoin + '\'' +
39+
", settleCoin='" + settleCoin + '\'' +
40+
", orderFilter='" + orderFilter + '\'' +
41+
", stopOrderType='" + stopOrderType + '\'' +
42+
'}';
43+
}
44+
}
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.knowm.xchange.bybit.dto.trade;
2+
3+
import lombok.Getter;
4+
import org.knowm.xchange.instrument.Instrument;
5+
import org.knowm.xchange.service.trade.params.CancelOrderByUserReferenceParams;
6+
import org.knowm.xchange.service.trade.params.DefaultCancelOrderByInstrumentAndIdParams;
7+
8+
@Getter
9+
public class BybitCancelOrderParams extends DefaultCancelOrderByInstrumentAndIdParams
10+
implements CancelOrderByUserReferenceParams {
11+
12+
private final String userReference;
13+
14+
public BybitCancelOrderParams(Instrument instrument, String orderId, String userReference) {
15+
super(instrument, orderId);
16+
this.userReference = userReference;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "BybitCancelOrderParams{" +
22+
"instrument='" + getInstrument() + '\'' +
23+
", orderId='" + getOrderId() + '\'' +
24+
", userReference=" + getUserReference() +
25+
'}';
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.knowm.xchange.bybit.dto.trade;
2+
3+
import lombok.Getter;
4+
import org.knowm.xchange.bybit.dto.BybitCategory;
5+
import org.knowm.xchange.instrument.Instrument;
6+
import org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument;
7+
8+
@Getter
9+
public class BybitOpenOrdersParam extends DefaultOpenOrdersParamInstrument {
10+
private final BybitCategory category;
11+
12+
public BybitOpenOrdersParam(Instrument instrument, BybitCategory category) {
13+
super(instrument);
14+
this.category = category;
15+
}
16+
@Override
17+
public String toString() {
18+
return "BybitOrderQueryParams{" +
19+
"category='" + category + '\'' +
20+
", instrument='" + getInstrument() + '\'' +
21+
'}';
22+
}
23+
}

xchange-bybit/src/main/java/org/knowm/xchange/bybit/dto/trade/details/BybitOrderDetails.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
import com.fasterxml.jackson.annotation.JsonSubTypes;
55
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
66
import com.fasterxml.jackson.annotation.JsonTypeInfo;
7+
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
78
import lombok.Data;
89
import lombok.Value;
910
import lombok.extern.jackson.Jacksonized;
1011
import org.knowm.xchange.bybit.dto.BybitCategorizedPayload;
12+
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitInverseOrderDetails;
1113
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitLinearOrderDetails;
1214
import org.knowm.xchange.bybit.dto.trade.details.BybitOrderDetails.BybitSpotOrderDetails;
15+
import org.knowm.xchange.bybit.dto.trade.details.inverse.BybitInverseOrderDetail;
1316
import org.knowm.xchange.bybit.dto.trade.details.linear.BybitLinearOrderDetail;
1417
import org.knowm.xchange.bybit.dto.trade.details.spot.BybitSpotOrderDetail;
1518

1619
@Data
17-
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "category", visible = true)
20+
@JsonTypeInfo(use = Id.NAME, property = "category", visible = true,defaultImpl = BybitOrderDetails.class)
1821
@JsonSubTypes({
1922
@Type(value = BybitLinearOrderDetails.class, name = "linear"),
20-
@Type(value = BybitOrderDetails.class, name = "inverse"),
23+
@Type(value = BybitInverseOrderDetails.class, name = "inverse"),
2124
@Type(value = BybitOrderDetails.class, name = "option"),
2225
@Type(value = BybitSpotOrderDetails.class, name = "spot"),
2326
})
27+
2428
public class BybitOrderDetails<T extends BybitOrderDetail> extends BybitCategorizedPayload<T> {
2529

2630
@JsonProperty("nextPageCursor")
@@ -33,4 +37,8 @@ public static class BybitLinearOrderDetails extends BybitOrderDetails<BybitLinea
3337
@Jacksonized
3438
@Value
3539
public static class BybitSpotOrderDetails extends BybitOrderDetails<BybitSpotOrderDetail> {}
40+
41+
@Jacksonized
42+
@Value
43+
public static class BybitInverseOrderDetails extends BybitOrderDetails<BybitInverseOrderDetail> {}
3644
}

0 commit comments

Comments
 (0)