Search in sources :

Example 21 with Currency

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

the class BinanceExchange method postInit.

protected void postInit(Map<String, AssetDetail> assetDetailMap) {
    // populate currency pair keys only, exchange does not provide any other metadata for download
    Map<CurrencyPair, CurrencyPairMetaData> currencyPairs = exchangeMetaData.getCurrencyPairs();
    Map<Currency, CurrencyMetaData> currencies = exchangeMetaData.getCurrencies();
    // Clear all hardcoded currencies when loading dynamically from exchange.
    if (assetDetailMap != null) {
        currencies.clear();
    }
    Symbol[] symbols = exchangeInfo.getSymbols();
    for (Symbol symbol : symbols) {
        if (symbol.getStatus().equals("TRADING")) {
            // Symbols which are trading
            int basePrecision = Integer.parseInt(symbol.getBaseAssetPrecision());
            int counterPrecision = Integer.parseInt(symbol.getQuotePrecision());
            int pairPrecision = 8;
            int amountPrecision = 8;
            BigDecimal minQty = null;
            BigDecimal maxQty = null;
            BigDecimal stepSize = null;
            BigDecimal counterMinQty = null;
            BigDecimal counterMaxQty = null;
            Filter[] filters = symbol.getFilters();
            CurrencyPair currentCurrencyPair = new CurrencyPair(symbol.getBaseAsset(), symbol.getQuoteAsset());
            for (Filter filter : filters) {
                if (filter.getFilterType().equals("PRICE_FILTER")) {
                    pairPrecision = Math.min(pairPrecision, numberOfDecimals(filter.getTickSize()));
                    counterMaxQty = new BigDecimal(filter.getMaxPrice()).stripTrailingZeros();
                } else if (filter.getFilterType().equals("LOT_SIZE")) {
                    amountPrecision = Math.min(amountPrecision, numberOfDecimals(filter.getStepSize()));
                    minQty = new BigDecimal(filter.getMinQty()).stripTrailingZeros();
                    maxQty = new BigDecimal(filter.getMaxQty()).stripTrailingZeros();
                    stepSize = new BigDecimal(filter.getStepSize()).stripTrailingZeros();
                } else if (filter.getFilterType().equals("MIN_NOTIONAL")) {
                    counterMinQty = new BigDecimal(filter.getMinNotional()).stripTrailingZeros();
                }
            }
            boolean marketOrderAllowed = Arrays.asList(symbol.getOrderTypes()).contains("MARKET");
            currencyPairs.put(currentCurrencyPair, new CurrencyPairMetaData(// Trading fee at Binance is 0.1 %
            new BigDecimal("0.1"), // Min amount
            minQty, // Max amount
            maxQty, counterMinQty, counterMaxQty, // base precision
            amountPrecision, // counter precision
            pairPrecision, null, null, /* TODO get fee tiers, although this is not necessary now
                      because their API returns current fee directly */
            stepSize, null, marketOrderAllowed));
            Currency baseCurrency = currentCurrencyPair.base;
            CurrencyMetaData baseCurrencyMetaData = BinanceAdapters.adaptCurrencyMetaData(currencies, baseCurrency, assetDetailMap, basePrecision);
            currencies.put(baseCurrency, baseCurrencyMetaData);
            Currency counterCurrency = currentCurrencyPair.counter;
            CurrencyMetaData counterCurrencyMetaData = BinanceAdapters.adaptCurrencyMetaData(currencies, counterCurrency, assetDetailMap, counterPrecision);
            currencies.put(counterCurrency, counterCurrencyMetaData);
        }
    }
}
Also used : CurrencyPairMetaData(org.knowm.xchange.dto.meta.CurrencyPairMetaData) CurrencyMetaData(org.knowm.xchange.dto.meta.CurrencyMetaData) Symbol(org.knowm.xchange.binance.dto.meta.exchangeinfo.Symbol) BigDecimal(java.math.BigDecimal) Filter(org.knowm.xchange.binance.dto.meta.exchangeinfo.Filter) Currency(org.knowm.xchange.currency.Currency) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 22 with Currency

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

the class CurrencyPairDeserializer method getCurrencyPairFromString.

public static CurrencyPair getCurrencyPairFromString(String currencyPairString) {
    if (currencyPairString == null || currencyPairString.isEmpty()) {
        return null;
    }
    /*
     * Preserve case if exchange is sending mixed-case, otherwise toUpperCase()
     */
    final boolean isMixedCase = currencyPairString.matches(".*[a-z]+.*") && currencyPairString.matches(".*[A-Z]+.*");
    if (!isMixedCase) {
        currencyPairString = currencyPairString.toUpperCase();
    }
    /*
     * Assume all symbols are alphanumeric; anything else is a separator
     */
    final String[] symbols = currencyPairString.split("[^a-zA-Z0-9]");
    if (symbols.length == 2) {
        return new CurrencyPair(symbols[0], symbols[1]);
    }
    /*
     * The common case of two 3-character symbols (eg: "BTCUSD")
     */
    if (currencyPairString.length() == 6) {
        final String tradeCurrency = currencyPairString.substring(0, 3);
        final String priceCurrency = currencyPairString.substring(3);
        return new CurrencyPair(tradeCurrency, priceCurrency);
    }
    /*
     * Last-ditch effort to obtain the correct CurrencyPair (eg: "DOGEBTC", "BCBTC", or even "USDEUSD")
     */
    int bestGuess = currencyPairString.length() / 2;
    int bestLength = 0;
    for (int i = 1; i < currencyPairString.length() - 1; ++i) {
        final Currency tradeCurrency = Currency.getInstanceNoCreate(currencyPairString.substring(0, i));
        final Currency priceCurrency = Currency.getInstanceNoCreate(currencyPairString.substring(i));
        if (tradeCurrency != null) {
            if (priceCurrency != null) {
                return new CurrencyPair(tradeCurrency, priceCurrency);
            } else if (i > bestLength) {
                bestLength = i;
                bestGuess = i;
            }
        } else if (priceCurrency != null && currencyPairString.length() - i > bestLength) {
            bestLength = currencyPairString.length() - i;
            bestGuess = i;
        }
    }
    final String tradeCurrency = currencyPairString.substring(0, bestGuess);
    final String priceCurrency = currencyPairString.substring(bestGuess);
    return new CurrencyPair(tradeCurrency, priceCurrency);
}
Also used : Currency(org.knowm.xchange.currency.Currency) CurrencyPair(org.knowm.xchange.currency.CurrencyPair)

