/// <summary>
        /// Creates a fixed coupon calculation period.
        /// </summary>
        /// <param name="accrualStartDate"></param>
        /// <param name="accrualEndDate"></param>
        /// <param name="notionalAmount"></param>
        /// <param name="calculation"></param>
        /// <returns></returns>
        public static CalculationPeriod[] CreateSimpleCouponItem(DateTime accrualStartDate, DateTime accrualEndDate,
                                                                 Money notionalAmount, Calculation calculation)
        {
            IDayCounter dayCounter        = DayCounterHelper.Parse(calculation.dayCountFraction.Value);
            var         calculationPeriod = new CalculationPeriod();
            int         numDays           = dayCounter.DayCount(accrualStartDate, accrualEndDate);

            calculationPeriod.adjustedStartDate             = accrualStartDate;
            calculationPeriod.adjustedStartDateSpecified    = true;
            calculationPeriod.adjustedEndDate               = accrualEndDate;
            calculationPeriod.adjustedEndDateSpecified      = true;
            calculationPeriod.dayCountYearFraction          = (decimal)dayCounter.YearFraction(accrualStartDate, accrualEndDate);
            calculationPeriod.dayCountYearFractionSpecified = true;
            calculationPeriod.calculationPeriodNumberOfDays = numDays.ToString(CultureInfo.InvariantCulture);
            calculationPeriod.Item = notionalAmount.amount;
            calculationPeriod.unadjustedEndDateSpecified   = false;
            calculationPeriod.unadjustedStartDateSpecified = false;
            calculationPeriod.Item1 = calculation;
            var rate = ((Schedule)calculation.Items[0]).initialValue;

            calculationPeriod.forecastRate          = rate;
            calculationPeriod.forecastRateSpecified = true;
            calculationPeriod.forecastAmount        = MoneyHelper.Mul(notionalAmount,
                                                                      calculationPeriod.dayCountYearFraction *
                                                                      calculationPeriod.forecastRate);
            var calculationPeriods = new List <CalculationPeriod> {
                calculationPeriod
            };

            return(calculationPeriods.ToArray());
        }
Exemple #2
0
 /// <summary>
 /// Sets the unadjusted dates.
 /// </summary>
 /// <param name="calculationPeriod">The calculation period.</param>
 /// <param name="unadjustedStartDate">The unadjusted start date.</param>
 /// <param name="unadjustedEndDate">The unadjusted end date.</param>
 public static void SetUnadjustedDates(CalculationPeriod calculationPeriod, DateTime unadjustedStartDate, DateTime unadjustedEndDate)
 {
     calculationPeriod.unadjustedStartDate          = unadjustedStartDate;
     calculationPeriod.unadjustedStartDateSpecified = true;
     calculationPeriod.unadjustedEndDate            = unadjustedEndDate;
     calculationPeriod.unadjustedEndDateSpecified   = true;
 }
Exemple #3
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="fullFirstCoupon">if set to <c>true</c> [full first coupon].</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDatesFromTermDate(DateTime effectiveDate, DateTime terminationDate, Period periodInterval, Boolean fullFirstCoupon)
        {
            Boolean bReachedFirstCoupon   = false;
            var     unadjustedPeriodDates = new List <CalculationPeriod>();

            DateTime unadjustedEndDate = terminationDate;
            int      couponPeriod      = 1;

            while (!bReachedFirstCoupon)
            {
                DateTime unadjustedStartDate = AddPeriod(terminationDate, periodInterval, (couponPeriod * -1));

                if ((!fullFirstCoupon) && (DateTime.Compare(unadjustedStartDate, effectiveDate) < 0))
                {
                    bReachedFirstCoupon = true;
                    unadjustedStartDate = effectiveDate;
                }
                else if ((!fullFirstCoupon) && (DateTime.Compare(unadjustedStartDate, effectiveDate) == 0))
                {
                    bReachedFirstCoupon = true;
                }
                else if ((DateTime.Compare(unadjustedStartDate, effectiveDate) <= 0))
                {
                    bReachedFirstCoupon = true;
                }

                CalculationPeriod calculationPeriod = CreateUnadjustedCalculationPeriod(unadjustedStartDate, unadjustedEndDate);
                unadjustedPeriodDates.Insert(0, calculationPeriod);
                unadjustedEndDate = unadjustedStartDate;
                couponPeriod++;
            }
            return(unadjustedPeriodDates);
        }
