/** * Finds the previous 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 previous business day is returned. * * @param date the date to adjust * @return the input date if it is a business day, or the previous business day * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate previousOrSame(LocalDate date) { return isHoliday(date) ? previous(date) : date; }
public void test_THU_FRI_previous() { assertEquals(HolidayCalendars.THU_FRI.previous(THU_2014_07_10), WED_2014_07_09); assertEquals(HolidayCalendars.THU_FRI.previous(FRI_2014_07_11), WED_2014_07_09); assertEquals(HolidayCalendars.THU_FRI.previous(SAT_2014_07_12), WED_2014_07_09); assertEquals(HolidayCalendars.THU_FRI.previous(SUN_2014_07_13), SAT_2014_07_12); }
private LocalDate previousOutOfRange(LocalDate date) { if (date.getYear() >= 0 && date.getYear() < 10000) { return HolidayCalendar.super.previous(date); } throw new IllegalArgumentException("Date is outside the accepted range (year 0000 to 10,000): " + date); }
public void test_FRI_SAT_previous() { assertEquals(HolidayCalendars.FRI_SAT.previous(FRI_2014_07_11), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.previous(SAT_2014_07_12), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.previous(SUN_2014_07_13), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.previous(MON_2014_07_14), SUN_2014_07_13); }
public void test_SAT_SUN_previous() { assertEquals(HolidayCalendars.SAT_SUN.previous(SAT_2014_07_12), FRI_2014_07_11); assertEquals(HolidayCalendars.SAT_SUN.previous(SUN_2014_07_13), FRI_2014_07_11); assertEquals(HolidayCalendars.SAT_SUN.previous(MON_2014_07_14), FRI_2014_07_11); assertEquals(HolidayCalendars.SAT_SUN.previous(TUE_2014_07_15), MON_2014_07_14); }
/** * 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 previous business day, always returning an earlier date. * <p> * Given a date, this method returns the previous business day. * * @param date the date to adjust * @return the first business day before the input date * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate previous(LocalDate date) { LocalDate previous = plusDays(date, -1); return isHoliday(previous) ? previous(previous) : previous; }
@Test(dataProvider = "previous") public void test_previous(LocalDate date, LocalDate expectedPrevious, HolidayCalendar cal) { assertEquals(cal.previous(date), expectedPrevious); }
@Test(dataProvider = "previous") public void test_previous(LocalDate date, LocalDate expectedPrevious) { HolidayCalendar test = new MockHolCal(); assertEquals(test.previous(date), expectedPrevious); }
@Override public LocalDate adjust(LocalDate date) { ArgChecker.notNull(date, "date"); return ausy.previous(date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY))); } },
@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); } } };
/** * Finds the next business day within the month, returning the input date if it is a business day, * or the last business day of the month if the next business day is in a different month. * <p> * Given a date, this method returns a business day. * If the input date is a business day, it is returned. * If the next business day is within the same month, it is returned. * Otherwise, the last business day of the month is returned. * <p> * Note that the result of this method may be earlier than the input date. * <p> * This corresponds to the {@linkplain BusinessDayConventions#MODIFIED_FOLLOWING modified following} * business day convention. * * @param date the date to adjust * @return the input date if it is a business day, the next business day if within the same month * or the last business day of the month * @throws IllegalArgumentException if the calculation is outside the supported range */ public default LocalDate nextSameOrLastInMonth(LocalDate date) { LocalDate nextOrSame = nextOrSame(date); return (nextOrSame.getMonthValue() != date.getMonthValue() ? previous(date) : nextOrSame); }
@Override public LocalDate adjust(LocalDate date, HolidayCalendar calendar) { LocalDate adjusted = calendar.nextOrSame(date); if (adjusted.getMonthValue() != date.getMonthValue() || (adjusted.getDayOfMonth() > 15 && date.getDayOfMonth() <= 15)) { adjusted = calendar.previous(date); } return adjusted; } },
private ObservationDetails(OvernightCompoundedRateComputation computation, OvernightIndexRates rates) { this.computation = computation; this.rates = rates; this.indexFixingDateSeries = rates.getFixings(); this.dayCount = computation.getIndex().getDayCount(); // Details of the cutoff period this.firstFixing = computation.getStartDate(); this.lastFixingP1 = computation.getEndDate(); this.lastFixing = computation.getFixingCalendar().previous(lastFixingP1); this.cutoffOffset = Math.max(computation.getRateCutOffDays(), 1); this.accrualFactorCutoff = new double[cutoffOffset - 1]; LocalDate currentFixing = lastFixing; for (int i = 0; i < cutoffOffset - 1; i++) { currentFixing = computation.getFixingCalendar().previous(currentFixing); LocalDate effectiveDate = computation.calculateEffectiveFromFixing(currentFixing); LocalDate maturityDate = computation.calculateMaturityFromEffective(effectiveDate); accrualFactorCutoff[i] = dayCount.yearFraction(effectiveDate, maturityDate); } this.lastFixingNonCutoff = currentFixing; LocalDate startUnderlyingPeriod = computation.calculateEffectiveFromFixing(firstFixing); LocalDate endUnderlyingPeriod = computation.calculateMaturityFromFixing(lastFixing); this.accrualFactorTotal = dayCount.yearFraction(startUnderlyingPeriod, endUnderlyingPeriod); }
private ObservationDetails(OvernightCompoundedAnnualRateComputation computation, OvernightIndexRates rates) { this.computation = computation; this.rates = rates; this.indexFixingDateSeries = rates.getFixings(); this.dayCount = computation.getIndex().getDayCount(); this.firstFixing = computation.getStartDate(); this.lastFixingP1 = computation.getEndDate(); this.lastFixing = computation.getFixingCalendar().previous(lastFixingP1); LocalDate startUnderlyingPeriod = computation.calculateEffectiveFromFixing(firstFixing); LocalDate endUnderlyingPeriod = computation.calculateMaturityFromFixing(lastFixing); this.accrualFactorTotal = dayCount.yearFraction(startUnderlyingPeriod, endUnderlyingPeriod); }
private double rateForward(OvernightAveragedRateComputation computation, OvernightIndexRates rates) { OvernightIndex index = computation.getIndex(); HolidayCalendar calendar = computation.getFixingCalendar(); LocalDate startFixingDate = computation.getStartDate(); LocalDate endFixingDateP1 = computation.getEndDate(); LocalDate endFixingDate = calendar.previous(endFixingDateP1); LocalDate onRateEndDate = computation.calculateMaturityFromFixing(endFixingDate); LocalDate onRateStartDate = computation.calculateEffectiveFromFixing(startFixingDate); LocalDate onRateNoCutOffEndDate = onRateEndDate; int cutoffOffset = computation.getRateCutOffDays() > 1 ? computation.getRateCutOffDays() : 1; double accumulatedInterest = 0.0d; double accrualFactorTotal = index.getDayCount().yearFraction(onRateStartDate, onRateEndDate); if (cutoffOffset > 1) { // Cut-off period LocalDate currentFixingDate = endFixingDate; OvernightIndexObservation lastIndexObs = null; double cutOffAccrualFactorTotal = 0d; for (int i = 1; i < cutoffOffset; i++) { currentFixingDate = calendar.previous(currentFixingDate); lastIndexObs = computation.observeOn(currentFixingDate); onRateNoCutOffEndDate = lastIndexObs.getMaturityDate(); cutOffAccrualFactorTotal += lastIndexObs.getYearFraction(); } double forwardRateCutOff = rates.rate(lastIndexObs); accumulatedInterest += cutOffAccrualFactorTotal * forwardRateCutOff; } // Approximated part accumulatedInterest += approximatedInterest(computation.observeOn(onRateStartDate), onRateNoCutOffEndDate, rates); // final rate return accumulatedInterest / accrualFactorTotal; }
LocalDate startFixingDate = computation.getStartDate(); LocalDate endFixingDateP1 = computation.getEndDate(); LocalDate endFixingDate = calendar.previous(endFixingDateP1); LocalDate onRateEndDate = computation.calculateMaturityFromFixing(endFixingDate); LocalDate onRateStartDate = computation.calculateEffectiveFromFixing(startFixingDate); LocalDate cutOffEffectiveDate; for (int i = 0; i < cutoffOffset; i++) { currentFixingDate = calendar.previous(currentFixingDate); cutOffEffectiveDate = computation.calculateEffectiveFromFixing(currentFixingDate); lastNonCutOffMatDate = computation.calculateMaturityFromEffective(cutOffEffectiveDate);
lastNonCutoffFixing = computation.getFixingCalendar().previous(lastNonCutoffFixing); lastIndexObs = computation.observeOn(lastNonCutoffFixing); accrualFactorTotal += lastIndexObs.getYearFraction();
lastNonCutoffFixing = computation.getFixingCalendar().previous(lastNonCutoffFixing); lastIndexObs = computation.observeOn(lastNonCutoffFixing); accrualFactorTotal += lastIndexObs.getYearFraction();