Example #1
0
        public double GetForwardRate(Date startDate, Date endDate, Compound compound = Compound.Simple, IDayCount dayCount = null)
        {
            var t0 = DayCount.CalcDayCountFraction(ReferenceDate, startDate);
            var t1 = DayCount.CalcDayCountFraction(ReferenceDate, endDate);

            return(GetForwardRate(t0, t1 - t0, compound, dayCount));
        }
Example #2
0
        public double GetDf(Date startDate, Date endDate)
        {
            var t0 = DayCount.CalcDayCountFraction(ReferenceDate, startDate);
            var t1 = DayCount.CalcDayCountFraction(ReferenceDate, endDate);

            return(GetDf(t1) / GetDf(t0));
        }
Example #3
0
        public Tuple <Date, double>[] GetPaymentRates(IMarketCondition market)
        {
            List <CfCalculationDetail[]> temp;
            var coupons  = Coupon.GetCoupon(Accruals, market.FixingCurve.Value, market.HistoricalIndexRates, out temp);
            var accDates = Accruals.ToList();
            var ret      = new List <Tuple <Date, double> > ();

            for (var i = 1; i < accDates.Count; ++i)
            {
                ret.Add(
                    Tuple.Create(accDates[i], coupons[i - 1] * DayCount.CalcDayCountFraction(accDates[i - 1], accDates[i]))
                    );
            }
            return(ret.ToArray());
        }
Example #4
0
        public Cashflow[] GetCashflows(IMarketCondition market, bool netted = true)
        {
            var coupon = Notional * DepositRate * DayCount.CalcDayCountFraction(StartDate, UnderlyingMaturityDate);

            if (netted)
            {
                return(new[] { new Cashflow(StartDate, UnderlyingMaturityDate, UnderlyingMaturityDate, coupon + Notional, Currency, CashflowType.Principal, true, market.GetDf(UnderlyingMaturityDate), null) });
            }
            else
            {
                return(new[]
                {
                    new Cashflow(StartDate, UnderlyingMaturityDate, UnderlyingMaturityDate, coupon, Currency, CashflowType.Coupon, true, market.GetDf(UnderlyingMaturityDate), null),
                    new Cashflow(StartDate, UnderlyingMaturityDate, UnderlyingMaturityDate, Notional, Currency, CashflowType.Principal, true, market.GetDf(UnderlyingMaturityDate), null)
                });
            }
        }
