Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Upbit] add support for market orders and candlestick #4823

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,120 @@
package org.knowm.xchange.upbit.dto.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.math.BigDecimal;

public class UpbitCandleStickData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use lombok for boilerplate-code here

Copy link
Contributor Author

@aaron-jang aaron-jang Feb 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bigscoop Thanks for the suggestion! I've used Lombok to simplify the code.


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
+ '\''
+ '}';
}
}
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
Loading
Loading