Exemple #4
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);
        }
        private static void UpdateCashflowsWithDetailedCashflows(Cashflows cashflows, IEnumerable <DetailedCashflowRangeItem> listDetailedCashflows /*, bool fixedLeg*/)
        {
            var paymentCalculationPeriods = new List <PaymentCalculationPeriod>();

            foreach (DetailedCashflowRangeItem detailedCashflowRangeItem in listDetailedCashflows)
            {
                var paymentCalculationPeriod = new PaymentCalculationPeriod();
                var calculationPeriod        = new CalculationPeriod();
                paymentCalculationPeriod.Items = new object[] { calculationPeriod };
                paymentCalculationPeriod.adjustedPaymentDate          = detailedCashflowRangeItem.PaymentDate;
                paymentCalculationPeriod.adjustedPaymentDateSpecified = true;
                PaymentCalculationPeriodHelper.SetCalculationPeriodStartDate(paymentCalculationPeriod, detailedCashflowRangeItem.StartDate);
                PaymentCalculationPeriodHelper.SetCalculationPeriodEndDate(paymentCalculationPeriod, detailedCashflowRangeItem.EndDate);
                // Update notional amount
                //
                PaymentCalculationPeriodHelper.SetNotionalAmount(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.NotionalAmount);
                if (detailedCashflowRangeItem.CouponType == "Fixed")
                {
                    //CalculationPeriod calculationPeriod = PaymentCalculationPeriodHelper.GetCalculationPeriods(paymentCalculationPeriod)[0];

                    //if (XsdClassesFieldResolver.CalculationPeriod_HasFixedRate(calculationPeriod))
                    //{
                    //  Fixed->Fixed
                    //
                    //PaymentCalculationPeriodHelper.SetRate(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Rate);

                    XsdClassesFieldResolver.SetCalculationPeriodFixedRate(calculationPeriod, (decimal)detailedCashflowRangeItem.Rate);
                    //}
                    //else if (XsdClassesFieldResolver.CalculationPeriod_HasFloatingRateDefinition(calculationPeriod))
//                    {
//                        //  Float->Fixed
//                        //
//                        PaymentCalculationPeriodHelper.ReplaceFloatingRateWithFixedRate(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Rate);
//                    }
//                    else
//                    {
//                        throw new NotImplementedException();
//                    }
                }
                else if (detailedCashflowRangeItem.CouponType == "Float")
                {
                    //  Create floating rate definiton...
                    //
                    var floatingRateDefinition = new FloatingRateDefinition();
                    //XsdClassesFieldResolver.CalculationPeriod_SetFloatingRateDefinition(calculationPeriod, floatingRateDefinition);
                    calculationPeriod.Item1 = floatingRateDefinition;
                    // After the spread is reset - we need to update calculated rate.
                    //
                    PaymentCalculationPeriodHelper.SetSpread(paymentCalculationPeriod, (decimal)detailedCashflowRangeItem.Spread);
                }
                else
                {
                    string message = $"Unsupported coupon type '{detailedCashflowRangeItem.CouponType}";
                    throw new System.Exception(message);
                }
                paymentCalculationPeriods.Add(paymentCalculationPeriod);
            }
            cashflows.cashflowsMatchParameters = false;
            cashflows.paymentCalculationPeriod = paymentCalculationPeriods.ToArray();
        }
        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();
            }
        }
        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);
        }
