private static void GeneratePeriodsBackward(DateTime firstRollDate, CalculationPeriodFrequency frequency,
                                                    BusinessDayAdjustments calculationPeriodDatesAdjustments,
                                                    CalculationPeriodsPrincipalExchangesAndStubs result,
                                                    DateTime adjustedEffectiveDate, StubPeriodTypeEnum?initialStubType
                                                    , IBusinessCalendar paymentCalendar)
        {
            DateTime periodEndDate               = firstRollDate;
            DateTime periodStartDate             = AddPeriod(periodEndDate, IntervalHelper.FromFrequency(frequency), -1);
            bool     encounteredShortInitialStub = false;

            //if (paymentCalendar == null)
            //{
            //    paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, calculationPeriodDatesAdjustments.businessCenters);
            //}
            do
            {
                var calculationPeriod = new CalculationPeriod();
                //  Apply ROLL CONVENTION (NOT A BUSINESS DAY CONVENTION!) to unadjusted period start and end dates.
                //
                DateTime rollConventionAdjustedPeriodStartDate = periodStartDate;
                DateTime rollConventionAdjustedPeriodEndDate   = periodEndDate;
                var      frequencyPeriod = EnumHelper.Parse <PeriodEnum>(frequency.period, true);
                if (frequencyPeriod != PeriodEnum.D)//adjust if the frequency is NOT expressed in days
                {
                    rollConventionAdjustedPeriodStartDate = RollConventionEnumHelper.AdjustDate(frequency.rollConvention, periodStartDate);
                    rollConventionAdjustedPeriodEndDate   = RollConventionEnumHelper.AdjustDate(frequency.rollConvention, periodEndDate);
                }
                CalculationPeriodHelper.SetUnadjustedDates(calculationPeriod, rollConventionAdjustedPeriodStartDate, rollConventionAdjustedPeriodEndDate);
                //  Set adjusted period dates
                //
                DateTime adjustedPeriodStartDate = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, rollConventionAdjustedPeriodStartDate, calculationPeriodDatesAdjustments);
                DateTime adjustedPeriodEndDate   = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, rollConventionAdjustedPeriodEndDate, calculationPeriodDatesAdjustments);
                CalculationPeriodHelper.SetAdjustedDates(calculationPeriod, adjustedPeriodStartDate, adjustedPeriodEndDate);
                //if (calculationPeriod.unadjustedStartDate > adjustedEffectiveDate)
                if (calculationPeriod.adjustedStartDate > adjustedEffectiveDate)
                {
                    result.InsertFirst(calculationPeriod);
                    periodEndDate   = periodStartDate;
                    periodStartDate = AddPeriod(periodEndDate, IntervalHelper.FromFrequency(frequency), -1);
                }
                //else if (calculationPeriod.unadjustedStartDate == adjustedEffectiveDate)//first period - not stub
                else if (calculationPeriod.adjustedStartDate == adjustedEffectiveDate)//first period - not stub
                {
                    result.InsertFirst(calculationPeriod);
                    break;
                }
                else//first period - short stub (merge with next period if a long stub specified)
                {
                    encounteredShortInitialStub = true;
                    //calculationPeriod.unadjustedStartDate = adjustedEffectiveDate;
                    calculationPeriod.adjustedStartDate = adjustedEffectiveDate;
                    result.InitialStubCalculationPeriod = calculationPeriod;
                    break;
                }
            } while (true);
            if (encounteredShortInitialStub && initialStubType == StubPeriodTypeEnum.LongInitial)
            {
                result.CreateLongInitialStub();
            }
        }
        /// <summary>
        /// Gets the adjusted calculation period start dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="firstRegularPeriodDate">The first regular period date.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <param name="businessCalendar">The businessCalendar.</param>
        /// <param name="businessDayConvention">The business day convention.</param>
        /// <returns>A vertical range of dates.</returns>
        public static DateTime[] GetAdjustedCalculationPeriodDates(DateTime effectiveDate, DateTime terminationDate,
                                                                   string periodInterval, string rollConvention, DateTime firstRegularPeriodDate, string stubPeriodType,
                                                                   IBusinessCalendar businessCalendar, string businessDayConvention)
        {
            const string       dateToReturn = "unadjustedStartDate";
            StubPeriodTypeEnum?stubType     = null;

            if (!string.IsNullOrEmpty(stubPeriodType))
            {
                stubType = (StubPeriodTypeEnum)Enum.Parse(typeof(StubPeriodTypeEnum), stubPeriodType, true);
            }
            var periods  = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, firstRegularPeriodDate, PeriodHelper.Parse(periodInterval), RollConventionEnumHelper.Parse(rollConvention), stubType);
            var dates    = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(periods, dateToReturn);
            var newDates = new DateTime[dates.Count];
            var index    = 0;

            foreach (var date in dates)
            {
                var newDate = BusinessCalendarHelper.Advance(businessCalendar, date, "Calendar", "0D", businessDayConvention);
                newDates[index] = newDate;
                index++;
            }
            var result = newDates;

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Generates the unadjusted calculation dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="calculationPeriodDates">The calculation period dates.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDates(DateTime effectiveDate, DateTime terminationDate, CalculationPeriodDates calculationPeriodDates)
        {
            DateTime startDate = effectiveDate;
            DateTime endDate   = terminationDate;

            var unadjustedDatePeriods = new List <CalculationPeriod>();

            AdjustableDate firstPeriodStartDate = calculationPeriodDates.firstPeriodStartDate;

            if (firstPeriodStartDate != null)
            {
                // Use the firstPeriodStartDate as the start date if it has been specified
                if ((firstPeriodStartDate.unadjustedDate != null) && (firstPeriodStartDate.unadjustedDate.Value != effectiveDate))
                {
                    startDate = firstPeriodStartDate.unadjustedDate.Value;
                }
            }

            DateTime firstRegularPeriodDate = startDate;

            if (calculationPeriodDates.firstRegularPeriodStartDateSpecified)
            {
                firstRegularPeriodDate = calculationPeriodDates.firstRegularPeriodStartDate;
            }

            StubPeriodTypeEnum?stubPeriodType = null;

            stubPeriodType = calculationPeriodDates.stubPeriodTypeSpecified ? calculationPeriodDates.stubPeriodType : stubPeriodType;

            if (calculationPeriodDates.firstRegularPeriodStartDateSpecified && calculationPeriodDates.lastRegularPeriodEndDateSpecified)
            {
                //Dates must converge
                Boolean bContainsIrregularPeriod;
                List <CalculationPeriod> periods = GetForwardRegularPeriods(calculationPeriodDates.firstRegularPeriodStartDate, endDate, calculationPeriodDates.calculationPeriodFrequency, out bContainsIrregularPeriod);

                if ((periods.Count > 0) && (periods[periods.Count - 1].unadjustedEndDate != calculationPeriodDates.lastRegularPeriodEndDate))
                {
                    throw new ArgumentOutOfRangeException("Irregular period found between the first regular period start and last regular period end");
                }

                unadjustedDatePeriods = GenerateUnadjustedCalculationDates(startDate, endDate, firstRegularPeriodDate, calculationPeriodDates.calculationPeriodFrequency, stubPeriodType);
            }
            else if (calculationPeriodDates.firstRegularPeriodStartDateSpecified)
            {
                unadjustedDatePeriods = GenerateUnadjustedCalculationDates(startDate, endDate, firstRegularPeriodDate, calculationPeriodDates.calculationPeriodFrequency, stubPeriodType);
            }
            else if (calculationPeriodDates.lastRegularPeriodEndDateSpecified)
            {
                Period periodInterval = CalculationPeriodFrequencyToInterval(calculationPeriodDates.calculationPeriodFrequency);
                unadjustedDatePeriods = GenerateUnadjustedCalculationDatesFromTermDate(startDate, endDate, periodInterval, calculationPeriodDates.lastRegularPeriodEndDate, calculationPeriodDates.calculationPeriodFrequency.rollConvention, stubPeriodType);
            }

            return(unadjustedDatePeriods);
        }
