Search in sources :

Example 1 with TradeHistoryParamsSorted

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);
}
Also used : TradeHistoryParamOffset(org.knowm.xchange.service.trade.params.TradeHistoryParamOffset) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) ArrayList(java.util.ArrayList) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Date(java.util.Date) UserTrades(org.knowm.xchange.dto.trade.UserTrades) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) BaseYoBitResponse(org.knowm.xchange.yobit.dto.BaseYoBitResponse) Map(java.util.Map) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 2 with TradeHistoryParamsSorted

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);
}
Also used : TradeHistoryParams(org.knowm.xchange.service.trade.params.TradeHistoryParams) OrderType(org.knowm.xchange.dto.Order.OrderType) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) DefaultTradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamCurrencyPair) Exchange(org.knowm.xchange.Exchange) OrderHistory(org.knowm.xchange.dragonex.dto.trade.OrderHistory) TradeSortType(org.knowm.xchange.dto.marketdata.Trades.TradeSortType) DealHistoryRequest(org.knowm.xchange.dragonex.dto.trade.DealHistoryRequest) OrderHistoryRequest(org.knowm.xchange.dragonex.dto.trade.OrderHistoryRequest) CancelOrderByIdParams(org.knowm.xchange.service.trade.params.CancelOrderByIdParams) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DealHistory(org.knowm.xchange.dragonex.dto.trade.DealHistory) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) IOException(java.io.IOException) UserTrades(org.knowm.xchange.dto.trade.UserTrades) OrderReference(org.knowm.xchange.dragonex.dto.trade.OrderReference) UserTrade(org.knowm.xchange.dto.trade.UserTrade) Collectors(java.util.stream.Collectors) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) UserOrder(org.knowm.xchange.dragonex.dto.trade.UserOrder) OrderPlacement(org.knowm.xchange.dragonex.dto.trade.OrderPlacement) List(java.util.List) Order(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted.Order) TradeService(org.knowm.xchange.service.trade.TradeService) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) OpenOrdersParams(org.knowm.xchange.service.trade.params.orders.OpenOrdersParams) CancelOrderParams(org.knowm.xchange.service.trade.params.CancelOrderParams) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) DealHistoryRequest(org.knowm.xchange.dragonex.dto.trade.DealHistoryRequest) UserTrade(org.knowm.xchange.dto.trade.UserTrade) UserTrades(org.knowm.xchange.dto.trade.UserTrades) DealHistory(org.knowm.xchange.dragonex.dto.trade.DealHistory) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) DefaultTradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamCurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) DefaultTradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 3 with TradeHistoryParamsSorted

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);
}
Also used : TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) DeribitExchange(org.knowm.xchange.deribit.v2.DeribitExchange) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) Date(java.util.Date) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) Kind(org.knowm.xchange.deribit.v2.dto.Kind) OpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamInstrument) DefaultOpenOrdersParamInstrument(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamInstrument) TradeHistoryParamInstrument(org.knowm.xchange.service.trade.params.TradeHistoryParamInstrument) Instrument(org.knowm.xchange.instrument.Instrument) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) NotYetImplementedForExchangeException(org.knowm.xchange.exceptions.NotYetImplementedForExchangeException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 4 with TradeHistoryParamsSorted

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);
}
Also used : TradeHistoryParamOffset(org.knowm.xchange.service.trade.params.TradeHistoryParamOffset) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BitmexAdapters(org.knowm.xchange.bitmex.BitmexAdapters) Date(java.util.Date) UserTrades(org.knowm.xchange.dto.trade.UserTrades) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 5 with TradeHistoryParamsSorted

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);
    }
}
Also used : Trade(org.knowm.xchange.bitfinex.v2.dto.trade.Trade) DefaultTradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.DefaultTradeHistoryParamsTimeSpan) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) TradeHistoryParamsSorted(org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted) BitfinexException(org.knowm.xchange.bitfinex.dto.BitfinexException) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Aggregations

TradeHistoryParamsSorted (org.knowm.xchange.service.trade.params.TradeHistoryParamsSorted)9 TradeHistoryParamCurrencyPair (org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)8 TradeHistoryParamsTimeSpan (org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan)7 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)6 TradeHistoryParamLimit (org.knowm.xchange.service.trade.params.TradeHistoryParamLimit)6 TradeHistoryParamOffset (org.knowm.xchange.service.trade.params.TradeHistoryParamOffset)6 OpenOrdersParamCurrencyPair (org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair)5 TradeHistoryParamsIdSpan (org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan)4 Date (java.util.Date)3 UserTrade (org.knowm.xchange.dto.trade.UserTrade)3 UserTrades (org.knowm.xchange.dto.trade.UserTrades)3 ArrayList (java.util.ArrayList)2 ExchangeException (org.knowm.xchange.exceptions.ExchangeException)2 TradeHistoryParamPaging (org.knowm.xchange.service.trade.params.TradeHistoryParamPaging)2 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Exchange (org.knowm.xchange.Exchange)1 BitfinexException (org.knowm.xchange.bitfinex.dto.BitfinexException)1