/** * 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); }
public void test_presentValue_position() { CurrencyAmount computedTrade = TRADE_PRICER.presentValue(POSITION, PROVIDER); CurrencyAmount computedProduct = PRODUCT_PRICER.presentValue(PRODUCT, PROVIDER, VAL_DATE); assertEquals(computedTrade.getAmount(), computedProduct.multipliedBy(QUANTITY).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_negated() { assertEquals(CCY_AMOUNT.negated(), CCY_AMOUNT_NEGATIVE); assertEquals(CCY_AMOUNT_NEGATIVE.negated(), CCY_AMOUNT); assertEquals(CurrencyAmount.zero(Currency.USD), CurrencyAmount.zero(Currency.USD).negated()); assertEquals(CurrencyAmount.of(Currency.USD, -0d).negated(), CurrencyAmount.zero(Currency.USD)); }
/** * Obtains an instance of {@code Money} for the specified {@link CurrencyAmount}. * * @param currencyAmount the instance of {@link CurrencyAmount} wrapping the currency and amount. * @return the currency amount */ public static Money of(CurrencyAmount currencyAmount) { return new Money(currencyAmount.getCurrency(), BigDecimal.valueOf(currencyAmount.getAmount())); }
private CurrencyAmount presentValueFromProductPresentValue( ResolvedFixedCouponBondTrade trade, LegalEntityDiscountingProvider provider, CurrencyAmount productPresentValue) { CurrencyAmount pvProduct = productPresentValue.multipliedBy(trade.getQuantity()); CurrencyAmount pvPayment = presentValuePayment(trade, provider); return pvProduct.plus(pvPayment); }
/** * 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); }
public void test_presentValueWithZSpread_periodic() { CurrencyAmount computedTrade = TRADE_PRICER.presentValueWithZSpread(TRADE, PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); CurrencyAmount computedProduct = PRODUCT_PRICER.presentValueWithZSpread( PRODUCT, PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR, SETTLEMENT); CurrencyAmount pvPayment = PRICER_NOMINAL.presentValue(UPFRONT_PAYMENT, ZeroRateDiscountFactors.of(EUR, VAL_DATE, CURVE_REPO)); assertEquals(computedTrade.getAmount(), computedProduct.multipliedBy(QUANTITY).plus(pvPayment).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_of_String() { CurrencyAmount test = CurrencyAmount.of("USD", AMT1); assertEquals(test.getCurrency(), Currency.USD); assertEquals(test.getAmount(), AMT1, 0); }
public void test_of_rate_bothZero() { FxSingle test = FxSingle.of(CurrencyAmount.zero(GBP), FxRate.of(USD, GBP, 1.6d), DATE_2015_06_30); assertEquals(test.getBaseCurrencyAmount(), CurrencyAmount.zero(GBP)); assertEquals(test.getCounterCurrencyAmount().getAmount(), CurrencyAmount.zero(USD).getAmount(), 1e-12); assertEquals(test.getPaymentDate(), DATE_2015_06_30); assertEquals(test.getCurrencyPair(), CurrencyPair.of(GBP, USD)); assertEquals(test.getReceiveCurrencyAmount(), CurrencyAmount.of(USD, 0d)); }
private double forecastValueFwdSensitivity(ResolvedFra fra, double forwardRate, double eps) { RateComputationFn<RateComputation> obsFuncNew = mock(RateComputationFn.class); RatesProvider provNew = mock(RatesProvider.class); when(provNew.getValuationDate()).thenReturn(VAL_DATE); when(obsFuncNew.rate(fra.getFloatingRate(), fra.getStartDate(), fra.getEndDate(), provNew)) .thenReturn(forwardRate + eps); CurrencyAmount upValue = new DiscountingFraProductPricer(obsFuncNew).forecastValue(fra, provNew); when(obsFuncNew.rate(fra.getFloatingRate(), fra.getStartDate(), fra.getEndDate(), provNew)) .thenReturn(forwardRate - eps); CurrencyAmount downValue = new DiscountingFraProductPricer(obsFuncNew).forecastValue(fra, provNew); return upValue.minus(downValue).multipliedBy(0.5 / eps).getAmount(); }
public void test_zero_Currency() { CurrencyAmount test = CurrencyAmount.zero(Currency.USD); assertEquals(test.getCurrency(), Currency.USD); assertEquals(test.getAmount(), 0d, 0); }
/** * Returns a copy of this {@code CurrencyAmount} with the specified amount subtracted. * <p> * This subtracts the specified amount to this monetary amount, returning a new object. * The addition simply uses standard {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToSubtract the amount to subtract, in the same currency * @return an amount based on this with the specified amount subtracted * @throws IllegalArgumentException if the currencies are not equal */ public CurrencyAmount minus(CurrencyAmount amountToSubtract) { ArgChecker.notNull(amountToSubtract, "amountToSubtract"); ArgChecker.isTrue(amountToSubtract.getCurrency().equals(currency), "Unable to subtract amounts in different currencies"); return minus(amountToSubtract.getAmount()); }
public void test_presentValue_started() { ResolvedFxSwap product = ResolvedFxSwap.ofForwardPoints( CurrencyAmount.of(USD, NOMINAL_USD), KRW, FX_RATE, FX_FWD_POINTS, PAYMENT_DATE_PAST, PAYMENT_DATE_NEAR); MultiCurrencyAmount computed = PRICER.presentValue(product, PROVIDER); double expected_usd = -NOMINAL_USD * PROVIDER.discountFactor(USD, PAYMENT_DATE_NEAR); double expected_krw = NOMINAL_USD * (FX_RATE + FX_FWD_POINTS) * PROVIDER.discountFactor(KRW, PAYMENT_DATE_NEAR); assertEquals(computed.getAmount(USD).getAmount(), expected_usd, NOMINAL_USD * TOL); assertEquals(computed.getAmount(KRW).getAmount(), expected_krw, NOMINAL_USD * FX_RATE * TOL); // currency exposure MultiCurrencyAmount exposure = PRICER.currencyExposure(product, PROVIDER); assertEquals(exposure, computed); }
public void test_minus_MultiCurrencyAmount_mergeAndAdd() { CurrencyAmount ca = CurrencyAmount.of(Currency.AUD, 117); CurrencyAmount cb = CurrencyAmount.of(Currency.USD, 12); CurrencyAmount cc = CurrencyAmount.of(Currency.AUD, 3); CurrencyAmount cd = CurrencyAmount.of(Currency.NZD, 3); MultiCurrencyAmount mc1 = MultiCurrencyAmount.of(ca, cb); MultiCurrencyAmount mc2 = MultiCurrencyAmount.of(cc, cd); MultiCurrencyAmount test = mc1.minus(mc2); assertMCA(test, cb, cd.negated(), CurrencyAmount.of(Currency.AUD, 114)); }
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); }
public void test_beanBuilder_invalid() { BeanBuilder<? extends MultiCurrencyAmount> test = MultiCurrencyAmount.meta().builder() .set(MultiCurrencyAmount.meta().amounts(), ImmutableSortedSet.of(CA1, CA2, CurrencyAmount.of(CA1.getCurrency(), AMT3))); assertThrowsIllegalArg(() -> test.build()); }
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); }