Exemple #8
0
        public static FloatingRateDefinition CreateFloatingRateDefinition(FloatingRate floatingRate,
                                                                          DayCountFraction dayCountFraction,
                                                                          CalculationPeriod calculationPeriod,
                                                                          IRateCurve forecastCurve)
        {
            //throw new NotImplementedException();
            // assign floating rate def with adjusted fixing date
            //
            var floatingRateDefinition = new FloatingRateDefinition {
                spread = 0.000m, spreadSpecified = true
            };
            // Spread = 0.0
            //
            var rateObservation = new RateObservation();

            floatingRateDefinition.rateObservation = new[] { rateObservation };
            rateObservation.forecastRate           = GetForecastRate(calculationPeriod, forecastCurve, dayCountFraction);
            rateObservation.forecastRateSpecified  = true;
            Decimal finalRate = rateObservation.forecastRate;

            // If spread specified - add it to final rate.
            //
            if (floatingRateDefinition.spreadSpecified)
            {
                finalRate += floatingRateDefinition.spread;
            }
            floatingRateDefinition.calculatedRate          = finalRate;
            floatingRateDefinition.calculatedRateSpecified = true;
            return(floatingRateDefinition);
        }
        /// <summary>
        /// Builds the calculation period.
        /// </summary>
        /// <returns></returns>
        protected override CalculationPeriod BuildCalculationPeriod()
        {
            CalculationPeriod cp = base.BuildCalculationPeriod();

            cp.Item1 = GetRate();
            return(cp);
        }
Exemple #10
0
        public static FloatingRateDefinition CreateFloatingRateDefinition(CalculationPeriod calculationPeriod)
        {
            var floatingRateDefinition = new FloatingRateDefinition();

            //floatingRateDefinition.spread = 0.0m;
            //floatingRateDefinition.spreadSpecified = true;
            return(floatingRateDefinition);
        }
Exemple #11
0
        IPeriodVariables <I> IDefinePeriod <I> .InPeriod(string caption)
        {
            _currentPeriod = _periodHolders
                             .Where(p => p.WithinGroup == _currentGroup)
                             .Single(p => p.Name == caption);

            return(this);
        }
Exemple #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="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));
        }
Exemple #13
0
        /// <summary>
        /// Determines whether [is short stub] [the specified calculation period].
        /// </summary>
        /// <param name="calculationPeriod">The calculation period.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <returns>
        ///     <c>true</c> if [is short stub] [the specified calculation period]; otherwise, <c>false</c>.
        /// </returns>
        static public Boolean IsShortStub(CalculationPeriod calculationPeriod, Period periodInterval)
        {
            Boolean retval = false;

            if (calculationPeriod != null)
            {
                DateTime normalEndDate = AddPeriod(calculationPeriod.unadjustedStartDate, periodInterval, 1);
                retval = (calculationPeriod.unadjustedEndDate < normalEndDate);
            }
            return(retval);
        }
Exemple #14
0
        /// <summary>
        /// Determines whether [has final stub] [the specified calculation periods].
        /// </summary>
        /// <param name="calculationPeriods">The calculation periods.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <returns>
        ///     <c>true</c> if [has final stub] [the specified calculation periods]; otherwise, <c>false</c>.
        /// </returns>
        public static Boolean HasFinalStub(List <CalculationPeriod> calculationPeriods, Period periodInterval)
        {
            Boolean retval = false;

            if (calculationPeriods != null && calculationPeriods.Count > 0)
            {
                CalculationPeriod finalPeriod     = calculationPeriods[calculationPeriods.Count - 1];
                DateTime          normalStartDate = AddPeriod(finalPeriod.unadjustedEndDate, periodInterval, -1);
                retval = (finalPeriod.unadjustedStartDate != normalStartDate);
            }
            return(retval);
        }
