Search in sources :

Example 96 with Currency

use of org.knowm.xchange.currency.Currency in project XChange by knowm.

the class DsxAccountService method getFundingHistory.

@Override
public List<FundingRecord> getFundingHistory(TradeHistoryParams params) throws IOException {
    List<DsxTransaction> transactions;
    String currencyCode = null;
    Integer limit = 1000;
    int offset = 0;
    if (params instanceof TradeHistoryParamCurrency) {
        Currency currency = ((TradeHistoryParamCurrency) params).getCurrency();
        currencyCode = currency != null ? currency.getCurrencyCode() : null;
    }
    if (params instanceof TradeHistoryParamLimit) {
        limit = ((TradeHistoryParamLimit) params).getLimit();
    }
    if (params instanceof TradeHistoryParamOffset) {
        offset = ((TradeHistoryParamOffset) params).getOffset().intValue();
    }
    transactions = getTransactions(currencyCode, limit, offset);
    List<FundingRecord> records = new ArrayList<>();
    for (DsxTransaction transaction : transactions) {
        records.add(DsxAdapters.adapt(transaction));
    }
    return records;
}
Also used : DsxTransaction(org.knowm.xchange.dsx.dto.DsxTransaction) TradeHistoryParamOffset(org.knowm.xchange.service.trade.params.TradeHistoryParamOffset) TradeHistoryParamCurrency(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrency) Currency(org.knowm.xchange.currency.Currency) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) ArrayList(java.util.ArrayList) TradeHistoryParamCurrency(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrency) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit)

Example 97 with Currency

use of org.knowm.xchange.currency.Currency in project XChange by knowm.

the class BinanceTradeService method getTradeHistory.

