use of org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted in project XChange by knowm.
the class YoBitTradeService method getTradeHistory.
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
Integer count = 1000;
if (params instanceof TradeHistoryParamLimit) {
count = ((TradeHistoryParamLimit) params).getLimit();
}
Long offset = 0L;
if (params instanceof TradeHistoryParamOffset) {
offset = ((TradeHistoryParamOffset) params).getOffset();
}
String market = null;
if (params instanceof TradeHistoryParamCurrencyPair) {
CurrencyPair currencyPair = ((TradeHistoryParamCurrencyPair) params).getCurrencyPair();
market = YoBitAdapters.adaptCcyPairToUrlFormat(currencyPair);
}
Long fromTransactionId = null;
Long endTransactionId = null;
if (params instanceof TradeHistoryParamsIdSpan) {
TradeHistoryParamsIdSpan tradeHistoryParamsIdSpan = (TradeHistoryParamsIdSpan) params;
String startId = tradeHistoryParamsIdSpan.getStartId();
if (startId != null)
fromTransactionId = Long.valueOf(startId);
String endId = tradeHistoryParamsIdSpan.getEndId();
if (endId != null)
endTransactionId = Long.valueOf(endId);
}
String order = "DESC";
if (params instanceof TradeHistoryParamsSorted) {
order = ((TradeHistoryParamsSorted) params).getOrder().equals(TradeHistoryParamsSorted.Order.desc) ? "DESC" : "ASC";
}
Long fromTimestamp = null;
Long toTimestamp = null;
if (params instanceof TradeHistoryParamsTimeSpan) {
TradeHistoryParamsTimeSpan tradeHistoryParamsTimeSpan = (TradeHistoryParamsTimeSpan) params;
Date startTime = tradeHistoryParamsTimeSpan.getStartTime();
if (startTime != null)
fromTimestamp = DateUtils.toUnixTimeNullSafe(startTime);
Date endTime = tradeHistoryParamsTimeSpan.getEndTime();
if (endTime != null)
toTimestamp = DateUtils.toUnixTimeNullSafe(endTime);
}
BaseYoBitResponse response = tradeHistory(count, offset, market, fromTransactionId, endTransactionId, order, fromTimestamp, toTimestamp);
List<UserTrade> trades = new ArrayList<>();
if (response.returnData != null) {
for (Object key : response.returnData.keySet()) {
Map tradeData = (Map) response.returnData.get(key);
trades.add(YoBitAdapters.adaptUserTrade(key, tradeData));
}
}
return new UserTrades(trades, Trades.TradeSortType.SortByTimestamp);
}
use of org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted in project XChange by knowm.
the class DragonexTradeService method getTradeHistory.
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
if (!(params instanceof TradeHistoryParamCurrencyPair)) {
throw new ExchangeException("You need to provide the currency pair.");
}
TradeHistoryParamCurrencyPair pairParams = (TradeHistoryParamCurrencyPair) params;
CurrencyPair pair = pairParams.getCurrencyPair();
if (pair == null) {
throw new ExchangeException("You need to provide the currency pair.");
}
long symbolId = exchange.symbolId(pair);
Integer direction = null;
if (params instanceof TradeHistoryParamsSorted) {
TradeHistoryParamsSorted sort = (TradeHistoryParamsSorted) params;
direction = sort.getOrder() == Order.asc ? 1 : 2;
}
DealHistoryRequest req = new DealHistoryRequest(symbolId, direction, null, 1000);
DealHistory dealHistory = super.dealHistory(exchange.getOrCreateToken().token, req);
List<UserTrade> trades = dealHistory.getList().stream().map(d -> {
CurrencyPair currencyPair = exchange.pair(d.symbolId);
return new UserTrade.Builder().type(d.orderType == 1 ? OrderType.BID : OrderType.ASK).originalAmount(d.volume).currencyPair(currencyPair).price(d.price).timestamp(d.getTimestamp()).id(d.tradeId).orderId(d.orderId).feeAmount(d.charge).feeCurrency(currencyPair.counter).build();
}).collect(Collectors.toList());
return new UserTrades(trades, TradeSortType.SortByTimestamp);
}
use of org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted in project XChange by knowm.
the class DeribitTradeService method getTradeHistory.
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
String instrumentName = null;
if (params instanceof TradeHistoryParamInstrument) {
Instrument instrument = ((TradeHistoryParamInstrument) params).getInstrument();
if (instrument != null) {
instrumentName = DeribitAdapters.adaptInstrumentName(instrument);
}
}
String currency = null;
// not implemented
Kind kind = null;
if (params instanceof TradeHistoryParamCurrencyPair) {
CurrencyPair currencyPair = ((TradeHistoryParamCurrencyPair) params).getCurrencyPair();
if (currencyPair != null) {
currency = currencyPair.base.getCurrencyCode();
}
}
Date startTime = null;
Date endTime = null;
if (params instanceof TradeHistoryParamsTimeSpan) {
startTime = ((TradeHistoryParamsTimeSpan) params).getStartTime();
endTime = ((TradeHistoryParamsTimeSpan) params).getEndTime();
}
String startId = null;
String endId = null;
if (params instanceof TradeHistoryParamsIdSpan) {
startId = ((TradeHistoryParamsIdSpan) params).getStartId();
endId = ((TradeHistoryParamsIdSpan) params).getEndId();
}
Integer limit = null;
if (params instanceof TradeHistoryParamLimit) {
limit = ((TradeHistoryParamLimit) params).getLimit();
}
String sorting = null;
if (params instanceof TradeHistoryParamsSorted) {
TradeHistoryParamsSorted.Order order = ((TradeHistoryParamsSorted) params).getOrder();
sorting = order == TradeHistoryParamsSorted.Order.asc ? "asc" : order == TradeHistoryParamsSorted.Order.desc ? "desc" : null;
}
Boolean includeOld = null;
if (params instanceof DeribitTradeHistoryParamsOld) {
includeOld = ((DeribitTradeHistoryParamsOld) params).isIncludeOld();
}
org.knowm.xchange.deribit.v2.dto.trade.UserTrades userTrades = null;
if (startTime != null && endTime != null) {
if (instrumentName != null) {
userTrades = super.getUserTradesByInstrumentAndTime(instrumentName, startTime, endTime, limit, includeOld, sorting);
} else if (currency != null) {
userTrades = super.getUserTradesByCurrencyAndTime(currency, kind, startTime, endTime, limit, includeOld, sorting);
}
} else {
if (instrumentName != null) {
Integer startSeq = startId != null ? Integer.valueOf(startId) : null;
Integer endSeq = endId != null ? Integer.valueOf(endId) : null;
userTrades = super.getUserTradesByInstrument(instrumentName, startSeq, endSeq, limit, includeOld, sorting);
} else if (currency != null) {
userTrades = super.getUserTradesByCurrency(currency, kind, startId, endId, limit, includeOld, sorting);
}
}
if (userTrades == null) {
throw new ExchangeException("You should specify either instrument or currency pair");
}
return DeribitAdapters.adaptUserTrades(userTrades);
}
use of org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted in project XChange by knowm.
the class BitmexTradeService method getTradeHistory.
@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
String symbol = null;
if (params instanceof TradeHistoryParamCurrencyPair) {
symbol = BitmexAdapters.adaptCurrencyPairToSymbol(((TradeHistoryParamCurrencyPair) params).getCurrencyPair());
}
Long start = null;
if (params instanceof TradeHistoryParamOffset) {
start = ((TradeHistoryParamOffset) params).getOffset();
}
Date startTime = null;
Date endTime = null;
if (params instanceof TradeHistoryParamsTimeSpan) {
TradeHistoryParamsTimeSpan timeSpan = (TradeHistoryParamsTimeSpan) params;
startTime = timeSpan.getStartTime();
endTime = timeSpan.getEndTime();
}
int count = 100;
if (params instanceof TradeHistoryParamLimit) {
TradeHistoryParamLimit limit = (TradeHistoryParamLimit) params;
if (limit.getLimit() != null) {
count = limit.getLimit();
}
}
boolean reverse = (params instanceof TradeHistoryParamsSorted) && ((TradeHistoryParamsSorted) params).getOrder() == TradeHistoryParamsSorted.Order.desc;
List<UserTrade> userTrades = getTradeHistory(symbol, null, null, count, start, reverse, startTime, endTime).stream().map(BitmexAdapters::adoptUserTrade).filter(Objects::nonNull).collect(Collectors.toList());
return new UserTrades(userTrades, TradeSortType.SortByTimestamp);
}
use of org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted in project XChange by knowm.
the class BitfinexTradeService method getTradeHistory.
/**
* @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 {
try {
String symbol = null;
if (params instanceof TradeHistoryParamCurrencyPair && ((TradeHistoryParamCurrencyPair) params).getCurrencyPair() != null) {
symbol = BitfinexAdapters.adaptCurrencyPair(((TradeHistoryParamCurrencyPair) params).getCurrencyPair());
}
Long startTime = 0L;
Long endTime = null;
Long limit = 50L;
Long sort = null;
if (params instanceof TradeHistoryParamsTimeSpan) {
TradeHistoryParamsTimeSpan paramsTimeSpan = (TradeHistoryParamsTimeSpan) params;
startTime = DateUtils.toMillisNullSafe(paramsTimeSpan.getStartTime());
endTime = DateUtils.toMillisNullSafe(paramsTimeSpan.getEndTime());
}
if (params instanceof TradeHistoryParamLimit) {
TradeHistoryParamLimit tradeHistoryParamLimit = (TradeHistoryParamLimit) params;
if (tradeHistoryParamLimit.getLimit() != null) {
limit = Long.valueOf(tradeHistoryParamLimit.getLimit());
}
}
if (params instanceof TradeHistoryParamsSorted) {
TradeHistoryParamsSorted tradeHistoryParamsSorted = (TradeHistoryParamsSorted) params;
sort = tradeHistoryParamsSorted.getOrder() == TradeHistoryParamsSorted.Order.asc ? 1L : -1L;
}
final List<Trade> trades = getBitfinexTradesV2(symbol, startTime, endTime, limit, sort);
return BitfinexAdapters.adaptTradeHistoryV2(trades);
} catch (BitfinexException e) {
throw BitfinexErrorAdapter.adapt(e);
}
}
Aggregations