package org.knowm.xchange.gemini.v1.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.MarketOrder;
import org.knowm.xchange.dto.trade.OpenOrders;
import org.knowm.xchange.dto.trade.UserTrades;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.exceptions.NotAvailableFromExchangeException;
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;
import org.knowm.xchange.gemini.v1.GeminiAdapters;
import org.knowm.xchange.gemini.v1.GeminiOrderType;
import org.knowm.xchange.gemini.v1.dto.trade.GeminiOrderStatusResponse;
import org.knowm.xchange.gemini.v1.dto.trade.GeminiTradeResponse;
import org.knowm.xchange.service.trade.TradeService;
import org.knowm.xchange.service.trade.params.*;
import org.knowm.xchange.service.trade.params.orders.OpenOrdersParams;
import org.knowm.xchange.utils.DateUtils;
public class GeminiTradeService extends GeminiTradeServiceRaw implements TradeService {
private static final OpenOrders noOpenOrders = new OpenOrders(new ArrayList<LimitOrder>());
public GeminiTradeService(Exchange exchange) {
super(exchange);
}
@Override
public OpenOrders getOpenOrders() throws IOException {
return getOpenOrders(createOpenOrdersParams());
}
@Override
public OpenOrders getOpenOrders(OpenOrdersParams params) throws ExchangeException, NotAvailableFromExchangeException, NotYetImplementedForExchangeException, IOException {
GeminiOrderStatusResponse[] activeOrders = getGeminiOpenOrders();
if (activeOrders.length <= 0) {
return noOpenOrders;
} else {
return GeminiAdapters.adaptOrders(activeOrders);
}
}
@Override
public String placeMarketOrder(MarketOrder marketOrder) throws IOException {
GeminiOrderStatusResponse newOrder = placeGeminiMarketOrder(marketOrder, GeminiOrderType.MARKET);
return String.valueOf(newOrder.getId());
}
@Override
public String placeLimitOrder(LimitOrder limitOrder) throws IOException {
GeminiOrderStatusResponse newOrder = placeGeminiLimitOrder(limitOrder, GeminiOrderType.LIMIT, false);
return String.valueOf(newOrder.getId());
}
@Override
public boolean cancelOrder(String orderId) throws IOException {
return cancelGeminiOrder(orderId);
}
/**
* @param params Implementation of {@link TradeHistoryParamCurrencyPair} is mandatory. Can optionally implement {@link TradeHistoryParamPaging} and
* {@link TradeHistoryParamsTimeSpan#getStartTime()}. All other TradeHistoryParams types will be ignored.
*/
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
final String symbol;
if (params instanceof TradeHistoryParamCurrencyPair && ((TradeHistoryParamCurrencyPair) params).getCurrencyPair() != null) {
symbol = GeminiAdapters.adaptCurrencyPair(((TradeHistoryParamCurrencyPair) params).getCurrencyPair());
} else {
// Exchange will return the errors below if CurrencyPair is not provided.
// field not on request: "Key symbol was not present."
// field supplied but blank: "Key symbol may not be the empty string"
throw new ExchangeException("CurrencyPair must be supplied");
}
final long timestamp;
if (params instanceof TradeHistoryParamsTimeSpan) {
Date startTime = ((TradeHistoryParamsTimeSpan) params).getStartTime();
timestamp = DateUtils.toUnixTime(startTime);
} else {
timestamp = 0;
}
final int limit;
if (params instanceof TradeHistoryParamPaging) {
TradeHistoryParamPaging pagingParams = (TradeHistoryParamPaging) params;
Integer pageLength = pagingParams.getPageLength();
Integer pageNum = pagingParams.getPageNumber();
limit = (pageLength != null && pageNum != null) ? pageLength * (pageNum + 1) : 50;
} else {
limit = 50;
}
final GeminiTradeResponse[] trades = getGeminiTradeHistory(symbol, timestamp, limit);
return GeminiAdapters.adaptTradeHistory(trades, symbol);
}
@Override
public TradeHistoryParams createTradeHistoryParams() {
return new GeminiTradeHistoryParams(new Date(0), 50, CurrencyPair.BTC_USD);
}
@Override
public OpenOrdersParams createOpenOrdersParams() {
return null;
}
public static class GeminiTradeHistoryParams extends DefaultTradeHistoryParamsTimeSpan
implements TradeHistoryParamCurrencyPair, TradeHistoryParamPaging {
private int count;
private CurrencyPair pair;
private Integer pageNumber;
public GeminiTradeHistoryParams(Date startTime, int count, CurrencyPair pair) {
super(startTime);
this.count = count;
this.pair = pair;
}
@Override
public void setPageLength(Integer count) {
this.count = count;
}
@Override
public Integer getPageLength() {
return count;
}
@Override
public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}
@Override
public Integer getPageNumber() {
return pageNumber;
}
@Override
public CurrencyPair getCurrencyPair() {
return pair;
}
@Override
public void setCurrencyPair(CurrencyPair pair) {
this.pair = pair;
}
}
@Override
public Collection<Order> getOrder(String... orderIds)
throws ExchangeException, NotAvailableFromExchangeException, NotYetImplementedForExchangeException, IOException {
throw new NotYetImplementedForExchangeException();
}
}