private static ImmutableSet<Currency> buildCurrencies(List<ResolvedSwapLeg> legs) { // avoid streams as profiling showed a hotspot ImmutableSet.Builder<Currency> builder = ImmutableSet.builder(); for (ResolvedSwapLeg leg : legs) { builder.add(leg.getCurrency()); } return builder.build(); }
/** * Gets the currency of the underlying swap. * <p> * The underlying swap must have a single currency. * * @return the currency of the swap */ public Currency getCurrency() { return underlyingSwap.getReceiveLeg().get().getCurrency(); }
private static ImmutableSet<Currency> buildCurrencies(ResolvedIborCapFloorLeg capFloorLeg, ResolvedSwapLeg payLeg) { ImmutableSet.Builder<Currency> builder = ImmutableSet.builder(); builder.add(capFloorLeg.getCurrency()); if (payLeg != null) { builder.add(payLeg.getCurrency()); } return builder.build(); }
@Override public ResolvedSwap resolve(ReferenceData refData) { // avoid streams as profiling showed a hotspot // most efficient to loop around legs once ImmutableList.Builder<ResolvedSwapLeg> resolvedLegs = ImmutableList.builder(); ImmutableSet.Builder<Currency> currencies = ImmutableSet.builder(); ImmutableSet.Builder<Index> indices = ImmutableSet.builder(); for (SwapLeg leg : legs) { ResolvedSwapLeg resolvedLeg = leg.resolve(refData); resolvedLegs.add(resolvedLeg); currencies.add(resolvedLeg.getCurrency()); leg.collectIndices(indices); } return new ResolvedSwap(resolvedLegs.build(), currencies.build(), indices.build()); }
/** * Returns the set of currencies referred to by the CMS. * <p> * This returns the complete set of payment currencies for the CMS. * This will typically return one currency, but could return two. * * @return the set of payment currencies referred to by this swap */ public ImmutableSet<Currency> allPaymentCurrencies() { if (payLeg == null) { return ImmutableSet.of(cmsLeg.getCurrency()); } return ImmutableSet.of(cmsLeg.getCurrency(), payLeg.getCurrency()); }
/** * Calculates the forecast value of the swap leg. * <p> * The forecast value of the leg is the value on the valuation date without present value discounting. * The result is returned using the payment currency of the leg. * * @param leg the leg * @param provider the rates provider * @return the forecast value of the swap leg */ public CurrencyAmount forecastValue(ResolvedSwapLeg leg, RatesProvider provider) { return CurrencyAmount.of(leg.getCurrency(), forecastValueInternal(leg, provider)); }
/** * Calculates the present value of the swap leg. * <p> * The present value of the leg is the value on the valuation date. * This is the discounted forecast value. * The result is returned using the payment currency of the leg. * * @param leg the leg * @param provider the rates provider * @return the present value of the swap leg */ public CurrencyAmount presentValue(ResolvedSwapLeg leg, RatesProvider provider) { return CurrencyAmount.of(leg.getCurrency(), presentValueInternal(leg, provider)); }
/** * Calculates the accrued interest since the last payment. * <p> * This determines the payment period applicable at the valuation date and calculates * the accrued interest since the last payment. * The result is returned using the payment currency of the leg. * * @param leg the leg * @param provider the rates provider * @return the accrued interest of the swap leg */ public CurrencyAmount accruedInterest(ResolvedSwapLeg leg, RatesProvider provider) { Optional<SwapPaymentPeriod> period = leg.findPaymentPeriod(provider.getValuationDate()); if (period.isPresent()) { double accruedInterest = paymentPeriodPricer.accruedInterest(period.get(), provider); return CurrencyAmount.of(leg.getCurrency(), accruedInterest); } return CurrencyAmount.zero(leg.getCurrency()); }
/** * Calculates the current cash of the swap leg. * * @param leg the leg * @param provider the rates provider * @return the current cash of the swap leg */ public CurrencyAmount currentCash(ResolvedSwapLeg leg, RatesProvider provider) { return CurrencyAmount.of(leg.getCurrency(), currentCashPeriodsInternal(leg, provider) + (currentCashEventsInternal(leg, provider))); }
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); } }
/** * Calculates the present value of the swap leg, converted to the specified currency. * <p> * The present value of the leg is the value on the valuation date. * This is the discounted forecast value. * The result is converted to the specified currency. * * @param leg the leg * @param currency the currency to convert to * @param provider the rates provider * @return the present value of the swap leg in the specified currency */ public CurrencyAmount presentValue(ResolvedSwapLeg leg, Currency currency, RatesProvider provider) { double pv = presentValueInternal(leg, provider); return CurrencyAmount.of(currency, (pv * provider.fxRate(leg.getCurrency(), currency))); }
public void test_currentCash_zero() { ResolvedSwapLeg expSwapLeg = IBOR_SWAP_LEG_REC_GBP; CurrencyAmount computed = PRICER_LEG.currentCash(expSwapLeg, RATES_GBP); assertEquals(computed, CurrencyAmount.zero(expSwapLeg.getCurrency())); }
/** * Calculates the numeraire, used to multiply the results. * * @param swaption the swap * @param fixedLeg the fixed leg * @param forward the forward rate * @param ratesProvider the rates provider * @return the numeraire */ protected double calculateNumeraire( ResolvedSwaption swaption, ResolvedSwapLeg fixedLeg, double forward, RatesProvider ratesProvider) { double annuityCash = swapPricer.getLegPricer().annuityCash(fixedLeg, forward); CashSwaptionSettlement cashSettlement = (CashSwaptionSettlement) swaption.getSwaptionSettlement(); double discountSettle = ratesProvider.discountFactor(fixedLeg.getCurrency(), cashSettlement.getSettlementDate()); return Math.abs(annuityCash * discountSettle); }
/** * Calculates the present value of the swap product, converted to the specified currency. * <p> * The present value of the product is the value on the valuation date. * This is the discounted forecast value. * The result is converted to the specified currency. * * @param swap the product * @param currency the currency to convert to * @param provider the rates provider * @return the present value of the swap product in the specified currency */ public CurrencyAmount presentValue(ResolvedSwap swap, Currency currency, RatesProvider provider) { double totalPv = 0; for (ResolvedSwapLeg leg : swap.getLegs()) { double pv = legPricer.presentValueInternal(leg, provider); totalPv += (pv * provider.fxRate(leg.getCurrency(), currency)); } return CurrencyAmount.of(currency, totalPv); }
public void test_currentCash_payPeriod() { ResolvedSwapLeg expSwapLeg = FIXED_SWAP_LEG_PAY_USD; LocalDate paymentDate = expSwapLeg.getPaymentPeriods().get(0).getPaymentDate(); RatesProvider prov = new MockRatesProvider(paymentDate); SwapPaymentPeriodPricer<SwapPaymentPeriod> mockPeriod = mock(SwapPaymentPeriodPricer.class); double expected = 1234d; when(mockPeriod.currentCash(expSwapLeg.getPaymentPeriods().get(0), prov)).thenReturn(expected); DiscountingSwapLegPricer pricer = new DiscountingSwapLegPricer(mockPeriod, SwapPaymentEventPricer.standard()); CurrencyAmount computed = pricer.currentCash(expSwapLeg, prov); assertEquals(computed, CurrencyAmount.of(expSwapLeg.getCurrency(), expected)); }
public void test_currentCash_payEvent() { ResolvedSwapLeg expSwapLeg = FIXED_SWAP_LEG_PAY_USD; LocalDate paymentDate = expSwapLeg.getPaymentEvents().get(0).getPaymentDate(); RatesProvider prov = new MockRatesProvider(paymentDate); SwapPaymentEventPricer<SwapPaymentEvent> mockEvent = mock(SwapPaymentEventPricer.class); double expected = 1234d; when(mockEvent.currentCash(expSwapLeg.getPaymentEvents().get(0), prov)).thenReturn(expected); DiscountingSwapLegPricer pricer = new DiscountingSwapLegPricer(SwapPaymentPeriodPricer.standard(), mockEvent); CurrencyAmount computed = pricer.currentCash(expSwapLeg, prov); assertEquals(computed, CurrencyAmount.of(expSwapLeg.getCurrency(), expected)); }
public void test_builder() { ResolvedSwapLeg test = ResolvedSwapLeg.builder() .type(IBOR) .payReceive(RECEIVE) .paymentPeriods(RPP1) .paymentEvents(NOTIONAL_EXCHANGE) .build(); assertEquals(test.getType(), IBOR); assertEquals(test.getPayReceive(), RECEIVE); assertEquals(test.getStartDate(), DATE_2014_06_30); assertEquals(test.getEndDate(), DATE_2014_09_30); assertEquals(test.getCurrency(), GBP); assertEquals(test.getPaymentPeriods(), ImmutableList.of(RPP1)); assertEquals(test.getPaymentEvents(), ImmutableList.of(NOTIONAL_EXCHANGE)); }
/** * Obtains an instance from a swap leg and amount. * * @param leg the swap leg * @param amount the amount * @return the swap leg amount */ public static SwapLegAmount of(ResolvedSwapLeg leg, CurrencyAmount amount) { return builder() .amount(amount) .payReceive(leg.getPayReceive()) .type(leg.getType()) .currency(leg.getCurrency()) .build(); }
public void test_presentValueDelta_atMaturity() { double forward = PRICER_SWAP.parRate(RSWAP_REC, RATE_PROVIDER_AT_MATURITY); ResolvedSwapLeg fixedLeg = SWAPTION_REC_LONG.getUnderlying().getLegs(SwapLegType.FIXED).get(0); double annuityCash = PRICER_SWAP.getLegPricer().annuityCash(fixedLeg, forward); CashSwaptionSettlement cashSettlement = (CashSwaptionSettlement) SWAPTION_REC_LONG.getSwaptionSettlement(); double discountSettle = RATE_PROVIDER_AT_MATURITY.discountFactor(fixedLeg.getCurrency(), cashSettlement.getSettlementDate()); double pvbpCash = Math.abs(annuityCash * discountSettle); CurrencyAmount deltaRec = PRICER.presentValueDelta(SWAPTION_REC_LONG, RATE_PROVIDER_AT_MATURITY, VOLS_AT_MATURITY); assertEquals(deltaRec.getAmount(), RATE > forward ? -pvbpCash : 0, TOLERANCE_DELTA); CurrencyAmount deltaPay = PRICER.presentValueDelta(SWAPTION_PAY_SHORT, RATE_PROVIDER_AT_MATURITY, VOLS_AT_MATURITY); assertEquals(deltaPay.getAmount(), RATE > forward ? 0 : pvbpCash, TOLERANCE_DELTA); }
public void test_presentValueDelta_parity() { double forward = PRICER_SWAP.parRate(RSWAP_REC, RATE_PROVIDER); ResolvedSwapLeg fixedLeg = SWAPTION_REC_LONG.getUnderlying().getLegs(SwapLegType.FIXED).get(0); double annuityCash = PRICER_SWAP.getLegPricer().annuityCash(fixedLeg, forward); CashSwaptionSettlement cashSettlement = (CashSwaptionSettlement) SWAPTION_REC_LONG.getSwaptionSettlement(); double discountSettle = RATE_PROVIDER.discountFactor(fixedLeg.getCurrency(), cashSettlement.getSettlementDate()); double pvbpCash = Math.abs(annuityCash * discountSettle); CurrencyAmount deltaRec = PRICER.presentValueDelta(SWAPTION_REC_LONG, RATE_PROVIDER, VOLS); CurrencyAmount deltaPay = PRICER.presentValueDelta(SWAPTION_PAY_SHORT, RATE_PROVIDER, VOLS); assertEquals(deltaRec.getAmount() + deltaPay.getAmount(), -pvbpCash, TOLERANCE_DELTA); }