Exemple #1
0
        //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure.
        public static double npv(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                                 Date settlementDate = null, Date npvDate = null)
        {
            if (leg.empty())
            {
                return(0.0);
            }

            if (settlementDate == null)
            {
                settlementDate = Settings.evaluationDate();
            }

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

            double totalNPV = 0.0;

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

            return(totalNPV / discountCurve.discount(npvDate));
        }
        //@}

        //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure.
        public static double npv(List <CashFlow> cashflows, YieldTermStructure discountCurve,
                                 Date settlementDate = null, Date npvDate = null, int exDividendDays = 0)
        {
            if (cashflows.Count == 0)
            {
                return(0.0);
            }

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

            double totalNPV = cashflows.Where(x => !x.hasOccurred(settlementDate + exDividendDays)).
                              Sum(c => c.amount() * discountCurve.discount(c.date()));

            if (npvDate == null)
            {
                return(totalNPV);
            }
            else
            {
                return(totalNPV / discountCurve.discount(npvDate));
            }
        }
        //! 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));
            }
        }
Exemple #4
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 #5
0
            public void visit(Coupon c)
            {
                double bps = c.nominal() *
                             c.accrualPeriod() *
                             discountCurve_.discount(c.date());

                bps_ += bps;
            }
        public LongstaffSchwartzPathPricer(TimeGrid times, IEarlyExercisePathPricer <PathType, double> pathPricer,
                                           YieldTermStructure termStructure)
        {
            calibrationPhase_ = true;
            pathPricer_       = pathPricer;
            coeff_            = new InitializedList <Vector>(times.size() - 1);
            dF_ = new InitializedList <double>(times.size() - 1);
            v_  = pathPricer_.basisSystem();

            for (int i = 0; i < times.size() - 1; ++i)
            {
                dF_[i] = termStructure.discount(times[i + 1])
                         / termStructure.discount(times[i]);
            }
        }
Exemple #7
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);
        }
Exemple #8
0
        // Basis-point sensitivity of the cash flows.
        // The result is the change in NPV due to a uniform 1-basis-point change in the rate paid by the cash flows. The change for each coupon is discounted according to the given term structure.
        public static double bps(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                                 Date settlementDate = null, Date npvDate = null)
        {
            if (leg.empty())
            {
                return(0.0);
            }

            if (settlementDate == null)
            {
                settlementDate = Settings.evaluationDate();
            }

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

            BPSCalculator calc = new BPSCalculator(discountCurve);

            for (int i = 0; i < leg.Count; ++i)
            {
                if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) &&
                    !leg[i].tradingExCoupon(settlementDate))
                {
                    leg[i].accept(calc);
                }
            }
            return(Const.BASIS_POINT * calc.bps() / discountCurve.discount(npvDate));
        }
Exemple #9
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 = 0.0;
         if (leg.empty())
         {
            bps = 0.0;
            return;
         }

         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);
            }
         }
         double d = discountCurve.discount(npvDate);
         npv /= d;
         bps = basisPoint_ * calc.bps() / d;
      }
Exemple #10
0
      // Basis-point sensitivity of the cash flows.
      // The result is the change in NPV due to a uniform 1-basis-point change in the rate paid by the cash flows. The change for each coupon is discounted according to the given term structure.
      public static double bps(Leg leg, YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                               Date settlementDate = null, Date npvDate = null)
      {
         if (leg.empty())
            return 0.0;

         if (settlementDate == null)
            settlementDate = Settings.evaluationDate();

         if (npvDate == null)
            npvDate = settlementDate;

         BPSCalculator calc = new BPSCalculator(discountCurve);
         for (int i = 0; i < leg.Count; ++i)
         {
				if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) &&
					 !leg[i].tradingExCoupon(settlementDate))
               leg[i].accept(calc);
         }
         return basisPoint_ * calc.bps() / discountCurve.discount(npvDate);
      }
Exemple #11
0
      //// need to refactor the bond classes and remove the following  
      //public static Date previousCouponDate(List<CashFlow> leg, bool includeSettlementDateFlows,Date refDate)
      //{
      //   var cf = previousCashFlow(leg,includeSettlementDateFlows, refDate);
      //   if (cf == leg.Last()) return null;
      //   return cf.date();
      //}
      //public static Date nextCouponDate(List<CashFlow> leg, bool includeSettlementDateFlows,Date refDate)
      //{
      //   var cf = nextCashFlow(leg,includeSettlementDateFlows, refDate);
      //   if (cf == leg.Last()) return null;
      //   return cf.date();
      //}
      ////@}

      #region YieldTermStructure functions

      //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure.
      public static double npv(Leg leg,YieldTermStructure discountCurve, bool includeSettlementDateFlows,
                               Date settlementDate = null, Date npvDate = null) 
      {

         if (leg.empty())
            return 0.0;

         if (settlementDate == null)
            settlementDate = Settings.evaluationDate();

         if (npvDate == null)
            npvDate = settlementDate;

         double totalNPV = 0.0;
         for (int i=0; i<leg.Count; ++i) 
         {
            if (!leg[i].hasOccurred(settlementDate, includeSettlementDateFlows) && !leg[i].tradingExCoupon(settlementDate))
               totalNPV += leg[i].amount() * discountCurve.discount(leg[i].date());
         }

         return totalNPV/discountCurve.discount(npvDate);
    }
