public void test_convertedTo_rateProvider() { FxRateProvider provider = (ccy1, ccy2) -> 2.5d; assertEquals(CCY_AMOUNT.convertedTo(CCY2, provider), CurrencyAmount.of(CCY2, AMT1 * 2.5d)); assertEquals(CCY_AMOUNT.convertedTo(CCY1, provider), CCY_AMOUNT); }
public void test_convertedTo_explicitRate() { assertEquals(CCY_AMOUNT.convertedTo(CCY2, 2.5d), CurrencyAmount.of(CCY2, AMT1 * 2.5d)); assertEquals(CCY_AMOUNT.convertedTo(CCY1, 1d), CCY_AMOUNT); assertThrowsIllegalArg(() -> CCY_AMOUNT.convertedTo(CCY1, 1.5d)); }
@Override public SwapLegAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { CurrencyAmount convertedAmount = amount.convertedTo(resultCurrency, rateProvider); return this.toBuilder().amount(convertedAmount).build(); }
/** * Converts this cash flow to an equivalent amount in the specified currency. * <p> * The result will have both the present and forecast value expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public CashFlow convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (presentValue.getCurrency().equals(resultCurrency) && forecastValue.getCurrency().equals(resultCurrency)) { return this; } CurrencyAmount pv = presentValue.convertedTo(resultCurrency, rateProvider); CurrencyAmount fv = forecastValue.convertedTo(resultCurrency, rateProvider); return new CashFlow(paymentDate, pv, fv, discountFactor); }
@Override public PortfolioItemSummary summarize() { // Pay USD 1mm @ USD/CNY 6.62 NDF : 21Jan18 StringBuilder buf = new StringBuilder(64); CurrencyAmount notional = product.getSettlementCurrencyNotional(); CurrencyAmount counter = notional.convertedTo(product.getNonDeliverableCurrency(), product.getAgreedFxRate()); buf.append(SummarizerUtils.fx(notional, counter)); buf.append(" NDF : "); buf.append(SummarizerUtils.date(product.getPaymentDate())); return SummarizerUtils.summary( this, ProductType.FX_NDF, buf.toString(), product.getSettlementCurrency(), product.getNonDeliverableCurrency()); }
/** * Converts this payment to an equivalent payment in the specified currency. * <p> * The result will be expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public Payment convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (getCurrency().equals(resultCurrency)) { return this; } return Payment.of(value.convertedTo(resultCurrency, rateProvider), date); }
/** * Converts this amount to an equivalent amount the specified currency. * <p> * The result will be expressed in terms of the given currency. * If conversion is needed, the provider will be used to supply the FX rate. * * @param resultCurrency the currency of the result * @param rateProvider the provider of FX rates * @return the converted instance, which should be expressed in the specified currency * @throws RuntimeException if no FX rate could be found */ @Override public CurrencyAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (amounts.size() == 1) { return amounts.first().convertedTo(resultCurrency, rateProvider); } double total = 0d; for (CurrencyAmount amount : amounts) { total += rateProvider.convert(amount.getAmount(), amount.getCurrency(), resultCurrency); } return CurrencyAmount.of(resultCurrency, total); }
private static FxSingle create( CurrencyAmount amount, FxRate fxRate, LocalDate paymentDate, BusinessDayAdjustment paymentDateAdjustment) { ArgChecker.notNull(amount, "amount"); ArgChecker.notNull(fxRate, "fxRate"); ArgChecker.notNull(paymentDate, "paymentDate"); CurrencyPair pair = fxRate.getPair(); if (!pair.contains(amount.getCurrency())) { throw new IllegalArgumentException(Messages.format( "FxRate '{}' and CurrencyAmount '{}' must have a currency in common", fxRate, amount)); } Currency currency2 = pair.getBase().equals(amount.getCurrency()) ? pair.getCounter() : pair.getBase(); CurrencyAmount amountCurrency2 = amount.convertedTo(currency2, fxRate).negated(); return create(amount, amountCurrency2, paymentDate, paymentDateAdjustment); }
/** * Creates an {@code ResolvedFxSingle} using a rate. * <p> * This create an FX specifying a value date, notional in one currency, the second currency * and the FX rate between the two. * The currencies of the payments must differ. * <p> * This factory identifies the currency pair of the exchange and assigns the payments * to match the base or counter currency of the standardized currency pair. * For example, a EUR/USD exchange always has EUR as the base payment and USD as the counter payment. * <p> * No payment date adjustments apply. * * @param amountCurrency1 the amount of the near leg in the first currency * @param fxRate the near FX rate * @param paymentDate date that the FX settles * @return the resolved foreign exchange transaction */ public static ResolvedFxSingle of(CurrencyAmount amountCurrency1, FxRate fxRate, LocalDate paymentDate) { CurrencyPair pair = fxRate.getPair(); ArgChecker.isTrue(pair.contains(amountCurrency1.getCurrency())); Currency currency2 = pair.getBase().equals(amountCurrency1.getCurrency()) ? pair.getCounter() : pair.getBase(); CurrencyAmount amountCurrency2 = amountCurrency1.convertedTo(currency2, fxRate).negated(); return ResolvedFxSingle.of(Payment.of(amountCurrency1, paymentDate), Payment.of(amountCurrency2, paymentDate)); }