Exemple #1
0
        //! NPV and BPS of the cash flows.
        // The NPV and BPS of the cash flows calculated together for performance reason
        public static void npvbps(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                                  Date settlementDate, Date npvDate, out double npv, out double bps)
        {
            npv = bps = 0.0;
            if (leg.empty())
            {
                bps = 0.0;
                return;
            }

            for (int i = 0; i < leg.Count; ++i)
            {
                CashFlow cf = leg[i];
                if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows) &&
                    !cf.tradingExCoupon(settlementDate))
                {
                    Coupon cp = leg[i] as Coupon;
                    double df = discountCurve.discount(cf.date());
                    npv += cf.amount() * df;
                    if (cp != null)
                    {
                        bps += cp.nominal() * cp.accrualPeriod() * df;
                    }
                }
            }
            double d = discountCurve.discount(npvDate);

            npv /= d;
            bps  = Const.BASIS_POINT * bps / d;
        }
Exemple #2
0
            public void visit(Coupon c)
            {
                double bps = c.nominal() *
                             c.accrualPeriod() *
                             discountCurve_.discount(c.date());

                bps_ += bps;
            }
Exemple #3
0
        /*! used internally to collect notional information from the
         *  coupons. It should not be called by derived classes,
         *  unless they already provide redemption cash flows (in
         *  which case they must set up the redemptions_ data member
         *  independently).  It will fill the notionalSchedule_ and
         *  notionals_ data members.
         */
        protected void calculateNotionalsFromCashflows()
        {
            notionalSchedule_.Clear();
            notionals_.Clear();

            Date lastPaymentDate = new Date();

            notionalSchedule_.Add(new Date());
            for (int i = 0; i < cashflows_.Count; ++i)
            {
                Coupon coupon = cashflows_[i] as Coupon;
                if (coupon == null)
                {
                    continue;
                }

                double notional = coupon.nominal();
                // we add the notional only if it is the first one...
                if (notionals_.empty())
                {
                    notionals_.Add(coupon.nominal());
                    lastPaymentDate = coupon.date();
                }
                else if (!Utils.close(notional, notionals_.Last()))
                {
                    // ...or if it has changed.
                    Utils.QL_REQUIRE(notional < notionals_.Last(), () => "increasing coupon notionals");
                    notionals_.Add(coupon.nominal());
                    // in this case, we also add the last valid date for
                    // the previous one...
                    notionalSchedule_.Add(lastPaymentDate);
                    // ...and store the candidate for this one.
                    lastPaymentDate = coupon.date();
                }
                else
                {
                    // otherwise, we just extend the valid range of dates
                    // for the current notional.
                    lastPaymentDate = coupon.date();
                }
            }
            Utils.QL_REQUIRE(!notionals_.empty(), () => "no coupons provided");
            notionals_.Add(0.0);
            notionalSchedule_.Add(lastPaymentDate);
        }
Exemple #4
0
        private static double aggregateRate(Leg leg, CashFlow cf)
        {
            if (cf == null)
            {
                return(0.0);
            }

            Date       paymentDate      = cf.date();
            bool       firstCouponFound = false;
            double     nominal          = 0.0;
            double     accrualPeriod    = 0.0;
            DayCounter dc     = null;
            double     result = 0.0;

            foreach (CashFlow x in leg.Where(x => x.date() == paymentDate))
            {
                Coupon cp = x as Coupon;
                if (cp != null)
                {
                    if (firstCouponFound)
                    {
                        Utils.QL_REQUIRE(nominal.IsEqual(cp.nominal()) &&
                                         accrualPeriod.IsEqual(cp.accrualPeriod()) &&
                                         dc == cp.dayCounter(), () =>
                                         "cannot aggregate two different coupons on "
                                         + paymentDate);
                    }
                    else
                    {
                        firstCouponFound = true;
                        nominal          = cp.nominal();
                        accrualPeriod    = cp.accrualPeriod();
                        dc = cp.dayCounter();
                    }
                    result += cp.rate();
                }
            }

            Utils.QL_REQUIRE(firstCouponFound, () => "no coupon paid at cashflow date " + paymentDate);
            return(result);
        }
Exemple #5
0
        public static double nominal(Leg leg, bool includeSettlementDateFlows, Date settlementDate = null)
        {
            CashFlow cf = nextCashFlow(leg, includeSettlementDateFlows, settlementDate);

            if (cf == null)
            {
                return(0.0);
            }

            Date paymentDate = cf.date();

            foreach (CashFlow x in leg.Where(x => x.date() == paymentDate))
            {
                Coupon cp = x as Coupon;
                if (cp != null)
                {
                    return(cp.nominal());
                }
            }
            return(0.0);
        }