Example #5
0
        public Cashflow[] GetCashflows(IMarketCondition market, bool netted = true)
        {
            //var payTerm = Frequency.GetTerm();
            //var accDates = new List<Date> {StartDate};
            //accDates.AddRange(new Schedule(FirstPaymentDate, MaturityDate, payTerm, Stub.ShortEnd).ToArray())
            //var Accruals = new Schedule(_implicitAccStartDate, MaturityDate, payTerm, Stub.ShortEnd).ToArray();
            var len    = Accruals.Length;
            var tArray = new double[len - 1];

            for (var i = 0; i < len - 1; ++i)
            {
                tArray[i] = DayCount.CalcDayCountFraction(Accruals[i], Accruals[i + 1]);
            }
            double[] begPrincipal;
            double[] interest;
            double[] principalPay;
            double[] prepayment;
            double[] defaultPrincipal;
            if (!IsFloatingRate)
            {
                MortgageCalculator.GetPaymentDetails(tArray, Notional, Coupon, Frequency, AmortizationType, out begPrincipal, out interest, out principalPay, out prepayment, out defaultPrincipal, _numPastPayment);
                var beg = Accruals[0];
                var end = Accruals[1];
                if (StartDate != beg)
                {
                    //first interest should be adjusted
                    interest[0] = interest[0] * (end - StartDate) / (end - beg);
                }
            }
            else
            {
                double[] begPrincipal1;
                double[] interest1;
                double[] principalPay1;
                double[] prepayment1;
                double[] defaultPrincipal1;
                MortgageCalculator.GetPaymentDetails(tArray, Notional, Coupon, Frequency, AmortizationType, out begPrincipal1, out interest1, out principalPay1, out prepayment1, out defaultPrincipal1, _numPastPayment);

                var ratePairs = market.HistoricalIndexRates.Value[IndexType].Select(x => Tuple.Create(x.Key, x.Value)).OrderBy(x => x.Item1).ToArray();
                int index;
                for (index = ratePairs.Length - 1; index > 0; --index)
                {
                    if (ratePairs[index].Item1 <= ResetDate)
                    {
                        break;
                    }
                }
                var newRate = ratePairs[index].Item2 * FloatingRateMultiplier;

                double[] begPrincipal2;
                double[] interest2;
                double[] principalPay2;
                double[] prepayment2;
                double[] defaultPrincipal2;
                var      n = Accruals.FirstIndexOf(x => x > ResetDate);
                var      p = n < 2 ? Notional : begPrincipal1[n - 2];
                MortgageCalculator.GetPaymentDetails(tArray.Skip(n - 1).ToArray(), p, newRate, Frequency, AmortizationType, out begPrincipal2, out interest2, out principalPay2, out prepayment2, out defaultPrincipal2, _numPastPayment);

                //accDate[n-1] accDate[n]
                var start = Accruals[n - 1];
                var end   = Accruals[n];

                var newInterst = p * (DayCount.CalcDayCountFraction(start, ResetDate) * Coupon + DayCount.CalcDayCountFraction(ResetDate, end) * newRate);
                interest1[n - 1]         = newInterst;
                principalPay1[n - 1]     = principalPay2[0];
                prepayment1[n - 1]       = prepayment2[0];
                defaultPrincipal1[n - 1] = defaultPrincipal2[0];
                begPrincipal1[n - 1]     = p - principalPay1[n - 1] - prepayment1[n - 1] - defaultPrincipal1[n - 1];

                double[] begPrincipal3;
                double[] interest3;
                double[] principalPay3;
                double[] prepayment3;
                double[] defaultPrincipal3;
                MortgageCalculator.GetPaymentDetails(tArray.Skip(n).ToArray(), begPrincipal1[n - 1], newRate, Frequency, AmortizationType, out begPrincipal3, out interest3, out principalPay3, out prepayment3, out defaultPrincipal3, _numPastPayment);

                var beg = Accruals[0];
                end = Accruals[1];
                if (StartDate != beg)
                {
                    //first interest should be adjusted
                    interest1[0] = interest1[0] * (end - StartDate) / (end - beg);
                }

                var tempList = begPrincipal1.Take(n).ToList();
                tempList.AddRange(begPrincipal3);
                begPrincipal = tempList.ToArray();

                tempList = interest1.Take(n).ToList();
                tempList.AddRange(interest3);
                interest = tempList.ToArray();

                tempList = principalPay1.Take(n).ToList();
                tempList.AddRange(principalPay3);
                principalPay = tempList.ToArray();

                tempList = prepayment1.Take(n).ToList();
                tempList.AddRange(prepayment3);
                prepayment = tempList.ToArray();

                tempList = defaultPrincipal1.Take(n).ToList();
                tempList.AddRange(defaultPrincipal3);
                defaultPrincipal = tempList.ToArray();
            }

            var cashflows = new List <Cashflow>();

            for (var i = 0; i < begPrincipal.Length; ++i)
            {
                var df = market.GetDf(Accruals[i + 1]);
                cashflows.Add(new Cashflow(Accruals[i], Accruals[i + 1], Accruals[i + 1], principalPay[i], Currency, CashflowType.Principal, true, df, null));
                cashflows.Add(new Cashflow(Accruals[i], Accruals[i + 1], Accruals[i + 1], interest[i], Currency, CashflowType.Coupon, true, df, null));
                cashflows.Add(new Cashflow(Accruals[i], Accruals[i + 1], Accruals[i + 1], -interest[i] * TaxRate, Currency, CashflowType.Tax, true, df, null));
                cashflows.Add(new Cashflow(Accruals[i], Accruals[i + 1], Accruals[i + 1], prepayment[i], Currency, CashflowType.Prepayment, true, df, null));
                cashflows.Add(new Cashflow(Accruals[i], Accruals[i + 1], Accruals[i + 1], defaultPrincipal[i], Currency, CashflowType.PrincipalLossOnDefault, true, df, null));
            }
            return(cashflows.ToArray());
        }