Esempio n. 4
0
        public List <DateTime> BuildDates(ILogger logger, ICoreCache cache, String nameSpace, BillSwapPricerDatesRange billSwapPricerDatesRange, IBusinessCalendar paymentCalendar)
        {
            CalculationPeriodFrequency frequency   = CalculationPeriodFrequencyHelper.Parse(billSwapPricerDatesRange.RollFrequency, billSwapPricerDatesRange.RollConvention);
            StubPeriodTypeEnum?        initialStub = null;

            if (!String.IsNullOrEmpty(billSwapPricerDatesRange.InitialStubPeriod))
            {
                initialStub = EnumHelper.Parse <StubPeriodTypeEnum>(billSwapPricerDatesRange.InitialStubPeriod);
            }
            StubPeriodTypeEnum?finalStub = null;

            if (!String.IsNullOrEmpty(billSwapPricerDatesRange.FinalStubPeriod))
            {
                finalStub = EnumHelper.Parse <StubPeriodTypeEnum>(billSwapPricerDatesRange.FinalStubPeriod);
            }
            //BusinessDayAdjustments adjustments = BusinessDayAdjustmentsHelper.Create(BusinessDayConventionEnum.NONE, "");
            //if (paymentCalendar == null)
            //{
            //    paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, adjustments.businessCenters);//Make sure this builds a valid calendar!
            //}
            BusinessDayAdjustments calculationPeriodDayAdjustments = BusinessDayAdjustmentsHelper.Create(billSwapPricerDatesRange.BusinessDayConvention, billSwapPricerDatesRange.Calendar);

            if (paymentCalendar == null)
            {
                paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, calculationPeriodDayAdjustments.businessCenters, nameSpace);
            }
            CalculationPeriodsPrincipalExchangesAndStubs result = CalculationPeriodGenerator.GenerateAdjustedCalculationPeriods(
                billSwapPricerDatesRange.StartDate,
                billSwapPricerDatesRange.EndDate,
                billSwapPricerDatesRange.FirstRegularPeriodStartDate,
                frequency,
                calculationPeriodDayAdjustments,
                initialStub,
                finalStub,
                paymentCalendar
                );

            foreach (CalculationPeriod regularCalculationPeriod in result.GetRegularAndStubPeriods())
            {
                // Adjust both startDate & endDate of period
                //
                CalculationPeriodHelper.SetAdjustedDates(regularCalculationPeriod,
                                                         AdjustedDateHelper.ToAdjustedDate(paymentCalendar, regularCalculationPeriod.unadjustedStartDate, calculationPeriodDayAdjustments),
                                                         AdjustedDateHelper.ToAdjustedDate(paymentCalendar, regularCalculationPeriod.unadjustedEndDate, calculationPeriodDayAdjustments));
            }
            var listResult = result.GetRegularAndStubPeriods().Select(regularCalculationPeriod => regularCalculationPeriod.adjustedStartDate).ToList();

            listResult.Add(result.GetRegularAndStubPeriods()[result.GetRegularAndStubPeriods().Count - 1].adjustedEndDate);
            return(listResult);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the unadjusted the calculation dates
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="intervalToFirstRegularPeriodStart">The interval to first regular period start.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        public static DateTime[] UnadjustedCalculationDatesFromFirstRegularInterval(DateTime effectiveDate, DateTime terminationDate, string intervalToFirstRegularPeriodStart, string periodInterval, string rollConvention, string stubPeriodType)
        {
            const string       dateToReturn = "unadjustedStartDate";
            StubPeriodTypeEnum?stubType     = null;

            if (!string.IsNullOrEmpty(stubPeriodType))
            {
                stubType = (StubPeriodTypeEnum)Enum.Parse(typeof(StubPeriodTypeEnum), stubPeriodType, true);
            }
            var periods = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, PeriodHelper.Parse(periodInterval), PeriodHelper.Parse(intervalToFirstRegularPeriodStart), RollConventionEnumHelper.Parse(rollConvention), stubType);
            var dates   = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(periods, dateToReturn);
            var result  = dates.ToArray();

            return(result);
        }
        public static CalculationPeriodsPrincipalExchangesAndStubs GenerateAdjustedCalculationPeriods(
            DateTime unadjustedEffectiveDate,
            DateTime unadjustedTerminationDate,
            DateTime unadjustedFirstRollDate, //this one is still UN adjusted
            CalculationPeriodFrequency frequency,
            BusinessDayAdjustments calculationPeriodDatesAdjustments,
            StubPeriodTypeEnum?initialStubType,
            StubPeriodTypeEnum?finalStubType,
            IBusinessCalendar paymentCalendar)
        {
            #region Param check

            if (initialStubType.HasValue)
            {
                if ((initialStubType != StubPeriodTypeEnum.LongInitial) && (initialStubType != StubPeriodTypeEnum.ShortInitial))
                {
                    throw new ArgumentOutOfRangeException(nameof(initialStubType), initialStubType, "Wrong stub type.");
                }
            }
            if (finalStubType.HasValue)
            {
                if ((finalStubType == StubPeriodTypeEnum.LongFinal) && (initialStubType == StubPeriodTypeEnum.ShortFinal))
                {
                    throw new ArgumentOutOfRangeException(nameof(finalStubType), finalStubType, "Wrong stub type.");
                }
            }
//            if (!RollConventionEnumHelper.IsAdjusted(frequency.rollConvention, unadjustedFirstRollDate))
//            {
//                string message = String.Format("First roll date {0}, should be pre-adjusted according to RollConvention {1}", unadjustedFirstRollDate, frequency.rollConvention);
//                throw new Exception(message);
//            }

            #endregion

            var result = new CalculationPeriodsPrincipalExchangesAndStubs();
            //  Start calculations from 'first roll date' and generate dates backwards and forwards
            //
            //  Generate periods backwards (toward the unadjustedEffectiveDate)
            //
            GeneratePeriodsBackward(unadjustedFirstRollDate, frequency, calculationPeriodDatesAdjustments, result, unadjustedEffectiveDate, initialStubType, paymentCalendar);
            //  Generate periods forwards (toward the unadjustedTerminationDate)
            //
            GeneratePeriodsForward(unadjustedFirstRollDate, frequency, calculationPeriodDatesAdjustments, result, unadjustedTerminationDate, finalStubType, paymentCalendar);
            return(result);
        }
