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);
        }
        private static CalculationPeriod Create(DateTime periodStartDate, DateTime periodEndDate
                                                , CalculationPeriodFrequency frequency, BusinessDayAdjustments dateAdjustments
                                                , IBusinessCalendar paymentCalendar, bool isFirstPeriodCreated)
        {
            var calculationPeriod = new CalculationPeriod();
            //  Apply ROLL CONVENTION (NOT A BUSINESS DAY CONVENTION!) to unadjusted period start and end dates.
            //
            DateTime rollConventionPeriodStartDate = periodStartDate;
            DateTime rollConventionPeriodEndDate   = periodEndDate;
            var      frequencyPeriod = EnumHelper.Parse <PeriodEnum>(frequency.period, true);

            if (frequencyPeriod != PeriodEnum.D)//adjust if the frequency is NOT expressed in days
            {
                rollConventionPeriodStartDate = RollConventionEnumHelper.AdjustDate(frequency.rollConvention, periodStartDate);
                rollConventionPeriodEndDate   = RollConventionEnumHelper.AdjustDate(frequency.rollConvention, periodEndDate);
            }
            CalculationPeriodHelper.SetUnadjustedDates(calculationPeriod, rollConventionPeriodStartDate, rollConventionPeriodEndDate);
            //  Set adjusted period dates
            //
            DateTime adjustedPeriodStartDate = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, rollConventionPeriodStartDate, dateAdjustments);
            DateTime adjustedPeriodEndDate   = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, rollConventionPeriodEndDate, dateAdjustments);

            if (isFirstPeriodCreated)
            {
                adjustedPeriodEndDate = periodEndDate;
            }
            CalculationPeriodHelper.SetAdjustedDates(calculationPeriod, adjustedPeriodStartDate, adjustedPeriodEndDate);
            return(calculationPeriod);
        }
 /// <summary>
 /// Gets the unadjusted calculation date schedule from term date.
 /// </summary>
 /// <param name="terminationDate">The termination date.</param>
 /// <param name="periodInterval">The period interval.</param>
 /// <param name="noOfCouponPeriods">The no of coupon periods.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateScheduleFromTermDate(DateTime terminationDate, Period periodInterval, int noOfCouponPeriods)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = periodInterval;
     _termDate      = terminationDate;
     RollConvention = RollConventionEnum.NONE;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDatesFromTermDate(terminationDate, periodInterval, noOfCouponPeriods);
     return(_unadjustedDateScheduleList);
 }
 /// <summary>
 /// Gets the unadjusted calculation date schedule.
 /// </summary>
 /// <param name="effectiveDate">The effective date.</param>
 /// <param name="periodToTerminationDate">The period to termination date.</param>
 /// <param name="periodFrequency">The period frequency.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateSchedule(DateTime effectiveDate, CalculationPeriodFrequency periodToTerminationDate, CalculationPeriodFrequency periodFrequency)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = CalculationPeriodHelper.CalculationPeriodFrequencyToInterval(periodFrequency);
     _effectiveDate         = effectiveDate;
     _termDate      = PeriodInterval.Add(effectiveDate);
     RollConvention = periodFrequency.rollConvention;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(effectiveDate, periodToTerminationDate, periodFrequency);
     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);
 }
 /// <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 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="fullFirstCoupon">if set to <c>true</c> [full first coupon].</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateScheduleFromTermDate(DateTime effectiveDate, DateTime terminationDate, Period periodInterval, Boolean fullFirstCoupon)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = periodInterval;
     _effectiveDate         = effectiveDate;
     _termDate      = terminationDate;
     RollConvention = RollConventionEnum.NONE;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDatesFromTermDate(effectiveDate, terminationDate, periodInterval, fullFirstCoupon);
     return(_unadjustedDateScheduleList);
 }
 /// <summary>
 /// Gets the unadjusted calculation date schedule.
 /// </summary>
 /// <param name="effectiveDate">The effective date.</param>
 /// <param name="intervalToTerminationDate">The interval to termination date.</param>
 /// <param name="periodInterval">The period interval.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateSchedule(DateTime effectiveDate, Period intervalToTerminationDate, Period periodInterval)
 {
     CalculationPeriodDates = null;
     PeriodInterval         = periodInterval;
     _effectiveDate         = effectiveDate;
     _termDate      = intervalToTerminationDate.Add(effectiveDate);
     RollConvention = RollConventionEnum.NONE;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(effectiveDate, intervalToTerminationDate, periodInterval);
     return(_unadjustedDateScheduleList);
 }
 /// <summary>
 /// Gets the unadjusted calculation date schedule.
 /// </summary>
 /// <param name="calculationPeriodDates">The calculation period dates.</param>
 /// <returns></returns>
 public List <CalculationPeriod> GetUnadjustedCalculationDateSchedule(CalculationPeriodDates calculationPeriodDates)
 {
     CalculationPeriodDates = calculationPeriodDates;
     PeriodInterval         = CalculationPeriodHelper.CalculationPeriodFrequencyToInterval(calculationPeriodDates.calculationPeriodFrequency);
     _effectiveDate         = XsdClassesFieldResolver.CalculationPeriodDatesGetEffectiveDate(calculationPeriodDates).unadjustedDate.Value;
     _termDate      = XsdClassesFieldResolver.CalculationPeriodDatesGetTerminationDate(calculationPeriodDates).unadjustedDate.Value;
     RollConvention = calculationPeriodDates.calculationPeriodFrequency.rollConvention;
     _unadjustedDateScheduleList = CalculationPeriodHelper.GenerateUnadjustedCalculationDates(_effectiveDate, _termDate, calculationPeriodDates);
     return(_unadjustedDateScheduleList);
 }