Exemple #15
0
        /// <summary>
        /// Creates the unadjusted calculation period.
        /// </summary>
        /// <param name="unadjustedStartDate">The unadjusted start date.</param>
        /// <param name="unadjustedEndDate">The unadjusted end date.</param>
        /// <returns></returns>
        static public CalculationPeriod CreateUnadjustedCalculationPeriod(DateTime unadjustedStartDate, DateTime unadjustedEndDate)
        {
            var calculationPeriod = new CalculationPeriod
            {
                unadjustedStartDateSpecified = true,
                unadjustedEndDateSpecified   = true,
                unadjustedStartDate          = unadjustedStartDate,
                unadjustedEndDate            = unadjustedEndDate
            };

            return(calculationPeriod);
        }
Exemple #16
0
        /// <summary>
        /// Gets the value of supplied calculation period property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="period">The period.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static T GetCalculationPeriodProperty <T>(CalculationPeriod period, string propertyName)
        {
            T            value      = default;
            Type         periodType = period.GetType();
            PropertyInfo p          = periodType.GetProperty(propertyName);

            if (p != null)
            {
                value = (T)p.GetValue(period, null);
            }
            return(value);
        }
        private static DateTime CalculatePaymentDate(PaymentDates paymentDates,
                                                     PayRelativeToEnum payRelativeTo,
                                                     IList <CalculationPeriod> calculationPeriodsInPamentPeriod,
                                                     Offset paymentDaysOffset,
                                                     BusinessDayAdjustments paymentDatesBusinessDayAdjustments,
                                                     IBusinessCalendar paymentCalendar)
        {
            switch (payRelativeTo)
            {
            case PayRelativeToEnum.CalculationPeriodStartDate:
            {
                // To get the calculation period start date - obtain a reference to FIRST calculation period in the payment period.
                //
                CalculationPeriod      firstCalculatinPeriodInPaymentPeriod = calculationPeriodsInPamentPeriod[0];
                BusinessDayAdjustments paymentDatesAdjustments = paymentDates.paymentDatesAdjustments;
                // Adjust using paymentDatesAdjustments...
                //
                DateTime adjustedPaymentDate = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, firstCalculatinPeriodInPaymentPeriod.unadjustedStartDate, paymentDatesAdjustments);
                // Apply offset
                //
                if (null != paymentDaysOffset)
                {
                    return(AdjustedDateHelper.ToAdjustedDate(paymentCalendar, adjustedPaymentDate, paymentDatesBusinessDayAdjustments, paymentDaysOffset));
                }
                return(adjustedPaymentDate);
            }

            case PayRelativeToEnum.CalculationPeriodEndDate:
            {
                // To get the calculation period end date - obtain a reference to LAST calculation period in the payment period.
                //
                CalculationPeriod      lastCalculatinPeriodInPaymentPeriod = calculationPeriodsInPamentPeriod[calculationPeriodsInPamentPeriod.Count - 1];
                BusinessDayAdjustments paymentDatesAdjustments             = paymentDates.paymentDatesAdjustments;
                // Adjust using paymentDatesAdjustments...
                //
                DateTime adjustedPaymentDate = AdjustedDateHelper.ToAdjustedDate(paymentCalendar, lastCalculatinPeriodInPaymentPeriod.unadjustedEndDate, paymentDatesAdjustments);
                // Apply offset (if present)
                //
                return(null != paymentDaysOffset?AdjustedDateHelper.ToAdjustedDate(paymentCalendar, adjustedPaymentDate, paymentDatesBusinessDayAdjustments, paymentDaysOffset) : adjustedPaymentDate);
            }

            case PayRelativeToEnum.ResetDate:
            {
                throw new NotImplementedException("payRelativeTo to ResetDate");
            }

            default:
            {
                throw new NotImplementedException("payRelativeTo");
            }
            }
        }
        private void AddExpense(List <MonthlyCashStatement> result, YearMonth month, MonthlyCashStatementCategory category, List <MonthlyCashStatement> movements)
        {
            var actualMovements = movements.Where(m => m.Category == category && m.Month == month).ToList();

            var paidAmount  = actualMovements.Sum(_ => _.Amount);
            var wasPaid     = actualMovements.Any(_ => _.IsFinalPayment) || Math.Abs(category.Amount) <= Math.Abs(paidAmount);
            var expenseDate = month.GetDate(category.DayOfMonth);

            if (!wasPaid && CalculationPeriod.Contains(expenseDate) && category.Effective.Contains(expenseDate))
            {
                result.Add(new MonthlyCashStatement(category, month, expenseDate, category.Amount - paidAmount, "<план>"));
            }
        }