Esempio n. 7
0
        /// <summary>
        /// Generates the unadjusted calculation dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="intervalToTerminationDate">The interval to termination date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <returns></returns>
        public static List <CalculationPeriod> GenerateUnadjustedCalculationDates(DateTime effectiveDate, Period intervalToTerminationDate, Period periodInterval, RollConventionEnum rollConvention)
        {
            DateTime startDate = effectiveDate;

            // Adjust the effective date
            if (rollConvention != RollConventionEnum.NONE)
            {
                startDate = RollConventionEnumHelper.AdjustDate(rollConvention, effectiveDate);
            }
            Double divisor = IntervalHelper.Div(intervalToTerminationDate, periodInterval);

            // The divisor has to be a whole number (i.e. the period must roll to the term date interval
            if ((divisor % 1) != 0)
            {
                throw new ArithmeticException("The period frequency will not roll to the supplied termination date interval");
            }
            DateTime           terminationDate = intervalToTerminationDate.Add(startDate);
            StubPeriodTypeEnum?stubPeriodType  = null;

            return(GenerateUnadjustedCalculationDates(startDate, terminationDate, effectiveDate, periodInterval, rollConvention, stubPeriodType));
        }
 /// <summary>
 /// Gets the unadjusted calculation date schedule from term date.
 /// </summary>
 /// <param name="effectiveDate">The effective date.</param>
 /// <param name="terminationDate">The termination date.</param>
 /// <param name="periodInterval">The period interval.</param>
 /// <param name="lastRegularPeriodEndDate">The last regular period end date.</param>
 /// <param name="rollConvention">The roll convention.</param>
 /// <param name="stubPeriodType">Type of the stub period.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateScheduleFromTermDate(DateTime effectiveDate, DateTime terminationDate, Period periodInterval, DateTime lastRegularPeriodEndDate, RollConventionEnum rollConvention, StubPeriodTypeEnum?stubPeriodType)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = periodInterval;
     _effectiveDate         = effectiveDate;
     _termDate      = terminationDate;
     RollConvention = rollConvention;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDatesFromTermDate(effectiveDate, terminationDate, periodInterval, lastRegularPeriodEndDate, rollConvention, stubPeriodType);
     return(_unadjustedDateScheduleList);
 }
 /// <summary>
 /// Gets the unadjusted calculation date schedule.
 /// </summary>
 /// <param name="effectiveDate">The effective date.</param>
 /// <param name="terminationDate">The termination date.</param>
 /// <param name="regularPeriodStartDate">The regular period start date.</param>
 /// <param name="periodFrequency">The period frequency.</param>
 /// <param name="stubPeriodType">Type of the stub period.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateSchedule(DateTime effectiveDate, DateTime terminationDate, DateTime regularPeriodStartDate, CalculationPeriodFrequency periodFrequency, StubPeriodTypeEnum?stubPeriodType)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = CalculationPeriodHelper.CalculationPeriodFrequencyToInterval(periodFrequency);
     _effectiveDate         = effectiveDate;
     _termDate      = terminationDate;
     RollConvention = periodFrequency.rollConvention;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, regularPeriodStartDate, periodFrequency, stubPeriodType);
     return(_unadjustedDateScheduleList);
 }
