Skip to content

Commit

Permalink
Merge pull request #4982 from bigscoop/kucoin-orderbook
Browse files Browse the repository at this point in the history
[kucoin] Implement orderbook for Instrument
  • Loading branch information
timmolter authored Dec 14, 2024
2 parents de9ac37 + 7855c04 commit 49f4f01
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public class KucoinAdapters {

private static final String TAKER_FEE_RATE = "takerFeeRate";

public static String adaptCurrencyPair(CurrencyPair pair) {
return pair == null ? null : pair.base.getCurrencyCode() + "-" + pair.counter.getCurrencyCode();
public static String adaptCurrencyPair(Instrument instrument) {
return instrument == null ? null : instrument.getBase().getCurrencyCode() + "-" + instrument.getCounter().getCurrencyCode();
}

public static CurrencyPair adaptCurrencyPair(String symbol) {
Expand Down Expand Up @@ -223,26 +223,26 @@ private static WalletHealth getWalletHealth(CurrenciesResponse currenciesRespons
return walletHealth;
}

public static OrderBook adaptOrderBook(CurrencyPair currencyPair, OrderBookResponse kc) {
public static OrderBook adaptOrderBook(Instrument instrument, OrderBookResponse kc) {
Date timestamp = new Date(kc.getTime());
List<LimitOrder> asks =
kc.getAsks().stream()
.map(PriceAndSize::new)
.sorted(Ordering.natural().onResultOf(s -> s.price))
.map(s -> adaptLimitOrder(currencyPair, ASK, s, timestamp))
.map(s -> adaptLimitOrder(instrument, ASK, s, timestamp))
.collect(toCollection(LinkedList::new));
List<LimitOrder> bids =
kc.getBids().stream()
.map(PriceAndSize::new)
.sorted(Ordering.natural().onResultOf((PriceAndSize s) -> s.price).reversed())
.map(s -> adaptLimitOrder(currencyPair, BID, s, timestamp))
.map(s -> adaptLimitOrder(instrument, BID, s, timestamp))
.collect(toCollection(LinkedList::new));
return new OrderBook(timestamp, asks, bids, true);
}

private static LimitOrder adaptLimitOrder(
CurrencyPair currencyPair, OrderType orderType, PriceAndSize priceAndSize, Date timestamp) {
return new LimitOrder.Builder(orderType, currencyPair)
Instrument instrument, OrderType orderType, PriceAndSize priceAndSize, Date timestamp) {
return new LimitOrder.Builder(orderType, instrument)
.limitPrice(priceAndSize.price)
.originalAmount(priceAndSize.size)
.orderStatus(NEW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.service.marketdata.MarketDataService;
import org.knowm.xchange.service.marketdata.params.Params;

Expand Down Expand Up @@ -43,14 +44,19 @@ public List<Ticker> getTickers(Params params) throws IOException {

@Override
public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws IOException {
return getOrderBook((Instrument) currencyPair, args);
}

@Override
public OrderBook getOrderBook(Instrument instrument, Object... args) throws IOException {
if (Arrays.asList(args).contains(PARAM_FULL_ORDERBOOK)) {
return KucoinAdapters.adaptOrderBook(currencyPair, getKucoinOrderBookFull(currencyPair));
return KucoinAdapters.adaptOrderBook(instrument, getKucoinOrderBookFull(instrument));
} else {
if (Arrays.asList(args).contains(PARAM_PARTIAL_SHALLOW_ORDERBOOK)) {
return KucoinAdapters.adaptOrderBook(
currencyPair, getKucoinOrderBookPartialShallow(currencyPair));
instrument, getKucoinOrderBookPartialShallow(instrument));
}
return KucoinAdapters.adaptOrderBook(currencyPair, getKucoinOrderBookPartial(currencyPair));
return KucoinAdapters.adaptOrderBook(instrument, getKucoinOrderBookPartial(instrument));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.knowm.xchange.client.ResilienceRegistries;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.kucoin.dto.KlineIntervalType;
import org.knowm.xchange.kucoin.dto.response.AllTickersResponse;
import org.knowm.xchange.kucoin.dto.response.CurrenciesResponse;
Expand Down Expand Up @@ -131,37 +132,37 @@ public CurrencyResponseV2 getKucoinCurrencies(Currency currency) throws IOExcept
.call());
}

public OrderBookResponse getKucoinOrderBookPartial(CurrencyPair pair) throws IOException {
public OrderBookResponse getKucoinOrderBookPartial(Instrument instrument) throws IOException {
return classifyingExceptions(
() ->
decorateApiCall(
() ->
orderBookApi.getPartOrderBookAggregated(
KucoinAdapters.adaptCurrencyPair(pair)))
KucoinAdapters.adaptCurrencyPair(instrument)))
.withRetry(retry("partialOrderBook"))
.withRateLimiter(rateLimiter(PUBLIC_REST_ENDPOINT_RATE_LIMITER))
.call());
}

public OrderBookResponse getKucoinOrderBookPartialShallow(CurrencyPair pair) throws IOException {
public OrderBookResponse getKucoinOrderBookPartialShallow(Instrument instrument) throws IOException {
return classifyingExceptions(
() ->
decorateApiCall(
() ->
orderBookApi.getPartOrderBookShallowAggregated(
KucoinAdapters.adaptCurrencyPair(pair)))
KucoinAdapters.adaptCurrencyPair(instrument)))
.withRetry(retry("partialShallowOrderBook"))
.withRateLimiter(rateLimiter(PUBLIC_REST_ENDPOINT_RATE_LIMITER))
.call());
}

public OrderBookResponse getKucoinOrderBookFull(CurrencyPair pair) throws IOException {
public OrderBookResponse getKucoinOrderBookFull(Instrument instrument) throws IOException {
return classifyingExceptions(
() ->
decorateApiCall(
() ->
orderBookApi.getFullOrderBookAggregated(
KucoinAdapters.adaptCurrencyPair(pair),
KucoinAdapters.adaptCurrencyPair(instrument),
apiKey,
digest,
nonceFactory,
Expand Down

0 comments on commit 49f4f01

Please sign in to comment.