public void sendPayment(Author author, BigDecimal amount, String url) throws TransferFailedException { try { String note = "Commit payment:\n__" + author.getUsername() + "__ " + url; Transaction transaction = new Transaction(); transaction.setTo(author.getEmail()); transaction.setAmount(Money.of(CurrencyUnit.of("BTC"), amount, RoundingMode.DOWN)); transaction.setNotes(note); Transaction response = coinbase.sendMoney(transaction); if (response.getStatus() != Transaction.Status.COMPLETE) { throw new TransferFailedException(); } } catch (CoinbaseException | IOException e) { throw new TransferFailedException(e); } }
@Override public CurrencyUnit fromNonNullString(String s) { try { return CurrencyUnit.ofNumericCode(s); } catch (IllegalCurrencyException e) { return CurrencyUnit.of(s); } }
@Override public CurrencyUnit fromNonNullValue(String s) { return CurrencyUnit.of(s); }
/** * Registers a country allowing it to be used. * <p> * This method is called by {@link #registerCurrencies()} to perform the * actual creation of a country. * * @param countryCode the country code, not null * @param currencyCode the currency code, not null */ protected final void registerCountry(String countryCode, String currencyCode) { CurrencyUnit.registerCountry(countryCode, CurrencyUnit.of(currencyCode)); }
/** * Obtains an instance of {@code CurrencyUnit} matching the specified JDK currency. * <p> * This converts the JDK currency instance to a currency unit using the code. * * @param currency the currency, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ public static CurrencyUnit of(Currency currency) { MoneyUtils.checkNotNull(currency, "Currency must not be null"); return of(currency.getCurrencyCode()); }
public static Money getAsMoney(String string) { if (!StringUtils.hasText(string)) { return null; } String[] split = string.split(" "); return Money.of(CurrencyUnit.of(split[1]), parseBigDecimal(split[0])); }
public static long toMinorUnits(final String currencyIsoCode, final BigDecimal amountBD, final RoundingMode roundingMode) { // The payment amount specified in minor units, without the decimal separator final CurrencyUnit currencyUnit = CurrencyUnit.of(currencyIsoCode); return Money.of(currencyUnit, amountBD, roundingMode).getAmountMinorLong(); } }
private CurrencyUnit readCurrency(ObjectInput in) throws IOException { String code = in.readUTF(); CurrencyUnit singletonCurrency = CurrencyUnit.of(code); if (singletonCurrency.getNumericCode() != in.readShort()) { throw new InvalidObjectException("Deserialization found a mismatch in the numeric code for currency " + code); } if (singletonCurrency.getDecimalPlaces() != in.readShort()) { throw new InvalidObjectException("Deserialization found a mismatch in the decimal places for currency " + code); } return singletonCurrency; }
@Override public void applyConfiguration(SessionFactory sessionFactory) { CurrencyUnitConfigured columnMapper = (CurrencyUnitConfigured) getColumnMapper(); if (currencyUnit == null) { String currencyString = null; if (parameterValues != null) { currencyString = parameterValues.getProperty("currencyCode"); } if (currencyString == null) { currencyString = ConfigurationHelper.getProperty("currencyCode"); } if (currencyString != null) { currencyUnit = CurrencyUnit.of(currencyString); } else { throw new IllegalStateException(getClass().getSimpleName() + " requires currencyCode to be defined as a parameter, or the jadira.usertype.currencyCode Hibernate property to be defined"); } } columnMapper.setCurrencyUnit(currencyUnit); } }
throw new IllegalArgumentException("Money amount '" + moneyStr + "' cannot be parsed"); return BigMoney.of(CurrencyUnit.of(currStr), new BigDecimal(amountStr));
/** * Adapts a Transaction[] to a Trades Object */ public static Trades adaptTrades(BitfloorTransaction[] transactions, String currency, String tradableIdentifier) { List<Trade> trades = new ArrayList<Trade>(); for (BitfloorTransaction tx : transactions) { trades.add(new Trade(null, tx.getAmount(), tradableIdentifier, currency, BigMoney.of(CurrencyUnit.of(currency), tx.getPrice()), tx.getTimestamp())); } return new Trades(trades); }
public void calcProfitLoss() { BigMoney equivBTCStartBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney equivBTCEndBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney startBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigMoney endBal = BigMoney.zero(CurrencyUnit.of("BTC")); BigDecimal startRate = BigDecimal.ZERO; BigDecimal endRate = BigDecimal.ZERO; endBal = balances.get(currency).get(balances.get(currency).size() - 1); if (currency.equals(CurrencyUnit.of("BTC"))) { equivBTCStartBal = equivBTCStartBal.plus(startBal); equivBTCEndBal = equivBTCEndBal.plus(endBal); endRate=rates.get(currency).get(rates.get(currency).size() - 1).getAmount(); equivBTCStartBal = equivBTCStartBal.plus(startBal.convertedTo(CurrencyUnit.of("BTC"),BigDecimal.ONE.divide(startRate,16,RoundingMode.HALF_EVEN))); equivBTCEndBal = equivBTCEndBal.plus(endBal.convertedTo(CurrencyUnit.of("BTC"),BigDecimal.ONE.divide(endRate,16,RoundingMode.HALF_EVEN))); } else { log.info("Not enough "+currency.getCode()+" ticker data collected yet to calculate profit/loss");
public CurrencyRepresentation(Currency currency, Locale locale) { this.symbol = CURRENCY_FORMATTER.withLocale(locale).print( Money.of(CurrencyUnit.of(currency), BigDecimal.TEN, RoundingMode.HALF_EVEN)); this.localSymbol = MoneyUtil.getLocalSymbol(currency); }
public PriceRepresentation(BigDecimal amount, Currency currency, Locale locale) { MoneyFormatter formatter = new MoneyFormatterBuilder().appendAmount(MoneyAmountStyle.of(locale)).toFormatter(); CurrencyUnit currencyUnit = CurrencyUnit.of(currency); this.amount = formatter.withLocale(locale) .print(Money.of(currencyUnit, amount, RoundingMode.HALF_EVEN)); this.currency = new CurrencyRepresentation(currency, locale); if (amount.doubleValue() == amount.intValue()) { this.amountCompact = "" + amount.intValue(); } else { this.amountCompact = this.amount; } }
/** * Adapts a BitcoinCentralTrade[] to Trades DTO * * @param bitcoinCentralTrades * @param currency * @param tradableIdentifier * @return */ // TODO implement timestamp after bitcoin central provides a better timestamp public static Trades adaptTrades(BitcoinCentralTrade[] bitcoinCentralTrades, String currency, String tradableIdentifier) { List<Trade> trades = new ArrayList<Trade>(); for (BitcoinCentralTrade bitcoinCentralTrade : bitcoinCentralTrades) { trades.add(new Trade(null, bitcoinCentralTrade.getTradedBtc(), tradableIdentifier, bitcoinCentralTrade.getCurrency(), BigMoney.of(CurrencyUnit.of(bitcoinCentralTrade.getCurrency()), bitcoinCentralTrade.getPpc()), null)); } return new Trades(trades); }
public TrendTradingAgent(TrendObserver observer, String exchangeName) { log = LoggerFactory.getLogger(TrendTradingAgent.class); this.observer = observer; this.exchangeName = exchangeName; tradeService = ExchangeManager.getInstance(exchangeName).getExchange().getPollingTradeService(); localCurrency = observer.getCurrency(); maxBTC = BigMoney.of(CurrencyUnit.of("BTC"),new BigDecimal(Application.getInstance().getConfig("MaxBTC"))); maxLocal = BigMoney.of(localCurrency,new BigDecimal(Application.getInstance().getConfig("MaxLocal"))); minBTC = BigMoney.of(CurrencyUnit.of("BTC"),new BigDecimal(Application.getInstance().getConfig("MinBTC"))); minLocal = BigMoney.of(localCurrency,new BigDecimal(Application.getInstance().getConfig("MinLocal"))); maxWeight = Double.valueOf(Application.getInstance().getConfig("MaxLoss")); riskAlgorithm = Integer.valueOf(Application.getInstance().getConfig("RiskAlgorithm")); asksInARow = ExchangeManager.getInstance(exchangeName).getAsksInARow(); bidsInARow = ExchangeManager.getInstance(exchangeName).getBidsInARow(); if(!asksInARow.containsKey(localCurrency)){ asksInARow.put(localCurrency,new Double(0)); } if(!bidsInARow.containsKey(localCurrency)){ bidsInARow.put(localCurrency,new Double(0)); } }
private String getFormattedAmountByLocaleAndInvoiceCurrency(final BigDecimal amount) { final String invoiceCurrencyCode = invoice.getCurrency().toString(); final CurrencyUnit currencyUnit = CurrencyUnit.of(invoiceCurrencyCode); final DecimalFormat numberFormatter = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale); final DecimalFormatSymbols dfs = numberFormatter.getDecimalFormatSymbols(); dfs.setInternationalCurrencySymbol(currencyUnit.getCurrencyCode()); try { Currency currency = Currency.fromCode(invoiceCurrencyCode); dfs.setCurrencySymbol(currency.getSymbol()); } catch (final IllegalArgumentException e) { dfs.setCurrencySymbol(currencyUnit.getSymbol(locale)); } numberFormatter.setDecimalFormatSymbols(dfs); numberFormatter.setMinimumFractionDigits(currencyUnit.getDefaultFractionDigits()); numberFormatter.setMaximumFractionDigits(currencyUnit.getDefaultFractionDigits()); return numberFormatter.format(amount.doubleValue()); }
@Override public void parse(MoneyParseContext context) { int endPos = context.getIndex() + 3; if (endPos > context.getTextLength()) { context.setError(); } else { String code = context.getTextSubstring(context.getIndex(), endPos); try { context.setCurrency(CurrencyUnit.of(code)); context.setIndex(endPos); } catch (IllegalCurrencyException ex) { context.setError(); } } } },