/** * Returns a new array containing the values from this array with the values from the amount subtracted. * <p> * The amount is subtracted from each element in this array. * * @param amount the amount to subtract * @return a new array containing the values from this array with the values from the amount subtracted */ public MultiCurrencyAmountArray minus(MultiCurrencyAmount amount) { ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), amount.getCurrencies())) { DoubleArray array = values.get(currency); if (array == null) { builder.put(currency, DoubleArray.filled(size, -amount.getAmount(currency).getAmount())); } else if (!amount.contains(currency)) { builder.put(currency, array); } else { builder.put(currency, array.minus(amount.getAmount(currency).getAmount())); } } return MultiCurrencyAmountArray.of(builder.build()); }
/** * Returns a new array containing the values from this array with the values from the amount added. * <p> * The amount is added to each element in this array. * * @param amount the amount to add * @return a new array containing the values from this array added to the values in the other array */ public MultiCurrencyAmountArray plus(MultiCurrencyAmount amount) { ImmutableMap.Builder<Currency, DoubleArray> builder = ImmutableMap.builder(); for (Currency currency : Sets.union(values.keySet(), amount.getCurrencies())) { DoubleArray array = values.get(currency); if (array == null) { builder.put(currency, DoubleArray.filled(size, amount.getAmount(currency).getAmount())); } else if (!amount.contains(currency)) { builder.put(currency, array); } else { builder.put(currency, array.plus(amount.getAmount(currency).getAmount())); } } return MultiCurrencyAmountArray.of(builder.build()); }
public void test_contains_null() { MultiCurrencyAmount base = MultiCurrencyAmount.of(CA1, CA2); assertThrowsIllegalArg(() -> base.contains(null)); }
private void assertMCA(MultiCurrencyAmount actual, CurrencyAmount... expected) { assertEquals(actual.size(), expected.length); assertEquals(actual.getAmounts().size(), expected.length); assertEquals(actual.getAmounts(), ImmutableSet.copyOf(expected)); Set<Currency> currencies = new HashSet<>(); for (CurrencyAmount expectedAmount : expected) { currencies.add(expectedAmount.getCurrency()); assertEquals(actual.contains(expectedAmount.getCurrency()), true); assertEquals(actual.getAmount(expectedAmount.getCurrency()), expectedAmount); assertEquals(actual.getAmountOrZero(expectedAmount.getCurrency()), expectedAmount); } assertEquals(actual.getCurrencies(), currencies); Currency nonExisting = Currency.of("FRZ"); assertEquals(actual.contains(nonExisting), false); assertThrowsIllegalArg(() -> actual.getAmount(nonExisting)); assertEquals(actual.getAmountOrZero(nonExisting), CurrencyAmount.zero(nonExisting)); }
public void test_presentValueZSpread_settle_before_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }
public void test_presentValue_settle_before_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_BEFORE_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_BEFORE_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), 0, TOLERANCE_PV); }
public void test_presentValue_settle_on_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_ON_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .plus(-PRICE * NOTIONAL_AMOUNT) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_ON_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_ON_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), -PRICE * NOTIONAL_AMOUNT * QUANTITY, TOLERANCE_PV); }
public void test_presentValueZSpread_settle_on_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_ON_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .plus(-PRICE * NOTIONAL_AMOUNT) .multipliedBy(QUANTITY); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_ON_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }
public void test_presentValue_settle_after_val() { CurrencyAmount pvComputed = PRICER_TRADE.presentValue(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER); CurrencyAmount pvExpected = PRICER_PRODUCT.presentValue(BILL_PRODUCT.resolve(REF_DATA), PROVIDER) .multipliedBy(QUANTITY) .plus(PRICER_PAYMENT.presentValue(BILL_TRADE_SETTLE_AFTER_VAL.getSettlement().get(), PROVIDER.repoCurveDiscountFactors(BILL_PRODUCT.getSecurityId(), BILL_PRODUCT.getLegalEntityId(), BILL_PRODUCT.getCurrency()) .getDiscountFactors())); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); CurrencyAmount cashComputed = PRICER_TRADE.currentCash(BILL_TRADE_SETTLE_AFTER_VAL, VAL_DATE); assertEquals(cashComputed.getCurrency(), EUR); assertEquals(cashComputed.getAmount(), 0, TOLERANCE_PV); }
public void test_currencyExposure_fx() { ResolvedSwapLeg expSwapLeg = FIXED_FX_RESET_SWAP_LEG_PAY_GBP; PointSensitivities point = PRICER_LEG.presentValueSensitivity(expSwapLeg, RATES_GBP_USD).build(); MultiCurrencyAmount expected = RATES_GBP_USD.currencyExposure(point.convertedTo(USD, RATES_GBP_USD)) .plus(PRICER_LEG.presentValue(expSwapLeg, RATES_GBP_USD)); MultiCurrencyAmount computed = PRICER_LEG.currencyExposure(expSwapLeg, RATES_GBP_USD); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), EPS * NOTIONAL); assertFalse(computed.contains(GBP)); // 0 GBP }
public void test_presentValueZSpread_settle_after_val() { CurrencyAmount pvComputed = PRICER_TRADE .presentValueWithZSpread(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); CurrencyAmount pvExpected = PRICER_PRODUCT .presentValueWithZSpread(BILL_PRODUCT.resolve(REF_DATA), PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0) .multipliedBy(QUANTITY) .plus(PRICER_PAYMENT.presentValue(BILL_TRADE_SETTLE_AFTER_VAL.getSettlement().get(), PROVIDER.repoCurveDiscountFactors(BILL_PRODUCT.getSecurityId(), BILL_PRODUCT.getLegalEntityId(), BILL_PRODUCT.getCurrency()) .getDiscountFactors())); assertEquals(pvComputed.getCurrency(), EUR); assertEquals(pvComputed.getAmount(), pvExpected.getAmount(), TOLERANCE_PV); MultiCurrencyAmount ceComputed = PRICER_TRADE .currencyExposureWithZSpread(BILL_TRADE_SETTLE_AFTER_VAL, PROVIDER, Z_SPREAD, CompoundedRateType.CONTINUOUS, 0); assertEquals(ceComputed.getCurrencies().size(), 1); assertTrue(ceComputed.contains(EUR)); assertEquals(ceComputed.getAmount(EUR).getAmount(), pvExpected.getAmount(), TOLERANCE_PV); }
PAYMENT_PERIOD_FULL_GS_FX_USD.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider))); assertEquals(computedUSD.getAmount(USD).getAmount(), expectedUSD.getAmount(USD).getAmount(), TOLERANCE_PV); assertFalse(computedUSD.contains(GBP)); // 0 GBP PAYMENT_PERIOD_FULL_GS_FX_GBP.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider))); assertEquals(computedGBP.getAmount(GBP).getAmount(), expectedGBP.getAmount(GBP).getAmount(), TOLERANCE_PV); assertFalse(computedGBP.contains(USD)); // 0 USD double expectedFdUSD = (pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider)) / EPS_FD; assertTrue(!computedUSD.contains(GBP) && DoubleMath.fuzzyEquals(expectedFdUSD, 0d, TOLERANCE_PV)); double expectedFdGBP = -(pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider)) * FX_RATE * FX_RATE / EPS_FD; assertTrue(!computedGBP.contains(USD) && DoubleMath.fuzzyEquals(expectedFdGBP, 0d, TOLERANCE_PV));
public void test_currencyExposure_fx_onFixing_noTimeSeries() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; ImmutableRatesProvider provider = MULTI_GBP_USD; // USD MultiCurrencyAmount computedUSD = pricer.currencyExposure(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); PointSensitivities pointUSD = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider).build(); MultiCurrencyAmount expectedUSD = provider.currencyExposure(pointUSD.convertedTo(GBP, provider)).plus( CurrencyAmount.of( PAYMENT_PERIOD_FULL_GS_FX_USD.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider))); assertEquals(computedUSD.getAmount(GBP).getAmount(), expectedUSD.getAmount(GBP).getAmount(), TOLERANCE_PV); assertFalse(computedUSD.contains(USD)); // 0 GBP // GBP MultiCurrencyAmount computedGBP = pricer.currencyExposure(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); PointSensitivities pointGBP = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider).build(); MultiCurrencyAmount expectedGBP = provider.currencyExposure(pointGBP.convertedTo(USD, provider)).plus(CurrencyAmount.of( PAYMENT_PERIOD_FULL_GS_FX_GBP.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider))); assertEquals(computedGBP.getAmount(USD).getAmount(), expectedGBP.getAmount(USD).getAmount(), TOLERANCE_PV); assertFalse(computedGBP.contains(GBP)); // 0 GBP // FD approximation ImmutableRatesProvider provUp = RatesProviderDataSets.multiGbpUsd(VAL_DATE).toBuilder() .fxRateProvider(FX_MATRIX_BUMP) .build(); double expectedFdUSD = (pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider)) / EPS_FD; assertEquals(computedUSD.getAmount(GBP).getAmount(), expectedFdUSD, EPS_FD * NOTIONAL_100); double expectedFdGBP = -(pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider)) * FX_RATE * FX_RATE / EPS_FD; assertEquals(computedGBP.getAmount(USD).getAmount(), expectedFdGBP, EPS_FD * NOTIONAL_100); }
public void test_currencyExposure_fx() { DiscountingRatePaymentPeriodPricer pricer = DiscountingRatePaymentPeriodPricer.DEFAULT; LocalDate valuationDate = VAL_DATE.minusWeeks(1); ImmutableRatesProvider provider = RatesProviderDataSets.multiGbpUsd(valuationDate); // USD MultiCurrencyAmount computedUSD = pricer.currencyExposure(PAYMENT_PERIOD_FULL_GS_FX_USD, provider); PointSensitivities pointUSD = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_USD, provider).build(); MultiCurrencyAmount expectedUSD = provider.currencyExposure(pointUSD.convertedTo(GBP, provider)).plus(CurrencyAmount.of( PAYMENT_PERIOD_FULL_GS_FX_USD.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider))); assertEquals(computedUSD.getAmount(GBP).getAmount(), expectedUSD.getAmount(GBP).getAmount(), TOLERANCE_PV); assertFalse(computedUSD.contains(USD)); // 0 USD // GBP MultiCurrencyAmount computedGBP = pricer.currencyExposure(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider); PointSensitivities pointGBP = pricer.presentValueSensitivity(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider).build(); MultiCurrencyAmount expectedGBP = provider.currencyExposure(pointGBP.convertedTo(USD, provider)).plus(CurrencyAmount.of( PAYMENT_PERIOD_FULL_GS_FX_GBP.getCurrency(), pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider))); assertEquals(computedGBP.getAmount(USD).getAmount(), expectedGBP.getAmount(USD).getAmount(), TOLERANCE_PV); assertFalse(computedGBP.contains(GBP)); // 0 GBP // FD approximation ImmutableRatesProvider provUp = RatesProviderDataSets.multiGbpUsd(valuationDate).toBuilder() .fxRateProvider(FX_MATRIX_BUMP) .build(); double expectedFdUSD = (pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_USD, provider)) / EPS_FD; assertEquals(computedUSD.getAmount(GBP).getAmount(), expectedFdUSD, EPS_FD * NOTIONAL_100); double expectedFdGBP = -(pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provUp) - pricer.presentValue(PAYMENT_PERIOD_FULL_GS_FX_GBP, provider)) * FX_RATE * FX_RATE / EPS_FD; assertEquals(computedGBP.getAmount(USD).getAmount(), expectedFdGBP, EPS_FD * NOTIONAL_100); }
MultiCurrencyAmount expectedUSD = prov.currencyExposure(pointUSD.convertedTo(USD, prov)).plus(CurrencyAmount.of( resetNotionalUSD.getCurrency(), test.presentValue(resetNotionalUSD, prov))); assertFalse(computedUSD.contains(USD)); // 0 USD assertEquals(computedUSD.getAmount(GBP).getAmount(), expectedUSD.getAmount(GBP).getAmount(), eps * NOTIONAL); MultiCurrencyAmount expectedGBP = prov.currencyExposure(pointGBP.convertedTo(GBP, prov)).plus(CurrencyAmount.of( resetNotionalGBP.getCurrency(), test.presentValue(resetNotionalGBP, prov))); assertFalse(computedGBP.contains(GBP)); // 0 GBP assertEquals(computedGBP.getAmount(USD).getAmount(), expectedGBP.getAmount(USD).getAmount(), eps * NOTIONAL); double expectedFdUSD = -(test.presentValue(resetNotionalUSD, provUp) - test.presentValue(resetNotionalUSD, prov)) * FX_RATE * FX_RATE / EPS_FD; assertTrue(!computedUSD.contains(USD) && DoubleMath.fuzzyEquals(expectedFdUSD, 0d, eps)); double expectedFdGBP = (test.presentValue(resetNotionalGBP, provUp) - test.presentValue(resetNotionalGBP, prov)) / EPS_FD; assertTrue(!computedGBP.contains(GBP) && DoubleMath.fuzzyEquals(expectedFdGBP, 0d, eps));
MultiCurrencyAmount expectedUSD = prov.currencyExposure(pointUSD.convertedTo(USD, prov)).plus(CurrencyAmount.of( FX_RESET_NOTIONAL_EXCHANGE_REC_USD.getCurrency(), test.presentValue(FX_RESET_NOTIONAL_EXCHANGE_REC_USD, prov))); assertFalse(computedUSD.contains(GBP)); // 0 GBP assertEquals(computedUSD.getAmount(USD).getAmount(), expectedUSD.getAmount(USD).getAmount(), eps * NOTIONAL); MultiCurrencyAmount expectedGBP = prov.currencyExposure(pointGBP.convertedTo(GBP, prov)).plus(CurrencyAmount.of( FX_RESET_NOTIONAL_EXCHANGE_PAY_GBP.getCurrency(), test.presentValue(FX_RESET_NOTIONAL_EXCHANGE_PAY_GBP, prov))); assertFalse(computedGBP.contains(USD)); // 0 USD assertEquals(computedGBP.getAmount(GBP).getAmount(), expectedGBP.getAmount(GBP).getAmount(), eps * NOTIONAL);
MultiCurrencyAmount expectedUSD = prov.currencyExposure(pointUSD.convertedTo(USD, prov)).plus(CurrencyAmount.of( resetNotionalUSD.getCurrency(), test.presentValue(resetNotionalUSD, prov))); assertFalse(computedUSD.contains(GBP)); // 0 GBP assertEquals(computedUSD.getAmount(USD).getAmount(), expectedUSD.getAmount(USD).getAmount(), eps * NOTIONAL); MultiCurrencyAmount expectedGBP = prov.currencyExposure(pointGBP.convertedTo(GBP, prov)).plus(CurrencyAmount.of( resetNotionalGBP.getCurrency(), test.presentValue(resetNotionalGBP, prov))); assertFalse(computedGBP.contains(USD)); // 0 USD assertEquals(computedGBP.getAmount(GBP).getAmount(), expectedGBP.getAmount(GBP).getAmount(), eps * NOTIONAL);