private Optional<FxReset> buildFxReset( int periodIndex, SchedulePeriod period, DateAdjuster fixingDateAdjuster, Function<LocalDate, FxIndexObservation> obsFn) { if (periodIndex == 0 && initialNotionalValue != null) { //if first notional is fixed then no FxReset is applied return Optional.empty(); } LocalDate fixingDate = fixingDateAdjuster.adjust(fixingRelativeTo.selectBaseDate(period)); return Optional.of(FxReset.of(obsFn.apply(fixingDate), referenceCurrency)); }
@Test(dataProvider = "nextLeapDay") public void test_nextOrSameLeapDay_Temporal(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = (LocalDate) DateAdjusters.nextOrSameLeapDay().adjustInto(date); if (month == 2 && day == 29) { assertEquals(test, date); } else { assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); } }
private IborIndexObservation create( LocalDate fixingDate, HolidayCalendar fixingCal, DateAdjuster effectiveAdjuster, DateAdjuster maturityAdjuster) { LocalDate fixingBusinessDay = fixingCal.nextOrSame(fixingDate); LocalDate effectiveDate = effectiveAdjuster.adjust(fixingBusinessDay); LocalDate maturityDate = maturityAdjuster.adjust(effectiveDate); double yearFraction = dayCount.yearFraction(effectiveDate, maturityDate); return new IborIndexObservation(this, fixingDate, effectiveDate, maturityDate, yearFraction); }
@Test(dataProvider = "nextLeapDay") public void test_nextLeapDay_Temporal(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = (LocalDate) DateAdjusters.nextLeapDay().adjustInto(date); assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); }
private ImmutableList<SwapPaymentEvent> createEvents( List<NotionalPaymentPeriod> adjPaymentPeriods, DateAdjuster paymentDateAdjuster, ReferenceData refData) { ImmutableList.Builder<SwapPaymentEvent> events = ImmutableList.builder(); LocalDate initialExchangeDate = paymentDateAdjuster.adjust(adjPaymentPeriods.get(0).getStartDate()); events.addAll(NotionalSchedule.createEvents( adjPaymentPeriods, initialExchangeDate, initialExchange, intermediateExchange, finalExchange, refData)); paymentEvents.forEach(pe -> events.add(pe.adjustPaymentDate(paymentDateAdjuster))); return events.build(); }
private FxIndexObservation create( LocalDate fixingDate, HolidayCalendar fixingCal, DateAdjuster maturityAdjuster) { LocalDate fixingBusinessDay = fixingCal.nextOrSame(fixingDate); LocalDate maturityDate = maturityAdjuster.adjust(fixingBusinessDay); return new FxIndexObservation(this, fixingDate, maturityDate); }
/** * Converts this period to one where the start and end dates are adjusted using the specified adjuster. * <p> * The start date of the result will be the start date of this period as altered by the specified adjuster. * The end date of the result will be the end date of this period as altered by the specified adjuster. * The unadjusted start date and unadjusted end date will be the same as in this period. * <p> * The adjuster will typically be obtained from {@link BusinessDayAdjustment#resolve(ReferenceData)}. * * @param adjuster the adjuster to use * @return the adjusted schedule period */ public SchedulePeriod toAdjusted(DateAdjuster adjuster) { // implementation needs to return 'this' if unchanged to optimize downstream code LocalDate resultStart = adjuster.adjust(startDate); LocalDate resultEnd = adjuster.adjust(endDate); if (resultStart.equals(startDate) && resultEnd.equals(endDate)) { return this; } return of(resultStart, resultEnd, unadjustedStartDate, unadjustedEndDate); }
/** * Adjusts the temporal according to the rules of the implementation. * <p> * This method implements {@link TemporalAdjuster} by calling {@link #adjust(LocalDate)}. * Note that conversion to {@code LocalDate} ignores the calendar system * of the input, which is the desired behaviour in this case. * * @param temporal the temporal to adjust * @return the adjusted temporal * @throws DateTimeException if unable to make the adjustment * @throws ArithmeticException if numeric overflow occurs */ @Override public default Temporal adjustInto(Temporal temporal) { // conversion to LocalDate ensures that other calendar systems are ignored return temporal.with(adjust(LocalDate.from(temporal))); }
@Test(dataProvider = "convention", dataProviderClass = BusinessDayConventionTest.class) public void test_adjustDate(BusinessDayConvention convention, LocalDate input, LocalDate expected) { BusinessDayAdjustment test = BusinessDayAdjustment.of(convention, SAT_SUN); assertEquals(test.adjust(input, REF_DATA), expected); assertEquals(test.resolve(REF_DATA).adjust(input), expected); }
@Test(dataProvider = "nextLeapDay") public void test_nextOrSameLeapDay_LocalDate(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = DateAdjusters.nextOrSameLeapDay().adjust(date); if (month == 2 && day == 29) { assertEquals(test, date); } else { assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); } }
@Test(dataProvider = "nextLeapDay") public void test_nextLeapDay_LocalDate(int year, int month, int day, int expectedYear) { LocalDate date = LocalDate.of(year, month, day); LocalDate test = DateAdjusters.nextLeapDay().adjust(date); assertEquals(test.getYear(), expectedYear); assertEquals(test.getMonthValue(), 2); assertEquals(test.getDayOfMonth(), 29); }
@Override public FraTrade createTrade( LocalDate tradeDate, Period periodToStart, Period periodToEnd, BuySell buySell, double notional, double fixedRate, ReferenceData refData) { LocalDate spotValue = calculateSpotDateFromTradeDate(tradeDate, refData); LocalDate startDate = spotValue.plus(periodToStart); LocalDate endDate = spotValue.plus(periodToEnd); DateAdjuster bda = getBusinessDayAdjustment().resolve(refData); // start/end dates are adjusted when FRA trade is created and not adjusted later // payment date is adjusted when FRA trade is created and potentially adjusted again when resolving LocalDate adjustedStart = bda.adjust(startDate); LocalDate adjustedEnd = bda.adjust(endDate); LocalDate adjustedPay = getPaymentDateOffset().adjust(adjustedStart, refData); return toTrade(tradeDate, adjustedStart, adjustedEnd, adjustedPay, buySell, notional, fixedRate); }
@Test(dataProvider = "adjust") public void test_adjust(int months, LocalDate date, LocalDate expected) { PeriodAdjustment test = PeriodAdjustment.of(Period.ofMonths(months), LAST_DAY, BDA_FOLLOW_SAT_SUN); assertEquals(test.adjust(date, REF_DATA), expected); assertEquals(test.resolve(REF_DATA).adjust(date), expected); }
@Test(dataProvider = "adjust") public void test_adjust(int months, LocalDate date, LocalDate expected) { TenorAdjustment test = TenorAdjustment.of(Tenor.ofMonths(months), LAST_DAY, BDA_FOLLOW_SAT_SUN); assertEquals(test.adjust(date, REF_DATA), expected); assertEquals(test.resolve(REF_DATA).adjust(date), expected); }
private RateComputation createRateComputationWithResetPeriods( Schedule resetSchedule, DateAdjuster fixingDateAdjuster, Function<LocalDate, IborIndexObservation> iborObservationFn, int scheduleIndex, Double overrideFirstRate, ReferenceData refData) { List<IborAveragedFixing> fixings = new ArrayList<>(); for (int i = 0; i < resetSchedule.size(); i++) { SchedulePeriod resetPeriod = resetSchedule.getPeriod(i); LocalDate fixingDate = fixingDateAdjuster.adjust(fixingRelativeTo.selectBaseDate(resetPeriod)); if (scheduleIndex == 0 && i == 0 && firstFixingDateOffset != null) { fixingDate = firstFixingDateOffset.resolve(refData).adjust(fixingRelativeTo.selectBaseDate(resetPeriod)); } fixings.add(IborAveragedFixing.builder() .observation(iborObservationFn.apply(fixingDate)) .fixedRate(overrideFirstRate != null && i == 0 ? overrideFirstRate : null) .weight(resetPeriods.getResetMethod() == UNWEIGHTED ? 1 : resetPeriod.lengthInDays()) .build()); } return IborAveragedRateComputation.of(fixings); }
@Override public ResolvedTermDeposit resolve(ReferenceData refData) { DateAdjuster bda = getBusinessDayAdjustment().orElse(BusinessDayAdjustment.NONE).resolve(refData); LocalDate start = bda.adjust(startDate); LocalDate end = bda.adjust(endDate); double yearFraction = dayCount.yearFraction(start, end); return ResolvedTermDeposit.builder() .startDate(start) .endDate(end) .yearFraction(yearFraction) .currency(getCurrency()) .notional(buySell.normalize(notional)) .rate(rate) .build(); }
private List<SwapPaymentPeriod> createPaymentPeriods(Schedule resolvedPayments, ReferenceData refData) { // resolve amount schedule against payment schedule DoubleArray amounts = amount.resolveValues(resolvedPayments); // resolve against reference data once DateAdjuster paymentDateAdjuster = paymentSchedule.getPaymentDateOffset().resolve(refData); // build up payment periods using schedule ImmutableList.Builder<SwapPaymentPeriod> paymentPeriods = ImmutableList.builder(); for (int index = 0; index < resolvedPayments.size(); index++) { SchedulePeriod paymentPeriod = resolvedPayments.getPeriod(index); LocalDate baseDate = paymentSchedule.getPaymentRelativeTo().selectBaseDate(paymentPeriod); LocalDate paymentDate = paymentDateAdjuster.adjust(baseDate); double amount = payReceive.normalize(amounts.get(index)); Payment payment = Payment.of(CurrencyAmount.of(currency, amount), paymentDate); paymentPeriods.add(KnownAmountSwapPaymentPeriod.of(payment, paymentPeriod)); } return paymentPeriods.build(); }
public void test_ofBusinessDays3_adjust() { ImmutableHolidayCalendar cal = ImmutableHolidayCalendar.of(WED_THU, ImmutableList.of(), WEDNESDAY, THURSDAY); ReferenceData refData = ImmutableReferenceData.of(ImmutableMap.of(WED_THU, cal)).combinedWith(REF_DATA); DaysAdjustment test = DaysAdjustment.ofBusinessDays(3, SAT_SUN, BDA_FOLLOW_WED_THU); LocalDate base = date(2014, 8, 15); // Fri assertEquals(test.adjust(base, refData), date(2014, 8, 22)); // Fri (3 days gives Wed, following moves to Fri) assertEquals(test.resolve(refData).adjust(base), date(2014, 8, 22)); // Fri (3 days gives Wed, following moves to Fri) }
LocalDate paymentDate = paymentDateAdjuster.adjust(paymentRelativeTo.selectBaseDate(paymentPeriod));