Example #6
0
        public Cashflow[] GetCashflows(IMarketCondition market, bool netted = true)
        {
            var accDates     = Accruals.ToArray();
            var cashflowType = (Coupon is FixedCoupon) ? CashflowType.FixedLegInterest : CashflowType.FloatingLegInterest;
            var cashflows    = new List <Cashflow>();

            for (var i = 0; i < accDates.Length - 1; ++i)
            {
                CfCalculationDetail[] temp;
                var paymentRate = Coupon.GetCoupon(accDates[i], accDates[i + 1], market.FixingCurve.Value, market.HistoricalIndexRates, out temp) * DayCount.CalcDayCountFraction(accDates[i], accDates[i + 1]);
                cashflows.Add(
                    new Cashflow(
                        accDates[i],
                        accDates[i + 1],
                        accDates[i + 1],
                        Notional * paymentRate,
                        Currency,
                        cashflowType,
                        temp.Aggregate(true, (current, x) => current && x.IsFixed),
                        market.DiscountCurve.Value.GetDf(accDates[i + 1]),
                        temp
                        )
                    );
            }

            if (NotionalExchange)
            {
                if (cashflows.Count >= 1)
                {
                    var last = cashflows[cashflows.Count - 1];
                    if (netted)
                    {
                        cashflows[cashflows.Count - 1] = new Cashflow(last.AccrualStartDate, last.AccrualEndDate, last.PaymentDate, last.PaymentAmount + Notional, Currency, CashflowType.Principal, last.IsFixed, market.DiscountCurve.Value.GetDf(last.PaymentDate), last.CalculationDetails);
                    }
                    else
                    {
                        cashflows.Add(new Cashflow(last.AccrualStartDate, last.AccrualEndDate, last.PaymentDate, Notional, Currency, CashflowType.Principal, true, market.DiscountCurve.Value.GetDf(last.PaymentDate), null));
                    }
                }
            }

            if (TerminationDate != null && !double.IsNaN(TerminationAmount))
            {
                return(cashflows.Where(x => x.PaymentDate < TerminationDate).ToArray()
                       .Union(new[] { new Cashflow(TerminationDate, TerminationDate, TerminationDate, TerminationAmount, Currency, CashflowType.TerminationFee, true, market.DiscountCurve.Value.GetDf(TerminationDate), null) })
                       .ToArray());
            }
            else
            {
                return(cashflows.ToArray());
            }
        }
Example #7
0
        public double GetAccruedInterest(Date calcDate, IMarketCondition market, bool isEod = true)
        {
            var totalCoupon = DepositRate * DayCount.CalcDayCountFraction(StartDate, UnderlyingMaturityDate) * Notional;

            return(totalCoupon * (calcDate - StartDate + (isEod ? 1 : 0)) / (UnderlyingMaturityDate - StartDate));
        }
Example #8
0
 public double ModelValue(IMarketCondition market, MktInstrumentCalibMethod calibMethod = MktInstrumentCalibMethod.Default)
 {
     return((1.0 / market.DiscountCurve.Value.GetDf(market.ValuationDate, UnderlyingMaturityDate) - 1.0) /
            DayCount.CalcDayCountFraction(market.ValuationDate, UnderlyingMaturityDate));
 }
Example #9
0
        public double ZeroRate(Date startDate, Date endDate, Compound compound = Compound.Continuous)
        {
            var df = GetDf(startDate, endDate);

            return(compound.CalcRateFromDf(df, DayCount.CalcDayCountFraction(startDate, endDate)));
        }
Example #10
0
        public double GetSpotRate(Date date)
        {
            var t = DayCount.CalcDayCountFraction(ReferenceDate, date);

            return(GetSpotRate(t));
        }
Example #11
0
        public double GetCompoundedRate2(Date startDate, Date endDate)
        {
            var t = DayCount.CalcDayCountFraction(startDate, endDate);

            return(GetCompoundedRate(t));
        }