Esempio n. 10
0
        /// <summary>
        /// Generates the unadjusted calculation dates from term date.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="lastRegularPeriodEndDate">The last regular period end date.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDatesFromTermDate(DateTime effectiveDate, DateTime terminationDate, Period periodInterval, DateTime lastRegularPeriodEndDate, RollConventionEnum rollConvention, StubPeriodTypeEnum?stubPeriodType)
        {
            var      unadjustedPeriodDates      = new List <CalculationPeriod>();
            DateTime lastRegularPeriodStartDate = AddPeriod(lastRegularPeriodEndDate, periodInterval, -1);

            if (lastRegularPeriodStartDate > effectiveDate)
            {
                Boolean bHasIrregularInitial;
                List <CalculationPeriod> regularPeriods = GetBackwardRegularPeriods(lastRegularPeriodStartDate, effectiveDate, periodInterval, rollConvention, out bHasIrregularInitial);

                if (regularPeriods.Count > 0)
                {
                    DateTime firstRegularPeriodStartDate = regularPeriods[0].unadjustedStartDate;
                    unadjustedPeriodDates = GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, firstRegularPeriodStartDate, periodInterval, rollConvention, stubPeriodType);
                }
            }
            return(unadjustedPeriodDates);
        }
Esempio n. 11
0
        /// <summary>
        /// Generates the unadjusted calculation dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="intervalToFirstRegularPeriodStartDate">The interval to first regular period start date.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDates(DateTime effectiveDate, DateTime terminationDate, Period periodInterval, Period intervalToFirstRegularPeriodStartDate, RollConventionEnum rollConvention, StubPeriodTypeEnum?stubPeriodType)
        {
            DateTime firstRegularPeriodStartDate = GetFirstRegularPeriodStartDate(intervalToFirstRegularPeriodStartDate, rollConvention, effectiveDate);

            return(GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, firstRegularPeriodStartDate, periodInterval, rollConvention, stubPeriodType));
        }
