public void test_plus_MultiCurrencyAmount_empty() { CurrencyAmount ca = CurrencyAmount.of(Currency.AUD, 117); CurrencyAmount cb = CurrencyAmount.of(Currency.USD, 12); MultiCurrencyAmount mc1 = MultiCurrencyAmount.of(ca, cb); MultiCurrencyAmount mc2 = MultiCurrencyAmount.of(); MultiCurrencyAmount test = mc1.plus(mc2); assertMCA(test, ca, cb); }
/** * Calculates the current cash of the swap product. * * @param swap the product * @param provider the rates provider * @return the current cash of the swap product */ public MultiCurrencyAmount currentCash(ResolvedSwap swap, RatesProvider provider) { MultiCurrencyAmount ce = MultiCurrencyAmount.empty(); for (ResolvedSwapLeg leg : swap.getLegs()) { ce = ce.plus(legPricer.currentCash(leg, provider)); } return ce; }
/** * 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 copy of this {@code MultiCurrencyAmount} with the specified amount added. * <p> * This adds the specified amount to this monetary amount, returning a new object. * If the currency is already present, the amount is added to the existing amount. * If the currency is not yet present, the currency-amount is added to the map. * The addition uses standard {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToAdd the amount to add * @return an amount based on this with the specified amount added */ public MultiCurrencyAmount plus(MultiCurrencyAmount amountToAdd) { ArgChecker.notNull(amountToAdd, "amountToAdd"); return Stream.concat(amounts.stream(), amountToAdd.stream()).collect(toMultiCurrencyAmount()); }
public void test_convertedTo_rateProvider_noConversionSize1() { FxRateProvider provider = (ccy1, ccy2) -> { throw new IllegalArgumentException(); }; MultiCurrencyAmount test = MultiCurrencyAmount.of(CA2); assertEquals(test.convertedTo(CCY2, provider), CA2); }
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_currencyExposure() { MultiCurrencyAmount ceComputed = PRICER_TRADE.currencyExposure(OPTION_TRADE, RATES_PROVIDER, VOLS); MultiCurrencyAmount ceExpected = PRICER_PRODUCT.currencyExposure(OPTION_PRODUCT, RATES_PROVIDER, VOLS) .plus(PRICER_PAYMENT.presentValue(PREMIUM, RATES_PROVIDER)); assertEquals(ceComputed.size(), 2); assertEquals(ceComputed.getAmount(EUR).getAmount(), ceExpected.getAmount(EUR).getAmount(), TOL * NOTIONAL); assertEquals(ceComputed.getAmount(USD).getAmount(), ceExpected.getAmount(USD).getAmount(), TOL * NOTIONAL); }
public void test_currencyExposureFromCleanPrice() { MultiCurrencyAmount computed = PRICER.currencyExposureFromCleanPrice( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE); PointSensitivities point = PRICER.presentValueSensitivityFromCleanPrice( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus( PRICER.presentValueFromCleanPrice(TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE)); assertEquals(computed.getAmounts().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_currencyExposure() { MultiCurrencyAmount computed = PRICER.currencyExposure(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER, VALUATION); PointSensitivities point = PRICER.presentValueSensitivity(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER).build(); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point) .plus(PRICER.presentValue(PRODUCT, RATES_PROVIDER, ISSUER_RATES_PROVIDER)); assertEquals(computed.getCurrencies().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * TOL); }
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_currencyExposure_afterMaturity() { MultiCurrencyAmount computedRec = SWAPTION_PRICER.currencyExposure( SWAPTION_REC_LONG, RATE_PROVIDER_AFTER_MATURITY, VOLS_AFTER_MATURITY); MultiCurrencyAmount computedPay = SWAPTION_PRICER.currencyExposure( SWAPTION_PAY_SHORT, RATE_PROVIDER_AFTER_MATURITY, VOLS_AFTER_MATURITY); assertEquals(computedRec.size(), 1); assertEquals(computedRec.getAmount(USD).getAmount(), 0d, NOTIONAL * TOL); assertEquals(computedPay.size(), 1); assertEquals(computedPay.getAmount(USD).getAmount(), 0d, NOTIONAL * TOL); }
private static MultiCurrencyAmount swapValue( RatesProvider provider, ResolvedSwap swap, ToDoubleBiFunction<ResolvedSwapLeg, RatesProvider> legFn) { if (swap.isCrossCurrency()) { return swap.getLegs().stream() .map(leg -> CurrencyAmount.of(leg.getCurrency(), legFn.applyAsDouble(leg, provider))) .collect(toMultiCurrencyAmount()); } else { Currency currency = swap.getLegs().iterator().next().getCurrency(); double total = 0d; for (ResolvedSwapLeg leg : swap.getLegs()) { total += legFn.applyAsDouble(leg, provider); } return MultiCurrencyAmount.of(currency, total); } }
public void test_currencyExposure() { MultiCurrencyAmount computed1 = TRADE_PRICER.currencyExposure(CMS_TRADE_PREMIUM, RATES_PROVIDER, VOLATILITIES); MultiCurrencyAmount computed2 = TRADE_PRICER.currencyExposure(CMS_TRADE, RATES_PROVIDER, VOLATILITIES); MultiCurrencyAmount pv1 = TRADE_PRICER.presentValue(CMS_TRADE_PREMIUM, RATES_PROVIDER, VOLATILITIES); PointSensitivities pt1 = TRADE_PRICER.presentValueSensitivityRates(CMS_TRADE_PREMIUM, RATES_PROVIDER, VOLATILITIES); MultiCurrencyAmount expected1 = RATES_PROVIDER.currencyExposure(pt1).plus(pv1); MultiCurrencyAmount pv2 = TRADE_PRICER.presentValue(CMS_TRADE, RATES_PROVIDER, VOLATILITIES); PointSensitivities pt2 = TRADE_PRICER.presentValueSensitivityRates(CMS_TRADE, RATES_PROVIDER, VOLATILITIES); MultiCurrencyAmount expected2 = RATES_PROVIDER.currencyExposure(pt2).plus(pv2); assertEquals(computed1.getAmount(EUR).getAmount(), expected1.getAmount(EUR).getAmount(), NOTIONAL_VALUE * TOL); assertEquals(computed2.getAmount(EUR).getAmount(), expected2.getAmount(EUR).getAmount(), NOTIONAL_VALUE * TOL); }
public void test_stream() { MultiCurrencyAmount base = MultiCurrencyAmount.of(CA1, CA2); MultiCurrencyAmount test = base.stream() .map(ca -> ca.mapAmount(a -> a * 3)) .collect(toMultiCurrencyAmount()); assertMCA(test, CA1.mapAmount(a -> a * 3), CA2.mapAmount(a -> a * 3)); }
public void test_presentValue_inflation() { DiscountingSwapLegPricer pricerLeg = DiscountingSwapLegPricer.DEFAULT; DiscountingSwapProductPricer pricerSwap = new DiscountingSwapProductPricer(pricerLeg); LocalDate paymentDate = SWAP_INFLATION.getLegs().get(0).getPaymentPeriods().get(0).getPaymentDate(); double fixedRate = INFLATION_FIXED_SWAP_LEG_PAY_GBP_FIXED_RATE; MultiCurrencyAmount pvComputed = pricerSwap.presentValue(SWAP_INFLATION, RATES_GBP_INFLATION); double pvExpected = (-(INDEX_VALUES[0] / START_INDEX - 1.0) + Math.pow(1 + fixedRate, 5) - 1.0) * NOTIONAL * RATES_GBP_INFLATION.discountFactor(GBP, paymentDate); assertTrue(pvComputed.getCurrencies().size() == 1); assertEquals(pvComputed.getAmount(GBP).getAmount(), pvExpected, NOTIONAL * TOLERANCE_RATE); }
public void test_empty_amounts() { MultiCurrencyAmountArray array = MultiCurrencyAmountArray.of( MultiCurrencyAmount.empty(), MultiCurrencyAmount.empty()); assertThat(array.size()).isEqualTo(2); assertThat(array.get(0)).isEqualTo(MultiCurrencyAmount.empty()); assertThat(array.get(1)).isEqualTo(MultiCurrencyAmount.empty()); }
/** * Calculates the current cash. * * @param fx the product * @param valuationDate the valuation date * @return the current cash */ public MultiCurrencyAmount currentCash(ResolvedFxSingle fx, LocalDate valuationDate) { if (valuationDate.isEqual(fx.getPaymentDate())) { return MultiCurrencyAmount.of(fx.getBaseCurrencyPayment().getValue(), fx.getCounterCurrencyPayment().getValue()); } return MultiCurrencyAmount.empty(); }
MultiCurrencyAmount pvOis = SWAP_PRICER.presentValue( ((ResolvedSwapTrade) usdTrades.get(USD_DSC_NB_DEPO_NODES + i)).getProduct(), result); assertEquals(pvOis.getAmount(USD).getAmount(), 0.0, TOLERANCE_PV); MultiCurrencyAmount pvFx = FX_PRICER.presentValue( ((ResolvedFxSwapTrade) eurTrades.get(i)).getProduct(), result); assertEquals(pvFx.convertedTo(USD, result).getAmount(), 0.0, TOLERANCE_PV);
/** * Returns the total of the sensitivity values. * <p> * The result is the total of all values, in whatever currency they are defined. * * @return the total sensitivity */ public MultiCurrencyAmount total() { return sensitivities.stream() .map(CrossGammaParameterSensitivity::total) .collect(toMultiCurrencyAmount()); }