@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws IOException {
    try {
        Assert.isTrue(params instanceof TradeHistoryParamCurrencyPair, "You need to provide the currency pair to get the user trades.");
        TradeHistoryParamCurrencyPair pairParams = (TradeHistoryParamCurrencyPair) params;
        CurrencyPair pair = pairParams.getCurrencyPair();
        if (pair == null) {
            throw new ExchangeException("You need to provide the currency pair to get the user trades.");
        }
        Long orderId = null;
        Long startTime = null;
        Long endTime = null;
        if (params instanceof TradeHistoryParamsTimeSpan) {
            if (((TradeHistoryParamsTimeSpan) params).getStartTime() != null) {
                startTime = ((TradeHistoryParamsTimeSpan) params).getStartTime().getTime();
            }
            if (((TradeHistoryParamsTimeSpan) params).getEndTime() != null) {
                endTime = ((TradeHistoryParamsTimeSpan) params).getEndTime().getTime();
            }
        }
        Long fromId = null;
        if (params instanceof TradeHistoryParamsIdSpan) {
            TradeHistoryParamsIdSpan idParams = (TradeHistoryParamsIdSpan) params;
            try {
                fromId = BinanceAdapters.id(idParams.getStartId());
            } catch (Throwable ignored) {
            }
        }
        if ((fromId != null) && (startTime != null || endTime != null)) {
            throw new ExchangeException("You should either specify the id from which you get the user trades from or start and end times. If you specify both, Binance will only honour the fromId parameter.");
        }
        Integer limit = null;
        if (params instanceof TradeHistoryParamLimit) {
            TradeHistoryParamLimit limitParams = (TradeHistoryParamLimit) params;
            limit = limitParams.getLimit();
        }
        List<BinanceTrade> binanceTrades = super.myTrades(pair, orderId, startTime, endTime, fromId, limit);
        List<UserTrade> trades = binanceTrades.stream().map(t -> new UserTrade.Builder().type(BinanceAdapters.convertType(t.isBuyer)).originalAmount(t.qty).currencyPair(pair).price(t.price).timestamp(t.getTime()).id(Long.toString(t.id)).orderId(Long.toString(t.orderId)).feeAmount(t.commission).feeCurrency(Currency.getInstance(t.commissionAsset)).build()).collect(Collectors.toList());
        long lastId = binanceTrades.stream().map(t -> t.id).max(Long::compareTo).orElse(0L);
        return new UserTrades(trades, lastId, Trades.TradeSortType.SortByTimestamp);
    } catch (BinanceException e) {
        throw BinanceErrorAdapter.adapt(e);
    }
}
Also used : StopOrder(org.knowm.xchange.dto.trade.StopOrder) TradeHistoryParams(org.knowm.xchange.service.trade.params.TradeHistoryParams) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OrderType(org.knowm.xchange.binance.dto.trade.OrderType) NotAvailableFromExchangeException(org.knowm.xchange.exceptions.NotAvailableFromExchangeException) BinanceException(org.knowm.xchange.binance.dto.BinanceException) BinanceNewOrder(org.knowm.xchange.binance.dto.trade.BinanceNewOrder) IOrderFlags(org.knowm.xchange.dto.Order.IOrderFlags) BinanceOrder(org.knowm.xchange.binance.dto.trade.BinanceOrder) DefaultOpenOrdersParam(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParam) BinanceTrade(org.knowm.xchange.binance.dto.trade.BinanceTrade) ArrayList(java.util.ArrayList) Value(lombok.Value) BigDecimal(java.math.BigDecimal) Order(org.knowm.xchange.dto.Order) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) BinanceAdapters(org.knowm.xchange.binance.BinanceAdapters) CancelOrderByIdParams(org.knowm.xchange.service.trade.params.CancelOrderByIdParams) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) OpenOrders(org.knowm.xchange.dto.trade.OpenOrders) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) BinanceAuthenticated(org.knowm.xchange.binance.BinanceAuthenticated) OrderQueryParams(org.knowm.xchange.service.trade.params.orders.OrderQueryParams) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) ResilienceRegistries(org.knowm.xchange.client.ResilienceRegistries) BinanceExchange(org.knowm.xchange.binance.BinanceExchange) Collection(java.util.Collection) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) IOException(java.io.IOException) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) TimeInForce(org.knowm.xchange.binance.dto.trade.TimeInForce) Collectors(java.util.stream.Collectors) Trades(org.knowm.xchange.dto.marketdata.Trades) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) TradeService(org.knowm.xchange.service.trade.TradeService) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) OrderQueryParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair) Assert(org.knowm.xchange.utils.Assert) OpenOrdersParams(org.knowm.xchange.service.trade.params.orders.OpenOrdersParams) Optional(java.util.Optional) MarketOrder(org.knowm.xchange.dto.trade.MarketOrder) CancelOrderParams(org.knowm.xchange.service.trade.params.CancelOrderParams) BinanceErrorAdapter(org.knowm.xchange.binance.BinanceErrorAdapter) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) BinanceTrade(org.knowm.xchange.binance.dto.trade.BinanceTrade) TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) TradeHistoryParamsIdSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsIdSpan) UserTrade(org.knowm.xchange.dto.trade.UserTrade) UserTrades(org.knowm.xchange.dto.trade.UserTrades) BinanceException(org.knowm.xchange.binance.dto.BinanceException) NotAvailableFromExchangeException(org.knowm.xchange.exceptions.NotAvailableFromExchangeException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair) CancelOrderByCurrencyPair(org.knowm.xchange.service.trade.params.CancelOrderByCurrencyPair) OpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OpenOrdersParamCurrencyPair) DefaultOpenOrdersParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.DefaultOpenOrdersParamCurrencyPair) OrderQueryParamCurrencyPair(org.knowm.xchange.service.trade.params.orders.OrderQueryParamCurrencyPair) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 98 with Currency

use of org.knowm.xchange.currency.Currency in project XChange by knowm.

the class BitmexExchange method updateExchangeMetaData.

public void updateExchangeMetaData() {
    List<BitmexTicker> tickers = ((BitmexMarketDataServiceRaw) marketDataService).getActiveTickers();
    List<CurrencyPair> activeCurrencyPairs = new ArrayList<>();
    Set<Currency> activeCurrencies = new HashSet<>();
    tickers.forEach(ticker -> collectCurrenciesAndPairs(ticker, activeCurrencyPairs, activeCurrencies));
    Map<CurrencyPair, CurrencyPairMetaData> pairsMap = exchangeMetaData.getCurrencyPairs();
    Map<Currency, CurrencyMetaData> currenciesMap = exchangeMetaData.getCurrencies();
    // Remove pairs that are no-longer in use
    pairsMap.keySet().retainAll(activeCurrencyPairs);
    // Remove currencies that are no-longer in use
    currenciesMap.keySet().retainAll(activeCurrencies);
    // Add missing pairs and currencies
    activeCurrencyPairs.forEach(cp -> {
        if (!pairsMap.containsKey(cp)) {
            pairsMap.put(cp, new CurrencyPairMetaData(null, BigDecimal.ONE, null, getPriceScale(tickers, cp), null));
        }
        if (!currenciesMap.containsKey(cp.base)) {
            currenciesMap.put(cp.base, null);
        }
        if (!currenciesMap.containsKey(cp.counter)) {
            currenciesMap.put(cp.counter, null);
        }
    });
}
Also used : CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) BitmexMarketDataServiceRaw(org.knowm.xchange.bitmex.service.BitmexMarketDataServiceRaw) ArrayList(java.util.ArrayList) BitmexTicker(org.knowm.xchange.bitmex.dto.account.BitmexTicker) Currency(org.knowm.xchange.currency.Currency) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) HashSet(java.util.HashSet)