Exemple #12
0
 //////////////////////////////////////////////////////////////////////////////////////
 // methods
 public double price(YieldTermStructure yts)
 {
     return(amount() * yts.discount(date()));
 }
Exemple #13
0
        }                                                                     // initial guess

        public double guess(YieldTermStructure c, Date d)
        {
            return(c.discount(d, true));
        }                                                                                // further guesses
Exemple #14
0
 public double guess(YieldTermStructure c, Date d)
 {
     return c.discount(d, true);
 }
 public void visit(Coupon c)
 {
     result_ += c.accrualPeriod() * c.nominal() * termStructure_.discount(c.date());
 }
Exemple #16
0
        public override void initialize(FloatingRateCoupon coupon)
        {
            coupon_ = coupon as CmsCoupon;
            Utils.QL_REQUIRE( coupon_ != null, () => "CMS coupon needed" );
            gearing_ = coupon_.gearing();
            spread_ = coupon_.spread();

            fixingDate_ = coupon_.fixingDate();
            paymentDate_ = coupon_.date();
            SwapIndex swapIndex = coupon_.swapIndex();
            rateCurve_ = swapIndex.forwardingTermStructure().link;

            Date today = Settings.evaluationDate();

            if (paymentDate_ > today)
                discount_ = rateCurve_.discount(paymentDate_);
            else
                discount_ = 1.0;

            spreadLegValue_ = spread_ * coupon_.accrualPeriod() * discount_;

            if (fixingDate_ > today) {
                swapTenor_ = swapIndex.tenor();
                VanillaSwap swap = swapIndex.underlyingSwap(fixingDate_);

                swapRateValue_ = swap.fairRate();

                double bp = 1.0e-4;
                annuity_ = (swap.floatingLegBPS() / bp);

                int q = (int)swapIndex.fixedLegTenor().frequency();
                Schedule schedule = swap.fixedSchedule();
                DayCounter dc = swapIndex.dayCounter();
                //DayCounter dc = coupon.dayCounter();
                double startTime = dc.yearFraction(rateCurve_.referenceDate(), swap.startDate());
                double swapFirstPaymentTime = dc.yearFraction(rateCurve_.referenceDate(), schedule.date(1));
                double paymentTime = dc.yearFraction(rateCurve_.referenceDate(), paymentDate_);
                double delta = (paymentTime - startTime) / (swapFirstPaymentTime - startTime);

                switch (modelOfYieldCurve_) {
                    case GFunctionFactory.YieldCurveModel.Standard:
                        gFunction_ = GFunctionFactory.newGFunctionStandard(q, delta, swapTenor_.length());
                        break;
                    case GFunctionFactory.YieldCurveModel.ExactYield:
                        gFunction_ = GFunctionFactory.newGFunctionExactYield(coupon_);
                        break;
                    case GFunctionFactory.YieldCurveModel.ParallelShifts: {
                            Handle<Quote> nullMeanReversionQuote = new Handle<Quote>(new SimpleQuote(0.0));
                            gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, nullMeanReversionQuote);
                        }
                        break;
                    case GFunctionFactory.YieldCurveModel.NonParallelShifts:
                        gFunction_ = GFunctionFactory.newGFunctionWithShifts(coupon_, meanReversion_);
                        break;
                    default:
                        throw new ApplicationException("unknown/illegal gFunction type");
                }
                vanillaOptionPricer_ = new BlackVanillaOptionPricer(swapRateValue_, fixingDate_, swapTenor_, swaptionVolatility().link);
            }
        }
Exemple #17
0
 //////////////////////////////////////////////////////////////////////////////////////
 // methods
 public double price(YieldTermStructure yts)
 {
    return amount() * yts.discount(date());
 }
Exemple #18
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==0.0)
            return 0.0;

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

         return targetNpv.Value/bps;
    }
      //@}

      //! NPV of the cash flows. The NPV is the sum of the cash flows, each discounted according to the given term structure.
      public static double npv(List<CashFlow> cashflows, YieldTermStructure discountCurve, 
                               Date settlementDate = null,Date npvDate = null, int exDividendDays = 0) 
      {
         if (cashflows.Count == 0)
            return 0.0;

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

         double totalNPV = cashflows.Where(x => !x.hasOccurred(settlementDate + exDividendDays)).
                                        Sum(c => c.amount() * discountCurve.discount(c.date()));

         if (npvDate == null) 
            return totalNPV;
         else 
            return totalNPV / discountCurve.discount(npvDate);
      }
Exemple #20
0
      //! 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 = Settings.evaluationDate();

         if (npvDate == null)
            npvDate = settlementDate;

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

         
         return NPV / discountCurve.discount(npvDate);
      }
Exemple #21
0
        //! 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);
        }