Skip to content

Commit

Permalink
Merge pull request #4869 from rizer1980/okx_getTickets
Browse files Browse the repository at this point in the history
[okx] get tickets
  • Loading branch information
timmolter authored May 5, 2024
2 parents 0746f10 + e25d628 commit f9f9052
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
12 changes: 12 additions & 0 deletions xchange-okex/src/main/java/org/knowm/xchange/okex/Okex.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
@Produces(APPLICATION_JSON)
public interface Okex {
String instrumentsPath = "/public/instruments"; // Stated as 20 req/2 sec
String tickerPath = "/market/ticker"; // Stated as 20 req/2 sec
String tickersPath = "/market/tickers"; // Stated as 20 req/2 sec

// To avoid 429s, actual req/second may need to be lowered!
Map<String, List<Integer>> publicPathRateLimits =
new HashMap<String, List<Integer>>() {
{
put(instrumentsPath, Arrays.asList(8, 1));
put(tickerPath, Arrays.asList(8, 1));
put(tickersPath, Arrays.asList(8, 1));
}
};

Expand Down Expand Up @@ -57,6 +61,14 @@ OkexResponse<List<OkexTicker>> getTicker(
@HeaderParam("X-SIMULATED-TRADING") String simulatedTrading)
throws IOException, OkexException;

@GET
@Path("/market/tickers")
@Consumes(MediaType.APPLICATION_JSON)
OkexResponse<List<OkexTicker>> getTickers(
@QueryParam("instType") String instType,
@HeaderParam("X-SIMULATED-TRADING") String simulatedTrading)
throws IOException, OkexException;

@GET
@Path("/market/books")
OkexResponse<List<OkexOrderbook>> getOrderbook(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ public static Ticker adaptTicker(OkexTicker okexTicker) {
.high(okexTicker.getHigh24h())
.low(okexTicker.getLow24h())
// .vwap(null)
.volume(okexTicker.getVolume24h())
.quoteVolume(okexTicker.getVolumeCurrency24h())
.volume((okexTicker.getInstrumentType().equals("SWAP") || okexTicker.getInstrumentType().equals("FUTURES")) ?
okexTicker.getVolumeCurrency24h() : okexTicker.getVolume24h())
.quoteVolume((okexTicker.getInstrumentType().equals("SWAP") || okexTicker.getInstrumentType().equals("FUTURES")) ?
okexTicker.getVolumeCurrency24h().multiply(okexTicker.getLast()) : okexTicker.getVolumeCurrency24h())
.timestamp(okexTicker.getTimestamp())
.bidSize(okexTicker.getBidSize())
.askSize(okexTicker.getAskSize())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.knowm.xchange.okex.dto;

public enum OkexInstType {
SPOT,
MARGIN,
SWAP,
FUTURES,
OPTION,
ANY
import org.knowm.xchange.service.marketdata.params.Params;

public enum OkexInstType implements Params {

SPOT, MARGIN, SWAP, FUTURES, OPTION, ANY
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.knowm.xchange.client.ResilienceRegistries;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.*;
import org.knowm.xchange.dto.marketdata.CandleStickData;
import org.knowm.xchange.dto.marketdata.FundingRate;
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.exceptions.NotYetImplementedForExchangeException;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.okex.OkexAdapters;
import org.knowm.xchange.okex.OkexExchange;
import org.knowm.xchange.okex.dto.OkexInstType;
import org.knowm.xchange.okex.dto.OkexResponse;
import org.knowm.xchange.okex.dto.marketdata.OkexCandleStick;
import org.knowm.xchange.service.marketdata.MarketDataService;
import org.knowm.xchange.service.marketdata.params.Params;
import org.knowm.xchange.service.trade.params.CandleStickDataParams;
import org.knowm.xchange.service.trade.params.DefaultCandleStickParam;
import org.knowm.xchange.service.trade.params.DefaultCandleStickParamWithLimit;
Expand Down Expand Up @@ -78,4 +85,13 @@ public FundingRate getFundingRate(Instrument instrument) throws IOException {
return OkexAdapters.adaptFundingRate(
getOkexFundingRate(OkexAdapters.adaptInstrument(instrument)).getData());
}


public List<Ticker> getTickers(Params params) throws IOException {
if (!(params instanceof OkexInstType)) {
throw new IllegalArgumentException("Params must be instance of OkexInstType");
}
OkexInstType instType = (OkexInstType) params;
return getOkexTickers(instType).getData().stream().map(OkexAdapters::adaptTicker).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
import org.knowm.xchange.okex.OkexAuthenticated;
import org.knowm.xchange.okex.OkexExchange;
import org.knowm.xchange.okex.dto.OkexException;
import org.knowm.xchange.okex.dto.OkexInstType;
import org.knowm.xchange.okex.dto.OkexResponse;
import org.knowm.xchange.okex.dto.marketdata.*;
import org.knowm.xchange.okex.dto.marketdata.OkexCandleStick;
import org.knowm.xchange.okex.dto.marketdata.OkexCurrency;
import org.knowm.xchange.okex.dto.marketdata.OkexFundingRate;
import org.knowm.xchange.okex.dto.marketdata.OkexInstrument;
import org.knowm.xchange.okex.dto.marketdata.OkexOrderbook;
import org.knowm.xchange.okex.dto.marketdata.OkexTicker;
import org.knowm.xchange.okex.dto.marketdata.OkexTrade;
import org.knowm.xchange.utils.DateUtils;

/** Author: Max Gao ([email protected]) Created: 08-06-2021 */
Expand Down Expand Up @@ -48,14 +55,32 @@ public OkexResponse<List<OkexTicker>> getOkexTicker(String instrumentId)
try {
return decorateApiCall(
() ->
okex.getTicker(
instrumentId,
(String)
exchange
.getExchangeSpecification()
.getExchangeSpecificParametersItem(PARAM_SIMULATED)))
.withRateLimiter(rateLimiter(Okex.instrumentsPath))
.call();
okex.getTicker(
instrumentId,
(String)
exchange
.getExchangeSpecification()
.getExchangeSpecificParametersItem(PARAM_SIMULATED)))
.withRateLimiter(rateLimiter(Okex.tickerPath))
.call();
} catch (OkexException e) {
throw handleError(e);
}
}

public OkexResponse<List<OkexTicker>> getOkexTickers(OkexInstType instType)
throws OkexException, IOException {
try {
return decorateApiCall(
() ->
okex.getTickers(
instType.toString(),
(String)
exchange
.getExchangeSpecification()
.getExchangeSpecificParametersItem(PARAM_SIMULATED)))
.withRateLimiter(rateLimiter(Okex.tickersPath))
.call();
} catch (OkexException e) {
throw handleError(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.knowm.xchange.okex;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -20,6 +21,7 @@
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.okex.dto.OkexInstType;
import org.knowm.xchange.okex.dto.OkexResponse;
import org.knowm.xchange.okex.dto.marketdata.OkexCandleStick;
import org.knowm.xchange.okex.service.OkexMarketDataService;
Expand Down Expand Up @@ -72,6 +74,15 @@ public void checkTicker() throws IOException {
assertThat(swapTicker.getInstrument()).isEqualTo(instrument);
}

@Test
public void checkTickers() throws IOException {
List<Ticker> spotTickers = exchange.getMarketDataService().getTickers(OkexInstType.SPOT);
List<Ticker> swapTickers = exchange.getMarketDataService().getTickers(OkexInstType.SWAP);

assertTrue(spotTickers.stream().anyMatch(f->f.getInstrument().equals(new CurrencyPair("BTC/USDT"))));
assertTrue(swapTickers.stream().anyMatch(f -> f.getInstrument().equals(new FuturesContract("BTC/USDT/SWAP"))));
}

@Test
public void checkTrades() throws IOException {
Trades spotTrades = exchange.getMarketDataService().getTrades(currencyPair);
Expand All @@ -89,7 +100,7 @@ public void testCandleHist() throws IOException {
OkexResponse<List<OkexCandleStick>> barHistDtos =
((OkexMarketDataService) exchange.getMarketDataService())
.getHistoryCandle("BTC-USDT", null, null, null, null);
Assert.assertTrue(Objects.nonNull(barHistDtos) && !barHistDtos.getData().isEmpty());
assertTrue(Objects.nonNull(barHistDtos) && !barHistDtos.getData().isEmpty());
}

@Test
Expand Down

0 comments on commit f9f9052

Please sign in to comment.