Esempio n. 12
0
        /// <summary>
        /// Generates the unadjusted calculation dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="regularPeriodStartDate">The regular period start date.</param>
        /// <param name="calculationPeriodFrequency">The calculation period frequency.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDates(DateTime effectiveDate, DateTime terminationDate, DateTime regularPeriodStartDate, CalculationPeriodFrequency calculationPeriodFrequency, StubPeriodTypeEnum?stubPeriodType)
        {
            Period periodInterval = CalculationPeriodFrequencyToInterval(calculationPeriodFrequency);

            return(GenerateUnadjustedCalculationDates(effectiveDate, terminationDate, regularPeriodStartDate, periodInterval, calculationPeriodFrequency.rollConvention, stubPeriodType));
        }
Esempio n. 13
0
        /// <summary>
        /// Generates the unadjusted calculation dates.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="terminationDate">The termination date.</param>
        /// <param name="regularPeriodStartDate">The regular period start date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDates(DateTime effectiveDate, DateTime terminationDate, DateTime regularPeriodStartDate, Period periodInterval, RollConventionEnum rollConvention, StubPeriodTypeEnum?stubPeriodType)
        {
            DateTime startDate = effectiveDate;
            DateTime endDate   = terminationDate;

            Boolean bContainsIrregularFinalPeriod;
            List <CalculationPeriod> forwardRegularPeriods = GetForwardRegularPeriods(regularPeriodStartDate, endDate, periodInterval, rollConvention, out bContainsIrregularFinalPeriod);

            if (bContainsIrregularFinalPeriod)
            {
                if (forwardRegularPeriods.Count == 0)
                {
                    CalculationPeriod calculationPeriod = CreateUnadjustedCalculationPeriod(regularPeriodStartDate, endDate);
                    forwardRegularPeriods.Add(calculationPeriod);
                }
                else
                {
                    forwardRegularPeriods = SetFinalIrregularStubPeriod(forwardRegularPeriods, endDate, stubPeriodType ?? StubPeriodTypeEnum.LongFinal);
                }
            }

            var backwardRegularPeriods = new List <CalculationPeriod>();

            if (regularPeriodStartDate > startDate)
            {
                var     irregularInitialStubType = StubPeriodTypeEnum.ShortInitial;
                Boolean bContainsIrregularInitialPeriod;
                backwardRegularPeriods = GetBackwardRegularPeriods(regularPeriodStartDate, startDate, periodInterval, rollConvention, out bContainsIrregularInitialPeriod);
                if ((bContainsIrregularInitialPeriod && stubPeriodType != null) && (stubPeriodType != StubPeriodTypeEnum.ShortInitial && stubPeriodType != StubPeriodTypeEnum.LongInitial))
                {
                    DateTime normalPeriodStart = AddPeriod(startDate, periodInterval, 1);
                    if (regularPeriodStartDate > normalPeriodStart)
                    {
                        irregularInitialStubType = StubPeriodTypeEnum.LongInitial;
                    }
                }
                else if ((bContainsIrregularInitialPeriod && stubPeriodType != null) && (stubPeriodType == StubPeriodTypeEnum.ShortInitial || stubPeriodType == StubPeriodTypeEnum.LongInitial))
                {
                    irregularInitialStubType = (StubPeriodTypeEnum)stubPeriodType;
                }

                if (bContainsIrregularInitialPeriod)
                {
                    if (backwardRegularPeriods.Count == 0)
                    {
                        CalculationPeriod calculationPeriod = CreateUnadjustedCalculationPeriod(startDate, regularPeriodStartDate);
                        backwardRegularPeriods.Add(calculationPeriod);
                    }
                    else
                    {
                        backwardRegularPeriods = SetInitialIrregularStubPeriod(backwardRegularPeriods, startDate, irregularInitialStubType);
                    }
                }
            }
            return(MergeCalculationPeriods(backwardRegularPeriods, forwardRegularPeriods));
        }