Exemple #19
0
        /// <summary>
        /// Determines whether [has initial stub] [the specified calculation periods].
        /// </summary>
        /// <param name="calculationPeriods">The calculation periods.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <returns>
        ///     <c>true</c> if [has initial stub] [the specified calculation periods]; otherwise, <c>false</c>.
        /// </returns>
        static public Boolean HasInitialStub(List <CalculationPeriod> calculationPeriods, Period periodInterval)
        {
            Boolean retval = false;

            if (calculationPeriods != null && calculationPeriods.Count > 0)
            {
                CalculationPeriod initialPeriod = calculationPeriods[0];

                DateTime normalEndDate = AddPeriod(initialPeriod.unadjustedStartDate, periodInterval, 1);
                retval = (initialPeriod.unadjustedEndDate == normalEndDate) ? false : true;
            }
            return(retval);
        }
        /// <summary>
        /// Builds the calculation period.
        /// </summary>
        /// <returns></returns>
        override protected CalculationPeriod BuildCalculationPeriod()
        {
            CalculationPeriod cp = base.BuildCalculationPeriod();

            FloatingRateDefinition floatingRateDefinition = FloatingRateDefinitionHelper.CreateSimple(ForecastRateIndex.floatingRateIndex, ForecastRateIndex.indexTenor, AdjustedFixingDate, GetRate(), Margin);

            cp.Item1 = floatingRateDefinition;

            if (floatingRateDefinition.calculatedRateSpecified)
            {
                cp.forecastRate          = floatingRateDefinition.calculatedRate;
                cp.forecastRateSpecified = true;
            }
            cp.forecastAmount = MoneyHelper.GetAmount(CalculationResults.ExpectedValue, NotionalAmount.currency.Value);
            return(cp);
        }
        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);
        }
Exemple #22
0
        private static decimal GetForecastRate(CalculationPeriod calculationPeriod, IRateCurve forecastCurve, DayCountFraction dayCountFraction)
        {
            double      startOfPeriodDiscount = forecastCurve.GetDiscountFactor(calculationPeriod.adjustedStartDate);
            double      endOfPeriodDiscount   = forecastCurve.GetDiscountFactor(calculationPeriod.adjustedEndDate);
            IDayCounter dayCounter            = DayCounterHelper.Parse(dayCountFraction.Value);
            double      accrualPeriod         = dayCounter.YearFraction(calculationPeriod.adjustedStartDate, calculationPeriod.adjustedEndDate);

            if (0 == accrualPeriod)
            {
                string message =
                    $"Accrual period is 0 days. calculationPeriod.adjustedStartDate = '{calculationPeriod.adjustedStartDate}', calculationPeriod.adjustedEndDate = '{calculationPeriod.adjustedEndDate}'";
                throw new System.Exception(message);
            }
            double forecastContinuouslyCompoundingRate = (startOfPeriodDiscount / endOfPeriodDiscount - 1.0) / accrualPeriod;

            return((decimal)forecastContinuouslyCompoundingRate);
        }
