/// <summary>Gets an adjusted date with respect to a specific <see cref="System.DateTime"/> object. /// </summary> /// <param name="date">The date.</param> /// <param name="holidayCalendar">The holiday calendar.</param> /// <returns>The <see cref="System.DateTime"/> object that is given by <paramref name="date"/> taken into account the business day /// convention represented by the current instance. /// </returns> /// <remarks>Perhaps the return value is not a business day, for example in the case of some end-of-month adjustment or 'no adjustment'.</remarks> public DateTime GetAdjustedDate(DateTime date, IHolidayCalendar holidayCalendar) { switch (date.Month) { case 1: case 2: case 3: var endOfQ1Date = new DateTime(date.Year, 3, 31); return(holidayCalendar.GetForwardAdjustedBusinessDay(endOfQ1Date)); case 4: case 5: case 6: var endOfQ2Date = new DateTime(date.Year, 6, 30); return(holidayCalendar.GetForwardAdjustedBusinessDay(endOfQ2Date)); case 7: case 8: case 9: var endOfQ3Date = new DateTime(date.Year, 9, 30); return(holidayCalendar.GetForwardAdjustedBusinessDay(endOfQ3Date)); default: var endOfQ4Date = new DateTime(date.Year, 12, 31); return(holidayCalendar.GetForwardAdjustedBusinessDay(endOfQ4Date)); } }
/// <summary>Gets the start and end date of the timeframe. /// </summary> /// <param name="referenceDate">A reference date (can be a business day or a holiday).</param> /// <param name="holidayCalendar">The (settlement) holiday calendar.</param> /// <param name="startDate">The start date of the time span with respect to <paramref name="referenceDate" /> (output).</param> /// <param name="endDate">The end date of the time span with respect to <paramref name="referenceDate" /> (output).</param> /// <param name="logger">An optional logger.</param> public void GetStartAndEndDate(DateTime referenceDate, IHolidayCalendar holidayCalendar, out DateTime startDate, out DateTime endDate, ILogger logger = null) { DateTime adjReferenceDate = SpotDateAdjustment.GetAdjustedDate(referenceDate, holidayCalendar); if ((logger != null) && (adjReferenceDate != referenceDate)) { logger.LogInformation("Reference date {0} has been adjusted to {1}.", referenceDate.ToShortDateString(), adjReferenceDate.ToShortDateString()); } DateTime spotDate = adjReferenceDate; switch (Tenor.TenorType) { case TenorType.Overnight: startDate = StartDateAdjustment.GetAdjustedDate(spotDate, holidayCalendar); endDate = EndDateAdjustment.GetAdjustedDate(startDate.AddDays(1), holidayCalendar); break; case TenorType.TomorrowNext: if ((SpotDateAdjustment.AdjustmentType != BusinessDayAdjustmentType.AdjustmentToBusinessDay) && (holidayCalendar.IsBusinessDay(spotDate) == false)) { spotDate = holidayCalendar.GetForwardAdjustedBusinessDay(spotDate); } startDate = StartDateAdjustment.GetAdjustedDate(holidayCalendar.AddBusinessDays(spotDate, 1), holidayCalendar); endDate = EndDateAdjustment.GetAdjustedDate(startDate.AddDays(1), holidayCalendar); break; case TenorType.RegularTenor: if ((SpotDateAdjustment.AdjustmentType != BusinessDayAdjustmentType.AdjustmentToBusinessDay) && (holidayCalendar.IsBusinessDay(spotDate) == false)) { if (BusinessDaysToSettle > 0) { spotDate = holidayCalendar.GetForwardAdjustedBusinessDay(spotDate); } else if (BusinessDaysToSettle < 0) { spotDate = holidayCalendar.GetPreviousAdjustedBusinessDay(spotDate); } } if (BusinessDaysToSettle != 0) { startDate = StartDateAdjustment.GetAdjustedDate(holidayCalendar.AddBusinessDays(spotDate, BusinessDaysToSettle), holidayCalendar); } else { startDate = StartDateAdjustment.GetAdjustedDate(spotDate, holidayCalendar); } endDate = EndDateAdjustment.GetAdjustedDate(startDate.AddTenorTimeSpan(Tenor), holidayCalendar); break; default: throw new NotImplementedException(); } }
/// <summary>Gets an adjusted date with respect to a specific <see cref="System.DateTime"/> object. /// </summary> /// <param name="date">The date.</param> /// <param name="holidayCalendar">The holiday calendar.</param> /// <returns>The <see cref="System.DateTime"/> object that is given by <paramref name="date"/> taken into account the business day /// convention represented by the current instance. /// </returns> /// <remarks>Perhaps the return value is not a business day, for example in the case of some end-of-month adjustment or 'no adjustment'.</remarks> public DateTime GetAdjustedDate(DateTime date, IHolidayCalendar holidayCalendar) { DateTime nextBusinessDay = holidayCalendar.GetForwardAdjustedBusinessDay(date); if (nextBusinessDay.Month == date.Month) { return(nextBusinessDay); } return(holidayCalendar.GetPreviousAdjustedBusinessDay(date)); }
/// <summary>Gets the start and end date of the timeframe. /// </summary> /// <param name="referenceDate">A reference date (can be a business day or a holiday).</param> /// <param name="holidayCalendar">The (settlement) holiday calendar.</param> /// <param name="startDate">The start date of the time span with respect to <paramref name="referenceDate" /> (output).</param> /// <param name="endDate">The end date of the time span with respect to <paramref name="referenceDate" /> (output).</param> /// <param name="logger">An optional logger.</param> public void GetStartAndEndDate(DateTime referenceDate, IHolidayCalendar holidayCalendar, out DateTime startDate, out DateTime endDate, ILogger logger = null) { DateTime adjReferenceDate = SpotDateAdjustment.GetAdjustedDate(referenceDate, holidayCalendar); if ((logger != null) && (adjReferenceDate != referenceDate)) { logger.LogInformation("Reference date {0} has been adjusted to {1}.", referenceDate.ToShortDateString(), adjReferenceDate.ToShortDateString()); } DateTime spotDate = adjReferenceDate; if ((SpotDateAdjustment.AdjustmentType != BusinessDayAdjustmentType.AdjustmentToBusinessDay) && (holidayCalendar.IsBusinessDay(spotDate) == false)) { if (BusinessDaysToSettle > 0) { spotDate = holidayCalendar.GetForwardAdjustedBusinessDay(spotDate); } else if (BusinessDaysToSettle < 0) { spotDate = holidayCalendar.GetPreviousAdjustedBusinessDay(spotDate); } } if (BusinessDaysToSettle != 0) { startDate = StartDateAdjustment.GetAdjustedDate(holidayCalendar.AddBusinessDays(spotDate, BusinessDaysToSettle), holidayCalendar); } else { startDate = StartDateAdjustment.GetAdjustedDate(spotDate, holidayCalendar); } endDate = EndDateAdjustment.GetAdjustedDate(EndDate, holidayCalendar); if ((logger != null) && (endDate != EndDate)) { logger.LogInformation("End date {0} has been adjusted to {1}.", EndDate.ToShortDateString(), endDate.ToShortDateString()); } }
/// <summary>Gets an adjusted date with respect to a specific <see cref="System.DateTime"/> object. /// </summary> /// <param name="date">The date.</param> /// <param name="holidayCalendar">The holiday calendar.</param> /// <returns>The <see cref="System.DateTime"/> object that is given by <paramref name="date"/> taken into account the business day /// convention represented by the current instance. /// </returns> /// <remarks>Perhaps the return value is not a business day, for example in the case of some end-of-month adjustment or 'no adjustment'.</remarks> public DateTime GetAdjustedDate(DateTime date, IHolidayCalendar holidayCalendar) { DateTime lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)); return(holidayCalendar.GetForwardAdjustedBusinessDay(lastDayOfMonth)); }
/// <summary>Gets an adjusted date with respect to a specific <see cref="System.DateTime"/> object. /// </summary> /// <param name="date">The date.</param> /// <param name="holidayCalendar">The holiday calendar.</param> /// <returns>The <see cref="System.DateTime"/> object that is given by <paramref name="date"/> taken into account the business day /// convention represented by the current instance. /// </returns> /// <remarks>Perhaps the return value is not a business day, for example in the case of some end-of-month adjustment or 'no adjustment'.</remarks> public DateTime GetAdjustedDate(DateTime date, IHolidayCalendar holidayCalendar) { return(holidayCalendar.GetForwardAdjustedBusinessDay(date)); }