/** * Finds the notional on the specified date. * <p> * If the date falls before the start, the initial notional will be returned. * If the date falls after the end, the final notional will be returned. * <p> * An empty optional is returned if the leg has no notional, for example if the payment amount * is known and explicitly specified. * * @param date the date on which the notional is required * @return the notional on the specified date, if available */ public Optional<CurrencyAmount> findNotional(LocalDate date) { SwapPaymentPeriod paymentPeriod; if (!date.isAfter(paymentPeriods.get(0).getStartDate())) { // Use the first payment period if the date is before the start paymentPeriod = paymentPeriods.get(0); } else if (date.isAfter(paymentPeriods.get(paymentPeriods.size() - 1).getEndDate())) { // Use the last payment period if the date is after the end paymentPeriod = paymentPeriods.get(paymentPeriods.size() - 1); } else { paymentPeriod = findPaymentPeriod(date).get(); } if (!(paymentPeriod instanceof NotionalPaymentPeriod)) { return Optional.empty(); } return Optional.of(((NotionalPaymentPeriod) paymentPeriod).getNotionalAmount()); }
/** * 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()); }
public void test_findPaymentPeriod() { ResolvedSwapLeg test = ResolvedSwapLeg.builder() .type(IBOR) .payReceive(RECEIVE) .paymentPeriods(RPP1, RPP2) .build(); assertEquals(test.findPaymentPeriod(RPP1.getStartDate()), Optional.empty()); assertEquals(test.findPaymentPeriod(RPP1.getStartDate().plusDays(1)), Optional.of(RPP1)); assertEquals(test.findPaymentPeriod(RPP1.getEndDate()), Optional.of(RPP1)); assertEquals(test.findPaymentPeriod(RPP2.getStartDate()), Optional.of(RPP1)); assertEquals(test.findPaymentPeriod(RPP2.getStartDate().plusDays(1)), Optional.of(RPP2)); assertEquals(test.findPaymentPeriod(RPP2.getEndDate()), Optional.of(RPP2)); assertEquals(test.findPaymentPeriod(RPP2.getEndDate().plusDays(1)), Optional.empty()); }