Exemple #23
0
        /// <summary>
        /// Generates the unadjusted calculation dates from term date.
        /// </summary>
        /// <param name="effectiveDate">The effective date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="noOfCouponPeriods">The no of coupon periods.</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GenerateUnadjustedCalculationDatesFromTermDate(DateTime effectiveDate, Period periodInterval, int noOfCouponPeriods)
        {
            var      unadjustedPeriodDates = new List <CalculationPeriod>();
            DateTime unadjustedEndDate     = effectiveDate;

            if (noOfCouponPeriods < 1)
            {
                throw new ArgumentException("the number of coupon periods must be greater than zero");
            }
            for (int couponPeriod = 1; couponPeriod <= noOfCouponPeriods; couponPeriod++)
            {
                DateTime          unadjustedStartDate = AddPeriod(effectiveDate, periodInterval, (couponPeriod * -1));
                CalculationPeriod calculationPeriod   = CreateUnadjustedCalculationPeriod(unadjustedStartDate, unadjustedEndDate);
                unadjustedPeriodDates.Insert(0, calculationPeriod);
                unadjustedEndDate = unadjustedStartDate;
            }
            return(unadjustedPeriodDates);
        }
        public static CalculationPeriod Create(DateTime adjustedStartDate, DateTime adjustedEndDate, DateTime unadjustedStartDate, DateTime unadjustedEndDate)
        {
            CalculationPeriod calculationPeriod = new CalculationPeriod();

            calculationPeriod.unadjustedStartDate          = unadjustedStartDate;
            calculationPeriod.unadjustedStartDateSpecified = true;

            calculationPeriod.unadjustedEndDate          = unadjustedEndDate;
            calculationPeriod.unadjustedEndDateSpecified = true;

            calculationPeriod.adjustedStartDate          = adjustedStartDate;
            calculationPeriod.adjustedStartDateSpecified = true;

            calculationPeriod.adjustedEndDate          = adjustedEndDate;
            calculationPeriod.adjustedEndDateSpecified = true;


            return(calculationPeriod);
        }
Exemple #25
0
        private static string GetCouponType(PaymentCalculationPeriod pcalculationPeriod)
        {
            CalculationPeriod      calculationPeriod      = PaymentCalculationPeriodHelper.GetCalculationPeriods(pcalculationPeriod)[0];
            FloatingRateDefinition floatingRateDefinition = XsdClassesFieldResolver.CalculationPeriodGetFloatingRateDefinition(calculationPeriod);

            //  If has a Cap rate, finalRate = MAX(0, FinalRate - CapRate)
            //
            if (null != floatingRateDefinition.capRate)
            {
                return("Cap");
            }
            //  If has a Floor rate, finalRate = MAX(0, FloorRate - FinalRate)
            //
            if (null != floatingRateDefinition.floorRate)
            {
                return("Floor");
            }
            throw new System.Exception("Invalid coupond type. Only Cap & Floor coupons are expected here.");
        }
Exemple #26
0
        /// <summary>
        /// Gets the backward regular periods.
        /// </summary>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="periodInterval">The period interval.</param>
        /// <param name="rollConvention">The roll convention.</param>
        /// <param name="hasIrregularPeriod">if set to <c>true</c> [has irregular period].</param>
        /// <returns></returns>
        static public List <CalculationPeriod> GetBackwardRegularPeriods(DateTime startDate, DateTime endDate, Period periodInterval, RollConventionEnum rollConvention, out Boolean hasIrregularPeriod)
        {
            DateTime periodEndDate = startDate;

            hasIrregularPeriod = false;

            DateTime periodStartDate = AddPeriod(periodEndDate, periodInterval, -1);
            var      regularPeriods  = new List <CalculationPeriod>();

            while (true)
            {
                DateTime unadjustedStartDate = periodStartDate;
                DateTime unadjustedEndDate   = periodEndDate;
                if (rollConvention != RollConventionEnum.NONE)
                {
                    unadjustedStartDate = RollConventionEnumHelper.AdjustDate(rollConvention, periodStartDate);
                    unadjustedEndDate   = RollConventionEnumHelper.AdjustDate(rollConvention, periodEndDate);
                }

                CalculationPeriod calculationPeriod = CreateUnadjustedCalculationPeriod(unadjustedStartDate, unadjustedEndDate);

                if (calculationPeriod.unadjustedStartDate > endDate)
                {
                    regularPeriods.Insert(0, calculationPeriod);
                    periodEndDate   = periodStartDate;
                    periodStartDate = AddPeriod(periodEndDate, periodInterval, -1);
                }
                //last period - is not a stub
                else if (calculationPeriod.unadjustedStartDate == endDate)
                {
                    regularPeriods.Insert(0, calculationPeriod);
                    break;
                }
                else
                {
                    hasIrregularPeriod = true;
                    break;
                }
            }
            return(regularPeriods);
        }