Beispiel #11
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);
        }
        public void LongFinalStubSpecifiedButNoStub()
        {
            DateTime                   startDate       = new DateTime(2007, 01, 5);
            DateTime                   endDate         = new DateTime(2012, 01, 5);
            RollConventionEnum         rollConvention  = RollConventionEnum.Item5;
            CalculationPeriodFrequency periodFrequency = CalculationPeriodFrequencyHelper.Parse("3M", rollConvention.ToString());
            CalculationPeriodSchedule  cps             = new CalculationPeriodSchedule();
            Period periodInterval = CalculationPeriodHelper.CalculationPeriodFrequencyToInterval(periodFrequency);
            List <CalculationPeriod> unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, endDate, periodInterval, rollConvention, StubPeriodTypeEnum.LongFinal);

            Assert.AreEqual(unadjustedCalcPeriods.Count, 20);
            Assert.IsFalse(cps.HasFinalStub);
            Assert.IsFalse(cps.HasInitialStub);
        }
Beispiel #13
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 void LongInitialStubSpecified()
        {
            DateTime                   startDate              = new DateTime(2007, 01, 5);
            DateTime                   endDate                = new DateTime(2012, 01, 5);
            RollConventionEnum         rollConvention         = RollConventionEnum.Item26;
            CalculationPeriodFrequency periodFrequency        = CalculationPeriodFrequencyHelper.Parse("3M", rollConvention.ToString());
            DateTime                   firstRegularPeriodDate = new DateTime(2007, 4, 26);
            CalculationPeriodSchedule  cps = new CalculationPeriodSchedule();
            List <CalculationPeriod>   unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, endDate, firstRegularPeriodDate, periodFrequency, StubPeriodTypeEnum.LongInitial);

            Assert.AreEqual(unadjustedCalcPeriods.Count, 20);
            Assert.IsTrue(cps.HasFinalStub);
            Assert.IsTrue(cps.HasInitialStub);
            Assert.IsTrue(CalculationPeriodHelper.IsLongStub(unadjustedCalcPeriods[0], cps.PeriodInterval));
        }
        private static CalculationPeriod CreateStub(DateTime periodStartDate, DateTime periodEndDate
                                                    , BusinessDayAdjustments dateAdjustments, IBusinessCalendar paymentCalendar)
        {
            var calculationPeriod = new CalculationPeriod();

            //  Apply ROLL CONVENTION (NOT A BUSINESS DAY CONVENTION!) to unadjusted period start and end dates.
            //
            CalculationPeriodHelper.SetUnadjustedDates(calculationPeriod, periodStartDate, periodEndDate);
            //  Set adjusted period dates
            //
            DateTime adjustedPeriodStartDate = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, periodStartDate, dateAdjustments);
            DateTime adjustedPeriodEndDate   = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, periodEndDate, dateAdjustments);

            CalculationPeriodHelper.SetAdjustedDates(calculationPeriod, adjustedPeriodStartDate, adjustedPeriodEndDate);
            return(calculationPeriod);
        }
        public void ShortFinalStubSpecifiedFromTermination()
        {
            DateTime                   startDate       = new DateTime(2007, 01, 5);
            DateTime                   endDate         = new DateTime(2010, 01, 5);
            RollConventionEnum         rollConvention  = RollConventionEnum.Item12;
            CalculationPeriodFrequency periodFrequency = CalculationPeriodFrequencyHelper.Parse("3M", rollConvention.ToString());
            Period periodInterval             = CalculationPeriodHelper.CalculationPeriodFrequencyToInterval(periodFrequency);
            CalculationPeriodSchedule cps     = new CalculationPeriodSchedule();
            DateTime lastRegularPeriodEndDate = new DateTime(2009, 8, 12);
            List <CalculationPeriod> unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateScheduleFromTermDate(startDate, endDate, periodInterval, lastRegularPeriodEndDate, rollConvention, StubPeriodTypeEnum.ShortFinal);

            Assert.AreEqual(unadjustedCalcPeriods.Count, 13);
            Assert.IsTrue(cps.HasFinalStub);
            Assert.IsTrue(cps.HasInitialStub);
            Assert.IsTrue(CalculationPeriodHelper.IsShortStub(unadjustedCalcPeriods[unadjustedCalcPeriods.Count - 1], cps.PeriodInterval));
        }
        public void RegularPeriodIntervalUnadjustedDates()
        {
            Boolean bFailureSet = false;
            List <CalculationPeriod> unadjustedCalcPeriods;

            try
            {
                // 5 year 3 month cash flow
                DateTime startDate            = new DateTime(2007, 01, 5);
                Period   intervalToMaturity   = PeriodHelper.Parse("5Y");
                Period   periodInterval       = PeriodHelper.Parse("3M");
                CalculationPeriodSchedule cps = new CalculationPeriodSchedule();
                unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, intervalToMaturity, periodInterval);
                List <DateTime> dates = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(unadjustedCalcPeriods, "unadjustedStartDate");
                Assert.AreEqual(unadjustedCalcPeriods.Count, 20);
                Assert.IsFalse(cps.HasInitialStub);
                Assert.IsFalse(cps.HasFinalStub);

                // 3 year 6 month cash flow
                startDate          = new DateTime(2010, 12, 3);
                intervalToMaturity = PeriodHelper.Parse("3Y");
                periodInterval     = PeriodHelper.Parse("6M");
                cps = new CalculationPeriodSchedule();
                unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, intervalToMaturity, periodInterval);
                dates = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(unadjustedCalcPeriods, "unadjustedStartDate");
                Assert.AreEqual(unadjustedCalcPeriods.Count, 6);
                Assert.IsFalse(cps.HasInitialStub);
                Assert.IsFalse(cps.HasFinalStub);

                // This should generate an excpetion
                startDate          = new DateTime(2010, 12, 3);
                intervalToMaturity = PeriodHelper.Parse("3Y");
                periodInterval     = PeriodHelper.Parse("7M");
                cps = new CalculationPeriodSchedule();
                unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, intervalToMaturity, periodInterval);
                dates = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(unadjustedCalcPeriods, "unadjustedStartDate");
                Assert.AreEqual(unadjustedCalcPeriods.Count, 6);
                Assert.IsFalse(cps.HasInitialStub);
                Assert.IsFalse(cps.HasFinalStub);
                Assert.IsTrue(!bFailureSet);
            }
            catch (Exception ex)
            {
                bFailureSet = true;
                Console.WriteLine("RegularPeriodIntervalUnadjustedDates: " + ex.Message);
            }
        }
        public void RegularPeriodFrequencyUnadjustedDates()
        {
            // 5 year 3 month cash flow
            DateTime startDate = new DateTime(2007, 01, 5);

            RollConventionEnum rollConvention = RollConventionEnum.Item26;

            CalculationPeriodFrequency frequencyToMaturity = CalculationPeriodFrequencyHelper.Parse("5Y", rollConvention.ToString());
            CalculationPeriodFrequency periodFrequency     = CalculationPeriodFrequencyHelper.Parse("3M", rollConvention.ToString());
            CalculationPeriodSchedule  cps = new CalculationPeriodSchedule();
            List <CalculationPeriod>   unadjustedCalcPeriods = cps.GetUnadjustedCalculationDateSchedule(startDate, frequencyToMaturity, periodFrequency);
            List <DateTime>            dates = CalculationPeriodHelper.GetCalculationPeriodsProperty <DateTime>(unadjustedCalcPeriods, "unadjustedStartDate");

            Assert.AreEqual(unadjustedCalcPeriods.Count, 20);
            Assert.IsFalse(cps.HasInitialStub);
            Assert.IsFalse(cps.HasFinalStub);
        }
        /// <summary>
        /// Don't generate stubs?
        /// </summary>
        /// <param name="interestRateStream"></param>
        /// <param name="rollDates">from #1st roll date to last roll date (last roll dates is effectively the end of the swap)</param>
        /// <param name="paymentCalendar"></param>
        /// <returns></returns>
        public static CalculationPeriodsPrincipalExchangesAndStubs GenerateCalculationPeriodsPrincipalExchangesAndStubsFromRollDates(InterestRateStream interestRateStream,
                                                                                                                                     List <DateTime> rollDates,
                                                                                                                                     IBusinessCalendar paymentCalendar)
        {
            CalculationPeriodDates calculationPeriodDates  = interestRateStream.calculationPeriodDates;
            AdjustableDate         adjustableEffectiveDate = XsdClassesFieldResolver.CalculationPeriodDatesGetEffectiveDate(calculationPeriodDates);
            var result = new CalculationPeriodsPrincipalExchangesAndStubs();

            for (int rollDateIndex = 0; rollDateIndex < rollDates.Count - 1; ++rollDateIndex)
            {
                DateTime startOfThePeriod  = rollDates[rollDateIndex];
                DateTime endOfThePeriod    = rollDates[rollDateIndex + 1];
                var      calculationPeriod = new CalculationPeriod();
                //  Set adjusted period dates
                //
                CalculationPeriodHelper.SetAdjustedDates(calculationPeriod,
                                                         startOfThePeriod,
                                                         endOfThePeriod);
                result.Add(calculationPeriod);
            }
            //  Add principle exchanges if this need is defined in parametric representation of the interest rate steam.
            //
            if (null != interestRateStream.principalExchanges)
            {
                //  Initial PE
                //
                if (interestRateStream.principalExchanges.initialExchange)
                {
                    //if (paymentCalendar == null)
                    //{
                    //    paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, adjustableEffectiveDate.dateAdjustments.businessCenters);
                    //}
                    PrincipalExchange initialExchange = PrincipalExchangeHelper.Create(AdjustedDateHelper.ToAdjustedDate(paymentCalendar, adjustableEffectiveDate));
                    result.InitialPrincipalExchange = initialExchange;
                }
                //  intermediatory PE
                //
                if (interestRateStream.principalExchanges.intermediateExchange)
                {
                    // Generate a list of intermediatory PE exchanges
                    //
                    Calculation calculation      = XsdClassesFieldResolver.CalculationPeriodAmountGetCalculation(interestRateStream.calculationPeriodAmount);
                    Notional    notionalSchedule = XsdClassesFieldResolver.CalculationGetNotionalSchedule(calculation);
                    if (null != notionalSchedule.notionalStepSchedule.step)//there should be steps - otherwise NO interm. exchanges.
                    {
                        foreach (DateTime stepDate in ScheduleHelper.GetStepDates(notionalSchedule.notionalStepSchedule))
                        {
                            PrincipalExchange intermediatoryExchange = PrincipalExchangeHelper.Create(stepDate);
                            result.Add(intermediatoryExchange);
                        }
                    }
                }
                //AdjustableDate adjustableTerminationDate = XsdClassesFieldResolver.CalculationPeriodDates_GetTerminationDate(calculationPeriodDates);
                DateTime lastRollDate = rollDates[rollDates.Count - 1];
                //  Final PE
                //
                if (interestRateStream.principalExchanges.finalExchange)
                {
                    //PrincipalExchange finalExchange = PrincipalExchangeHelper.Create(DateTypesHelper.ToAdjustedDate(adjustableTerminationDate));
                    PrincipalExchange finalExchange = PrincipalExchangeHelper.Create(lastRollDate);
                    result.FinalPrincipalExchange = finalExchange;
                }
            }
            return(result);
        }