From 0a675702897f245ca08846b58055cdb21bb34e5e Mon Sep 17 00:00:00 2001 From: Aaron Jang Date: Sun, 11 Feb 2024 21:04:18 +0900 Subject: [PATCH 1/3] [upbit] added market orders --- .../upbit/trade/UpbitLimitOrderDemo.java | 36 ---------- .../examples/upbit/trade/UpbitTradeDemo.java | 66 +++++++++++++++++++ .../xchange/upbit/UpbitAuthenticated.java | 2 +- .../upbit/dto/trade/UpbitOrderRequest.java | 2 + .../upbit/service/UpbitTradeService.java | 6 ++ .../upbit/service/UpbitTradeServiceRaw.java | 28 +++++++- 6 files changed, 100 insertions(+), 40 deletions(-) delete mode 100644 xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitLimitOrderDemo.java create mode 100644 xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitTradeDemo.java diff --git a/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitLimitOrderDemo.java b/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitLimitOrderDemo.java deleted file mode 100644 index ba72db42093..00000000000 --- a/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitLimitOrderDemo.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.knowm.xchange.examples.upbit.trade; - -import java.io.IOException; -import java.math.BigDecimal; -import java.util.Collection; -import org.knowm.xchange.Exchange; -import org.knowm.xchange.currency.Currency; -import org.knowm.xchange.currency.CurrencyPair; -import org.knowm.xchange.dto.Order; -import org.knowm.xchange.dto.Order.OrderType; -import org.knowm.xchange.dto.trade.LimitOrder; -import org.knowm.xchange.examples.upbit.UpbitDemoUtils; -import org.knowm.xchange.service.trade.TradeService; - -/** Demonstrate requesting limit order at Upbit */ -public class UpbitLimitOrderDemo { - - public static void main(String[] args) throws Exception { - Exchange upbit = UpbitDemoUtils.createExchange(); - generic(upbit.getTradeService()); - } - - private static void generic(TradeService tradeService) throws IOException, InterruptedException { - - LimitOrder limitOrder = - new LimitOrder.Builder(OrderType.BID, new CurrencyPair(Currency.ETH, Currency.KRW)) - .originalAmount(new BigDecimal("0.01")) - .limitPrice(new BigDecimal("200000")) - .build(); - String id = tradeService.placeLimitOrder(limitOrder); - Thread.sleep(3000); - Collection orders = tradeService.getOrder(id); - orders.forEach(order -> System.out.println(order)); - System.out.println(tradeService.cancelOrder(id)); - } -} diff --git a/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitTradeDemo.java b/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitTradeDemo.java new file mode 100644 index 00000000000..708eca6e9e1 --- /dev/null +++ b/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/trade/UpbitTradeDemo.java @@ -0,0 +1,66 @@ +package org.knowm.xchange.examples.upbit.trade; + +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Collection; +import org.knowm.xchange.Exchange; +import org.knowm.xchange.currency.Currency; +import org.knowm.xchange.currency.CurrencyPair; +import org.knowm.xchange.dto.Order; +import org.knowm.xchange.dto.Order.OrderType; +import org.knowm.xchange.dto.trade.LimitOrder; +import org.knowm.xchange.dto.trade.MarketOrder; +import org.knowm.xchange.examples.upbit.UpbitDemoUtils; +import org.knowm.xchange.service.trade.TradeService; + +/** Demonstrate requesting limit order at Upbit */ +public class UpbitTradeDemo { + + public static void main(String[] args) throws Exception { + Exchange upbit = UpbitDemoUtils.createExchange(); + generic(upbit.getTradeService()); + } + + private static void generic(TradeService tradeService) throws IOException, InterruptedException { + + limitOrder(tradeService); + marketOrderBuy(tradeService); + marketOrderSell(tradeService); + } + + private static void limitOrder(TradeService tradeService) + throws IOException, InterruptedException { + LimitOrder limitOrder = + new LimitOrder.Builder(OrderType.BID, new CurrencyPair(Currency.ETH, Currency.KRW)) + .originalAmount(new BigDecimal("0.01")) + .limitPrice(new BigDecimal("200000")) + .build(); + String id = tradeService.placeLimitOrder(limitOrder); + Thread.sleep(3000); + Collection orders = tradeService.getOrder(id); + orders.forEach(System.out::println); + System.out.println(tradeService.cancelOrder(id)); + } + + private static void marketOrderBuy(TradeService tradeService) + throws IOException, InterruptedException { + MarketOrder marketOrder = + new MarketOrder.Builder(OrderType.BID, CurrencyPair.BTC_KRW) + .originalAmount(new BigDecimal("50000")) + .build(); + final String orderId = tradeService.placeMarketOrder(marketOrder); + final Collection order = tradeService.getOrder(orderId); + order.forEach(System.out::println); + } + + private static void marketOrderSell(TradeService tradeService) + throws IOException, InterruptedException { + MarketOrder marketOrder = + new MarketOrder.Builder(OrderType.ASK, CurrencyPair.BTC_KRW) + .originalAmount(new BigDecimal("0.00076675")) + .build(); + final String orderId = tradeService.placeMarketOrder(marketOrder); + final Collection order = tradeService.getOrder(orderId); + order.forEach(System.out::println); + } +} diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAuthenticated.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAuthenticated.java index d65215aba61..e0aee51611e 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAuthenticated.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAuthenticated.java @@ -28,7 +28,7 @@ UpbitBalances getWallet(@HeaderParam("Authorization") ParamsDigest signatureCrea @POST @Path("orders") - UpbitOrderResponse limitOrder( + UpbitOrderResponse placeOrder( @HeaderParam("Authorization") ParamsDigest signatureCreator, UpbitOrderRequest upbitOrderRequest) throws IOException, UpbitException; diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/trade/UpbitOrderRequest.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/trade/UpbitOrderRequest.java index a60bb2574ee..bd9836e9cf3 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/trade/UpbitOrderRequest.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/trade/UpbitOrderRequest.java @@ -1,7 +1,9 @@ package org.knowm.xchange.upbit.dto.trade; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +@JsonInclude(JsonInclude.Include.NON_NULL) public class UpbitOrderRequest { @JsonProperty("market") diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeService.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeService.java index bcb9267025b..8988492ca1a 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeService.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeService.java @@ -6,6 +6,7 @@ import org.knowm.xchange.Exchange; import org.knowm.xchange.dto.Order; import org.knowm.xchange.dto.trade.LimitOrder; +import org.knowm.xchange.dto.trade.MarketOrder; import org.knowm.xchange.service.trade.TradeService; import org.knowm.xchange.service.trade.params.CancelOrderParams; import org.knowm.xchange.service.trade.params.DefaultCancelOrderParamId; @@ -46,4 +47,9 @@ public Collection getOrder(OrderQueryParams... orderQueryParams) throws I } return rtn; } + + @Override + public String placeMarketOrder(MarketOrder marketOrder) throws IOException { + return super.marketOrder(marketOrder).getUuid(); + } } diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeServiceRaw.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeServiceRaw.java index 57ca6da571a..93ec695d817 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeServiceRaw.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitTradeServiceRaw.java @@ -2,7 +2,10 @@ import java.io.IOException; import org.knowm.xchange.Exchange; +import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.trade.LimitOrder; +import org.knowm.xchange.dto.trade.MarketOrder; +import org.knowm.xchange.instrument.Instrument; import org.knowm.xchange.upbit.UpbitUtils; import org.knowm.xchange.upbit.dto.account.UpbitBalances; import org.knowm.xchange.upbit.dto.trade.UpbitOrderRequest; @@ -21,15 +24,34 @@ public UpbitBalances getWallet() throws IOException { } public UpbitOrderResponse limitOrder(LimitOrder limitOrder) throws IOException { + final Instrument instrument = limitOrder.getInstrument(); + final String marketId = instrument.getCounter() + "-" + instrument.getBase(); + UpbitOrderRequest upbitOrderRequest = new UpbitOrderRequest(); - String marketId = - limitOrder.getCurrencyPair().counter + "-" + limitOrder.getCurrencyPair().base; upbitOrderRequest.setMarketId(marketId); upbitOrderRequest.setVolume(limitOrder.getOriginalAmount().toString()); upbitOrderRequest.setPrice(limitOrder.getLimitPrice().toString()); upbitOrderRequest.setSide(UpbitUtils.toSide(limitOrder.getType())); upbitOrderRequest.setOrderType("limit"); - return upbit.limitOrder(this.signatureCreator, upbitOrderRequest); + return upbit.placeOrder(this.signatureCreator, upbitOrderRequest); + } + + public UpbitOrderResponse marketOrder(MarketOrder marketOrder) throws IOException { + final Instrument instrument = marketOrder.getInstrument(); + final String marketId = instrument.getCounter() + "-" + instrument.getBase(); + + UpbitOrderRequest upbitOrderRequest = new UpbitOrderRequest(); + upbitOrderRequest.setMarketId(marketId); + upbitOrderRequest.setSide(UpbitUtils.toSide(marketOrder.getType())); + + if (marketOrder.getType() == OrderType.BID) { + upbitOrderRequest.setPrice(marketOrder.getOriginalAmount().toString()); + upbitOrderRequest.setOrderType("price"); + } else if (marketOrder.getType() == OrderType.ASK) { + upbitOrderRequest.setVolume(marketOrder.getOriginalAmount().toString()); + upbitOrderRequest.setOrderType("market"); + } + return upbit.placeOrder(this.signatureCreator, upbitOrderRequest); } public UpbitOrderResponse cancelOrderRaw(String cancelId) throws IOException { From 6bf35507edb034a5a5cf4c3cbef949dc8d2263f8 Mon Sep 17 00:00:00 2001 From: Aaron Jang Date: Sun, 11 Feb 2024 23:14:43 +0900 Subject: [PATCH 2/3] [upbit] added candlestick data api --- .../org/knowm/xchange/utils/DateUtils.java | 7 + ...ckerDemo.java => UpbitMarketDataDemo.java} | 12 +- .../java/org/knowm/xchange/upbit/Upbit.java | 16 ++- .../knowm/xchange/upbit/UpbitAdapters.java | 34 +++-- .../dto/marketdata/UpbitCandleStickData.java | 120 ++++++++++++++++++ .../service/UpbitCandleStickPeriodType.java | 43 +++++++ .../upbit/service/UpbitMarketDataService.java | 10 ++ .../service/UpbitMarketDataServiceRaw.java | 37 +++++- 8 files changed, 258 insertions(+), 21 deletions(-) rename xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/{UpbitTickerDemo.java => UpbitMarketDataDemo.java} (66%) create mode 100644 xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java create mode 100644 xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitCandleStickPeriodType.java diff --git a/xchange-core/src/main/java/org/knowm/xchange/utils/DateUtils.java b/xchange-core/src/main/java/org/knowm/xchange/utils/DateUtils.java index e5afb3bbaae..c5993196a59 100644 --- a/xchange-core/src/main/java/org/knowm/xchange/utils/DateUtils.java +++ b/xchange-core/src/main/java/org/knowm/xchange/utils/DateUtils.java @@ -50,6 +50,13 @@ public static String toUTCISODateString(Date date) { return isoDateFormat.format(date); } + public static String toISO8601DateString(Date date) { + + SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + iso8601Format.setTimeZone(TimeZone.getTimeZone("GMT")); + return iso8601Format.format(date); + } + public static String toISODateString(Date date) { SimpleDateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); return isoDateFormat.format(date); diff --git a/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitTickerDemo.java b/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitMarketDataDemo.java similarity index 66% rename from xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitTickerDemo.java rename to xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitMarketDataDemo.java index 1fcda1b8993..ac58d630bc3 100644 --- a/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitTickerDemo.java +++ b/xchange-examples/src/main/java/org/knowm/xchange/examples/upbit/marketdata/UpbitMarketDataDemo.java @@ -1,15 +1,18 @@ package org.knowm.xchange.examples.upbit.marketdata; import java.io.IOException; +import java.util.Date; import org.knowm.xchange.Exchange; import org.knowm.xchange.ExchangeFactory; import org.knowm.xchange.currency.Currency; import org.knowm.xchange.currency.CurrencyPair; import org.knowm.xchange.service.marketdata.MarketDataService; +import org.knowm.xchange.service.trade.params.DefaultCandleStickParam; +import org.knowm.xchange.service.trade.params.DefaultCandleStickParamWithLimit; import org.knowm.xchange.upbit.UpbitExchange; /** Demonstrate requesting Ticker at Upbit */ -public class UpbitTickerDemo { +public class UpbitMarketDataDemo { public static void main(String[] args) throws IOException { @@ -28,5 +31,12 @@ public static void main(String[] args) throws IOException { System.out.println(marketDataService.getTickers(null)); System.out.println(marketDataService.getTrades(pair)); + + System.out.println( + marketDataService.getCandleStickData( + pair, new DefaultCandleStickParam(new Date(), new Date(), 600))); + System.out.println( + marketDataService.getCandleStickData( + pair, new DefaultCandleStickParamWithLimit(new Date(), new Date(), 60, 5))); } } diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/Upbit.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/Upbit.java index 8384b4b5eb0..b6b67dad8f2 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/Upbit.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/Upbit.java @@ -2,14 +2,12 @@ import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; import jakarta.ws.rs.QueryParam; import java.io.IOException; import java.util.List; import org.knowm.xchange.upbit.dto.UpbitException; -import org.knowm.xchange.upbit.dto.marketdata.UpbitMarket; -import org.knowm.xchange.upbit.dto.marketdata.UpbitOrderBooks; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTickers; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTrades; +import org.knowm.xchange.upbit.dto.marketdata.*; @Path("v1") public interface Upbit { @@ -31,4 +29,14 @@ UpbitOrderBooks getOrderBook(@QueryParam("markets") String markets) @Path("trades/ticks") UpbitTrades getTrades(@QueryParam("market") String market, @QueryParam("count") int count) throws IOException, UpbitException; + + @GET + @Path("candles/{timeUnit}/{timeUnitCount}") + List getCandleStick( + @PathParam("timeUnit") String timeUnit, + @PathParam("timeUnitCount") long timeUnitCount, + @QueryParam("market") String market, + @QueryParam("to") String to, + @QueryParam("count") Integer count) + throws IOException, UpbitException; } diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAdapters.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAdapters.java index da22b31e6fc..7f55f0acb17 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAdapters.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/UpbitAdapters.java @@ -1,5 +1,6 @@ package org.knowm.xchange.upbit; +import java.io.IOException; import java.math.BigDecimal; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -16,23 +17,13 @@ import org.knowm.xchange.dto.Order.OrderType; import org.knowm.xchange.dto.account.Balance; import org.knowm.xchange.dto.account.Wallet; -import org.knowm.xchange.dto.marketdata.OrderBook; -import org.knowm.xchange.dto.marketdata.Ticker; -import org.knowm.xchange.dto.marketdata.Trade; -import org.knowm.xchange.dto.marketdata.Trades; +import org.knowm.xchange.dto.marketdata.*; import org.knowm.xchange.dto.meta.ExchangeMetaData; import org.knowm.xchange.dto.meta.InstrumentMetaData; import org.knowm.xchange.dto.trade.LimitOrder; import org.knowm.xchange.instrument.Instrument; import org.knowm.xchange.upbit.dto.account.UpbitBalances; -import org.knowm.xchange.upbit.dto.marketdata.UpbitMarket; -import org.knowm.xchange.upbit.dto.marketdata.UpbitOrderBook; -import org.knowm.xchange.upbit.dto.marketdata.UpbitOrderBookData; -import org.knowm.xchange.upbit.dto.marketdata.UpbitOrderBooks; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTicker; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTickers; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTrade; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTrades; +import org.knowm.xchange.upbit.dto.marketdata.*; import org.knowm.xchange.upbit.dto.trade.UpbitOrderResponse; import org.knowm.xchange.utils.DateUtils; @@ -176,4 +167,23 @@ public static ExchangeMetaData adaptMetadata(List markets) { Function.identity(), cp -> new InstrumentMetaData.Builder().build())); return new ExchangeMetaData(pairMeta, null, null, null, null); } + + public static CandleStickData adaptCandleStickData( + List candleStickData, CurrencyPair currencyPair) throws IOException { + + List candleSticks = new ArrayList<>(); + for (UpbitCandleStickData it : candleStickData) { + candleSticks.add( + new CandleStick.Builder() + .timestamp(DateUtils.fromISO8601DateString(it.getCandleDateTimeUtc())) + .open(it.getOpeningPrice()) + .high(it.getHighPrice()) + .low(it.getLowPrice()) + .close(it.getTracePrice()) + .volume(it.getCandleAccTradeVolume()) + .quotaVolume(it.getCandleAccTradePrice()) + .build()); + } + return new CandleStickData(currencyPair, candleSticks); + } } diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java new file mode 100644 index 00000000000..56f59820c6b --- /dev/null +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java @@ -0,0 +1,120 @@ +package org.knowm.xchange.upbit.dto.marketdata; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.math.BigDecimal; + +public class UpbitCandleStickData { + + private final String market; + private final String candleDateTimeUtc; + private final String candleDateTimeKst; + private final BigDecimal openingPrice; + private final BigDecimal highPrice; + private final BigDecimal lowPrice; + private final BigDecimal tracePrice; + private final Long timestamp; + private final BigDecimal candleAccTradePrice; + private final BigDecimal candleAccTradeVolume; + private final String firstDayOfPeriod; + + public UpbitCandleStickData( + @JsonProperty("market") String market, + @JsonProperty("candle_date_time_utc") String candleDateTimeUtc, + @JsonProperty("candle_date_time_kst") String candleDateTimeKst, + @JsonProperty("opening_price") BigDecimal openingPrice, + @JsonProperty("high_price") BigDecimal highPrice, + @JsonProperty("low_price") BigDecimal lowPrice, + @JsonProperty("trade_price") BigDecimal tracePrice, + @JsonProperty("timestamp") Long timestamp, + @JsonProperty("candle_acc_trade_price") BigDecimal candleAccTradePrice, + @JsonProperty("candle_acc_trade_volume") BigDecimal candleAccTradeVolume, + @JsonProperty("first_day_of_period") String firstDayOfPeriod) { + this.market = market; + this.candleDateTimeUtc = candleDateTimeUtc; + this.candleDateTimeKst = candleDateTimeKst; + this.openingPrice = openingPrice; + this.highPrice = highPrice; + this.lowPrice = lowPrice; + this.tracePrice = tracePrice; + this.timestamp = timestamp; + this.candleAccTradePrice = candleAccTradePrice; + this.candleAccTradeVolume = candleAccTradeVolume; + this.firstDayOfPeriod = firstDayOfPeriod; + } + + public String getMarket() { + return market; + } + + public String getCandleDateTimeUtc() { + return candleDateTimeUtc; + } + + public String getCandleDateTimeKst() { + return candleDateTimeKst; + } + + public BigDecimal getOpeningPrice() { + return openingPrice; + } + + public BigDecimal getHighPrice() { + return highPrice; + } + + public BigDecimal getLowPrice() { + return lowPrice; + } + + public BigDecimal getTracePrice() { + return tracePrice; + } + + public Long getTimestamp() { + return timestamp; + } + + public BigDecimal getCandleAccTradePrice() { + return candleAccTradePrice; + } + + public BigDecimal getCandleAccTradeVolume() { + return candleAccTradeVolume; + } + + public String getFirstDayOfPeriod() { + return firstDayOfPeriod; + } + + @Override + public String toString() { + return "UpbitCandleStickData{" + + "market='" + + market + + '\'' + + ", candleDateTimeUtc='" + + candleDateTimeUtc + + '\'' + + ", candleDateTimeKst='" + + candleDateTimeKst + + '\'' + + ", openingPrice=" + + openingPrice + + ", highPrice=" + + highPrice + + ", lowPrice=" + + lowPrice + + ", tracePrice=" + + tracePrice + + ", timestamp=" + + timestamp + + ", candleAccTradePrice=" + + candleAccTradePrice + + ", candleAccTradeVolume=" + + candleAccTradeVolume + + ", firstDayOfPeriod='" + + firstDayOfPeriod + + '\'' + + '}'; + } +} diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitCandleStickPeriodType.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitCandleStickPeriodType.java new file mode 100644 index 00000000000..ca671b37d50 --- /dev/null +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitCandleStickPeriodType.java @@ -0,0 +1,43 @@ +package org.knowm.xchange.upbit.service; + +public enum UpbitCandleStickPeriodType { + WEEK("weeks", 60 * 60 * 24 * 7), + DAY("days", 60 * 60 * 24), + MINUTE("minutes", 60); + + private final String pathName; + private final Integer periodInSeconds; + + UpbitCandleStickPeriodType(String pathName, Integer periodInSeconds) { + this.pathName = pathName; + this.periodInSeconds = periodInSeconds; + } + + public String getPathName() { + return pathName; + } + + static UpbitCandleStickPeriodType getPeriodTypeFromSecs(long seconds) { + UpbitCandleStickPeriodType result = null; + for (UpbitCandleStickPeriodType period : UpbitCandleStickPeriodType.values()) { + if (seconds % period.periodInSeconds == 0) { + result = period; + break; + } + } + return result; + } + + public static long[] getSupportedPeriodsInSecs() { + long[] result = new long[UpbitCandleStickPeriodType.values().length]; + int index = 0; + for (UpbitCandleStickPeriodType period : UpbitCandleStickPeriodType.values()) { + result[index++] = period.periodInSeconds; + } + return result; + } + + public long getUnitCount(long seconds) { + return seconds / periodInSeconds; + } +} diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataService.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataService.java index 2aad69f7c9f..b7eb29221a9 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataService.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataService.java @@ -5,6 +5,7 @@ import java.util.List; import org.knowm.xchange.Exchange; import org.knowm.xchange.currency.CurrencyPair; +import org.knowm.xchange.dto.marketdata.CandleStickData; import org.knowm.xchange.dto.marketdata.OrderBook; import org.knowm.xchange.dto.marketdata.Ticker; import org.knowm.xchange.dto.marketdata.Trades; @@ -12,6 +13,7 @@ import org.knowm.xchange.instrument.Instrument; 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.upbit.UpbitAdapters; /** @@ -54,4 +56,12 @@ public Trades getTrades(CurrencyPair currencyPair, Object... args) throws IOExce public ExchangeMetaData getMetaData() throws IOException { return UpbitAdapters.adaptMetadata(getMarketAll()); } + + @Override + public CandleStickData getCandleStickData(CurrencyPair currencyPair, CandleStickDataParams params) + throws IOException { + + return UpbitAdapters.adaptCandleStickData( + super.getUpbitCandleStickData(currencyPair, params), currencyPair); + } } diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataServiceRaw.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataServiceRaw.java index 0011a2aa255..1e56bd65ba3 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataServiceRaw.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/service/UpbitMarketDataServiceRaw.java @@ -1,16 +1,19 @@ package org.knowm.xchange.upbit.service; import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.knowm.xchange.Exchange; import org.knowm.xchange.currency.CurrencyPair; +import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException; import org.knowm.xchange.instrument.Instrument; +import org.knowm.xchange.service.trade.params.CandleStickDataParams; +import org.knowm.xchange.service.trade.params.DefaultCandleStickParam; +import org.knowm.xchange.service.trade.params.DefaultCandleStickParamWithLimit; import org.knowm.xchange.upbit.UpbitUtils; -import org.knowm.xchange.upbit.dto.marketdata.UpbitMarket; -import org.knowm.xchange.upbit.dto.marketdata.UpbitOrderBooks; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTickers; -import org.knowm.xchange.upbit.dto.marketdata.UpbitTrades; +import org.knowm.xchange.upbit.dto.marketdata.*; +import org.knowm.xchange.utils.DateUtils; /** * Implementation of the market data service for Korbit @@ -57,4 +60,30 @@ public UpbitOrderBooks getUpbitOrderBook(CurrencyPair currencyPair) throws IOExc public UpbitTrades getTrades(CurrencyPair currencyPair) throws IOException { return upbit.getTrades(UpbitUtils.toPairString(currencyPair), 100); } + + public List getUpbitCandleStickData( + CurrencyPair currencyPair, CandleStickDataParams params) throws IOException { + + if (!(params instanceof DefaultCandleStickParam)) { + throw new NotYetImplementedForExchangeException("Only DefaultCandleStickParam is supported"); + } + final DefaultCandleStickParam defaultCandleStickParam = (DefaultCandleStickParam) params; + final UpbitCandleStickPeriodType periodType = + UpbitCandleStickPeriodType.getPeriodTypeFromSecs(defaultCandleStickParam.getPeriodInSecs()); + if (periodType == null) { + throw new NotYetImplementedForExchangeException( + "Only discrete period values are supported;" + + Arrays.toString(UpbitCandleStickPeriodType.getSupportedPeriodsInSecs())); + } + + Integer limit = null; + if (params instanceof DefaultCandleStickParamWithLimit) { + limit = ((DefaultCandleStickParamWithLimit) params).getLimit(); + } + final String pathName = periodType.getPathName(); + final String pairString = UpbitUtils.toPairString(currencyPair); + final String utcString = DateUtils.toISO8601DateString(defaultCandleStickParam.getEndDate()); + final long unitCount = periodType.getUnitCount(defaultCandleStickParam.getPeriodInSecs()); + return upbit.getCandleStick(pathName, unitCount, pairString, utcString, limit); + } } From f65fc1b965ad32cbdb56f9f99a450f0485f292ac Mon Sep 17 00:00:00 2001 From: Aaron Jang Date: Mon, 12 Feb 2024 20:00:54 +0900 Subject: [PATCH 3/3] [upbit] use lombok @Getter, @ToString --- .../dto/marketdata/UpbitCandleStickData.java | 80 +------------------ 1 file changed, 4 insertions(+), 76 deletions(-) diff --git a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java index 56f59820c6b..558d0b50124 100644 --- a/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java +++ b/xchange-upbit/src/main/java/org/knowm/xchange/upbit/dto/marketdata/UpbitCandleStickData.java @@ -2,7 +2,11 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.math.BigDecimal; +import lombok.Getter; +import lombok.ToString; +@ToString +@Getter public class UpbitCandleStickData { private final String market; @@ -41,80 +45,4 @@ public UpbitCandleStickData( this.candleAccTradeVolume = candleAccTradeVolume; this.firstDayOfPeriod = firstDayOfPeriod; } - - public String getMarket() { - return market; - } - - public String getCandleDateTimeUtc() { - return candleDateTimeUtc; - } - - public String getCandleDateTimeKst() { - return candleDateTimeKst; - } - - public BigDecimal getOpeningPrice() { - return openingPrice; - } - - public BigDecimal getHighPrice() { - return highPrice; - } - - public BigDecimal getLowPrice() { - return lowPrice; - } - - public BigDecimal getTracePrice() { - return tracePrice; - } - - public Long getTimestamp() { - return timestamp; - } - - public BigDecimal getCandleAccTradePrice() { - return candleAccTradePrice; - } - - public BigDecimal getCandleAccTradeVolume() { - return candleAccTradeVolume; - } - - public String getFirstDayOfPeriod() { - return firstDayOfPeriod; - } - - @Override - public String toString() { - return "UpbitCandleStickData{" - + "market='" - + market - + '\'' - + ", candleDateTimeUtc='" - + candleDateTimeUtc - + '\'' - + ", candleDateTimeKst='" - + candleDateTimeKst - + '\'' - + ", openingPrice=" - + openingPrice - + ", highPrice=" - + highPrice - + ", lowPrice=" - + lowPrice - + ", tracePrice=" - + tracePrice - + ", timestamp=" - + timestamp - + ", candleAccTradePrice=" - + candleAccTradePrice - + ", candleAccTradeVolume=" - + candleAccTradeVolume - + ", firstDayOfPeriod='" - + firstDayOfPeriod - + '\'' - + '}'; - } }