Example 99 with Currency

use of org.knowm.xchange.currency.Currency in project XChange by knowm.

the class BitstampAdapters method adaptOrder.

/**
 * There is no method to discern market versus limit order type - so this returns a generic
 * BitstampGenericOrder as a status
 *
 * @param bitstampOrderStatusResponse
 * @param exchangeSymbols
 * @return
 */
public static BitstampGenericOrder adaptOrder(String orderId, BitstampOrderStatusResponse bitstampOrderStatusResponse, List<CurrencyPair> exchangeSymbols) {
    BitstampOrderTransaction[] bitstampTransactions = bitstampOrderStatusResponse.getTransactions();
    // Use only the first transaction, because we assume that for a single order id all transactions
    // will
    // be of the same currency pair
    Order.OrderStatus orderStatus = adaptOrderStatus(bitstampOrderStatusResponse.getStatus());
    if (bitstampTransactions.length > 0) {
        CurrencyPair currencyPair = adaptCurrencyPair(bitstampTransactions[0], exchangeSymbols);
        Date date = bitstampTransactions[0].getDatetime();
        BigDecimal averagePrice = Arrays.stream(bitstampTransactions).map(t -> t.getPrice()).reduce((x, y) -> x.add(y)).get().divide(BigDecimal.valueOf(bitstampTransactions.length), 2);
        BigDecimal cumulativeAmount = Arrays.stream(bitstampTransactions).map(t -> getBaseCurrencyAmountFromBitstampTransaction(t, currencyPair)).reduce((x, y) -> x.add(y)).get();
        BigDecimal totalFee = Arrays.stream(bitstampTransactions).map(t -> t.getFee()).reduce((x, y) -> x.add(y)).get();
        return new BitstampGenericOrder(// not discernable from response data
        null, // not discernable from the data
        null, currencyPair, orderId, date, averagePrice, cumulativeAmount, totalFee, orderStatus);
    } else {
        return new BitstampGenericOrder(// not discernable from response data
        null, // not discernable from the data
        null, // not discernable from the data
        null, orderId, // not discernable from the data
        null, // not discernable from the data
        new BigDecimal("0.0"), // not discernable from the data
        new BigDecimal("0.0"), // not discernable from the data
        new BigDecimal("0.0"), orderStatus);
    }
}
Also used : BitstampGenericOrder(org.knowm.xchange.bitstamp.order.dto.BitstampGenericOrder) Order(org.knowm.xchange.dto.Order) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) BitstampBalance(org.knowm.xchange.bitstamp.dto.account.BitstampBalance) Arrays(java.util.Arrays) BitstampOrderBook(org.knowm.xchange.bitstamp.dto.marketdata.BitstampOrderBook) OrderType(org.knowm.xchange.dto.Order.OrderType) Date(java.util.Date) HashMap(java.util.HashMap) BitstampTicker(org.knowm.xchange.bitstamp.dto.marketdata.BitstampTicker) FundingRecord(org.knowm.xchange.dto.account.FundingRecord) OrderBook(org.knowm.xchange.dto.marketdata.OrderBook) Balance(org.knowm.xchange.dto.account.Balance) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) TradeSortType(org.knowm.xchange.dto.marketdata.Trades.TradeSortType) BitstampGenericOrder(org.knowm.xchange.bitstamp.order.dto.BitstampGenericOrder) BigDecimal(java.math.BigDecimal) Order(org.knowm.xchange.dto.Order) CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) Map(java.util.Map) ExchangeMetaData(org.knowm.xchange.dto.meta.ExchangeMetaData) NotYetImplementedForExchangeException(org.knowm.xchange.exceptions.NotYetImplementedForExchangeException) AccountInfo(org.knowm.xchange.dto.account.AccountInfo) LimitOrder(org.knowm.xchange.dto.trade.LimitOrder) Ticker(org.knowm.xchange.dto.marketdata.Ticker) Wallet(org.knowm.xchange.dto.account.Wallet) Collection(java.util.Collection) BitstampPairInfo(org.knowm.xchange.bitstamp.dto.marketdata.BitstampPairInfo) ZERO(java.math.BigDecimal.ZERO) BitstampOrderStatusResponse(org.knowm.xchange.bitstamp.dto.trade.BitstampOrderStatusResponse) UserTrades(org.knowm.xchange.dto.trade.UserTrades) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BitstampOrderStatus(org.knowm.xchange.bitstamp.dto.trade.BitstampOrderStatus) Trades(org.knowm.xchange.dto.marketdata.Trades) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) BitstampTransaction(org.knowm.xchange.bitstamp.dto.marketdata.BitstampTransaction) BitstampUserTransaction(org.knowm.xchange.bitstamp.dto.trade.BitstampUserTransaction) List(java.util.List) Currency(org.knowm.xchange.currency.Currency) BitstampOrderTransaction(org.knowm.xchange.bitstamp.dto.trade.BitstampOrderTransaction) Trade(org.knowm.xchange.dto.marketdata.Trade) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) DateUtils(org.knowm.xchange.utils.DateUtils) BitstampOrderTransaction(org.knowm.xchange.bitstamp.dto.trade.BitstampOrderTransaction) Date(java.util.Date) BigDecimal(java.math.BigDecimal) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) BitstampGenericOrder(org.knowm.xchange.bitstamp.order.dto.BitstampGenericOrder)

