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);
}
}
}
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);
}
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);
}
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);
}
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());
}
Aggregations