Example 23 with Currency

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

the class ExmoAdapters method adaptBalance.

public static Balance adaptBalance(Map<String, String> balances, Map<String, String> reserved, String ccy) {
    Currency currency = Currency.getInstance(ccy);
    BigDecimal available = new BigDecimal(balances.get(ccy));
    BigDecimal frozen = new BigDecimal(reserved.get(ccy));
    return new Balance(currency, available.add(frozen), available, frozen);
}
Also used : Currency(org.knowm.xchange.currency.Currency) Balance(org.knowm.xchange.dto.account.Balance) BigDecimal(java.math.BigDecimal)

Example 24 with Currency

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

the class LunoTradeService method getTradeHistory.

@Override
public UserTrades getTradeHistory(TradeHistoryParams params) throws ExchangeException, IOException {
    if (!(params instanceof TradeHistoryParamCurrencyPair)) {
        throw new ExchangeException("THe currency pair is mandatory in order to get user trades.");
    }
    CurrencyPair currencyPair = ((TradeHistoryParamCurrencyPair) params).getCurrencyPair();
    Long since = null;
    if (params instanceof TradeHistoryParamsTimeSpan) {
        since = ((TradeHistoryParamsTimeSpan) params).getStartTime().getTime();
    }
    Integer limit = null;
    if (params instanceof TradeHistoryParamLimit) {
        limit = ((TradeHistoryParamLimit) params).getLimit();
    }
    LunoUserTrades lunoTrades = lunoAPI.listTrades(LunoUtil.toLunoPair(currencyPair), since, limit);
    List<UserTrade> trades = new ArrayList<>();
    for (org.knowm.xchange.luno.dto.trade.LunoUserTrades.UserTrade t : lunoTrades.getTrades()) {
        final CurrencyPair pair = LunoUtil.fromLunoPair(t.pair);
        // currently there is no trade id!
        final String tradeId = null;
        final BigDecimal feeAmount;
        final Currency feeCurrency;
        if (t.feeBase.compareTo(BigDecimal.ZERO) > 0) {
            feeAmount = t.feeBase;
            feeCurrency = pair.base;
        } else {
            feeAmount = t.feeCounter;
            feeCurrency = pair.counter;
        }
        trades.add(new UserTrade.Builder().type(t.buy ? OrderType.BID : OrderType.ASK).originalAmount(t.volume).currencyPair(pair).price(t.price).timestamp(t.getTimestamp()).id(tradeId).orderId(t.orderId).feeAmount(feeAmount).feeCurrency(feeCurrency).build());
    }
    return new UserTrades(trades, TradeSortType.SortByTimestamp);
}
Also used : TradeHistoryParamsTimeSpan(org.knowm.xchange.service.trade.params.TradeHistoryParamsTimeSpan) LunoUserTrades(org.knowm.xchange.luno.dto.trade.LunoUserTrades) ArrayList(java.util.ArrayList) Exchange(org.knowm.xchange.Exchange) UserTrade(org.knowm.xchange.dto.trade.UserTrade) BigDecimal(java.math.BigDecimal) UserTrades(org.knowm.xchange.dto.trade.UserTrades) LunoUserTrades(org.knowm.xchange.luno.dto.trade.LunoUserTrades) Currency(org.knowm.xchange.currency.Currency) NotAvailableFromExchangeException(org.knowm.xchange.exceptions.NotAvailableFromExchangeException) 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) CurrencyPair(org.knowm.xchange.currency.CurrencyPair) TradeHistoryParamCurrencyPair(org.knowm.xchange.service.trade.params.TradeHistoryParamCurrencyPair)

Example 25 with Currency

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

the class TheRockAdapters method adaptAccountInfo.

public static AccountInfo adaptAccountInfo(List<TheRockBalance> trBalances, String userName) {
    ArrayList<Balance> balances = new ArrayList<>(trBalances.size());
    for (TheRockBalance blc : trBalances) {
        Currency currency = Currency.getInstance(blc.getCurrency());
        balances.add(new Balance(currency, blc.getBalance(), blc.getTradingBalance()));
    }
    return new AccountInfo(userName, Wallet.Builder.from(balances).id("spot").build());
}
Also used : TheRockBalance(org.knowm.xchange.therock.dto.account.TheRockBalance) Currency(org.knowm.xchange.currency.Currency) ArrayList(java.util.ArrayList) Balance(org.knowm.xchange.dto.account.Balance) TheRockBalance(org.knowm.xchange.therock.dto.account.TheRockBalance) AccountInfo(org.knowm.xchange.dto.account.AccountInfo)

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