Example 100 with Currency

use of org.knowm.xchange.currency.Currency in project XChange by knowm.

the class BitmexAccountService method getFundingHistory.

@Override
public List<FundingRecord> getFundingHistory(TradeHistoryParams params) {
    Currency currency = null;
    Integer count = null;
    Long start = null;
    if (params instanceof TradeHistoryParamCurrency) {
        currency = ((TradeHistoryParamCurrency) params).getCurrency();
    } else {
        throw new ExchangeException("Currency must be supplied");
    }
    if (params instanceof TradeHistoryParamLimit) {
        count = ((TradeHistoryParamLimit) params).getLimit();
    }
    if (params instanceof TradeHistoryParamOffset) {
        start = ((TradeHistoryParamOffset) params).getOffset();
    }
    return getBitmexWalletHistory(currency, count, start).stream().map(BitmexAdapters::adaptFundingRecord).collect(Collectors.toList());
}
Also used : TradeHistoryParamOffset(org.knowm.xchange.service.trade.params.TradeHistoryParamOffset) TradeHistoryParamCurrency(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrency) Currency(org.knowm.xchange.currency.Currency) BitmexAdapters.adaptCurrency(org.knowm.xchange.bitmex.BitmexAdapters.adaptCurrency) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) TradeHistoryParamCurrency(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrency) TradeHistoryParamLimit(org.knowm.xchange.service.trade.params.TradeHistoryParamLimit)

Aggregations

Currency (org.knowm.xchange.currency.Currency)159 CurrencyPair (org.knowm.xchange.currency.CurrencyPair)75 BigDecimal (java.math.BigDecimal)67 ArrayList (java.util.ArrayList)60 Balance (org.knowm.xchange.dto.account.Balance)38 CurrencyMetaData (org.knowm.xchange.dto.meta.CurrencyMetaData)35 CurrencyPairMetaData (org.knowm.xchange.dto.meta.CurrencyPairMetaData)32 HashMap (java.util.HashMap)28 Test (org.junit.Test)27 Map (java.util.Map)22 Date (java.util.Date)21 AccountInfo (org.knowm.xchange.dto.account.AccountInfo)19 FundingRecord (org.knowm.xchange.dto.account.FundingRecord)19 IOException (java.io.IOException)18 ExchangeMetaData (org.knowm.xchange.dto.meta.ExchangeMetaData)18 List (java.util.List)17 ExchangeException (org.knowm.xchange.exceptions.ExchangeException)15 OrderType (org.knowm.xchange.dto.Order.OrderType)14 UserTrade (org.knowm.xchange.dto.trade.UserTrade)14 Collectors (java.util.stream.Collectors)11