Esempio n. 14
0
        /// <summary>
        /// Sets the final irregular stub period.
        /// </summary>
        /// <param name="regularPeriods">The regular periods.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="stubPeriodType">Type of the stub period.</param>
        /// <returns></returns>
        static private List <CalculationPeriod> SetFinalIrregularStubPeriod(List <CalculationPeriod> regularPeriods, DateTime endDate, StubPeriodTypeEnum?stubPeriodType)
        {
            if (stubPeriodType == null)
            {
                throw new ArgumentNullException("stubPeriodType");
            }

            CalculationPeriod lastRegularPeriod = regularPeriods[regularPeriods.Count - 1];

            if (lastRegularPeriod.unadjustedEndDate < endDate)
            {
                CalculationPeriod finalStub = CreateUnadjustedCalculationPeriod(lastRegularPeriod.unadjustedStartDate, endDate);
                if (stubPeriodType == StubPeriodTypeEnum.LongFinal)
                {
                    regularPeriods.Remove(lastRegularPeriod);
                    regularPeriods.Add(finalStub);
                }
                else
                {
                    finalStub.unadjustedStartDate = lastRegularPeriod.unadjustedEndDate;
                    regularPeriods.Add(finalStub);
                }
            }

            return(regularPeriods);
        }
Esempio n. 15
0
        /// <summary>
        /// Sets the initial irregular stub period.
        /// </summary>
        /// <param name="regularPeriods">The regular periods.</param>
        /// <param name="startDate">The start date.</param>
        /// <param name="stubType">Type of the stub.</param>
        /// <returns></returns>
        static internal List <CalculationPeriod> SetInitialIrregularStubPeriod(List <CalculationPeriod> regularPeriods, DateTime startDate, StubPeriodTypeEnum?stubType)
        {
            CalculationPeriod firstRegularPeriod = regularPeriods[0];

            if (firstRegularPeriod.unadjustedStartDate > startDate)
            {
                CalculationPeriod initialStub = CreateUnadjustedCalculationPeriod(startDate, firstRegularPeriod.unadjustedEndDate);
                if (stubType == StubPeriodTypeEnum.LongInitial)
                {
                    regularPeriods.Remove(firstRegularPeriod);
                    regularPeriods.Add(initialStub);
                }
                else
                {
                    //initialStub.unadjustedEndDate = firstRegularPeriod.unadjustedStartDate;
                    regularPeriods.Add(initialStub);
                }
            }

            return(regularPeriods);
        }