Skip to content

Commit

Permalink
Merge pull request #4823 from aaron-jang/develop
Browse files Browse the repository at this point in the history
[Upbit] add support for market orders and candlestick
  • Loading branch information
timmolter authored Feb 21, 2024
2 parents 83002a1 + f65fc1b commit 9d9b0b4
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Order> 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> 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> order = tradeService.getOrder(orderId);
order.forEach(System.out::println);
}
}
16 changes: 12 additions & 4 deletions xchange-upbit/src/main/java/org/knowm/xchange/upbit/Upbit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<UpbitCandleStickData> getCandleStick(
@PathParam("timeUnit") String timeUnit,
@PathParam("timeUnitCount") long timeUnitCount,
@QueryParam("market") String market,
@QueryParam("to") String to,
@QueryParam("count") Integer count)
throws IOException, UpbitException;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -176,4 +167,23 @@ public static ExchangeMetaData adaptMetadata(List<UpbitMarket> markets) {
Function.identity(), cp -> new InstrumentMetaData.Builder().build()));
return new ExchangeMetaData(pairMeta, null, null, null, null);
}

public static CandleStickData adaptCandleStickData(
List<UpbitCandleStickData> candleStickData, CurrencyPair currencyPair) throws IOException {

List<CandleStick> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.knowm.xchange.upbit.dto.marketdata;

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;
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;
}
}
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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;
import org.knowm.xchange.dto.meta.ExchangeMetaData;
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;

/**
Expand Down Expand Up @@ -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);
}
}
Loading

0 comments on commit 9d9b0b4

Please sign in to comment.