/** * Finds the next business day, returning the input date if it is a business day. * <p> * Given a date, this method returns a business day. * If the input date is a business day, it is returned. * Otherwise, the next business day is returned. * * @param date the date to adjust * @return the input date if it is a business day, or the next business day * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate nextOrSame(LocalDate date) { return isHoliday(date) ? next(date) : date; }
public void test_SAT_SUN_next() { assertEquals(HolidayCalendars.SAT_SUN.next(THU_2014_07_10), FRI_2014_07_11); assertEquals(HolidayCalendars.SAT_SUN.next(FRI_2014_07_11), MON_2014_07_14); assertEquals(HolidayCalendars.SAT_SUN.next(SAT_2014_07_12), MON_2014_07_14); assertEquals(HolidayCalendars.SAT_SUN.next(SAT_2014_07_12), MON_2014_07_14); }
public void test_FRI_SAT_next() { assertEquals(HolidayCalendars.FRI_SAT.next(WED_2014_07_09), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.next(THU_2014_07_10), SUN_2014_07_13); assertEquals(HolidayCalendars.FRI_SAT.next(FRI_2014_07_11), SUN_2014_07_13); assertEquals(HolidayCalendars.FRI_SAT.next(SAT_2014_07_12), SUN_2014_07_13); assertEquals(HolidayCalendars.FRI_SAT.next(SUN_2014_07_13), MON_2014_07_14); }
public void test_THU_FRI_next() { assertEquals(HolidayCalendars.THU_FRI.next(WED_2014_07_09), SAT_2014_07_12); assertEquals(HolidayCalendars.THU_FRI.next(THU_2014_07_10), SAT_2014_07_12); assertEquals(HolidayCalendars.THU_FRI.next(FRI_2014_07_11), SAT_2014_07_12); assertEquals(HolidayCalendars.THU_FRI.next(SAT_2014_07_12), SUN_2014_07_13); }
/** * Shifts the date by the specified number of business days. * <p> * If the amount is zero, the input date is returned. * If the amount is positive, later business days are chosen. * If the amount is negative, earlier business days are chosen. * * @param date the date to adjust * @param amount the number of business days to adjust by * @return the shifted date * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate shift(LocalDate date, int amount) { LocalDate adjusted = date; if (amount > 0) { for (int i = 0; i < amount; i++) { adjusted = next(adjusted); } } else if (amount < 0) { for (int i = 0; i > amount; i--) { adjusted = previous(adjusted); } } return adjusted; }
/** * Finds the next business day, always returning a later date. * <p> * Given a date, this method returns the next business day. * * @param date the date to adjust * @return the first business day after the input date * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate next(LocalDate date) { LocalDate next = plusDays(date, 1); return isHoliday(next) ? next(next) : next; }
@Test(dataProvider = "next") public void test_next(LocalDate date, LocalDate expectedNext, HolidayCalendar cal) { assertEquals(cal.next(date), expectedNext); }
@Test(dataProvider = "next") public void test_next(LocalDate date, LocalDate expectedNext) { HolidayCalendar test = new MockHolCal(); assertEquals(test.next(date), expectedNext); }
/** * Checks if the specified date is the last business day of the month. * <p> * This returns true if the date specified is the last valid business day of the month. * * @param date the date to check * @return true if the specified date is the last business day of the month * @throws IllegalArgumentException if the date is outside the supported range */ public default boolean isLastBusinessDayOfMonth(LocalDate date) { return isBusinessDay(date) && next(date).getMonthValue() != date.getMonthValue(); }
@Override public LocalDate adjust(LocalDate date, HolidayCalendar calendar) { LocalDate adjusted = calendar.previousOrSame(date); if (adjusted.getMonth() != date.getMonth()) { adjusted = calendar.next(date); } return adjusted; } },
@Override public LocalDate adjust(LocalDate date, HolidayCalendar calendar) { if (calendar.isBusinessDay(date)) { return date; } if (date.getDayOfWeek() == SUNDAY || date.getDayOfWeek() == MONDAY) { return calendar.next(date); } else { return calendar.previous(date); } } };
@Override public LocalDate next(LocalDate date) { try { // day-of-month: minus one for zero-based day-of-month, plus one to start from next day return shiftNext(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), 1); } catch (ArrayIndexOutOfBoundsException ex) { return HolidayCalendar.super.next(date); } }
private double valuationCompositionFactor() { LocalDate currentFixing = nextFixing; LocalDate currentPublication = computation.calculatePublicationFromFixing(currentFixing); if (rates.getValuationDate().equals(currentPublication) && !(currentFixing.isAfter(lastFixing))) { OptionalDouble fixedRate = indexFixingDateSeries.get(currentFixing); if (fixedRate.isPresent()) { nextFixing = computation.getFixingCalendar().next(nextFixing); LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); double accrualFactor = dayCount.yearFraction(effectiveDate, maturityDate); return Math.pow(1.0d + fixedRate.getAsDouble(), accrualFactor); } } return 1.0d; }
OvernightIndexObservation indexObs = computation.observeOn(currentFixing); indexObsList.add(indexObs); currentFixing = computation.getFixingCalendar().next(currentFixing); accrualFactorAccumulated += indexObs.getYearFraction();
private double valuationCompositionFactor() { LocalDate currentFixing = nextFixing; LocalDate currentPublication = computation.calculatePublicationFromFixing(currentFixing); if (rates.getValuationDate().equals(currentPublication) && !(currentFixing.isAfter(lastFixingNonCutoff))) { // If currentFixing > lastFixingNonCutoff, everything fixed OptionalDouble fixedRate = indexFixingDateSeries.get(currentFixing); if (fixedRate.isPresent()) { nextFixing = computation.getFixingCalendar().next(nextFixing); LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); double accrualFactor = dayCount.yearFraction(effectiveDate, maturityDate); if (currentFixing.isBefore(lastFixingNonCutoff)) { return 1.0d + accrualFactor * fixedRate.getAsDouble(); } double compositionFactor = 1.0d + accrualFactor * fixedRate.getAsDouble(); for (int i = 0; i < cutoffOffset - 1; i++) { compositionFactor *= 1.0d + accrualFactorCutoff[i] * fixedRate.getAsDouble(); } return compositionFactor; } } return 1.0d; }
private double pastCompositionFactor() { double compositionFactor = 1.0d; LocalDate currentFixing = firstFixing; LocalDate currentPublication = computation.calculatePublicationFromFixing(currentFixing); while (!(currentFixing.isAfter(lastFixing)) && rates.getValuationDate().isAfter(currentPublication)) { LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); double accrualFactor = dayCount.yearFraction(effectiveDate, maturityDate); double rate = checkedFixing(currentFixing, indexFixingDateSeries, computation.getIndex()); compositionFactor *= Math.pow(1.0d + rate, accrualFactor); currentFixing = computation.getFixingCalendar().next(currentFixing); currentPublication = computation.calculatePublicationFromFixing(currentFixing); } nextFixing = currentFixing; return compositionFactor; }
private double pastCompositionFactor() { double compositionFactor = 1.0d; LocalDate currentFixing = firstFixing; LocalDate currentPublication = computation.calculatePublicationFromFixing(currentFixing); while ((currentFixing.isBefore(lastFixingNonCutoff)) && // fixing in the non-cutoff period rates.getValuationDate().isAfter(currentPublication)) { // publication before valuation LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); double accrualFactor = dayCount.yearFraction(effectiveDate, maturityDate); compositionFactor *= 1.0d + accrualFactor * checkedFixing(currentFixing, indexFixingDateSeries, computation.getIndex()); currentFixing = computation.getFixingCalendar().next(currentFixing); currentPublication = computation.calculatePublicationFromFixing(currentFixing); } if (currentFixing.equals(lastFixingNonCutoff) && // fixing is on the last non-cutoff date, cutoff period known rates.getValuationDate().isAfter(currentPublication)) { // publication before valuation double rate = checkedFixing(currentFixing, indexFixingDateSeries, computation.getIndex()); LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); double accrualFactor = dayCount.yearFraction(effectiveDate, maturityDate); compositionFactor *= 1.0d + accrualFactor * rate; for (int i = 0; i < cutoffOffset - 1; i++) { compositionFactor *= 1.0d + accrualFactorCutoff[i] * rate; } currentFixing = computation.getFixingCalendar().next(currentFixing); } nextFixing = currentFixing; return compositionFactor; }
double rate = rates.overnightIndexRates(USD_FED_FUND).rate(obs); PointSensitivityBuilder rateSensi = rates.overnightIndexRates(USD_FED_FUND).ratePointSensitivity(obs); LocalDate nextDate = cmp.getFixingCalendar().next(date); long days = DAYS.between(date, nextDate); expectedRate += rate * days;
double rate = rates.overnightIndexRates(USD_FED_FUND).rate(obs); PointSensitivityBuilder rateSensi = rates.overnightIndexRates(USD_FED_FUND).ratePointSensitivity(obs); LocalDate nextDate = cmp.getFixingCalendar().next(date); long days = nextDate.getMonthValue() != 4 ? DAYS.between(date, nextDate) : DAYS.between(date, endDate.plusDays(1)); expectedRate += rate * days;