//! NPV of a single cash flows
        public static double npv(CashFlow cashflow, YieldTermStructure discountCurve,
                                 Date settlementDate = null, Date npvDate = null, int exDividendDays = 0)
        {
            double NPV = 0.0;

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

            if (settlementDate == null)
            {
                settlementDate = discountCurve.referenceDate();
            }

            if (!cashflow.hasOccurred(settlementDate + exDividendDays))
            {
                NPV = cashflow.amount() * discountCurve.discount(cashflow.date());
            }

            if (npvDate == null)
            {
                return(NPV);
            }
            else
            {
                return(NPV / discountCurve.discount(npvDate));
            }
        }
Esempio n. 2
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;
        }
Esempio n. 3
0
        // At-the-money rate of the cash flows.
        // The result is the fixed rate for which a fixed rate cash flow  vector, equivalent to the input vector, has the required NPV according to the given term structure. If the required NPV is
        //  not given, the input cash flow vector's NPV is used instead.
        public static double atmRate(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                                     Date settlementDate = null, Date npvDate = null, double?targetNpv = null)
        {
            if (settlementDate == null)
            {
                settlementDate = Settings.evaluationDate();
            }

            if (npvDate == null)
            {
                npvDate = settlementDate;
            }

            double        npv  = 0.0;
            BPSCalculator calc = new BPSCalculator(discountCurve);

            for (int i = 0; i < leg.Count; ++i)
            {
                CashFlow cf = leg[i];
                if (!cf.hasOccurred(settlementDate, includeSettlementDateFlows) &&
                    !cf.tradingExCoupon(settlementDate))
                {
                    npv += cf.amount() * discountCurve.discount(cf.date());
                    cf.accept(calc);
                }
            }

            if (targetNpv == null)
            {
                targetNpv = npv - calc.nonSensNPV();
            }
            else
            {
                targetNpv *= discountCurve.discount(npvDate);
                targetNpv -= calc.nonSensNPV();
            }

            if (targetNpv.IsEqual(0.0))
            {
                return(0.0);
            }

            double bps = calc.bps();

            Utils.QL_REQUIRE(bps.IsNotEqual(0.0), () => "null bps: impossible atm rate");

            return(targetNpv.Value / bps);
        }
Esempio n. 4
0
        public static double dirtyPriceFromYield(double faceAmount, List <CashFlow> cashflows, double yield, DayCounter dayCounter,
                                                 Compounding compounding, Frequency frequency, Date settlement)
        {
            if (frequency == Frequency.NoFrequency || frequency == Frequency.Once)
            {
                frequency = Frequency.Annual;
            }

            InterestRate y = new InterestRate(yield, dayCounter, compounding, frequency);

            double price    = 0.0;
            double discount = 1.0;
            Date   lastDate = null;

            for (int i = 0; i < cashflows.Count - 1; ++i)
            {
                if (cashflows[i].hasOccurred(settlement))
                {
                    continue;
                }

                Date   couponDate = cashflows[i].Date;
                double amount     = cashflows[i].amount();
                if (lastDate == null)
                {
                    // first not-expired coupon
                    if (i > 0)
                    {
                        lastDate = cashflows[i - 1].Date;
                    }
                    else
                    {
                        if (cashflows[i].GetType().IsSubclassOf(typeof(Coupon)))
                        {
                            lastDate = ((Coupon)cashflows[i]).accrualStartDate();
                        }
                        else
                        {
                            lastDate = couponDate - new Period(1, TimeUnit.Years);
                        }
                    }
                    discount *= y.discountFactor(settlement, couponDate, lastDate, couponDate);
                }
                else
                {
                    discount *= y.discountFactor(lastDate, couponDate);
                }
                lastDate = couponDate;

                price += amount * discount;
            }

            CashFlow redemption = cashflows.Last();

            if (!redemption.hasOccurred(settlement))
            {
                Date   redemptionDate = redemption.Date;
                double amount         = redemption.amount();
                if (lastDate == null)
                {
                    // no coupons
                    lastDate  = redemptionDate - new Period(1, TimeUnit.Years);
                    discount *= y.discountFactor(settlement, redemptionDate, lastDate, redemptionDate);
                }
                else
                {
                    discount *= y.discountFactor(lastDate, redemptionDate);
                }

                price += amount * discount;
            }

            return(price / faceAmount * 100.0);
        }