Example #1
0
            public ZSpreadFinder(Leg leg, YieldTermStructure discountCurve, double npv, DayCounter dc, Compounding comp, Frequency freq,
                                 bool includeSettlementDateFlows, Date settlementDate, Date npvDate)
            {
                leg_     = leg;
                npv_     = npv;
                zSpread_ = new SimpleQuote(0.0);
                curve_   = new ZeroSpreadedTermStructure(new Handle <YieldTermStructure>(discountCurve),
                                                         new Handle <Quote>(zSpread_), comp, freq, dc);
                includeSettlementDateFlows_ = includeSettlementDateFlows;
                settlementDate_             = settlementDate;
                npvDate_ = npvDate;

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

                if (npvDate == null)
                {
                    npvDate_ = settlementDate_;
                }

                // if the discount curve allows extrapolation, let's
                // the spreaded curve do too.
                curve_.enableExtrapolation(discountCurve.allowsExtrapolation());
            }
Example #2
0
        // NPV of the cash flows.
        //  The NPV is the sum of the cash flows, each discounted
        //  according to the z-spreaded term structure.  The result
        //  is affected by the choice of the z-spread compounding
        //  and the relative frequency and day counter.
        public static double npv(Leg leg, YieldTermStructure discountCurve, double zSpread, DayCounter dc, Compounding comp,
                                 Frequency freq, 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;
            }

            Handle <YieldTermStructure> discountCurveHandle = new Handle <YieldTermStructure>(discountCurve);
            Handle <Quote> zSpreadQuoteHandle = new Handle <Quote>(new SimpleQuote(zSpread));

            ZeroSpreadedTermStructure spreadedCurve = new ZeroSpreadedTermStructure(discountCurveHandle, zSpreadQuoteHandle,
                                                                                    comp, freq, dc);

            spreadedCurve.enableExtrapolation(discountCurveHandle.link.allowsExtrapolation());

            return(npv(leg, spreadedCurve, includeSettlementDateFlows, settlementDate, npvDate));
        }
Example #3
0
        public static double dirtyPriceFromZSpreadFunction(double faceAmount, List <CashFlow> cashflows, double zSpread,
                                                           DayCounter dc, Compounding comp, Frequency freq, Date settlement,
                                                           Handle <YieldTermStructure> discountCurve)
        {
            if (!(freq != Frequency.NoFrequency && freq != Frequency.Once))
            {
                throw new ApplicationException("invalid frequency:" + freq);
            }

            Quote zSpreadQuoteHandle = new SimpleQuote(zSpread);

            var spreadedCurve = new ZeroSpreadedTermStructure(discountCurve, zSpreadQuoteHandle, comp, freq, dc);

            double price = 0.0;

            foreach (CashFlow cf in cashflows.FindAll(x => !x.hasOccurred(settlement)))
            {
                Date   couponDate = cf.Date;
                double amount     = cf.amount();
                price += amount * spreadedCurve.discount(couponDate);
            }
            price /= spreadedCurve.discount(settlement);
            return(price / faceAmount * 100.0);
        }
Example #4
0
         public ZSpreadFinder(Leg leg,YieldTermStructure discountCurve,double npv,DayCounter dc,Compounding comp,Frequency freq,
                              bool includeSettlementDateFlows, Date settlementDate, Date npvDate)
         {
            leg_ = leg;
            npv_ = npv;
            zSpread_ = new SimpleQuote(0.0);
            curve_ = new ZeroSpreadedTermStructure(new Handle<YieldTermStructure>(discountCurve),
                                                   new Handle<Quote>(zSpread_), comp, freq, dc);
            includeSettlementDateFlows_ = includeSettlementDateFlows;
            settlementDate_ = settlementDate;
            npvDate_ = npvDate;

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

            if (npvDate == null)
               npvDate = settlementDate;

            // if the discount curve allows extrapolation, let's
            // the spreaded curve do too.
            curve_.enableExtrapolation(discountCurve.allowsExtrapolation());
         }
Example #5
0
      // NPV of the cash flows.
      //  The NPV is the sum of the cash flows, each discounted
      //  according to the z-spreaded term structure.  The result
      //  is affected by the choice of the z-spread compounding
      //  and the relative frequency and day counter.
      public static double npv(Leg leg,YieldTermStructure discountCurve,double zSpread,DayCounter dc,Compounding comp,
                               Frequency freq,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;

         Handle<YieldTermStructure> discountCurveHandle = new Handle<YieldTermStructure>(discountCurve);
         Handle<Quote> zSpreadQuoteHandle = new Handle<Quote>(new SimpleQuote(zSpread));

         ZeroSpreadedTermStructure spreadedCurve = new ZeroSpreadedTermStructure(discountCurveHandle,zSpreadQuoteHandle,
            comp, freq, dc);

         spreadedCurve.enableExtrapolation(discountCurveHandle.link.allowsExtrapolation());

         return npv(leg, spreadedCurve, includeSettlementDateFlows, settlementDate, npvDate);
      }
Example #6
0
        public void testZSpreadedObs()
        {
            // ("Testing observability of zero-spreaded term structure...");

             CommonVars vars = new CommonVars();

             SimpleQuote me = new SimpleQuote(0.01);
             Handle<Quote> mh = new Handle<Quote>(me);
             RelinkableHandle<YieldTermStructure> h = new RelinkableHandle<YieldTermStructure>(vars.dummyTermStructure);

             YieldTermStructure spreaded = new ZeroSpreadedTermStructure(h, mh);
             Flag flag = new Flag();
             spreaded.registerWith(flag.update);
             h.linkTo(vars.termStructure);
             if (!flag.isUp())
            Console.WriteLine("Observer was not notified of term structure change");
             flag.lower();
             me.setValue(0.005);
             if (!flag.isUp())
            Console.WriteLine("Observer was not notified of spread change");
        }
Example #7
0
        public void testZSpreaded()
        {
            // ("Testing consistency of zero-spreaded term structure...");

             CommonVars vars = new CommonVars();

             double tolerance = 1.0e-10;
             Quote me = new SimpleQuote(0.01);
             Handle<Quote> mh = new Handle<Quote>(me);
             YieldTermStructure spreaded = new ZeroSpreadedTermStructure(new Handle<YieldTermStructure>(vars.termStructure), mh);
             Date testDate = vars.termStructure.referenceDate() + new Period(5, TimeUnit.Years);
             DayCounter rfdc = vars.termStructure.dayCounter();
             double zero = vars.termStructure.zeroRate(testDate, rfdc, Compounding.Continuous, Frequency.NoFrequency).rate();
             double spreadedZero = spreaded.zeroRate(testDate, rfdc, Compounding.Continuous, Frequency.NoFrequency).rate();
             if (Math.Abs(zero - (spreadedZero - me.value())) > tolerance)
            Console.WriteLine("unable to reproduce zero yield from spreaded curve\n"
                + "    calculated: " + (spreadedZero - me.value()) + "\n"
                + "    expected:   " + zero);
        }