Exemple #27
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);
        }
Exemple #28
0
        public static void UpdateFloatingRateDefinition(FloatingRateDefinition floatingRateDefinition,
                                                        FloatingRateCalculation floatingRateCalculation,
                                                        DayCountFraction dayCountFraction,
                                                        CalculationPeriod calculationPeriod,
                                                        IRateCurve forecastCurve)
        {
            var rateObservation = new RateObservation();

            if (floatingRateDefinition.rateObservation != null)
            {
                if (floatingRateDefinition.rateObservation[0].adjustedFixingDateSpecified)
                {
                    rateObservation.adjustedFixingDate          = floatingRateDefinition.rateObservation[0].adjustedFixingDate;
                    rateObservation.adjustedFixingDateSpecified = true;
                }
            }
            floatingRateDefinition.rateObservation = new[] { rateObservation };
            rateObservation.forecastRate           = GetForecastRate(calculationPeriod, forecastCurve, dayCountFraction);
            rateObservation.forecastRateSpecified  = true;
            Decimal finalRate = rateObservation.forecastRate;

            // If spread specified - add it to the final rate.
            //
            if (floatingRateDefinition.spreadSpecified)
            {
                finalRate += floatingRateDefinition.spread;
            }
            // Apply rounding (if it's been specified)
            //
            if (null != floatingRateCalculation.finalRateRounding)
            {
                Rounding finalRateRounding = floatingRateCalculation.finalRateRounding;
                floatingRateDefinition.calculatedRate = RoundingHelper.Round(finalRate, finalRateRounding);
            }
            else
            {
                floatingRateDefinition.calculatedRate = finalRate;
            }
            floatingRateDefinition.calculatedRateSpecified = true;
        }
Exemple #29
0
        IPeriodVariables <I> IDefinePeriod <I> .BeginPeriod(string caption, Func <I, I> getNextPeriod)
        {
            _currentPeriod = _periodHolders
                             .Where(p => p.WithinGroup == _currentGroup)
                             .SingleOrDefault(p =>
                                              (p.Name != null && p.Name == caption)
                                              ||
                                              (p.Name == null && p.GetNextPeriod == getNextPeriod));

            if (_currentPeriod == null)
            {
                _currentPeriod = new CalculationPeriod <I>
                {
                    Name          = caption,
                    GetNextPeriod = getNextPeriod,
                    WithinGroup   = _currentGroup
                };

                _periodHolders.Add(_currentPeriod);
            }
            return(this);
        }
        public void    CreateLongFinalStub()
        {
            CalculationPeriod lastRegularPeriod = CalculationPeriods[CalculationPeriods.Count - 1];
            CalculationPeriod shortFinalStub    = FinalStubCalculationPeriod;

            if (lastRegularPeriod.unadjustedEndDate != shortFinalStub.unadjustedStartDate)
            {
                throw new System.Exception();
            }
            var longFinalStub = new CalculationPeriod
            {
                unadjustedStartDateSpecified = true,
                unadjustedEndDateSpecified   = true,
                unadjustedStartDate          = lastRegularPeriod.unadjustedStartDate,
                unadjustedEndDate            = shortFinalStub.unadjustedEndDate
            };

            FinalStubCalculationPeriod = longFinalStub;
            // Remove last regulart period (after it was merged with short stub)
            //
            CalculationPeriods.Remove(lastRegularPeriod);
        }