Inheritance: QLNet.Cashflows.RateLegBase
        //! simple annual compounding coupon rates
        public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule, List <double> coupons,
                             DayCounter accrualDayCounter, BusinessDayConvention paymentConvention,
                             double redemption, Date issueDate, Calendar paymentCalendar)
            : base(settlementDays, paymentCalendar == new Calendar() ? schedule.calendar() : paymentCalendar,
                   issueDate)
        {
            frequency_    = schedule.tenor().frequency();
            dayCounter_   = accrualDayCounter;
            maturityDate_ = schedule.endDate();

            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, accrualDayCounter)
                         .withPaymentCalendar(calendar_)
                         .withNotionals(faceAmount)
                         .withPaymentAdjustment(paymentConvention);

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });

            if (cashflows().Count == 0)
            {
                throw new ApplicationException("bond with no cashflows!");
            }

            if (redemptions_.Count != 1)
            {
                throw new ApplicationException("multiple redemptions created");
            }
        }
      public AmortizingFixedRateBond(
                          int settlementDays,
                          List<double> notionals,
                          Schedule schedule,
                          List<double> coupons,
                          DayCounter accrualDayCounter,
                          BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                          Date issueDate = null)
         :base(settlementDays, schedule.calendar(), issueDate)
      {
         frequency_ = schedule.tenor().frequency();
         dayCounter_ = accrualDayCounter;
         schedule_ = schedule;

         maturityDate_ = schedule.endDate();

         cashflows_ = new FixedRateLeg(schedule)
             .withCouponRates(coupons, accrualDayCounter)
             .withNotionals(notionals)
             .withPaymentAdjustment(paymentConvention).value();
             

         addRedemptionsToCashflows();

         if ( cashflows().empty())
            throw new ApplicationException("bond with no cashflows!");
      }
        public AmortizingFixedRateBond(
            int settlementDays,
            Calendar calendar,
            double faceAmount,
            Date startDate,
            Period bondTenor,
            Frequency sinkingFrequency,
            double coupon,
            DayCounter accrualDayCounter,
            BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
            Date issueDate = null)
            : base(settlementDays, calendar, issueDate)
        {
            frequency_  = sinkingFrequency;
            dayCounter_ = accrualDayCounter;

            maturityDate_ = startDate + bondTenor;
            schedule_     = sinkingSchedule(startDate, bondTenor, sinkingFrequency, calendar);
            cashflows_    = new FixedRateLeg(schedule_)
                            .withCouponRates(coupon, accrualDayCounter)
                            .withNotionals(sinkingNotionals(bondTenor, sinkingFrequency, coupon, faceAmount))
                            .withPaymentAdjustment(paymentConvention).value();

            addRedemptionsToCashflows();
        }
示例#4
0
        //! \name ructors
        //@{
        //! CDS quoted as running-spread only

        /*! @param side  Whether the protection is bought or sold.
         *  @param notional  Notional value
         *  @param spread  Running spread in fractional units.
         *  @param schedule  Coupon schedule.
         *  @param paymentConvention  Business-day convention for
         *                            payment-date adjustment.
         *  @param dayCounter  Day-count convention for accrual.
         *  @param settlesAccrual  Whether or not the accrued coupon is
         *                         due in the event of a default.
         *  @param paysAtDefaultTime  If set to true, any payments
         *                            triggered by a default event are
         *                            due at default time. If set to
         *                            false, they are due at the end of
         *                            the accrual period.
         *  @param protectionStart  The first date where a default
         *                          event will trigger the contract.
         */
        public CreditDefaultSwap(Protection.Side side,
                                 double notional,
                                 double spread,
                                 Schedule schedule,
                                 BusinessDayConvention convention,
                                 DayCounter dayCounter,
                                 bool settlesAccrual    = true,
                                 bool paysAtDefaultTime = true,
                                 Date protectionStart   = null,
                                 Claim claim            = null)
        {
            side_              = side;
            notional_          = notional;
            upfront_           = null;
            runningSpread_     = spread;
            settlesAccrual_    = settlesAccrual;
            paysAtDefaultTime_ = paysAtDefaultTime;
            claim_             = claim;
            protectionStart_   = protectionStart ?? schedule[0];

            Utils.QL_REQUIRE(protectionStart_ <= schedule[0], () => "protection can not start after accrual");
            leg_ = new FixedRateLeg(schedule)
                   .withCouponRates(spread, dayCounter)
                   .withNotionals(notional)
                   .withPaymentAdjustment(convention);

            upfrontPayment_ = new SimpleCashFlow(0.0, schedule[0]);

            if (claim_ == null)
            {
                claim_ = new FaceValueClaim();
            }

            claim_.registerWith(update);
        }
示例#5
0
        public void testDefaultSettlementDate()
        {
            // Testing default evaluation date in cashflows methods...
            Date today = Settings.evaluationDate();
            Schedule schedule = new
                MakeSchedule()
                .from(today-new Period(2,TimeUnit.Months)).to(today+new Period(4,TimeUnit.Months))
                .withFrequency(Frequency.Semiannual)
                .withCalendar(new TARGET())
                .withConvention(BusinessDayConvention.Unadjusted)
                .backwards().value();

            List<CashFlow> leg = new FixedRateLeg(schedule)
                        .withCouponRates(0.03, new Actual360())
                        .withPaymentCalendar(new TARGET())
                        .withNotionals(100.0)
                        .withPaymentAdjustment(BusinessDayConvention.Following);

            double accruedPeriod = CashFlows.accruedPeriod(leg, false);
            if (accruedPeriod == 0.0)
                Assert.Fail("null accrued period with default settlement date");

            int accruedDays = CashFlows.accruedDays(leg, false);
            if (accruedDays == 0)
                Assert.Fail("no accrued days with default settlement date");

            double accruedAmount = CashFlows.accruedAmount(leg, false);
            if (accruedAmount == 0.0)
                Assert.Fail("null accrued amount with default settlement date");
        }
示例#6
0
        //! fixed-rate bond

        /*! \ingroup instruments
         *
         *  \test calculations are tested by checking results against
         *        cached values.
         */


        //! simple annual compounding coupon rates
        public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule, List <double> coupons,
                             DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                             double redemption         = 100, Date issueDate = null, Calendar paymentCalendar = null,
                             Period exCouponPeriod     = null,
                             Calendar exCouponCalendar = null,
                             BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted,
                             bool exCouponEndOfMonth = false)
            : base(settlementDays, paymentCalendar ?? schedule.calendar(),
                   issueDate)
        {
            frequency_    = schedule.tenor().frequency();
            dayCounter_   = accrualDayCounter;
            maturityDate_ = schedule.endDate();

            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, accrualDayCounter)
                         .withExCouponPeriod(exCouponPeriod,
                                             exCouponCalendar,
                                             exCouponConvention,
                                             exCouponEndOfMonth)
                         .withPaymentCalendar(calendar_)
                         .withNotionals(faceAmount)
                         .withPaymentAdjustment(paymentConvention);

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });

            Utils.QL_REQUIRE(cashflows().Count != 0, () => "bond with no cashflows!");
            Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created");
        }
示例#7
0
        private double calculateYield()
        {
            // We create a bond cashflow from issue to maturity just
            // to calculate effective rate ( the rate that discount _marketValue )
            Schedule schedule = new Schedule(_issueDate, _maturityDate, new Period(_payFrequency),
                                             _calendar, BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                             DateGeneration.Rule.Backward, false);


            List <CashFlow> cashflows = new FixedRateLeg(schedule)
                                        .withCouponRates(_couponRate, _dCounter)
                                        .withPaymentCalendar(_calendar)
                                        .withNotionals(_faceValue)
                                        .withPaymentAdjustment(BusinessDayConvention.Unadjusted);

            // Add single redemption for yield calculation
            Redemption r = new Redemption(_faceValue, _maturityDate);

            cashflows.Add(r);

            // Calculate Amortizing Yield ( Effective Rate )
            Date testDate = CashFlows.previousCashFlowDate(cashflows, false, _tradeDate);

            return(CashFlows.yield(cashflows, _marketValue, _dCounter, Compounding.Simple, _payFrequency,
                                   false, testDate));
        }
示例#8
0
      //! fixed-rate bond
      /*! \ingroup instruments

          \test calculations are tested by checking results against
                cached values.
      */
 

      //! simple annual compounding coupon rates      
      public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule,List<double> coupons, 
                           DayCounter accrualDayCounter, BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                           double redemption = 100, Date issueDate = null,Calendar paymentCalendar = null,
			                  Period exCouponPeriod = null,
                           Calendar exCouponCalendar = null,
									BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted,
                           bool exCouponEndOfMonth = false)
         : base(settlementDays, paymentCalendar == null ? schedule.calendar() : paymentCalendar, 
                issueDate) 
      {
         frequency_ = schedule.tenor().frequency();
         dayCounter_ = accrualDayCounter;
         maturityDate_ = schedule.endDate();

         cashflows_ = new FixedRateLeg(schedule)
            .withCouponRates(coupons, accrualDayCounter)
				.withExCouponPeriod(exCouponPeriod,
										  exCouponCalendar,
										  exCouponConvention,
										  exCouponEndOfMonth)
            .withPaymentCalendar(calendar_)
            .withNotionals(faceAmount)
            .withPaymentAdjustment(paymentConvention); 

         addRedemptionsToCashflows(new List<double>() { redemption });

         if (cashflows().Count == 0)
            throw new ApplicationException("bond with no cashflows!");

         if (redemptions_.Count != 1)
            throw new ApplicationException("multiple redemptions created");
      }
示例#9
0
        public ConvertibleFixedCouponBond(Exercise exercise,
                                          double conversionRatio,
                                          DividendSchedule dividends,
                                          CallabilitySchedule callability,
                                          Handle <Quote> creditSpread,
                                          Date issueDate,
                                          int settlementDays,
                                          List <double> coupons,
                                          DayCounter dayCounter,
                                          Schedule schedule,
                                          double redemption = 100)
            : base(
                exercise, conversionRatio, dividends, callability, creditSpread, issueDate, settlementDays, schedule,
                redemption)
        {
            // !!! notional forcibly set to 100
            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, dayCounter)
                         .withNotionals(100.0)
                         .withPaymentAdjustment(schedule.businessDayConvention());

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });

            Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created");

            option_ = new option(this, exercise, conversionRatio, dividends, callability, creditSpread, cashflows_,
                                 dayCounter, schedule,
                                 issueDate, settlementDays, redemption);
        }
        public AmortizingFixedRateBond(
            int settlementDays,
            List <double> notionals,
            Schedule schedule,
            List <double> coupons,
            DayCounter accrualDayCounter,
            BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
            Date issueDate = null)
            : base(settlementDays, schedule.calendar(), issueDate)
        {
            frequency_  = schedule.tenor().frequency();
            dayCounter_ = accrualDayCounter;
            schedule_   = schedule;

            maturityDate_ = schedule.endDate();

            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, accrualDayCounter)
                         .withNotionals(notionals)
                         .withPaymentAdjustment(paymentConvention).value();


            addRedemptionsToCashflows();

            if (cashflows().empty())
            {
                throw new ApplicationException("bond with no cashflows!");
            }
        }
示例#11
0
        // constructor
        public VanillaSwap(Type type, double nominal,
                           Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount,
                           Schedule floatSchedule, IborIndex iborIndex, double spread, DayCounter floatingDayCount,
                           BusinessDayConvention?paymentConvention = null) :
            base(2)
        {
            type_             = type;
            nominal_          = nominal;
            fixedSchedule_    = fixedSchedule;
            fixedRate_        = fixedRate;
            fixedDayCount_    = fixedDayCount;
            floatingSchedule_ = floatSchedule;
            iborIndex_        = iborIndex;
            spread_           = spread;
            floatingDayCount_ = floatingDayCount;

            if (paymentConvention.HasValue)
            {
                paymentConvention_ = paymentConvention.Value;
            }
            else
            {
                paymentConvention_ = floatingSchedule_.businessDayConvention();
            }

            legs_[0] = new FixedRateLeg(fixedSchedule)
                       .withCouponRates(fixedRate, fixedDayCount)
                       .withPaymentAdjustment(paymentConvention_)
                       .withNotionals(nominal);

            legs_[1] = new IborLeg(floatSchedule, iborIndex)
                       .withPaymentDayCounter(floatingDayCount)
                       //.withFixingDays(iborIndex.fixingDays())
                       .withSpreads(spread)
                       .withNotionals(nominal)
                       .withPaymentAdjustment(paymentConvention_);

            foreach (var cf in legs_[1])
            {
                cf.registerWith(update);
            }

            switch (type_)
            {
            case Type.Payer:
                payer_[0] = -1.0;
                payer_[1] = +1.0;
                break;

            case Type.Receiver:
                payer_[0] = +1.0;
                payer_[1] = -1.0;
                break;

            default:
                Utils.QL_FAIL("Unknown vanilla-swap type");
                break;
            }
        }
示例#12
0
        public OvernightIndexedSwap(Type type,
                                    double fixedNominal,
                                    Schedule fixedSchedule,
                                    double fixedRate,
                                    DayCounter fixedDC,
                                    double overnightNominal,
                                    Schedule overnightSchedule,
                                    OvernightIndex overnightIndex,
                                    double spread) :
            base(2)
        {
            type_                      = type;
            fixedNominal_              = fixedNominal;
            overnightNominal_          = overnightNominal;
            fixedPaymentFrequency_     = fixedSchedule.tenor().frequency();
            overnightPaymentFrequency_ = overnightSchedule.tenor().frequency();
            fixedRate_                 = fixedRate;
            fixedDC_                   = fixedDC;
            overnightIndex_            = overnightIndex;
            spread_                    = spread;

            if (fixedDC_ == null)
            {
                fixedDC_ = overnightIndex_.dayCounter();
            }

            legs_[0] = new FixedRateLeg(fixedSchedule)
                       .withCouponRates(fixedRate_, fixedDC_)
                       .withNotionals(fixedNominal_);

            legs_[1] = new OvernightLeg(overnightSchedule, overnightIndex_)
                       .withNotionals(overnightNominal_)
                       .withSpreads(spread_);

            for (int j = 0; j < 2; ++j)
            {
                for (int i = 0; i < legs_[j].Count; i++)
                {
                    legs_[j][i].registerWith(update);
                }
            }

            switch (type_)
            {
            case Type.Payer:
                payer_[0] = -1.0;
                payer_[1] = +1.0;
                break;

            case Type.Receiver:
                payer_[0] = +1.0;
                payer_[1] = -1.0;
                break;

            default:
                Utils.QL_FAIL("Unknown overnight-swap type");
                break;
            }
        }
示例#13
0
      public YearOnYearInflationSwap(
         Type type,
         double nominal,
         Schedule fixedSchedule,
         double fixedRate,
         DayCounter fixedDayCount,
         Schedule yoySchedule,
         YoYInflationIndex yoyIndex,
         Period observationLag,
         double spread,
         DayCounter yoyDayCount,
         Calendar paymentCalendar,    // inflation index does not have a calendar
         BusinessDayConvention paymentConvention = BusinessDayConvention.ModifiedFollowing)
         : base(2)
      {
         type_ = type;
         nominal_ = nominal;
         fixedSchedule_ = fixedSchedule;
         fixedRate_ = fixedRate;
         fixedDayCount_ = fixedDayCount;
         yoySchedule_ = yoySchedule;
         yoyIndex_ = yoyIndex;
         observationLag_ = observationLag;
         spread_ = spread;
         yoyDayCount_ = yoyDayCount;
         paymentCalendar_ = paymentCalendar;
         paymentConvention_ = paymentConvention;

         // N.B. fixed leg gets its calendar from the schedule!
         List<CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule_)
         .withCouponRates(fixedRate_, fixedDayCount_)   // Simple compounding by default
         .withNotionals(nominal_)
         .withPaymentAdjustment(paymentConvention_);

         List<CashFlow> yoyLeg = new yoyInflationLeg(yoySchedule_, paymentCalendar_, yoyIndex_, observationLag_)
         .withSpreads(spread_)
         .withPaymentDayCounter(yoyDayCount_)
         .withNotionals(nominal_)
         .withPaymentAdjustment(paymentConvention_);

         yoyLeg.ForEach((i, x) =>  x.registerWith(update));


         legs_[0] = fixedLeg;
         legs_[1] = yoyLeg;
         if (type_ == Type.Payer)
         {
            payer_[0] = -1.0;
            payer_[1] = +1.0;
         }
         else
         {
            payer_[0] = +1.0;
            payer_[1] = -1.0;
         }

      }
示例#14
0
        protected override void performCalculations()
        {
            Period indexTenor = index_.tenor();
            double fixedRate = 0.04;             // dummy value
            Date   startDate, maturity;

            if (includeFirstSwaplet_)
            {
                startDate = termStructure_.link.referenceDate();
                maturity  = termStructure_.link.referenceDate() + length_;
            }
            else
            {
                startDate = termStructure_.link.referenceDate() + indexTenor;
                maturity  = termStructure_.link.referenceDate() + length_;
            }
            IborIndex dummyIndex = new IborIndex("dummy",
                                                 indexTenor,
                                                 index_.fixingDays(),
                                                 index_.currency(),
                                                 index_.fixingCalendar(),
                                                 index_.businessDayConvention(),
                                                 index_.endOfMonth(),
                                                 termStructure_.link.dayCounter(),
                                                 termStructure_);

            InitializedList <double> nominals = new InitializedList <double>(1, 1.0);

            Schedule floatSchedule = new Schedule(startDate, maturity,
                                                  index_.tenor(), index_.fixingCalendar(),
                                                  index_.businessDayConvention(),
                                                  index_.businessDayConvention(),
                                                  DateGeneration.Rule.Forward, false);
            List <CashFlow> floatingLeg = new IborLeg(floatSchedule, index_)
                                          .withFixingDays(0)
                                          .withNotionals(nominals)
                                          .withPaymentAdjustment(index_.businessDayConvention());

            Schedule fixedSchedule = new Schedule(startDate, maturity, new Period(fixedLegFrequency_),
                                                  index_.fixingCalendar(),
                                                  BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                                  DateGeneration.Rule.Forward, false);
            List <CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                                       .withCouponRates(fixedRate, fixedLegDayCounter_)
                                       .withNotionals(nominals)
                                       .withPaymentAdjustment(index_.businessDayConvention());

            Swap swap = new Swap(floatingLeg, fixedLeg);

            swap.setPricingEngine(new DiscountingSwapEngine(termStructure_, false));
            double fairRate = fixedRate - (double)(swap.NPV() / (swap.legBPS(1) / 1.0e-4));

            cap_ = new Cap(floatingLeg, new InitializedList <double>(1, fairRate));

            base.performCalculations();
        }
示例#15
0
        /// <summary>
        /// CDS quoted as running-spread only
        /// </summary>
        /// <param name="side">Whether the protection is bought or sold.</param>
        /// <param name="notional">Notional value</param>
        /// <param name="spread">Running spread in fractional units.</param>
        /// <param name="schedule">Coupon schedule.</param>
        /// <param name="convention">Business-day convention for payment-date adjustment.</param>
        /// <param name="dayCounter">Day-count convention for accrual.</param>
        /// <param name="settlesAccrual">Whether or not the accrued coupon is due in the event of a default.</param>
        /// <param name="paysAtDefaultTime">If set to true, any payments triggered by a default event are
        /// due at default time. If set to false, they are due at the end of the accrual period.</param>
        /// <param name="protectionStart">The first date where a default event will trigger the contract.</param>
        /// <param name="claim"></param>
        /// <param name="lastPeriodDayCounter">Day-count convention for accrual in last period</param>
        /// <param name="rebatesAccrual">The protection seller pays the accrued scheduled current coupon at the start
        /// of the contract. The rebate date is not provided but computed to be two days after protection start.</param>
        public CreditDefaultSwap(Protection.Side side,
                                 double notional,
                                 double spread,
                                 Schedule schedule,
                                 BusinessDayConvention convention,
                                 DayCounter dayCounter,
                                 bool settlesAccrual             = true,
                                 bool paysAtDefaultTime          = true,
                                 Date protectionStart            = null,
                                 Claim claim                     = null,
                                 DayCounter lastPeriodDayCounter = null,
                                 bool rebatesAccrual             = true)
        {
            side_              = side;
            notional_          = notional;
            upfront_           = null;
            runningSpread_     = spread;
            settlesAccrual_    = settlesAccrual;
            paysAtDefaultTime_ = paysAtDefaultTime;
            claim_             = claim;
            protectionStart_   = protectionStart ?? schedule[0];

            Utils.QL_REQUIRE(protectionStart_ <= schedule[0] ||
                             schedule.rule() == DateGeneration.Rule.CDS ||
                             schedule.rule() == DateGeneration.Rule.CDS2015
                             , () => "protection can not start after accrual");

            leg_ = new FixedRateLeg(schedule)
                   .withLastPeriodDayCounter(lastPeriodDayCounter)
                   .withCouponRates(spread, dayCounter)
                   .withNotionals(notional)
                   .withPaymentAdjustment(convention);

            Date effectiveUpfrontDate = schedule.calendar().advance(protectionStart_, 2, TimeUnit.Days, convention);

            // '2' is used above since the protection start is assumed to be on trade_date + 1
            if (rebatesAccrual)
            {
                FixedRateCoupon firstCoupon = leg_[0] as FixedRateCoupon;

                Date rebateDate = effectiveUpfrontDate;
                accrualRebate_ = new SimpleCashFlow(firstCoupon.accruedAmount(protectionStart_), rebateDate);
            }

            upfrontPayment_ = new SimpleCashFlow(0.0, effectiveUpfrontDate);

            if (claim_ == null)
            {
                claim_ = new FaceValueClaim();
            }

            claim_.registerWith(update);
            maturity_ = schedule.dates().Last();
        }
示例#16
0
        public FixedLoan(Type type, double nominal,
                         Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount,
                         Schedule principalSchedule, BusinessDayConvention?paymentConvention) :
            base(2)
        {
            type_              = type;
            nominal_           = nominal;
            fixedSchedule_     = fixedSchedule;
            fixedRate_         = fixedRate;
            fixedDayCount_     = fixedDayCount;
            principalSchedule_ = principalSchedule;

            if (paymentConvention.HasValue)
            {
                paymentConvention_ = paymentConvention.Value;
            }
            else
            {
                paymentConvention_ = fixedSchedule_.businessDayConvention();
            }

            List <CashFlow> principalLeg = new PricipalLeg(principalSchedule, fixedDayCount)
                                           .withNotionals(nominal)
                                           .withPaymentAdjustment(paymentConvention_)
                                           .withSign(type == Type.Loan ? -1 : 1);

            // temporary
            for (int i = 0; i < principalLeg.Count - 1; i++)
            {
                Principal p = (Principal)principalLeg[i];
                notionals_.Add(p.nominal());
            }

            List <CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                                       .withCouponRates(fixedRate, fixedDayCount)
                                       .withPaymentAdjustment(paymentConvention_)
                                       .withNotionals(notionals_);


            legs_[0] = fixedLeg;
            legs_[1] = principalLeg;
            if (type_ == Type.Loan)
            {
                payer_[0] = +1;
                payer_[1] = -1;
            }
            else
            {
                payer_[0] = -1;
                payer_[1] = +1;
            }
        }
示例#17
0
      public OvernightIndexedSwap(Type type,
                                  double nominal,
                                  Schedule schedule,
                                  double fixedRate,
                                  DayCounter fixedDC,
                                  OvernightIndex overnightIndex,
                                  double spread) : 
      base(2)
      {
      
         type_= type;
         nominal_ = nominal;
         paymentFrequency_ = schedule.tenor().frequency();
         fixedRate_ = fixedRate;
         fixedDC_ = fixedDC;
         overnightIndex_ = overnightIndex;
         spread_ = spread;

         if (fixedDC_== null)
            fixedDC_ = overnightIndex_.dayCounter();

         legs_[0] = new FixedRateLeg(schedule)
            .withCouponRates(fixedRate_, fixedDC_)
            .withNotionals(nominal_);

        legs_[1] = new OvernightLeg(schedule, overnightIndex_)
            .withNotionals(nominal_)
            .withSpreads(spread_);

         for (int j = 0; j < 2; ++j)
         {
            for (int i = 0; i < legs_[j].Count; i++)
               legs_[j][i].registerWith(update);
         }

         switch (type_) 
         {
            case Type.Payer:
               payer_[0] = -1.0;
               payer_[1] = +1.0;
               break;
            case Type.Receiver:
               payer_[0] = +1.0;
               payer_[1] = -1.0;
               break;
            default:
               throw new ApplicationException("Unknown overnight-swap type"); 
         
         }
      }
示例#18
0
        public FixedRateBond(int settlementDays,
                             double faceAmount,
                             Schedule schedule,
                             List <InterestRate> coupons,
                             BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                             double redemption         = 100,
                             Date issueDate            = null,
                             Calendar paymentCalendar  = null,
                             Period exCouponPeriod     = null,
                             Calendar exCouponCalendar = null,
                             BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted,
                             bool exCouponEndOfMonth = false)

            : base(settlementDays, paymentCalendar == null ? schedule.calendar() : paymentCalendar,
                   issueDate)
        {
            frequency_    = schedule.tenor().frequency();
            dayCounter_   = coupons[0].dayCounter();
            maturityDate_ = schedule.endDate();

            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons)
                         .withExCouponPeriod(exCouponPeriod,
                                             exCouponCalendar,
                                             exCouponConvention,
                                             exCouponEndOfMonth)
                         .withPaymentCalendar(calendar_)
                         .withNotionals(faceAmount)
                         .withPaymentAdjustment(paymentConvention);

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });


            if (cashflows().Count == 0)
            {
                throw new ApplicationException("bond with no cashflows!");
            }

            if (redemptions_.Count != 1)
            {
                throw new ApplicationException("multiple redemptions created");
            }
        }
示例#19
0
文件: FixedLoan.cs 项目: vdt/QLNet
        public FixedLoan(Type type, double nominal, Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount, Schedule principalSchedule, BusinessDayConvention? paymentConvention)
            : base(2)
        {
            type_ = type;
            nominal_ = nominal;
            fixedSchedule_ = fixedSchedule;
            fixedRate_ = fixedRate;
            fixedDayCount_ = fixedDayCount;
            principalSchedule_ = principalSchedule;

            if (paymentConvention.HasValue)
                paymentConvention_ = paymentConvention.Value;
            else
                paymentConvention_ = fixedSchedule_.businessDayConvention();

            List<CashFlow> principalLeg = new PricipalLeg(principalSchedule, fixedDayCount)
                .withNotionals(nominal)
                .withPaymentAdjustment(paymentConvention_)
                .withSign(type == Type.Loan ? -1 : 1);

            // temporary
            for (int i = 0; i < principalLeg.Count - 1; i++)
            {
                Principal p = (Principal)principalLeg[i];
                notionals_.Add(p.nominal());
            }

            List<CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                .withCouponRates(fixedRate, fixedDayCount)
                .withPaymentAdjustment(paymentConvention_)
                .withNotionals(notionals_);

            legs_[0] = fixedLeg;
            legs_[1] = principalLeg;
            if (type_ == Type.Loan)
            {
                payer_[0] = +1;
                payer_[1] = -1;
            }
            else
            {
                payer_[0] = -1;
                payer_[1] = +1;
            }
        }
示例#20
0
        public CallableFixedRateBond(int settlementDays,
                                     double faceAmount,
                                     Schedule schedule,
                                     List <double> coupons,
                                     DayCounter accrualDayCounter,
                                     BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                                     double redemption = 100.0,
                                     Date issueDate    = null,
                                     CallabilitySchedule putCallSchedule = null)
            : base(settlementDays, schedule, accrualDayCounter, issueDate, putCallSchedule)
        {
            if (putCallSchedule == null)
            {
                putCallSchedule = new CallabilitySchedule();
            }

            frequency_ = schedule.tenor().frequency();

            bool isZeroCouponBond = (coupons.Count == 1 && Utils.close(coupons[0], 0.0));

            if (!isZeroCouponBond)
            {
                cashflows_ = new FixedRateLeg(schedule)
                             .withCouponRates(coupons, accrualDayCounter)
                             .withNotionals(faceAmount)
                             .withPaymentAdjustment(paymentConvention);

                addRedemptionsToCashflows(new List <double>()
                {
                    redemption
                });
            }
            else
            {
                Date redemptionDate = calendar_.adjust(maturityDate_, paymentConvention);
                setSingleRedemption(faceAmount, redemption, redemptionDate);
            }

            // used for impliedVolatility() calculation
            SimpleQuote dummyVolQuote = new SimpleQuote(0.0);

            blackVolQuote_.linkTo(dummyVolQuote);
            blackEngine_ = new BlackCallableFixedRateBondEngine(blackVolQuote_, blackDiscountCurve_);
        }
      //! simple annual compounding coupon rates      
      public FixedRateBond(int settlementDays, double faceAmount, Schedule schedule,List<double> coupons, 
                           DayCounter accrualDayCounter, BusinessDayConvention paymentConvention,
                           double redemption, Date issueDate,Calendar paymentCalendar)
         : base(settlementDays, paymentCalendar == new Calendar() ? schedule.calendar() : paymentCalendar, 
                issueDate) 
      {
         frequency_ = schedule.tenor().frequency();
         dayCounter_ = accrualDayCounter;
         maturityDate_ = schedule.endDate();

         cashflows_ = new FixedRateLeg(schedule)
            .withCouponRates(coupons, accrualDayCounter)
            .withPaymentCalendar(calendar_)
            .withNotionals(faceAmount)
            .withPaymentAdjustment(paymentConvention);

         addRedemptionsToCashflows(new List<double>() { redemption });

         if (cashflows().Count == 0)
            throw new ApplicationException("bond with no cashflows!");

         if (redemptions_.Count != 1)
            throw new ApplicationException("multiple redemptions created");
      }
示例#22
0
        public CapHelper(Period length,
                         Handle <Quote> volatility,
                         IborIndex index,
                         // data for ATM swap-rate calculation
                         Frequency fixedLegFrequency,
                         DayCounter fixedLegDayCounter,
                         bool includeFirstSwaplet,
                         Handle <YieldTermStructure> termStructure,
                         bool calibrateVolatility /*= false*/)
            : base(volatility, termStructure, calibrateVolatility)
        {
            Period indexTenor = index.tenor();
            double fixedRate = 0.04; // dummy value
            Date   startDate, maturity;

            if (includeFirstSwaplet)
            {
                startDate = termStructure.link.referenceDate();
                maturity  = termStructure.link.referenceDate() + length;
            }
            else
            {
                startDate = termStructure.link.referenceDate() + indexTenor;
                maturity  = termStructure.link.referenceDate() + length;
            }
            IborIndex dummyIndex = new
                                   IborIndex("dummy",
                                             indexTenor,
                                             index.fixingDays(),
                                             index.currency(),
                                             index.fixingCalendar(),
                                             index.businessDayConvention(),
                                             index.endOfMonth(),
                                             termStructure.link.dayCounter(),
                                             termStructure);

            List <double> nominals = new InitializedList <double>(1, 1.0);

            Schedule floatSchedule = new Schedule(startDate, maturity,
                                                  index.tenor(), index.fixingCalendar(),
                                                  index.businessDayConvention(),
                                                  index.businessDayConvention(),
                                                  DateGeneration.Rule.Forward, false);
            List <CashFlow> floatingLeg;
            IborLeg         iborLeg = (IborLeg) new IborLeg(floatSchedule, index)
                                      .withFixingDays(0)
                                      .withNotionals(nominals)
                                      .withPaymentAdjustment(index.businessDayConvention());

            floatingLeg = iborLeg.value();
            Schedule fixedSchedule = new Schedule(startDate, maturity, new Period(fixedLegFrequency),
                                                  index.fixingCalendar(),
                                                  BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                                  DateGeneration.Rule.Forward, false);
            List <CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                                       .withCouponRates(fixedRate, fixedLegDayCounter)
                                       .withNotionals(nominals)
                                       .withPaymentAdjustment(index.businessDayConvention());

            Swap swap = new Swap(floatingLeg, fixedLeg);

            swap.setPricingEngine(new DiscountingSwapEngine(termStructure));
            double        bp           = 1.0e-4;
            double        fairRate     = fixedRate - (double)(swap.NPV() / (swap.legBPS(1) / bp));
            List <double> exerciceRate = new InitializedList <double>(1, fairRate);

            cap_         = new Cap(floatingLeg, exerciceRate);
            marketValue_ = blackPrice(volatility_.link.value());
        }
示例#23
0
        protected override void performCalculations()
        {
            Period indexTenor = index_.tenor();
            double fixedRate = 0.04; // dummy value
            Date startDate, maturity;
            if ( includeFirstSwaplet_ )
            {
                startDate = termStructure_.link.referenceDate();
                maturity = termStructure_.link.referenceDate() + length_;
            }
            else
            {
                startDate = termStructure_.link.referenceDate() + indexTenor;
                maturity = termStructure_.link.referenceDate() + length_;
            }
            IborIndex dummyIndex = new IborIndex( "dummy",
                             indexTenor,
                             index_.fixingDays(),
                             index_.currency(),
                             index_.fixingCalendar(),
                             index_.businessDayConvention(),
                             index_.endOfMonth(),
                             termStructure_.link.dayCounter(),
                             termStructure_ );

            InitializedList<double> nominals = new InitializedList<double>( 1, 1.0 );

            Schedule floatSchedule = new Schedule( startDate, maturity,
                                          index_.tenor(), index_.fixingCalendar(),
                                          index_.businessDayConvention(),
                                          index_.businessDayConvention(),
                                          DateGeneration.Rule.Forward, false );
            List<CashFlow> floatingLeg = new IborLeg( floatSchedule, index_ )
                 .withFixingDays( 0 )
                 .withNotionals( nominals )
                 .withPaymentAdjustment( index_.businessDayConvention() );

            Schedule fixedSchedule = new Schedule( startDate, maturity, new Period( fixedLegFrequency_ ),
                                          index_.fixingCalendar(),
                                          BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                          DateGeneration.Rule.Forward, false );
            List<CashFlow> fixedLeg = new FixedRateLeg( fixedSchedule )
                 .withCouponRates( fixedRate, fixedLegDayCounter_ )
                 .withNotionals( nominals )
                 .withPaymentAdjustment( index_.businessDayConvention() );

            Swap swap = new Swap( floatingLeg, fixedLeg );
            swap.setPricingEngine( new DiscountingSwapEngine( termStructure_, false ) );
            double fairRate = fixedRate - (double)(swap.NPV() / ( swap.legBPS( 1 ) / 1.0e-4 ));
            cap_ = new Cap( floatingLeg, new InitializedList<double>( 1, fairRate ) );

            base.performCalculations();
        }
        /*! simple annual compounding coupon rates
         *  with internal schedule calculation */
        public FixedRateBond(int settlementDays,
                             Calendar calendar,
                             double faceAmount,
                             Date startDate,
                             Date maturityDate,
                             Period tenor,
                             List <double> coupons,
                             DayCounter accrualDayCounter,
                             BusinessDayConvention accrualConvention,
                             BusinessDayConvention paymentConvention,
                             double redemption,
                             Date issueDate,
                             Date stubDate,
                             DateGeneration.Rule rule,
                             bool endOfMonth,
                             Calendar paymentCalendar)
            : base(settlementDays, paymentCalendar == new Calendar() ? calendar : paymentCalendar,
                   issueDate)
        {
            frequency_    = tenor.frequency();
            dayCounter_   = accrualDayCounter;
            maturityDate_ = maturityDate;

            Date firstDate, nextToLastDate;

            switch (rule)
            {
            case DateGeneration.Rule.Backward:
                firstDate      = null;
                nextToLastDate = stubDate;
                break;

            case DateGeneration.Rule.Forward:
                firstDate      = stubDate;
                nextToLastDate = null;
                break;

            case DateGeneration.Rule.Zero:
            case DateGeneration.Rule.ThirdWednesday:
            case DateGeneration.Rule.Twentieth:
            case DateGeneration.Rule.TwentiethIMM:
                throw new ApplicationException("stub date (" + stubDate + ") not allowed with " + rule + " DateGeneration::Rule");

            default:
                throw new ApplicationException("unknown DateGeneration::Rule (" + rule + ")");
            }


            Schedule schedule = new Schedule(startDate, maturityDate_, tenor,
                                             calendar, accrualConvention, accrualConvention,
                                             rule, endOfMonth,
                                             firstDate, nextToLastDate);


            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, accrualDayCounter)
                         .withPaymentCalendar(calendar_)
                         .withNotionals(faceAmount)
                         .withPaymentAdjustment(paymentConvention);

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });


            if (cashflows().Count == 0)
            {
                throw new ApplicationException("bond with no cashflows!");
            }

            if (redemptions_.Count != 1)
            {
                throw new ApplicationException("multiple redemptions created");
            }
        }
示例#25
0
        /*! simple annual compounding coupon rates
         *  with internal schedule calculation */
        public FixedRateBond(int settlementDays,
                             Calendar calendar,
                             double faceAmount,
                             Date startDate,
                             Date maturityDate,
                             Period tenor,
                             List <double> coupons,
                             DayCounter accrualDayCounter,
                             BusinessDayConvention accrualConvention = BusinessDayConvention.Following,
                             BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                             double redemption         = 100,
                             Date issueDate            = null,
                             Date stubDate             = null,
                             DateGeneration.Rule rule  = DateGeneration.Rule.Backward,
                             bool endOfMonth           = false,
                             Calendar paymentCalendar  = null,
                             Period exCouponPeriod     = null,
                             Calendar exCouponCalendar = null,
                             BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted,
                             bool exCouponEndOfMonth = false)
            : base(settlementDays, paymentCalendar ?? calendar,
                   issueDate)
        {
            frequency_    = tenor.frequency();
            dayCounter_   = accrualDayCounter;
            maturityDate_ = maturityDate;

            Date firstDate = null, nextToLastDate = null;

            switch (rule)
            {
            case DateGeneration.Rule.Backward:
                firstDate      = null;
                nextToLastDate = stubDate;
                break;

            case DateGeneration.Rule.Forward:
                firstDate      = stubDate;
                nextToLastDate = null;
                break;

            case DateGeneration.Rule.Zero:
            case DateGeneration.Rule.ThirdWednesday:
            case DateGeneration.Rule.Twentieth:
            case DateGeneration.Rule.TwentiethIMM:
                Utils.QL_FAIL("stub date (" + stubDate + ") not allowed with " + rule + " DateGeneration::Rule");
                break;

            default:
                Utils.QL_FAIL("unknown DateGeneration::Rule (" + rule + ")");
                break;
            }


            Schedule schedule = new Schedule(startDate, maturityDate_, tenor,
                                             calendar, accrualConvention, accrualConvention,
                                             rule, endOfMonth,
                                             firstDate, nextToLastDate);


            cashflows_ = new FixedRateLeg(schedule)
                         .withCouponRates(coupons, accrualDayCounter)
                         .withExCouponPeriod(exCouponPeriod,
                                             exCouponCalendar,
                                             exCouponConvention,
                                             exCouponEndOfMonth)
                         .withPaymentCalendar(calendar_)
                         .withNotionals(faceAmount)
                         .withPaymentAdjustment(paymentConvention);

            addRedemptionsToCashflows(new List <double>()
            {
                redemption
            });


            Utils.QL_REQUIRE(cashflows().Count != 0, () => "bond with no cashflows!");
            Utils.QL_REQUIRE(redemptions_.Count == 1, () => "multiple redemptions created");
        }
示例#26
0
      public ConvertibleFixedCouponBond( Exercise exercise,
                                         double conversionRatio,
                                         DividendSchedule dividends,
                                         CallabilitySchedule callability,
                                         Handle<Quote> creditSpread,
                                         Date issueDate,
                                         int settlementDays,
                                         List<double> coupons,
                                         DayCounter dayCounter,
                                         Schedule schedule,
                                         double redemption = 100)
         : base(exercise, conversionRatio, dividends, callability, creditSpread, issueDate, settlementDays, schedule, redemption) 
      {

        // !!! notional forcibly set to 100
        cashflows_ = new FixedRateLeg(schedule)
                           .withCouponRates(coupons, dayCounter)
                           .withNotionals(100.0)
                           .withPaymentAdjustment(schedule.businessDayConvention());

        addRedemptionsToCashflows(new List<double>(){redemption});

        Utils.QL_REQUIRE( redemptions_.Count == 1, () => "multiple redemptions created" );

        option_ = new option(this, exercise, conversionRatio, dividends, callability, creditSpread, cashflows_, dayCounter, schedule,
                             issueDate, settlementDays, redemption);
    }
示例#27
0
      private double calculateYield()
      {
         // We create a bond cashflow from issue to maturity just
         // to calculate effective rate ( the rate that discount _marketValue )
         Schedule schedule = new Schedule(_issueDate, _maturityDate, new Period(_payFrequency),
                                           _calendar, BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                           DateGeneration.Rule.Backward, false);


         List<CashFlow> cashflows = new FixedRateLeg(schedule)
            .withCouponRates(_couponRate, _dCounter)
            .withPaymentCalendar(_calendar)
            .withNotionals(_faceValue)
            .withPaymentAdjustment(BusinessDayConvention.Unadjusted);

         // Add single redemption for yield calculation
         Redemption r = new Redemption(_faceValue , _maturityDate);
         cashflows.Add( r );

         // Calculate Amortizing Yield ( Effective Rate )
         Date testDate = CashFlows.previousCashFlowDate(cashflows, false, _tradeDate);
         return CashFlows.yield(cashflows, _marketValue, _dCounter, Compounding.Simple, _payFrequency,
                                  false, testDate);
      }
示例#28
0
        /// <summary>
        /// CDS quoted as upfront and running spread
        /// </summary>
        /// <param name="side">Whether the protection is bought or sold.</param>
        /// <param name="notional"> Notional value</param>
        /// <param name="upfront">Upfront in fractional units.</param>
        /// <param name="runningSpread">Running spread in fractional units.</param>
        /// <param name="schedule">Coupon schedule.</param>
        /// <param name="convention">Business-day convention for payment-date adjustment.</param>
        /// <param name="dayCounter">Day-count convention for accrual.</param>
        /// <param name="settlesAccrual">Whether or not the accrued coupon is due in the event of a default.</param>
        /// <param name="paysAtDefaultTime">If set to true, any payments triggered by a default event are
        /// due at default time. If set to false, they are due at the end of the accrual period.</param>
        /// <param name="protectionStart">The first date where a default event will trigger the contract.</param>
        /// <param name="upfrontDate">Settlement date for the upfront payment.</param>
        /// <param name="claim"></param>
        /// <param name="lastPeriodDayCounter">Day-count convention for accrual in last period</param>
        /// <param name="rebatesAccrual">The protection seller pays the accrued scheduled current coupon at the start
        /// of the contract. The rebate date is not provided but computed to be two days after protection start.</param>
        public CreditDefaultSwap(Protection.Side side,
                                 double notional,
                                 double upfront,
                                 double runningSpread,
                                 Schedule schedule,
                                 BusinessDayConvention convention,
                                 DayCounter dayCounter,
                                 bool settlesAccrual             = true,
                                 bool paysAtDefaultTime          = true,
                                 Date protectionStart            = null,
                                 Date upfrontDate                = null,
                                 Claim claim                     = null,
                                 DayCounter lastPeriodDayCounter = null,
                                 bool rebatesAccrual             = true)
        {
            side_              = side;
            notional_          = notional;
            upfront_           = upfront;
            runningSpread_     = runningSpread;
            settlesAccrual_    = settlesAccrual;
            paysAtDefaultTime_ = paysAtDefaultTime;
            claim_             = claim;
            protectionStart_   = protectionStart ?? schedule[0];

            Utils.QL_REQUIRE(protectionStart_ <= schedule[0] ||
                             schedule.rule() == DateGeneration.Rule.CDS
                             , () => "protection can not start after accrual");
            leg_ = new FixedRateLeg(schedule)
                   .withLastPeriodDayCounter(lastPeriodDayCounter)
                   .withCouponRates(runningSpread, dayCounter)
                   .withNotionals(notional)
                   .withPaymentAdjustment(convention);

            // If empty, adjust to T+3 standard settlement, alternatively add
            //  an arbitrary date to the constructor
            Date effectiveUpfrontDate = upfrontDate == null?
                                        schedule.calendar().advance(protectionStart_, 2, TimeUnit.Days, convention) : upfrontDate;

            // '2' is used above since the protection start is assumed to be
            //   on trade_date + 1
            upfrontPayment_ = new SimpleCashFlow(notional * upfront, effectiveUpfrontDate);
            Utils.QL_REQUIRE(effectiveUpfrontDate >= protectionStart_, () => "upfront can not be due before contract start");

            if (rebatesAccrual)
            {
                FixedRateCoupon firstCoupon = leg_[0] as FixedRateCoupon;
                // adjust to T+3 standard settlement, alternatively add
                //  an arbitrary date to the constructor

                Date rebateDate = effectiveUpfrontDate;

                accrualRebate_ = new SimpleCashFlow(firstCoupon.accruedAmount(protectionStart_), rebateDate);
            }

            if (claim_ == null)
            {
                claim_ = new FaceValueClaim();
            }
            claim_.registerWith(update);

            maturity_ = schedule.dates().Last();
        }
示例#29
0
        public void testGenericBondImplied()
        {
            // Testing implied generic-bond value against asset-swap fair price with null spread...

             CommonVars vars = new CommonVars();

             Calendar bondCalendar = new TARGET();
             int settlementDays = 3;
             int fixingDays = 2;
             bool payFixeddouble = true;
             bool parAssetSwap = true;
             bool inArrears = false;

             // Fixed Underlying bond (Isin: DE0001135275 DBR 4 01/04/37)
             // maturity doesn't occur on a business day
             Date fixedBondStartDate1 =new Date(4,Month.January,2005);
             Date fixedBondMaturityDate1 =new Date(4,Month.January,2037);
             Schedule fixedBondSchedule1 = new Schedule(fixedBondStartDate1,
                                    fixedBondMaturityDate1,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg1 = new FixedRateLeg(fixedBondSchedule1)
            .withCouponRates(0.04, new ActualActual(ActualActual.Convention.ISDA))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption1 = bondCalendar.adjust(fixedBondMaturityDate1,
                                                         BusinessDayConvention.Following);
             fixedBondLeg1.Add((new SimpleCashFlow(100.0, fixedbondRedemption1)));
             Bond fixedBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  fixedBondMaturityDate1, fixedBondStartDate1, fixedBondLeg1);
             IPricingEngine bondEngine = new DiscountingBondEngine(vars.termStructure);
             IPricingEngine swapEngine= new DiscountingSwapEngine(vars.termStructure);
             fixedBond1.setPricingEngine(bondEngine);

             double fixedBondPrice1 = fixedBond1.cleanPrice();
             AssetSwap fixedBondAssetSwap1 = new AssetSwap(payFixeddouble,
                                       fixedBond1, fixedBondPrice1,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             fixedBondAssetSwap1.setPricingEngine(swapEngine);
             double fixedBondAssetSwapPrice1 = fixedBondAssetSwap1.fairCleanPrice();
             double tolerance = 1.0e-13;
             double error1 = Math.Abs(fixedBondAssetSwapPrice1-fixedBondPrice1);

             if (error1>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for fixed bond:"
                        + "\n  bond's clean price:    " + fixedBondPrice1
                        + "\n  asset swap fair price: " + fixedBondAssetSwapPrice1
                        + "\n  error:                 " + error1
                        + "\n  tolerance:             " + tolerance);
             }

             // Fixed Underlying bond (Isin: IT0006527060 IBRD 5 02/05/19)
             // maturity occurs on a business day
             Date fixedBondStartDate2 =new Date(5,Month.February,2005);
             Date fixedBondMaturityDate2 =new Date(5,Month.February,2019);
             Schedule fixedBondSchedule2= new Schedule(fixedBondStartDate2,
                                    fixedBondMaturityDate2,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg2 = new FixedRateLeg(fixedBondSchedule2)
            .withCouponRates(0.05, new Thirty360(Thirty360.Thirty360Convention.BondBasis))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption2 = bondCalendar.adjust(fixedBondMaturityDate2,BusinessDayConvention.Following);
             fixedBondLeg2.Add(new SimpleCashFlow(100.0, fixedbondRedemption2));
             Bond fixedBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  fixedBondMaturityDate2, fixedBondStartDate2, fixedBondLeg2);
             fixedBond2.setPricingEngine(bondEngine);

             double fixedBondPrice2 = fixedBond2.cleanPrice();
             AssetSwap fixedBondAssetSwap2= new AssetSwap(payFixeddouble,
                                       fixedBond2, fixedBondPrice2,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             fixedBondAssetSwap2.setPricingEngine(swapEngine);
             double fixedBondAssetSwapPrice2 = fixedBondAssetSwap2.fairCleanPrice();
             double error2 = Math.Abs(fixedBondAssetSwapPrice2-fixedBondPrice2);

             if (error2>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for fixed bond:"
                        + "\n  bond's clean price:    " + fixedBondPrice2
                        + "\n  asset swap fair price: " + fixedBondAssetSwapPrice2
                        + "\n  error:                 " + error2
                        + "\n  tolerance:             " + tolerance);
             }

             // FRN Underlying bond (Isin: IT0003543847 ISPIM 0 09/29/13)
             // maturity doesn't occur on a business day
             Date floatingBondStartDate1 =new Date(29,Month.September,2003);
             Date floatingBondMaturityDate1 =new Date(29,Month.September,2013);
             Schedule floatingBondSchedule1 = new Schedule(floatingBondStartDate1,
                                       floatingBondMaturityDate1,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg1 = new IborLeg(floatingBondSchedule1, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0056)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption1 =
            bondCalendar.adjust(floatingBondMaturityDate1, BusinessDayConvention.Following);
             floatingBondLeg1.Add(new SimpleCashFlow(100.0, floatingbondRedemption1));
             Bond floatingBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  floatingBondMaturityDate1, floatingBondStartDate1, floatingBondLeg1);
             floatingBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond1.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(27,Month.March,2007), 0.0402);
             double floatingBondPrice1 = floatingBond1.cleanPrice();
             AssetSwap floatingBondAssetSwap1= new AssetSwap(payFixeddouble,
                                          floatingBond1, floatingBondPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             floatingBondAssetSwap1.setPricingEngine(swapEngine);
             double floatingBondAssetSwapPrice1 = floatingBondAssetSwap1.fairCleanPrice();
             double error3 = Math.Abs(floatingBondAssetSwapPrice1-floatingBondPrice1);

             if (error3>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for floater:"
                        + "\n  bond's clean price:    " + floatingBondPrice1
                        + "\n  asset swap fair price: " +
                        floatingBondAssetSwapPrice1
                        + "\n  error:                 " + error3
                        + "\n  tolerance:             " + tolerance);
             }

             // FRN Underlying bond (Isin: XS0090566539 COE 0 09/24/18)
             // maturity occurs on a business day
             Date floatingBondStartDate2 =new Date(24,Month.September,2004);
             Date floatingBondMaturityDate2 =new Date(24,Month.September,2018);
             Schedule floatingBondSchedule2 = new Schedule(floatingBondStartDate2,
                                       floatingBondMaturityDate2,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg2 = new IborLeg(floatingBondSchedule2, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0025)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount)
            .withPaymentAdjustment(BusinessDayConvention.ModifiedFollowing);
             Date floatingbondRedemption2 =
            bondCalendar.adjust(floatingBondMaturityDate2, BusinessDayConvention.ModifiedFollowing);
             floatingBondLeg2.Add(new SimpleCashFlow(100.0, floatingbondRedemption2));
             Bond floatingBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  floatingBondMaturityDate2, floatingBondStartDate2, floatingBondLeg2);
             floatingBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond2.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(22,Month.March,2007), 0.04013);
             double currentCoupon=0.04013+0.0025;
             double floatingCurrentCoupon= floatingBond2.nextCouponRate();
             double error4= Math.Abs(floatingCurrentCoupon-currentCoupon);
             if (error4>tolerance) {
            Assert.Fail("wrong current coupon is returned for floater bond:"
                        + "\n  bond's calculated current coupon:      " +
                        currentCoupon
                        + "\n  current coupon asked to the bond: " +
                        floatingCurrentCoupon
                        + "\n  error:                 " + error4
                        + "\n  tolerance:             " + tolerance);
             }

             double floatingBondPrice2 = floatingBond2.cleanPrice();
             AssetSwap floatingBondAssetSwap2= new AssetSwap(payFixeddouble,
                                          floatingBond2, floatingBondPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             floatingBondAssetSwap2.setPricingEngine(swapEngine);
             double floatingBondAssetSwapPrice2 = floatingBondAssetSwap2.fairCleanPrice();
             double error5 = Math.Abs(floatingBondAssetSwapPrice2-floatingBondPrice2);

             if (error5>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for floater:"
                        + "\n  bond's clean price:    " + floatingBondPrice2
                        + "\n  asset swap fair price: " +
                        floatingBondAssetSwapPrice2
                        + "\n  error:                 " + error5
                        + "\n  tolerance:             " + tolerance);
             }

             // CMS Underlying bond (Isin: XS0228052402 CRDIT 0 8/22/20)
             // maturity doesn't occur on a business day
             Date cmsBondStartDate1 =new Date(22,Month.August,2005);
             Date cmsBondMaturityDate1 =new Date(22,Month.August,2020);
             Schedule cmsBondSchedule1= new Schedule(cmsBondStartDate1,
                                 cmsBondMaturityDate1,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg1 = new CmsLeg(cmsBondSchedule1, vars.swapIndex)
            .withFixingDays(fixingDays)
            .withPaymentDayCounter(new Thirty360())
            .withCaps(0.055)
            .withFloors(0.025)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption1 = bondCalendar.adjust(cmsBondMaturityDate1, BusinessDayConvention.Following);
             cmsBondLeg1.Add( new SimpleCashFlow(100.0, cmsbondRedemption1));
             Bond cmsBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  cmsBondMaturityDate1, cmsBondStartDate1, cmsBondLeg1);
             cmsBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond1.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(18,Month.August,2006), 0.04158);
             double cmsBondPrice1 = cmsBond1.cleanPrice();
             AssetSwap cmsBondAssetSwap1 = new AssetSwap(payFixeddouble,
                                    cmsBond1, cmsBondPrice1,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             cmsBondAssetSwap1.setPricingEngine(swapEngine);
             double cmsBondAssetSwapPrice1 = cmsBondAssetSwap1.fairCleanPrice();
             double error6 = Math.Abs(cmsBondAssetSwapPrice1-cmsBondPrice1);

             if (error6>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for cms bond:"
                        + "\n  bond's clean price:    " + cmsBondPrice1
                        + "\n  asset swap fair price: " + cmsBondAssetSwapPrice1
                        + "\n  error:                 " + error6
                        + "\n  tolerance:             " + tolerance);
             }

             // CMS Underlying bond (Isin: XS0218766664 ISPIM 0 5/6/15)
             // maturity occurs on a business day
             Date cmsBondStartDate2 =new Date(06,Month.May,2005);
             Date cmsBondMaturityDate2 =new Date(06,Month.May,2015);
             Schedule cmsBondSchedule2= new Schedule(cmsBondStartDate2,
                                 cmsBondMaturityDate2,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg2 = new CmsLeg(cmsBondSchedule2, vars.swapIndex)
            .withFixingDays(fixingDays)
            .withGearings(0.84)
            .inArrears(inArrears)
            .withPaymentDayCounter(new Thirty360())
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption2 = bondCalendar.adjust(cmsBondMaturityDate2,BusinessDayConvention.Following);
             cmsBondLeg2.Add(new SimpleCashFlow(100.0, cmsbondRedemption2));
             Bond cmsBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  cmsBondMaturityDate2, cmsBondStartDate2, cmsBondLeg2);
             cmsBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond2.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(04,Month.May,2006), 0.04217);
             double cmsBondPrice2 = cmsBond2.cleanPrice();
             AssetSwap cmsBondAssetSwap2= new AssetSwap(payFixeddouble,
                                    cmsBond2, cmsBondPrice2,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             cmsBondAssetSwap2.setPricingEngine(swapEngine);
             double cmsBondAssetSwapPrice2 = cmsBondAssetSwap2.fairCleanPrice();
             double error7 = Math.Abs(cmsBondAssetSwapPrice2-cmsBondPrice2);

             if (error7>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for cms bond:"
                        + "\n  bond's clean price:    " + cmsBondPrice2
                        + "\n  asset swap fair price: " + cmsBondAssetSwapPrice2
                        + "\n  error:                 " + error7
                        + "\n  tolerance:             " + tolerance);
             }

             // Zero Coupon bond (Isin: DE0004771662 IBRD 0 12/20/15)
             // maturity doesn't occur on a business day
             Date zeroCpnBondStartDate1 =new Date(19,Month.December,1985);
             Date zeroCpnBondMaturityDate1 =new Date(20,Month.December,2015);
             Date zeroCpnBondRedemption1 = bondCalendar.adjust(zeroCpnBondMaturityDate1,BusinessDayConvention.Following);
             List<CashFlow>zeroCpnBondLeg1 = new List<CashFlow>{new SimpleCashFlow(100.0, zeroCpnBondRedemption1)};
             Bond zeroCpnBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate1, zeroCpnBondStartDate1, zeroCpnBondLeg1);
             zeroCpnBond1.setPricingEngine(bondEngine);

             double zeroCpnBondPrice1 = zeroCpnBond1.cleanPrice();
             AssetSwap zeroCpnAssetSwap1 = new AssetSwap(payFixeddouble,
                                    zeroCpnBond1, zeroCpnBondPrice1,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             zeroCpnAssetSwap1.setPricingEngine(swapEngine);
             double zeroCpnBondAssetSwapPrice1 = zeroCpnAssetSwap1.fairCleanPrice();
             double error8 = Math.Abs(zeroCpnBondAssetSwapPrice1-zeroCpnBondPrice1);

             if (error8>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for zero cpn bond:"
                        + "\n  bond's clean price:    " + zeroCpnBondPrice1
                        + "\n  asset swap fair price: " + zeroCpnBondAssetSwapPrice1
                        + "\n  error:                 " + error8
                        + "\n  tolerance:             " + tolerance);
             }

             // Zero Coupon bond (Isin: IT0001200390 ISPIM 0 02/17/28)
             // maturity occurs on a business day
             Date zeroCpnBondStartDate2 =new Date(17,Month.February,1998);
             Date zeroCpnBondMaturityDate2 =new Date(17,Month.February,2028);
             Date zerocpbondRedemption2 = bondCalendar.adjust(zeroCpnBondMaturityDate2,BusinessDayConvention.Following);
             List<CashFlow>zeroCpnBondLeg2 = new List<CashFlow>{new SimpleCashFlow(100.0, zerocpbondRedemption2)};
             Bond zeroCpnBond2 = new  Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate2, zeroCpnBondStartDate2, zeroCpnBondLeg2);
             zeroCpnBond2.setPricingEngine(bondEngine);

             double zeroCpnBondPrice2 = zeroCpnBond2.cleanPrice();
             AssetSwap zeroCpnAssetSwap2= new AssetSwap(payFixeddouble,
                                    zeroCpnBond2, zeroCpnBondPrice2,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             zeroCpnAssetSwap2.setPricingEngine(swapEngine);
             double zeroCpnBondAssetSwapPrice2 = zeroCpnAssetSwap2.fairCleanPrice();
             double error9 = Math.Abs(cmsBondAssetSwapPrice2-cmsBondPrice2);

             if (error9>tolerance) {
            Assert.Fail("wrong zero spread asset swap price for zero cpn bond:"
                        + "\n  bond's clean price:    " + zeroCpnBondPrice2
                        + "\n  asset swap fair price: " + zeroCpnBondAssetSwapPrice2
                        + "\n  error:                 " + error9
                        + "\n  tolerance:             " + tolerance);
             }
        }
示例#30
0
        public void testZSpreadWithGenericBond()
        {
            // Testing clean and dirty price with null Z-spread against theoretical prices...

             CommonVars vars = new CommonVars();

             Calendar bondCalendar = new TARGET();
             int settlementDays = 3;
             int fixingDays = 2;
             bool inArrears = false;

             // Fixed Underlying bond (Isin: DE0001135275 DBR 4 01/04/37)
             // maturity doesn't occur on a business day

             Date fixedBondStartDate1 = new Date(4,Month.January,2005);
             Date fixedBondMaturityDate1 = new Date(4,Month.January,2037);
             Schedule fixedBondSchedule1= new Schedule(fixedBondStartDate1,
                                    fixedBondMaturityDate1,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg1 = new FixedRateLeg(fixedBondSchedule1)
            .withCouponRates(0.04, new ActualActual(ActualActual.Convention.ISDA))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption1 = bondCalendar.adjust(fixedBondMaturityDate1,
                                                         BusinessDayConvention.Following);
             fixedBondLeg1.Add(new SimpleCashFlow(100.0, fixedbondRedemption1));
             Bond fixedBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount, fixedBondMaturityDate1, fixedBondStartDate1,
                  fixedBondLeg1);
             IPricingEngine bondEngine = new DiscountingBondEngine(vars.termStructure);
             fixedBond1.setPricingEngine(bondEngine);

             double fixedBondImpliedValue1 = fixedBond1.cleanPrice();
             Date fixedBondSettlementDate1= fixedBond1.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double fixedBondCleanPrice1 = BondFunctions.cleanPrice(fixedBond1, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Annual, fixedBondSettlementDate1);
             double tolerance = 1.0e-13;
             double error1 = Math.Abs(fixedBondImpliedValue1-fixedBondCleanPrice1);
             if (error1>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: "
                        + fixedBondImpliedValue1
                        + "\n  par asset swap spread: " + fixedBondCleanPrice1
                        + "\n  error:                 " + error1
                        + "\n  tolerance:             " + tolerance);
             }

             // Fixed Underlying bond (Isin: IT0006527060 IBRD 5 02/05/19)
             // maturity occurs on a business day

             Date fixedBondStartDate2 = new Date(5,Month.February,2005);
             Date fixedBondMaturityDate2 = new Date(5,Month.February,2019);
             Schedule fixedBondSchedule2= new Schedule(fixedBondStartDate2,
                                    fixedBondMaturityDate2,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg2 = new FixedRateLeg(fixedBondSchedule2)
            .withCouponRates(0.05, new Thirty360(Thirty360.Thirty360Convention.BondBasis))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption2 = bondCalendar.adjust(fixedBondMaturityDate2, BusinessDayConvention.Following);
             fixedBondLeg2.Add(new SimpleCashFlow(100.0, fixedbondRedemption2));
             Bond fixedBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  fixedBondMaturityDate2, fixedBondStartDate2, fixedBondLeg2);
             fixedBond2.setPricingEngine(bondEngine);

             double fixedBondImpliedValue2 = fixedBond2.cleanPrice();
             Date fixedBondSettlementDate2= fixedBond2.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve

             double fixedBondCleanPrice2 = BondFunctions.cleanPrice(fixedBond2, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Annual, fixedBondSettlementDate2);
             double error3 = Math.Abs(fixedBondImpliedValue2-fixedBondCleanPrice2);
             if (error3>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: "
                        + fixedBondImpliedValue2
                        + "\n  par asset swap spread: " + fixedBondCleanPrice2
                        + "\n  error:                 " + error3
                        + "\n  tolerance:             " + tolerance);
             }

             // FRN Underlying bond (Isin: IT0003543847 ISPIM 0 09/29/13)
             // maturity doesn't occur on a business day

             Date floatingBondStartDate1 = new Date(29,Month.September,2003);
             Date floatingBondMaturityDate1 = new Date(29,Month.September,2013);
             Schedule floatingBondSchedule1= new Schedule(floatingBondStartDate1,
                                       floatingBondMaturityDate1,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg1 = new IborLeg(floatingBondSchedule1, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0056)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption1 =
            bondCalendar.adjust(floatingBondMaturityDate1, BusinessDayConvention.Following);
             floatingBondLeg1.Add(new SimpleCashFlow(100.0, floatingbondRedemption1));
             Bond floatingBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  floatingBondMaturityDate1, floatingBondStartDate1,
                  floatingBondLeg1);
             floatingBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond1.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(27,Month.March,2007), 0.0402);
             double floatingBondImpliedValue1 = floatingBond1.cleanPrice();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double floatingBondCleanPrice1 = BondFunctions.cleanPrice(floatingBond1, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Semiannual, fixedBondSettlementDate1);
             double error5 = Math.Abs(floatingBondImpliedValue1-floatingBondCleanPrice1);
             if (error5>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: " +
                        floatingBondImpliedValue1
                        + "\n  par asset swap spread: " + floatingBondCleanPrice1
                        + "\n  error:                 " + error5
                        + "\n  tolerance:             " + tolerance);
             }

             // FRN Underlying bond (Isin: XS0090566539 COE 0 09/24/18)
             // maturity occurs on a business day

             Date floatingBondStartDate2 = new Date(24,Month.September,2004);
             Date floatingBondMaturityDate2 = new Date(24,Month.September,2018);
             Schedule floatingBondSchedule2 = new Schedule(floatingBondStartDate2,
                                       floatingBondMaturityDate2,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg2 = new IborLeg(floatingBondSchedule2, vars.iborIndex)
            .withFixingDays(fixingDays)
            .withSpreads(0.0025)
            .withPaymentDayCounter(new Actual360())
            .inArrears(inArrears)
            .withPaymentAdjustment(BusinessDayConvention.ModifiedFollowing)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption2 = bondCalendar.adjust(floatingBondMaturityDate2, BusinessDayConvention.ModifiedFollowing);
             floatingBondLeg2.Add(new SimpleCashFlow(100.0, floatingbondRedemption2));
             Bond floatingBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount, floatingBondMaturityDate2,
            floatingBondStartDate2, floatingBondLeg2);
             floatingBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond2.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(22,Month.March,2007), 0.04013);
             double floatingBondImpliedValue2 = floatingBond2.cleanPrice();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double floatingBondCleanPrice2 = BondFunctions.cleanPrice(floatingBond2, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Semiannual,  fixedBondSettlementDate1);
             double error7 = Math.Abs(floatingBondImpliedValue2-floatingBondCleanPrice2);
             if (error7>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: " +
                        floatingBondImpliedValue2
                        + "\n  par asset swap spread: " + floatingBondCleanPrice2
                        + "\n  error:                 " + error7
                        + "\n  tolerance:             " + tolerance);
             }

             // CMS Underlying bond (Isin: XS0228052402 CRDIT 0 8/22/20)
             // maturity doesn't occur on a business day

             Date cmsBondStartDate1 = new Date(22,Month.August,2005);
             Date cmsBondMaturityDate1 = new Date(22,Month.August,2020);
             Schedule cmsBondSchedule1= new Schedule(cmsBondStartDate1,
                                 cmsBondMaturityDate1,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg1 = new CmsLeg(cmsBondSchedule1, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withCaps(0.055)
            .withFloors(0.025)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption1 = bondCalendar.adjust(cmsBondMaturityDate1, BusinessDayConvention.Following);
             cmsBondLeg1.Add(new SimpleCashFlow(100.0, cmsbondRedemption1));
             Bond cmsBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount, cmsBondMaturityDate1, cmsBondStartDate1,
            cmsBondLeg1);
             cmsBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond1.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(18,Month.August,2006), 0.04158);
             double cmsBondImpliedValue1 = cmsBond1.cleanPrice();
             Date cmsBondSettlementDate1= cmsBond1.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double cmsBondCleanPrice1 = BondFunctions.cleanPrice(cmsBond1, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Annual,
            cmsBondSettlementDate1);
             double error9 = Math.Abs(cmsBondImpliedValue1-cmsBondCleanPrice1);
             if (error9>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: " + cmsBondImpliedValue1
                        + "\n  par asset swap spread: " + cmsBondCleanPrice1
                        + "\n  error:                 " + error9
                        + "\n  tolerance:             " + tolerance);
             }

             // CMS Underlying bond (Isin: XS0218766664 ISPIM 0 5/6/15)
             // maturity occurs on a business day

             Date cmsBondStartDate2 = new Date(06,Month.May,2005);
             Date cmsBondMaturityDate2 = new Date(06,Month.May,2015);
             Schedule cmsBondSchedule2= new Schedule(cmsBondStartDate2,
                                 cmsBondMaturityDate2,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg2 = new CmsLeg(cmsBondSchedule2, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withGearings(0.84)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption2 = bondCalendar.adjust(cmsBondMaturityDate2,  BusinessDayConvention.Following);
             cmsBondLeg2.Add(new SimpleCashFlow(100.0, cmsbondRedemption2));
             Bond cmsBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  cmsBondMaturityDate2, cmsBondStartDate2, cmsBondLeg2);
             cmsBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond2.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(04,Month.May,2006), 0.04217);
             double cmsBondImpliedValue2 = cmsBond2.cleanPrice();
             Date cmsBondSettlementDate2= cmsBond2.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double cmsBondCleanPrice2 = BondFunctions.cleanPrice(cmsBond2, vars.termStructure, vars.spread,
            new Actual365Fixed(), vars.compounding, Frequency.Annual,
            cmsBondSettlementDate2);
             double error11 = Math.Abs(cmsBondImpliedValue2-cmsBondCleanPrice2);
             if (error11>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  market asset swap spread: " + cmsBondImpliedValue2
                        + "\n  par asset swap spread: " + cmsBondCleanPrice2
                        + "\n  error:                 " + error11
                        + "\n  tolerance:             " + tolerance);
             }

             // Zero Coupon bond (Isin: DE0004771662 IBRD 0 12/20/15)
             // maturity doesn't occur on a business day

             Date zeroCpnBondStartDate1 = new Date(19,Month.December,1985);
             Date zeroCpnBondMaturityDate1 = new Date(20,Month.December,2015);
             Date zeroCpnBondRedemption1 = bondCalendar.adjust(zeroCpnBondMaturityDate1,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg1 = new List<CashFlow>{new SimpleCashFlow(100.0, zeroCpnBondRedemption1)};
             Bond zeroCpnBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate1, zeroCpnBondStartDate1, zeroCpnBondLeg1);
             zeroCpnBond1.setPricingEngine(bondEngine);

             double zeroCpnBondImpliedValue1 = zeroCpnBond1.cleanPrice();
             Date zeroCpnBondSettlementDate1= zeroCpnBond1.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double zeroCpnBondCleanPrice1 =
            BondFunctions.cleanPrice(zeroCpnBond1,
                                 vars.termStructure,
                                 vars.spread,
                                 new Actual365Fixed(),
                                 vars.compounding, Frequency.Annual,
                                 zeroCpnBondSettlementDate1);
             double error13 = Math.Abs(zeroCpnBondImpliedValue1-zeroCpnBondCleanPrice1);
             if (error13>tolerance) {
            Assert.Fail("wrong clean price for zero coupon bond:"
                        + "\n  zero cpn implied value: " +
                        zeroCpnBondImpliedValue1
                        + "\n  zero cpn price: " + zeroCpnBondCleanPrice1
                        + "\n  error:                 " + error13
                        + "\n  tolerance:             " + tolerance);
             }

             // Zero Coupon bond (Isin: IT0001200390 ISPIM 0 02/17/28)
             // maturity occurs on a business day

             Date zeroCpnBondStartDate2 = new Date(17,Month.February,1998);
             Date zeroCpnBondMaturityDate2 = new Date(17,Month.February,2028);
             Date zerocpbondRedemption2 = bondCalendar.adjust(zeroCpnBondMaturityDate2,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg2 = new List<CashFlow>{new SimpleCashFlow(100.0, zerocpbondRedemption2)};
             Bond zeroCpnBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate2, zeroCpnBondStartDate2, zeroCpnBondLeg2);
             zeroCpnBond2.setPricingEngine(bondEngine);

             double zeroCpnBondImpliedValue2 = zeroCpnBond2.cleanPrice();
             Date zeroCpnBondSettlementDate2= zeroCpnBond2.settlementDate();
             // standard market conventions:
             // bond's frequency + coumpounding and daycounter of the YieldCurve
             double zeroCpnBondCleanPrice2 =
            BondFunctions.cleanPrice(zeroCpnBond2,
                                 vars.termStructure,
                                 vars.spread,
                                 new Actual365Fixed(),
                                 vars.compounding, Frequency.Annual,
                                 zeroCpnBondSettlementDate2);
             double error15 = Math.Abs(zeroCpnBondImpliedValue2-zeroCpnBondCleanPrice2);
             if (error15>tolerance) {
            Assert.Fail("wrong clean price for zero coupon bond:"
                        + "\n  zero cpn implied value: " +
                        zeroCpnBondImpliedValue2
                        + "\n  zero cpn price: " + zeroCpnBondCleanPrice2
                        + "\n  error:                 " + error15
                        + "\n  tolerance:             " + tolerance);
             }
        }
示例#31
0
        public void testSpecializedBondVsGenericBondUsingAsw()
        {
            // Testing asset-swap prices and spreads for specialized bond against equivalent generic bond...
             CommonVars vars = new CommonVars();

             Calendar bondCalendar = new TARGET();
             int settlementDays = 3;
             int fixingDays = 2;
             bool payFixedRate = true;
             bool parAssetSwap = true;
             bool inArrears = false;

             // Fixed bond (Isin: DE0001135275 DBR 4 01/04/37)
             // maturity doesn't occur on a business day
             Date fixedBondStartDate1 = new Date(4,Month.January,2005);
             Date fixedBondMaturityDate1 = new Date(4,Month.January,2037);
             Schedule fixedBondSchedule1 = new Schedule(fixedBondStartDate1,
                                    fixedBondMaturityDate1,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg1 = new FixedRateLeg(fixedBondSchedule1)
            .withCouponRates(0.04, new ActualActual(ActualActual.Convention.ISDA))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption1 = bondCalendar.adjust(fixedBondMaturityDate1, BusinessDayConvention.Following);
             fixedBondLeg1.Add(new SimpleCashFlow(100.0, fixedbondRedemption1));
             // generic bond
             Bond fixedBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  fixedBondMaturityDate1, fixedBondStartDate1, fixedBondLeg1);
             IPricingEngine bondEngine = new DiscountingBondEngine(vars.termStructure);
             IPricingEngine swapEngine = new DiscountingSwapEngine(vars.termStructure);
             fixedBond1.setPricingEngine(bondEngine);

             // equivalent specialized fixed rate bond
             Bond fixedSpecializedBond1 = new FixedRateBond(settlementDays, vars.faceAmount, fixedBondSchedule1,
                           new List<double>{0.04},
                           new ActualActual(ActualActual.Convention.ISDA), BusinessDayConvention.Following,
                           100.0, new Date(4,Month.January,2005));
             fixedSpecializedBond1.setPricingEngine(bondEngine);

             double fixedBondPrice1 = fixedBond1.cleanPrice();
             double fixedSpecializedBondPrice1 = fixedSpecializedBond1.cleanPrice();
             AssetSwap fixedBondAssetSwap1 = new AssetSwap(payFixedRate,
                                       fixedBond1, fixedBondPrice1,
                                       vars.iborIndex, vars.nonnullspread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             fixedBondAssetSwap1.setPricingEngine(swapEngine);
             AssetSwap fixedSpecializedBondAssetSwap1 = new AssetSwap(payFixedRate,
                                                fixedSpecializedBond1,
                                                fixedSpecializedBondPrice1,
                                                vars.iborIndex,
                                                vars.nonnullspread,
                                                null,
                                                vars.iborIndex.dayCounter(),
                                                parAssetSwap);
             fixedSpecializedBondAssetSwap1.setPricingEngine(swapEngine);
             double fixedBondAssetSwapPrice1 = fixedBondAssetSwap1.fairCleanPrice();
             double fixedSpecializedBondAssetSwapPrice1 =
            fixedSpecializedBondAssetSwap1.fairCleanPrice();
             double tolerance = 1.0e-13;
             double error1 =
            Math.Abs(fixedBondAssetSwapPrice1-fixedSpecializedBondAssetSwapPrice1);
             if (error1>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  generic  fixed rate bond's  clean price: "
                        + fixedBondAssetSwapPrice1
                        + "\n  equivalent specialized bond's clean price: "
                        + fixedSpecializedBondAssetSwapPrice1
                        + "\n  error:                 " + error1
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double fixedBondMktPrice1= 91.832;
             AssetSwap fixedBondASW1 = new AssetSwap(payFixedRate,
                                 fixedBond1, fixedBondMktPrice1,
                                 vars.iborIndex, vars.spread,
                                 null,
                                 vars.iborIndex.dayCounter(),
                                 parAssetSwap);
             fixedBondASW1.setPricingEngine(swapEngine);
             AssetSwap fixedSpecializedBondASW1 = new AssetSwap(payFixedRate,
                                          fixedSpecializedBond1,
                                          fixedBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             fixedSpecializedBondASW1.setPricingEngine(swapEngine);
             double fixedBondASWSpread1 = fixedBondASW1.fairSpread();
             double fixedSpecializedBondASWSpread1 = fixedSpecializedBondASW1.fairSpread();
             double error2 = Math.Abs(fixedBondASWSpread1-fixedSpecializedBondASWSpread1);
             if (error2>tolerance) {
            Assert.Fail("wrong asw spread  for fixed bond:"
                        + "\n  generic  fixed rate bond's  asw spread: "
                        + fixedBondASWSpread1
                        + "\n  equivalent specialized bond's asw spread: "
                        + fixedSpecializedBondASWSpread1
                        + "\n  error:                 " + error2
                        + "\n  tolerance:             " + tolerance);
             }

             //Fixed bond (Isin: IT0006527060 IBRD 5 02/05/19)
             //maturity occurs on a business day

             Date fixedBondStartDate2 = new Date(5,Month.February,2005);
             Date fixedBondMaturityDate2 = new Date(5,Month.February,2019);
             Schedule fixedBondSchedule2= new Schedule(fixedBondStartDate2,
                                    fixedBondMaturityDate2,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg2 = new FixedRateLeg(fixedBondSchedule2)
            .withCouponRates(0.05, new Thirty360(Thirty360.Thirty360Convention.BondBasis))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption2 = bondCalendar.adjust(fixedBondMaturityDate2, BusinessDayConvention.Following);
             fixedBondLeg2.Add(new SimpleCashFlow(100.0, fixedbondRedemption2));

             // generic bond
             Bond fixedBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  fixedBondMaturityDate2, fixedBondStartDate2, fixedBondLeg2);
             fixedBond2.setPricingEngine(bondEngine);

             // equivalent specialized fixed rate bond
             Bond fixedSpecializedBond2 = new FixedRateBond(settlementDays, vars.faceAmount, fixedBondSchedule2,
                           new List<double>{ 0.05},
                           new Thirty360(Thirty360.Thirty360Convention.BondBasis), BusinessDayConvention.Following,
                           100.0, new Date(5,Month.February,2005));
             fixedSpecializedBond2.setPricingEngine(bondEngine);

             double fixedBondPrice2 = fixedBond2.cleanPrice();
             double fixedSpecializedBondPrice2 = fixedSpecializedBond2.cleanPrice();
             AssetSwap fixedBondAssetSwap2 = new AssetSwap(payFixedRate,
                                       fixedBond2, fixedBondPrice2,
                                       vars.iborIndex, vars.nonnullspread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             fixedBondAssetSwap2.setPricingEngine(swapEngine);
             AssetSwap fixedSpecializedBondAssetSwap2 = new AssetSwap(payFixedRate,
                                                fixedSpecializedBond2,
                                                fixedSpecializedBondPrice2,
                                                vars.iborIndex,
                                                vars.nonnullspread,
                                                null,
                                                vars.iborIndex.dayCounter(),
                                                parAssetSwap);
             fixedSpecializedBondAssetSwap2.setPricingEngine(swapEngine);
             double fixedBondAssetSwapPrice2 = fixedBondAssetSwap2.fairCleanPrice();
             double fixedSpecializedBondAssetSwapPrice2 = fixedSpecializedBondAssetSwap2.fairCleanPrice();

             double error3 = Math.Abs(fixedBondAssetSwapPrice2-fixedSpecializedBondAssetSwapPrice2);
             if (error3>tolerance) {
            Assert.Fail("wrong clean price for fixed bond:"
                        + "\n  generic  fixed rate bond's clean price: "
                        + fixedBondAssetSwapPrice2
                        + "\n  equivalent specialized  bond's clean price: "
                        + fixedSpecializedBondAssetSwapPrice2
                        + "\n  error:                 " + error3
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double fixedBondMktPrice2= 102.178;
             AssetSwap fixedBondASW2 = new AssetSwap(payFixedRate,
                                 fixedBond2, fixedBondMktPrice2,
                                 vars.iborIndex, vars.spread,
                                 null,
                                 vars.iborIndex.dayCounter(),
                                 parAssetSwap);
             fixedBondASW2.setPricingEngine(swapEngine);
             AssetSwap fixedSpecializedBondASW2 = new AssetSwap(payFixedRate,
                                          fixedSpecializedBond2,
                                          fixedBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             fixedSpecializedBondASW2.setPricingEngine(swapEngine);
             double fixedBondASWSpread2 = fixedBondASW2.fairSpread();
             double fixedSpecializedBondASWSpread2 = fixedSpecializedBondASW2.fairSpread();
             double error4 = Math.Abs(fixedBondASWSpread2-fixedSpecializedBondASWSpread2);
             if (error4>tolerance) {
            Assert.Fail("wrong asw spread for fixed bond:"
                        + "\n  generic  fixed rate bond's  asw spread: "
                        + fixedBondASWSpread2
                        + "\n  equivalent specialized bond's asw spread: "
                        + fixedSpecializedBondASWSpread2
                        + "\n  error:                 " + error4
                        + "\n  tolerance:             " + tolerance);
             }

             //FRN bond (Isin: IT0003543847 ISPIM 0 09/29/13)
             //maturity doesn't occur on a business day
             Date floatingBondStartDate1 = new Date(29,Month.September,2003);
             Date floatingBondMaturityDate1 = new Date(29,Month.September,2013);
             Schedule floatingBondSchedule1= new Schedule(floatingBondStartDate1,
                                       floatingBondMaturityDate1,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg1 = new IborLeg(floatingBondSchedule1, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0056)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption1 = bondCalendar.adjust(floatingBondMaturityDate1, BusinessDayConvention.Following);
             floatingBondLeg1.Add(new SimpleCashFlow(100.0, floatingbondRedemption1));
             // generic bond
             Bond floatingBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  floatingBondMaturityDate1, floatingBondStartDate1, floatingBondLeg1);
             floatingBond1.setPricingEngine(bondEngine);

             // equivalent specialized floater
             Bond floatingSpecializedBond1 = new FloatingRateBond(settlementDays, vars.faceAmount,
                                 floatingBondSchedule1,
                                 vars.iborIndex, new Actual360(),
                                 BusinessDayConvention.Following, fixingDays,
                                 new List<double>{1},
                                 new List<double>{0.0056},
                                 new List<double>(), new List<double>(),
                                 inArrears,
                                 100.0, new Date(29,Month.September,2003));
             floatingSpecializedBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond1.cashflows(), vars.pricer);
             Utils.setCouponPricer(floatingSpecializedBond1.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(27,Month.March,2007), 0.0402);
             double floatingBondPrice1 = floatingBond1.cleanPrice();
             double floatingSpecializedBondPrice1= floatingSpecializedBond1.cleanPrice();
             AssetSwap floatingBondAssetSwap1= new AssetSwap(payFixedRate,
                                          floatingBond1, floatingBondPrice1,
                                          vars.iborIndex, vars.nonnullspread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             floatingBondAssetSwap1.setPricingEngine(swapEngine);
             AssetSwap floatingSpecializedBondAssetSwap1= new AssetSwap(payFixedRate,
                                                   floatingSpecializedBond1,
                                                   floatingSpecializedBondPrice1,
                                                   vars.iborIndex,
                                                   vars.nonnullspread,
                                                   null,
                                                   vars.iborIndex.dayCounter(),
                                                   parAssetSwap);
             floatingSpecializedBondAssetSwap1.setPricingEngine(swapEngine);
             double floatingBondAssetSwapPrice1 = floatingBondAssetSwap1.fairCleanPrice();
             double floatingSpecializedBondAssetSwapPrice1 =
            floatingSpecializedBondAssetSwap1.fairCleanPrice();

             double error5 =
            Math.Abs(floatingBondAssetSwapPrice1-floatingSpecializedBondAssetSwapPrice1);
             if (error5>tolerance) {
            Assert.Fail("wrong clean price for frnbond:"
                        + "\n  generic frn rate bond's clean price: "
                        + floatingBondAssetSwapPrice1
                        + "\n  equivalent specialized  bond's price: "
                        + floatingSpecializedBondAssetSwapPrice1
                        + "\n  error:                 " + error5
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double floatingBondMktPrice1= 101.33;
             AssetSwap floatingBondASW1= new AssetSwap(payFixedRate,
                                    floatingBond1, floatingBondMktPrice1,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             floatingBondASW1.setPricingEngine(swapEngine);
             AssetSwap floatingSpecializedBondASW1= new AssetSwap(payFixedRate,
                                             floatingSpecializedBond1,
                                             floatingBondMktPrice1,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             floatingSpecializedBondASW1.setPricingEngine(swapEngine);
             double floatingBondASWSpread1 = floatingBondASW1.fairSpread();
             double floatingSpecializedBondASWSpread1 =
            floatingSpecializedBondASW1.fairSpread();
             double error6 =
            Math.Abs(floatingBondASWSpread1-floatingSpecializedBondASWSpread1);
             if (error6>tolerance) {
            Assert.Fail("wrong asw spread for fixed bond:"
                        + "\n  generic  frn rate bond's  asw spread: "
                        + floatingBondASWSpread1
                        + "\n  equivalent specialized bond's asw spread: "
                        + floatingSpecializedBondASWSpread1
                        + "\n  error:                 " + error6
                        + "\n  tolerance:             " + tolerance);
             }
             //FRN bond (Isin: XS0090566539 COE 0 09/24/18)
             //maturity occurs on a business day
             Date floatingBondStartDate2 = new Date(24,Month.September,2004);
             Date floatingBondMaturityDate2 = new Date(24,Month.September,2018);
             Schedule floatingBondSchedule2= new Schedule(floatingBondStartDate2,
                                       floatingBondMaturityDate2,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg2 = new IborLeg(floatingBondSchedule2, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0025)
            .inArrears(inArrears)
            .withPaymentAdjustment(BusinessDayConvention.ModifiedFollowing)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption2 = bondCalendar.adjust(floatingBondMaturityDate2, BusinessDayConvention.ModifiedFollowing);
             floatingBondLeg2.Add(new SimpleCashFlow(100.0, floatingbondRedemption2));
             // generic bond
             Bond floatingBond2 = new  Bond(settlementDays, bondCalendar, vars.faceAmount,
                  floatingBondMaturityDate2, floatingBondStartDate2,floatingBondLeg2);
             floatingBond2.setPricingEngine(bondEngine);

             // equivalent specialized floater
             Bond floatingSpecializedBond2 = new FloatingRateBond(settlementDays, vars.faceAmount,
                              floatingBondSchedule2,
                              vars.iborIndex, new Actual360(),
                              BusinessDayConvention.ModifiedFollowing, fixingDays,
                              new List<double>{1},
                              new List<double>{0.0025},
                              new List<double>(), new List<double>(),
                              inArrears,
                              100.0, new Date(24,Month.September,2004));
             floatingSpecializedBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond2.cashflows(), vars.pricer);
             Utils.setCouponPricer(floatingSpecializedBond2.cashflows(), vars.pricer);

             vars.iborIndex.addFixing(new Date(22,Month.March,2007), 0.04013);

             double floatingBondPrice2 = floatingBond2.cleanPrice();
             double floatingSpecializedBondPrice2= floatingSpecializedBond2.cleanPrice();
             AssetSwap floatingBondAssetSwap2= new AssetSwap(payFixedRate,
                                          floatingBond2, floatingBondPrice2,
                                          vars.iborIndex, vars.nonnullspread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             floatingBondAssetSwap2.setPricingEngine(swapEngine);
             AssetSwap floatingSpecializedBondAssetSwap2= new AssetSwap(payFixedRate,
                                                   floatingSpecializedBond2,
                                                   floatingSpecializedBondPrice2,
                                                   vars.iborIndex,
                                                   vars.nonnullspread,
                                                   null,
                                                   vars.iborIndex.dayCounter(),
                                                   parAssetSwap);
             floatingSpecializedBondAssetSwap2.setPricingEngine(swapEngine);
             double floatingBondAssetSwapPrice2 = floatingBondAssetSwap2.fairCleanPrice();
             double floatingSpecializedBondAssetSwapPrice2 =
            floatingSpecializedBondAssetSwap2.fairCleanPrice();
             double error7 =
            Math.Abs(floatingBondAssetSwapPrice2-floatingSpecializedBondAssetSwapPrice2);
             if (error7>tolerance) {
            Assert.Fail("wrong clean price for frnbond:"
                        + "\n  generic frn rate bond's clean price: "
                        + floatingBondAssetSwapPrice2
                        + "\n  equivalent specialized frn  bond's price: "
                        + floatingSpecializedBondAssetSwapPrice2
                        + "\n  error:                 " + error7
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double floatingBondMktPrice2 = 101.26;
             AssetSwap floatingBondASW2= new AssetSwap(payFixedRate,
                                    floatingBond2, floatingBondMktPrice2,
                                    vars.iborIndex, vars.spread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             floatingBondASW2.setPricingEngine(swapEngine);
             AssetSwap floatingSpecializedBondASW2= new AssetSwap(payFixedRate,
                                             floatingSpecializedBond2,
                                             floatingBondMktPrice2,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             floatingSpecializedBondASW2.setPricingEngine(swapEngine);
             double floatingBondASWSpread2 = floatingBondASW2.fairSpread();
             double floatingSpecializedBondASWSpread2 =
            floatingSpecializedBondASW2.fairSpread();
             double error8 =
            Math.Abs(floatingBondASWSpread2-floatingSpecializedBondASWSpread2);
             if (error8>tolerance) {
            Assert.Fail("wrong asw spread for frn bond:"
                        + "\n  generic  frn rate bond's  asw spread: "
                        + floatingBondASWSpread2
                        + "\n  equivalent specialized bond's asw spread: "
                        + floatingSpecializedBondASWSpread2
                        + "\n  error:                 " + error8
                        + "\n  tolerance:             " + tolerance);
             }

             // CMS bond (Isin: XS0228052402 CRDIT 0 8/22/20)
             // maturity doesn't occur on a business day
             Date cmsBondStartDate1 = new Date(22,Month.August,2005);
             Date cmsBondMaturityDate1 = new Date(22,Month.August,2020);
             Schedule cmsBondSchedule1= new Schedule(cmsBondStartDate1,
                                 cmsBondMaturityDate1,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg1 = new CmsLeg(cmsBondSchedule1, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withCaps(0.055)
            .withFloors(0.025)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption1 = bondCalendar.adjust(cmsBondMaturityDate1, BusinessDayConvention.Following);
             cmsBondLeg1.Add(new SimpleCashFlow(100.0, cmsbondRedemption1));
             // generic cms bond
             Bond cmsBond1 = new  Bond(settlementDays, bondCalendar, vars.faceAmount,
                  cmsBondMaturityDate1, cmsBondStartDate1, cmsBondLeg1);
             cmsBond1.setPricingEngine(bondEngine);

             // equivalent specialized cms bond
             Bond cmsSpecializedBond1 = new CmsRateBond(settlementDays, vars.faceAmount, cmsBondSchedule1,
                     vars.swapIndex, new Thirty360(),
                     BusinessDayConvention.Following, fixingDays,
                     new List<double>{1.0}, new List<double>{0.0},
                     new List<double>{0.055}, new List<double>{0.025},
                     inArrears,
                     100.0, new Date(22,Month.August,2005));
             cmsSpecializedBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond1.cashflows(), vars.cmspricer);
             Utils.setCouponPricer(cmsSpecializedBond1.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(18,Month.August,2006), 0.04158);
             double cmsBondPrice1 = cmsBond1.cleanPrice();
             double cmsSpecializedBondPrice1 = cmsSpecializedBond1.cleanPrice();
             AssetSwap cmsBondAssetSwap1= new AssetSwap(payFixedRate,cmsBond1, cmsBondPrice1,
                                    vars.iborIndex, vars.nonnullspread,
                                    null,vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             cmsBondAssetSwap1.setPricingEngine(swapEngine);
             AssetSwap cmsSpecializedBondAssetSwap1= new AssetSwap(payFixedRate,cmsSpecializedBond1,
                                                cmsSpecializedBondPrice1,
                                                vars.iborIndex,
                                                vars.nonnullspread,
                                                null,
                                                vars.iborIndex.dayCounter(),
                                                parAssetSwap);
             cmsSpecializedBondAssetSwap1.setPricingEngine(swapEngine);
             double cmsBondAssetSwapPrice1 = cmsBondAssetSwap1.fairCleanPrice();
             double cmsSpecializedBondAssetSwapPrice1 =
            cmsSpecializedBondAssetSwap1.fairCleanPrice();
             double error9 =
            Math.Abs(cmsBondAssetSwapPrice1-cmsSpecializedBondAssetSwapPrice1);
             if (error9>tolerance) {
            Assert.Fail("wrong clean price for cmsbond:"
                        + "\n  generic bond's clean price: "
                        + cmsBondAssetSwapPrice1
                        + "\n  equivalent specialized cms rate bond's price: "
                        + cmsSpecializedBondAssetSwapPrice1
                        + "\n  error:                 " + error9
                        + "\n  tolerance:             " + tolerance);
             }
             double cmsBondMktPrice1 = 87.02;// market executable price as of 4th sept 2007
             AssetSwap cmsBondASW1= new AssetSwap(payFixedRate,
                              cmsBond1, cmsBondMktPrice1,
                              vars.iborIndex, vars.spread,
                              null,
                              vars.iborIndex.dayCounter(),
                              parAssetSwap);
             cmsBondASW1.setPricingEngine(swapEngine);
             AssetSwap cmsSpecializedBondASW1= new AssetSwap(payFixedRate,
                                          cmsSpecializedBond1,
                                          cmsBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             cmsSpecializedBondASW1.setPricingEngine(swapEngine);
             double cmsBondASWSpread1 = cmsBondASW1.fairSpread();
             double cmsSpecializedBondASWSpread1 = cmsSpecializedBondASW1.fairSpread();
             double error10 = Math.Abs(cmsBondASWSpread1-cmsSpecializedBondASWSpread1);
             if (error10>tolerance) {
            Assert.Fail("wrong asw spread for cm bond:"
                        + "\n  generic cms rate bond's  asw spread: "
                        + cmsBondASWSpread1
                        + "\n  equivalent specialized bond's asw spread: "
                        + cmsSpecializedBondASWSpread1
                        + "\n  error:                 " + error10
                        + "\n  tolerance:             " + tolerance);
             }

             //CMS bond (Isin: XS0218766664 ISPIM 0 5/6/15)
             //maturity occurs on a business day
             Date cmsBondStartDate2 = new Date(06,Month.May,2005);
             Date cmsBondMaturityDate2 = new Date(06,Month.May,2015);
             Schedule cmsBondSchedule2= new Schedule(cmsBondStartDate2,
                                 cmsBondMaturityDate2,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg2 = new CmsLeg(cmsBondSchedule2, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withGearings(0.84)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption2 = bondCalendar.adjust(cmsBondMaturityDate2,
                                                      BusinessDayConvention.Following);
             cmsBondLeg2.Add(new SimpleCashFlow(100.0, cmsbondRedemption2));
             // generic bond
             Bond cmsBond2 = new  Bond(settlementDays, bondCalendar, vars.faceAmount,
                  cmsBondMaturityDate2, cmsBondStartDate2, cmsBondLeg2);
             cmsBond2.setPricingEngine(bondEngine);

             // equivalent specialized cms bond
             Bond cmsSpecializedBond2 = new  CmsRateBond(settlementDays, vars.faceAmount, cmsBondSchedule2,
                     vars.swapIndex, new Thirty360(),
                     BusinessDayConvention.Following, fixingDays,
                     new List<double>{0.84}, new List<double>{0.0},
                     new List<double>(), new List<double>(),
                     inArrears,
                     100.0, new Date(06,Month.May,2005));
             cmsSpecializedBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond2.cashflows(), vars.cmspricer);
             Utils.setCouponPricer(cmsSpecializedBond2.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(04,Month.May,2006), 0.04217);
             double cmsBondPrice2 = cmsBond2.cleanPrice();
             double cmsSpecializedBondPrice2 = cmsSpecializedBond2.cleanPrice();
             AssetSwap cmsBondAssetSwap2= new AssetSwap(payFixedRate,cmsBond2, cmsBondPrice2,
                                    vars.iborIndex, vars.nonnullspread,
                                    null,
                                    vars.iborIndex.dayCounter(),
                                    parAssetSwap);
             cmsBondAssetSwap2.setPricingEngine(swapEngine);
             AssetSwap cmsSpecializedBondAssetSwap2= new AssetSwap(payFixedRate,cmsSpecializedBond2,
                                                cmsSpecializedBondPrice2,
                                                vars.iborIndex,
                                                vars.nonnullspread,
                                                null,
                                                vars.iborIndex.dayCounter(),
                                                parAssetSwap);
             cmsSpecializedBondAssetSwap2.setPricingEngine(swapEngine);
             double cmsBondAssetSwapPrice2 = cmsBondAssetSwap2.fairCleanPrice();
             double cmsSpecializedBondAssetSwapPrice2 =
            cmsSpecializedBondAssetSwap2.fairCleanPrice();
             double error11 =
            Math.Abs(cmsBondAssetSwapPrice2-cmsSpecializedBondAssetSwapPrice2);
             if (error11>tolerance) {
            Assert.Fail("wrong clean price for cmsbond:"
                        + "\n  generic  bond's clean price: "
                        + cmsBondAssetSwapPrice2
                        + "\n  equivalent specialized cms rate bond's price: "
                        + cmsSpecializedBondAssetSwapPrice2
                        + "\n  error:                 " + error11
                        + "\n  tolerance:             " + tolerance);
             }
             double cmsBondMktPrice2 = 94.35;// market executable price as of 4th sept 2007
             AssetSwap cmsBondASW2= new AssetSwap(payFixedRate,
                              cmsBond2, cmsBondMktPrice2,
                              vars.iborIndex, vars.spread,
                              null,
                              vars.iborIndex.dayCounter(),
                              parAssetSwap);
             cmsBondASW2.setPricingEngine(swapEngine);
             AssetSwap cmsSpecializedBondASW2= new AssetSwap(payFixedRate,
                                          cmsSpecializedBond2,
                                          cmsBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             cmsSpecializedBondASW2.setPricingEngine(swapEngine);
             double cmsBondASWSpread2 = cmsBondASW2.fairSpread();
             double cmsSpecializedBondASWSpread2 = cmsSpecializedBondASW2.fairSpread();
             double error12 = Math.Abs(cmsBondASWSpread2-cmsSpecializedBondASWSpread2);
             if (error12>tolerance) {
            Assert.Fail("wrong asw spread for cm bond:"
                        + "\n  generic cms rate bond's  asw spread: "
                        + cmsBondASWSpread2
                        + "\n  equivalent specialized bond's asw spread: "
                        + cmsSpecializedBondASWSpread2
                        + "\n  error:                 " + error12
                        + "\n  tolerance:             " + tolerance);
             }

              //  Zero-Coupon bond (Isin: DE0004771662 IBRD 0 12/20/15)
              //  maturity doesn't occur on a business day
             Date zeroCpnBondStartDate1 = new Date(19,Month.December,1985);
             Date zeroCpnBondMaturityDate1 = new Date(20,Month.December,2015);
             Date zeroCpnBondRedemption1 = bondCalendar.adjust(zeroCpnBondMaturityDate1,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg1 = new List<CashFlow>{new SimpleCashFlow(100.0, zeroCpnBondRedemption1)};
             // generic bond
             Bond zeroCpnBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate1, zeroCpnBondStartDate1, zeroCpnBondLeg1);
             zeroCpnBond1.setPricingEngine(bondEngine);

             // specialized zerocpn bond
             Bond zeroCpnSpecializedBond1= new ZeroCouponBond(settlementDays, bondCalendar, vars.faceAmount,
                     new Date(20,Month.December,2015),
                     BusinessDayConvention.Following,
                     100.0, new Date(19,Month.December,1985));
             zeroCpnSpecializedBond1.setPricingEngine(bondEngine);

             double zeroCpnBondPrice1 = zeroCpnBond1.cleanPrice();
             double zeroCpnSpecializedBondPrice1 = zeroCpnSpecializedBond1.cleanPrice();
             AssetSwap zeroCpnBondAssetSwap1= new AssetSwap(payFixedRate,zeroCpnBond1,
                                       zeroCpnBondPrice1,
                                       vars.iborIndex, vars.nonnullspread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             zeroCpnBondAssetSwap1.setPricingEngine(swapEngine);
             AssetSwap zeroCpnSpecializedBondAssetSwap1= new AssetSwap(payFixedRate,
                                                   zeroCpnSpecializedBond1,
                                                   zeroCpnSpecializedBondPrice1,
                                                   vars.iborIndex,
                                                   vars.nonnullspread,
                                                   null,
                                                   vars.iborIndex.dayCounter(),
                                                   parAssetSwap);
             zeroCpnSpecializedBondAssetSwap1.setPricingEngine(swapEngine);
             double zeroCpnBondAssetSwapPrice1 = zeroCpnBondAssetSwap1.fairCleanPrice();
             double zeroCpnSpecializedBondAssetSwapPrice1 =
            zeroCpnSpecializedBondAssetSwap1.fairCleanPrice();
             double error13 =
            Math.Abs(zeroCpnBondAssetSwapPrice1-zeroCpnSpecializedBondAssetSwapPrice1);
             if (error13>tolerance) {
            Assert.Fail("wrong clean price for zerocpn bond:"
                        + "\n  generic zero cpn bond's clean price: "
                        + zeroCpnBondAssetSwapPrice1
                        + "\n  specialized equivalent bond's price: "
                        + zeroCpnSpecializedBondAssetSwapPrice1
                        + "\n  error:                 " + error13
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double zeroCpnBondMktPrice1 = 72.277;
             AssetSwap zeroCpnBondASW1= new AssetSwap(payFixedRate,
                                 zeroCpnBond1,zeroCpnBondMktPrice1,
                                 vars.iborIndex, vars.spread,
                                 null,
                                 vars.iborIndex.dayCounter(),
                                 parAssetSwap);
             zeroCpnBondASW1.setPricingEngine(swapEngine);
             AssetSwap zeroCpnSpecializedBondASW1= new AssetSwap(payFixedRate,
                                             zeroCpnSpecializedBond1,
                                             zeroCpnBondMktPrice1,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             zeroCpnSpecializedBondASW1.setPricingEngine(swapEngine);
             double zeroCpnBondASWSpread1 = zeroCpnBondASW1.fairSpread();
             double zeroCpnSpecializedBondASWSpread1 =
            zeroCpnSpecializedBondASW1.fairSpread();
             double error14 =
            Math.Abs(zeroCpnBondASWSpread1-zeroCpnSpecializedBondASWSpread1);
             if (error14>tolerance) {
            Assert.Fail("wrong asw spread for zeroCpn bond:"
                        + "\n  generic zeroCpn bond's  asw spread: "
                        + zeroCpnBondASWSpread1
                        + "\n  equivalent specialized bond's asw spread: "
                        + zeroCpnSpecializedBondASWSpread1
                        + "\n  error:                 " + error14
                        + "\n  tolerance:             " + tolerance);
             }

              //  Zero Coupon bond (Isin: IT0001200390 ISPIM 0 02/17/28)
              //  maturity doesn't occur on a business day
             Date zeroCpnBondStartDate2 = new Date(17,Month.February,1998);
             Date zeroCpnBondMaturityDate2 = new Date(17,Month.February,2028);
             Date zerocpbondRedemption2 = bondCalendar.adjust(zeroCpnBondMaturityDate2,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg2 = new List<CashFlow>{new SimpleCashFlow(100.0, zerocpbondRedemption2)};
             // generic bond
             Bond zeroCpnBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate2, zeroCpnBondStartDate2, zeroCpnBondLeg2);
             zeroCpnBond2.setPricingEngine(bondEngine);

             // specialized zerocpn bond
             Bond zeroCpnSpecializedBond2 = new ZeroCouponBond(settlementDays, bondCalendar, vars.faceAmount,
                        new Date(17,Month.February,2028),
                        BusinessDayConvention.Following,
                        100.0, new Date(17,Month.February,1998));
             zeroCpnSpecializedBond2.setPricingEngine(bondEngine);

             double zeroCpnBondPrice2 = zeroCpnBond2.cleanPrice();
             double zeroCpnSpecializedBondPrice2 = zeroCpnSpecializedBond2.cleanPrice();

             AssetSwap zeroCpnBondAssetSwap2= new AssetSwap(payFixedRate,zeroCpnBond2,
                                       zeroCpnBondPrice2,
                                       vars.iborIndex, vars.nonnullspread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             zeroCpnBondAssetSwap2.setPricingEngine(swapEngine);
             AssetSwap zeroCpnSpecializedBondAssetSwap2= new AssetSwap(payFixedRate,
                                                   zeroCpnSpecializedBond2,
                                                   zeroCpnSpecializedBondPrice2,
                                                   vars.iborIndex,
                                                   vars.nonnullspread,
                                                   null,
                                                   vars.iborIndex.dayCounter(),
                                                   parAssetSwap);
             zeroCpnSpecializedBondAssetSwap2.setPricingEngine(swapEngine);
             double zeroCpnBondAssetSwapPrice2 = zeroCpnBondAssetSwap2.fairCleanPrice();
             double zeroCpnSpecializedBondAssetSwapPrice2 =
                                    zeroCpnSpecializedBondAssetSwap2.fairCleanPrice();
             double error15 = Math.Abs(zeroCpnBondAssetSwapPrice2
                                 -zeroCpnSpecializedBondAssetSwapPrice2);
             if (error15>tolerance) {
            Assert.Fail("wrong clean price for zerocpn bond:"
                        + "\n  generic zero cpn bond's clean price: "
                        + zeroCpnBondAssetSwapPrice2
                        + "\n  equivalent specialized bond's price: "
                        + zeroCpnSpecializedBondAssetSwapPrice2
                        + "\n  error:                 " + error15
                        + "\n  tolerance:             " + tolerance);
             }
             // market executable price as of 4th sept 2007
             double zeroCpnBondMktPrice2 = 72.277;
             AssetSwap zeroCpnBondASW2= new AssetSwap(payFixedRate,
                                 zeroCpnBond2,zeroCpnBondMktPrice2,
                                 vars.iborIndex, vars.spread,
                                 null,
                                 vars.iborIndex.dayCounter(),
                                 parAssetSwap);
             zeroCpnBondASW2.setPricingEngine(swapEngine);
             AssetSwap zeroCpnSpecializedBondASW2= new AssetSwap(payFixedRate,
                                             zeroCpnSpecializedBond2,
                                             zeroCpnBondMktPrice2,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             zeroCpnSpecializedBondASW2.setPricingEngine(swapEngine);
             double zeroCpnBondASWSpread2 = zeroCpnBondASW2.fairSpread();
             double zeroCpnSpecializedBondASWSpread2 =
            zeroCpnSpecializedBondASW2.fairSpread();
             double error16 =
            Math.Abs(zeroCpnBondASWSpread2-zeroCpnSpecializedBondASWSpread2);
             if (error16>tolerance) {
            Assert.Fail("wrong asw spread for zeroCpn bond:"
                        + "\n  generic zeroCpn bond's  asw spread: "
                        + zeroCpnBondASWSpread2
                        + "\n  equivalent specialized bond's asw spread: "
                        + zeroCpnSpecializedBondASWSpread2
                        + "\n  error:                 " + error16
                        + "\n  tolerance:             " + tolerance);
             }
        }
示例#32
0
        public void testMASWWithGenericBond()
        {
            // Testing market asset swap against par asset swap with generic bond...

             CommonVars vars = new CommonVars();

             Calendar bondCalendar = new TARGET();
             int settlementDays = 3;
             int fixingDays = 2;
             bool payFixedRate = true;
             bool parAssetSwap = true;
             bool mktAssetSwap = false;
             bool inArrears = false;

             // Fixed Underlying bond (Isin: DE0001135275 DBR 4 01/04/37)
             // maturity doesn't occur on a business day

             Date fixedBondStartDate1 = new Date(4,Month.January,2005);
             Date fixedBondMaturityDate1 = new Date(4,Month.January,2037);
             Schedule fixedBondSchedule1 = new Schedule(fixedBondStartDate1,
                                    fixedBondMaturityDate1,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg1 = new FixedRateLeg(fixedBondSchedule1)
            .withCouponRates(0.04, new ActualActual(ActualActual.Convention.ISDA))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption1 = bondCalendar.adjust(fixedBondMaturityDate1,   BusinessDayConvention.Following);
             fixedBondLeg1.Add(new SimpleCashFlow(100.0, fixedbondRedemption1));
             Bond fixedBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount, fixedBondMaturityDate1,
            fixedBondStartDate1, fixedBondLeg1);
             IPricingEngine bondEngine = new DiscountingBondEngine(vars.termStructure);
             IPricingEngine swapEngine = new DiscountingSwapEngine(vars.termStructure);
             fixedBond1.setPricingEngine(bondEngine);

             double fixedBondMktPrice1 = 89.22 ; // market price observed on 7th June 2007
             double fixedBondMktFullPrice1=fixedBondMktPrice1+fixedBond1.accruedAmount();
             AssetSwap fixedBondParAssetSwap1= new AssetSwap(payFixedRate,
                                          fixedBond1, fixedBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             fixedBondParAssetSwap1.setPricingEngine(swapEngine);
             double fixedBondParAssetSwapSpread1 = fixedBondParAssetSwap1.fairSpread();
             AssetSwap fixedBondMktAssetSwap1 = new AssetSwap(payFixedRate,
                                          fixedBond1, fixedBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          mktAssetSwap);
             fixedBondMktAssetSwap1.setPricingEngine(swapEngine);
             double fixedBondMktAssetSwapSpread1 = fixedBondMktAssetSwap1.fairSpread();

             double tolerance = 1.0e-13;
             double error1 =
            Math.Abs(fixedBondMktAssetSwapSpread1-
                     100*fixedBondParAssetSwapSpread1/fixedBondMktFullPrice1);

             if (error1>tolerance)
            Assert.Fail("wrong asset swap spreads for fixed bond:" +
                        "\n  market asset swap spread: " + fixedBondMktAssetSwapSpread1 +
                        "\n  par asset swap spread:    " + fixedBondParAssetSwapSpread1 +
                        "\n  error:                    " + error1 +
                        "\n  tolerance:                " + tolerance);

             // Fixed Underlying bond (Isin: IT0006527060 IBRD 5 02/05/19)
             // maturity occurs on a business day

             Date fixedBondStartDate2 = new Date(5,Month.February,2005);
             Date fixedBondMaturityDate2 = new Date(5,Month.February,2019);
             Schedule fixedBondSchedule2 = new Schedule(fixedBondStartDate2,
                                    fixedBondMaturityDate2,
                                    new Period(Frequency.Annual), bondCalendar,
                                    BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                    DateGeneration.Rule.Backward, false);
             List<CashFlow> fixedBondLeg2 = new FixedRateLeg(fixedBondSchedule2)
            .withCouponRates(0.05, new Thirty360(Thirty360.Thirty360Convention.BondBasis))
            .withNotionals(vars.faceAmount);
             Date fixedbondRedemption2 = bondCalendar.adjust(fixedBondMaturityDate2,  BusinessDayConvention.Following);
             fixedBondLeg2.Add(new SimpleCashFlow(100.0, fixedbondRedemption2));
             Bond fixedBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount, fixedBondMaturityDate2, fixedBondStartDate2,
            fixedBondLeg2);
             fixedBond2.setPricingEngine(bondEngine);

             double fixedBondMktPrice2 = 99.98 ; // market price observed on 7th June 2007
             double fixedBondMktFullPrice2=fixedBondMktPrice2+fixedBond2.accruedAmount();
             AssetSwap fixedBondParAssetSwap2= new AssetSwap(payFixedRate,
                                          fixedBond2, fixedBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             fixedBondParAssetSwap2.setPricingEngine(swapEngine);
             double fixedBondParAssetSwapSpread2 = fixedBondParAssetSwap2.fairSpread();
             AssetSwap fixedBondMktAssetSwap2= new AssetSwap(payFixedRate,
                                          fixedBond2, fixedBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          mktAssetSwap);
             fixedBondMktAssetSwap2.setPricingEngine(swapEngine);
             double fixedBondMktAssetSwapSpread2 = fixedBondMktAssetSwap2.fairSpread();
             double error2 = Math.Abs(fixedBondMktAssetSwapSpread2-
                     100*fixedBondParAssetSwapSpread2/fixedBondMktFullPrice2);

             if (error2>tolerance)
            Assert.Fail("wrong asset swap spreads for fixed bond:" +
                        "\n  market asset swap spread: " + fixedBondMktAssetSwapSpread2 +
                        "\n  par asset swap spread:    " + fixedBondParAssetSwapSpread2 +
                        "\n  error:                    " + error2 +
                        "\n  tolerance:                " + tolerance);

             // FRN Underlying bond (Isin: IT0003543847 ISPIM 0 09/29/13)
             // maturity doesn't occur on a business day

             Date floatingBondStartDate1 = new Date(29,Month.September,2003);
             Date floatingBondMaturityDate1 = new Date(29,Month.September,2013);
             Schedule floatingBondSchedule1= new Schedule(floatingBondStartDate1,
                                       floatingBondMaturityDate1,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg1 = new IborLeg(floatingBondSchedule1, vars.iborIndex)
            .withPaymentDayCounter(new Actual360())
            .withFixingDays(fixingDays)
            .withSpreads(0.0056)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption1 =
            bondCalendar.adjust(floatingBondMaturityDate1, BusinessDayConvention.Following);
             floatingBondLeg1.Add(new SimpleCashFlow(100.0, floatingbondRedemption1));
             Bond floatingBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount,  floatingBondMaturityDate1,
            floatingBondStartDate1, floatingBondLeg1);
             floatingBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond1.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(27,Month.March,2007), 0.0402);
             // market price observed on 7th June 2007
             double floatingBondMktPrice1 = 101.64 ;
             double floatingBondMktFullPrice1 =
            floatingBondMktPrice1+floatingBond1.accruedAmount();
             AssetSwap floatingBondParAssetSwap1 = new AssetSwap(payFixedRate,
                                             floatingBond1, floatingBondMktPrice1,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             floatingBondParAssetSwap1.setPricingEngine(swapEngine);
             double floatingBondParAssetSwapSpread1 =
            floatingBondParAssetSwap1.fairSpread();
             AssetSwap floatingBondMktAssetSwap1 = new AssetSwap(payFixedRate,
                                             floatingBond1, floatingBondMktPrice1,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             mktAssetSwap);
             floatingBondMktAssetSwap1.setPricingEngine(swapEngine);
             double floatingBondMktAssetSwapSpread1 =
            floatingBondMktAssetSwap1.fairSpread();
             double error3 = Math.Abs(floatingBondMktAssetSwapSpread1-
                     100*floatingBondParAssetSwapSpread1/floatingBondMktFullPrice1);

             if (error3>tolerance)
            Assert.Fail("wrong asset swap spreads for floating bond:" +
                        "\n  market asset swap spread: " + floatingBondMktAssetSwapSpread1 +
                        "\n  par asset swap spread:    " + floatingBondParAssetSwapSpread1 +
                        "\n  error:                    " + error3 +
                        "\n  tolerance:                " + tolerance);

             // FRN Underlying bond (Isin: XS0090566539 COE 0 09/24/18)
             // maturity occurs on a business day

             Date floatingBondStartDate2 = new Date(24,Month.September,2004);
             Date floatingBondMaturityDate2 = new Date(24,Month.September,2018);
             Schedule floatingBondSchedule2 = new Schedule(floatingBondStartDate2,
                                       floatingBondMaturityDate2,
                                       new Period(Frequency.Semiannual), bondCalendar,
                                       BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing,
                                       DateGeneration.Rule.Backward, false);
             List<CashFlow> floatingBondLeg2 = new IborLeg(floatingBondSchedule2, vars.iborIndex)
            .withFixingDays(fixingDays)
            .withSpreads(0.0025)
            .inArrears(inArrears)
            .withPaymentDayCounter(new Actual360())
            .withPaymentAdjustment(BusinessDayConvention.ModifiedFollowing)
            .withNotionals(vars.faceAmount);
             Date floatingbondRedemption2 =
            bondCalendar.adjust(floatingBondMaturityDate2, BusinessDayConvention.ModifiedFollowing);
             floatingBondLeg2.Add(new
            SimpleCashFlow(100.0, floatingbondRedemption2));
             Bond floatingBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount, floatingBondMaturityDate2,
            floatingBondStartDate2, floatingBondLeg2);
             floatingBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(floatingBond2.cashflows(), vars.pricer);
             vars.iborIndex.addFixing(new Date(22,Month.March,2007), 0.04013);
             // market price observed on 7th June 2007
             double floatingBondMktPrice2 = 101.248 ;
             double floatingBondMktFullPrice2 =
            floatingBondMktPrice2+floatingBond2.accruedAmount();
             AssetSwap floatingBondParAssetSwap2 = new AssetSwap(payFixedRate,
                                             floatingBond2, floatingBondMktPrice2,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             parAssetSwap);
             floatingBondParAssetSwap2.setPricingEngine(swapEngine);
             double floatingBondParAssetSwapSpread2 = floatingBondParAssetSwap2.fairSpread();
             AssetSwap floatingBondMktAssetSwap2 = new AssetSwap(payFixedRate,
                                             floatingBond2, floatingBondMktPrice2,
                                             vars.iborIndex, vars.spread,
                                             null,
                                             vars.iborIndex.dayCounter(),
                                             mktAssetSwap);
             floatingBondMktAssetSwap2.setPricingEngine(swapEngine);
             double floatingBondMktAssetSwapSpread2 =
            floatingBondMktAssetSwap2.fairSpread();
             double error4 = Math.Abs(floatingBondMktAssetSwapSpread2-
                     100*floatingBondParAssetSwapSpread2/floatingBondMktFullPrice2);

             if (error4>tolerance)
            Assert.Fail("wrong asset swap spreads for floating bond:" +
                        "\n  market asset swap spread: " + floatingBondMktAssetSwapSpread2 +
                        "\n  par asset swap spread:    " + floatingBondParAssetSwapSpread2 +
                        "\n  error:                    " + error4 +
                        "\n  tolerance:                " + tolerance);

             // CMS Underlying bond (Isin: XS0228052402 CRDIT 0 8/22/20)
             // maturity doesn't occur on a business day

             Date cmsBondStartDate1 = new Date(22,Month.August,2005);
             Date cmsBondMaturityDate1 = new Date(22,Month.August,2020);
             Schedule cmsBondSchedule1= new Schedule(cmsBondStartDate1,
                                 cmsBondMaturityDate1,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg1 = new CmsLeg(cmsBondSchedule1, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withCaps(0.055)
            .withFloors(0.025)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption1 = bondCalendar.adjust(cmsBondMaturityDate1, BusinessDayConvention.Following);
             cmsBondLeg1.Add(new SimpleCashFlow(100.0, cmsbondRedemption1));
             Bond cmsBond1 = new Bond(settlementDays, bondCalendar, vars.faceAmount, cmsBondMaturityDate1, cmsBondStartDate1,
            cmsBondLeg1);
             cmsBond1.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond1.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(18,Month.August,2006), 0.04158);
             double cmsBondMktPrice1 = 88.45 ; // market price observed on 7th June 2007
             double cmsBondMktFullPrice1 = cmsBondMktPrice1+cmsBond1.accruedAmount();
             AssetSwap cmsBondParAssetSwap1 = new AssetSwap(payFixedRate,
                                       cmsBond1, cmsBondMktPrice1,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             cmsBondParAssetSwap1.setPricingEngine(swapEngine);
             double cmsBondParAssetSwapSpread1 = cmsBondParAssetSwap1.fairSpread();
             AssetSwap cmsBondMktAssetSwap1 = new AssetSwap(payFixedRate,
                                       cmsBond1, cmsBondMktPrice1,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       mktAssetSwap);
             cmsBondMktAssetSwap1.setPricingEngine(swapEngine);
             double cmsBondMktAssetSwapSpread1 = cmsBondMktAssetSwap1.fairSpread();
             double error5 =
            Math.Abs(cmsBondMktAssetSwapSpread1-
                     100*cmsBondParAssetSwapSpread1/cmsBondMktFullPrice1);

             if (error5>tolerance)
            Assert.Fail("wrong asset swap spreads for cms bond:" +
                        "\n  market asset swap spread: " + cmsBondMktAssetSwapSpread1 +
                        "\n  par asset swap spread:    " + cmsBondParAssetSwapSpread1 +
                        "\n  error:                    " + error5 +
                        "\n  tolerance:                " + tolerance);

             // CMS Underlying bond (Isin: XS0218766664 ISPIM 0 5/6/15)
             // maturity occurs on a business day

             Date cmsBondStartDate2 = new Date(06,Month.May,2005);
             Date cmsBondMaturityDate2 = new Date(06,Month.May,2015);
             Schedule cmsBondSchedule2= new Schedule(cmsBondStartDate2,
                                 cmsBondMaturityDate2,
                                 new Period(Frequency.Annual), bondCalendar,
                                 BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                 DateGeneration.Rule.Backward, false);
             List<CashFlow> cmsBondLeg2 = new CmsLeg(cmsBondSchedule2, vars.swapIndex)
            .withPaymentDayCounter(new Thirty360())
            .withFixingDays(fixingDays)
            .withGearings(0.84)
            .inArrears(inArrears)
            .withNotionals(vars.faceAmount);
             Date cmsbondRedemption2 = bondCalendar.adjust(cmsBondMaturityDate2,  BusinessDayConvention.Following);
             cmsBondLeg2.Add(new SimpleCashFlow(100.0, cmsbondRedemption2));
             Bond cmsBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,  cmsBondMaturityDate2, cmsBondStartDate2,
            cmsBondLeg2);
             cmsBond2.setPricingEngine(bondEngine);

             Utils.setCouponPricer(cmsBond2.cashflows(), vars.cmspricer);
             vars.swapIndex.addFixing(new Date(04,Month.May,2006), 0.04217);
             double cmsBondMktPrice2 = 94.08 ; // market price observed on 7th June 2007
             double cmsBondMktFullPrice2 = cmsBondMktPrice2+cmsBond2.accruedAmount();
             AssetSwap cmsBondParAssetSwap2 = new AssetSwap(payFixedRate,
                                       cmsBond2, cmsBondMktPrice2,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       parAssetSwap);
             cmsBondParAssetSwap2.setPricingEngine(swapEngine);
             double cmsBondParAssetSwapSpread2 = cmsBondParAssetSwap2.fairSpread();
             AssetSwap cmsBondMktAssetSwap2 = new AssetSwap(payFixedRate,
                                       cmsBond2, cmsBondMktPrice2,
                                       vars.iborIndex, vars.spread,
                                       null,
                                       vars.iborIndex.dayCounter(),
                                       mktAssetSwap);
             cmsBondMktAssetSwap2.setPricingEngine(swapEngine);
             double cmsBondMktAssetSwapSpread2 = cmsBondMktAssetSwap2.fairSpread();
             double error6 =
            Math.Abs(cmsBondMktAssetSwapSpread2-
                     100*cmsBondParAssetSwapSpread2/cmsBondMktFullPrice2);

             if (error6>tolerance)
            Assert.Fail("wrong asset swap spreads for cms bond:" +
                        "\n  market asset swap spread: " + cmsBondMktAssetSwapSpread2 +
                        "\n  par asset swap spread:    " + cmsBondParAssetSwapSpread2 +
                        "\n  error:                    " + error6 +
                        "\n  tolerance:                " + tolerance);

             // Zero Coupon bond (Isin: DE0004771662 IBRD 0 12/20/15)
             // maturity doesn't occur on a business day

             Date zeroCpnBondStartDate1 = new Date(19,Month.December,1985);
             Date zeroCpnBondMaturityDate1 = new Date(20,Month.December,2015);
             Date zeroCpnBondRedemption1 = bondCalendar.adjust(zeroCpnBondMaturityDate1,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg1 = new List<CashFlow>{new SimpleCashFlow(100.0, zeroCpnBondRedemption1)};
             Bond zeroCpnBond1 = new  Bond(settlementDays, bondCalendar, vars.faceAmount, zeroCpnBondMaturityDate1,
            zeroCpnBondStartDate1, zeroCpnBondLeg1);
             zeroCpnBond1.setPricingEngine(bondEngine);

             // market price observed on 12th June 2007
             double zeroCpnBondMktPrice1 = 70.436 ;
             double zeroCpnBondMktFullPrice1 =
            zeroCpnBondMktPrice1+zeroCpnBond1.accruedAmount();
             AssetSwap zeroCpnBondParAssetSwap1 = new AssetSwap(payFixedRate,zeroCpnBond1,
                                          zeroCpnBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             zeroCpnBondParAssetSwap1.setPricingEngine(swapEngine);
             double zeroCpnBondParAssetSwapSpread1 = zeroCpnBondParAssetSwap1.fairSpread();
             AssetSwap zeroCpnBondMktAssetSwap1 = new AssetSwap(payFixedRate,zeroCpnBond1,
                                          zeroCpnBondMktPrice1,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          mktAssetSwap);
             zeroCpnBondMktAssetSwap1.setPricingEngine(swapEngine);
             double zeroCpnBondMktAssetSwapSpread1 = zeroCpnBondMktAssetSwap1.fairSpread();
             double error7 =
            Math.Abs(zeroCpnBondMktAssetSwapSpread1-
                     100*zeroCpnBondParAssetSwapSpread1/zeroCpnBondMktFullPrice1);

             if (error7>tolerance)
            Assert.Fail("wrong asset swap spreads for zero cpn bond:" +
                        "\n  market asset swap spread: " + zeroCpnBondMktAssetSwapSpread1 +
                        "\n  par asset swap spread:    " + zeroCpnBondParAssetSwapSpread1 +
                        "\n  error:                    " + error7 +
                        "\n  tolerance:                " + tolerance);

             // Zero Coupon bond (Isin: IT0001200390 ISPIM 0 02/17/28)
             // maturity occurs on a business day

             Date zeroCpnBondStartDate2 = new Date(17,Month.February,1998);
             Date zeroCpnBondMaturityDate2 = new Date(17,Month.February,2028);
             Date zerocpbondRedemption2 = bondCalendar.adjust(zeroCpnBondMaturityDate2,
                                                         BusinessDayConvention.Following);
             List<CashFlow> zeroCpnBondLeg2 = new List<CashFlow>{new SimpleCashFlow(100.0, zerocpbondRedemption2)};
             Bond zeroCpnBond2 = new Bond(settlementDays, bondCalendar, vars.faceAmount,
                  zeroCpnBondMaturityDate2, zeroCpnBondStartDate2, zeroCpnBondLeg2);
             zeroCpnBond2.setPricingEngine(bondEngine);

             // double zeroCpnBondPrice2 = zeroCpnBond2.cleanPrice();
             // market price observed on 12th June 2007
             double zeroCpnBondMktPrice2 = 35.160 ;
             double zeroCpnBondMktFullPrice2 =
            zeroCpnBondMktPrice2+zeroCpnBond2.accruedAmount();
             AssetSwap zeroCpnBondParAssetSwap2 = new AssetSwap(payFixedRate,zeroCpnBond2,
                                          zeroCpnBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          parAssetSwap);
             zeroCpnBondParAssetSwap2.setPricingEngine(swapEngine);
             double zeroCpnBondParAssetSwapSpread2 = zeroCpnBondParAssetSwap2.fairSpread();
             AssetSwap zeroCpnBondMktAssetSwap2 = new AssetSwap(payFixedRate,zeroCpnBond2,
                                          zeroCpnBondMktPrice2,
                                          vars.iborIndex, vars.spread,
                                          null,
                                          vars.iborIndex.dayCounter(),
                                          mktAssetSwap);
             zeroCpnBondMktAssetSwap2.setPricingEngine(swapEngine);
             double zeroCpnBondMktAssetSwapSpread2 = zeroCpnBondMktAssetSwap2.fairSpread();
             double error8 =
            Math.Abs(zeroCpnBondMktAssetSwapSpread2-
                     100*zeroCpnBondParAssetSwapSpread2/zeroCpnBondMktFullPrice2);

             if (error8>tolerance)
            Assert.Fail("wrong asset swap spreads for zero cpn bond:" +
                        "\n  market asset swap spread: " + zeroCpnBondMktAssetSwapSpread2 +
                        "\n  par asset swap spread:    " + zeroCpnBondParAssetSwapSpread2 +
                        "\n  error:                    " + error8 +
                        "\n  tolerance:                " + tolerance);
        }
示例#33
0
文件: T_Swaps.cs 项目: Yenyenx/qlnet
        public void testInArrears()
        {
            //("Testing in-arrears swap calculation...");

             CommonVars vars = new CommonVars();

             /* See Hull, 4th ed., page 550
            Note: the calculation in the book is wrong (work out the adjustment and you'll get 0.05 + 0.000115 T1) */
             Date maturity = vars.today + new Period(5, TimeUnit.Years);
             Calendar calendar = new NullCalendar();
             Schedule schedule = new Schedule(vars.today, maturity, new Period(Frequency.Annual), calendar,
                                          BusinessDayConvention.Following, BusinessDayConvention.Following,
                                          DateGeneration.Rule.Forward, false);
             DayCounter dayCounter = new SimpleDayCounter();

             List<double> nominals = new List<double>() { 100000000.0 };

             IborIndex index = new IborIndex("dummy", new Period(1, TimeUnit.Years), 0, new EURCurrency(), calendar,
                                         BusinessDayConvention.Following, false, dayCounter, vars.termStructure);
             double oneYear = 0.05;
             double r = Math.Log(1.0 + oneYear);
             vars.termStructure.linkTo(Utilities.flatRate(vars.today, r, dayCounter));

             List<double> coupons = new List<double>() { oneYear };
             List<CashFlow> fixedLeg = new FixedRateLeg(schedule)
                                 .withCouponRates(coupons, dayCounter)
                                 .withNotionals(nominals);

             List<double> gearings = new List<double>();
             List<double> spreads = new List<double>();
             int fixingDays = 0;

             double capletVolatility = 0.22;
             var vol = new Handle<OptionletVolatilityStructure>(
                        new ConstantOptionletVolatility(vars.today, new NullCalendar(),
                                                        BusinessDayConvention.Following, capletVolatility, dayCounter));
             IborCouponPricer pricer = new BlackIborCouponPricer(vol);

             List<CashFlow> floatingLeg = new IborLeg(schedule, index)
                                     .withPaymentDayCounter(dayCounter)
                                     .withFixingDays(fixingDays)
                                     .withGearings(gearings)
                                     .withSpreads(spreads)
                                     .inArrears()
                                     .withNotionals(nominals);
             Utils.setCouponPricer(floatingLeg, pricer);

             Swap swap = new Swap(floatingLeg, fixedLeg);
             swap.setPricingEngine(new DiscountingSwapEngine(vars.termStructure));

             double storedValue = -144813.0;
             double tolerance = 1.0;

             if (Math.Abs(swap.NPV() - storedValue) > tolerance)
            Assert.Fail("Wrong NPV calculation:\n"
                        + "    expected:   " + storedValue + "\n"
                        + "    calculated: " + swap.NPV());
        }
示例#34
0
        public CommercialPaper(Type type, double nominal,
                               Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount,
                               Schedule principalSchedule, BusinessDayConvention?paymentConvention) :
            base(2)
        {
            type_              = type;
            nominal_           = nominal;
            fixedSchedule_     = fixedSchedule;
            fixedRate_         = fixedRate;
            fixedDayCount_     = fixedDayCount;
            principalSchedule_ = principalSchedule;

            if (paymentConvention.HasValue)
            {
                paymentConvention_ = paymentConvention.Value;
            }
            else
            {
                paymentConvention_ = fixedSchedule_.businessDayConvention();
            }

            List <CashFlow> principalLeg = new PricipalLeg(principalSchedule, fixedDayCount)
                                           .withNotionals(nominal)
                                           .withPaymentAdjustment(paymentConvention_)
                                           .withSign(type == Type.Loan ? -1 : 1);

            // temporary
            for (int i = 0; i < principalLeg.Count - 1; i++)
            {
                Principal p = (Principal)principalLeg[i];
                notionals_.Add(p.nominal());
            }

            List <CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                                       .withCouponRates(fixedRate, fixedDayCount)
                                       .withPaymentAdjustment(paymentConvention_)
                                       .withNotionals(notionals_);

            // Discounting Pricipal
            notionals_.Clear();
            double n;

            for (int i = 0; i < fixedLeg.Count; i++)
            {
                FixedRateCoupon c = (FixedRateCoupon)fixedLeg[i];
                n = i > 0 ? notionals_.Last() : c.nominal();
                notionals_.Add(n / (1 + (c.rate() * c.dayCounter().yearFraction(c.referencePeriodStart, c.referencePeriodEnd))));
            }

            // New Leg
            List <CashFlow> discountedFixedLeg = new FixedRateLeg(fixedSchedule)
                                                 .withCouponRates(fixedRate, fixedDayCount)
                                                 .withPaymentAdjustment(paymentConvention_)
                                                 .withNotionals(notionals_);
            // Adjust Principal
            Principal p0 = (Principal)principalLeg[0];

            p0.setAmount(notionals_.Last());

            legs_[0] = discountedFixedLeg;
            legs_[1] = principalLeg;
            if (type_ == Type.Loan)
            {
                payer_[0] = +1;
                payer_[1] = -1;
            }
            else
            {
                payer_[0] = -1;
                payer_[1] = +1;
            }
        }
示例#35
0
文件: caphelper.cs 项目: ariesy/QLNet
        public CapHelper(Period length,
            Handle<Quote> volatility,
            IborIndex index,
            // data for ATM swap-rate calculation
            Frequency fixedLegFrequency,
            DayCounter fixedLegDayCounter,
            bool includeFirstSwaplet,
            Handle<YieldTermStructure> termStructure,
            bool calibrateVolatility /*= false*/)
            : base(volatility, termStructure, calibrateVolatility)
        {
            Period indexTenor = index.tenor();
            double fixedRate = 0.04; // dummy value
            Date startDate, maturity;
            if (includeFirstSwaplet) {
                startDate = termStructure.link.referenceDate();
                maturity = termStructure.link.referenceDate() + length;
            } else {
                startDate = termStructure.link.referenceDate() + indexTenor;
                maturity = termStructure.link.referenceDate() + length;
            }
            IborIndex dummyIndex=new
                IborIndex("dummy",
                          indexTenor,
                          index.fixingDays(),
                          index.currency(),
                          index.fixingCalendar(),
                          index.businessDayConvention(),
                          index.endOfMonth(),
                          termStructure.link.dayCounter(),
                          termStructure);

            List<double> nominals = new InitializedList<double>(1,1.0);

            Schedule floatSchedule=new Schedule(startDate, maturity,
                                   index.tenor(), index.fixingCalendar(),
                                   index.businessDayConvention(),
                                   index.businessDayConvention(),
                                   DateGeneration.Rule.Forward, false);
            List<CashFlow> floatingLeg;
            IborLeg iborLeg = (IborLeg) new IborLeg(floatSchedule, index)
                                            .withFixingDays(0)
                                            .withNotionals(nominals)
                                            .withPaymentAdjustment(index.businessDayConvention());
            floatingLeg = iborLeg.value();
            Schedule fixedSchedule=new Schedule(startDate, maturity, new Period(fixedLegFrequency),
                                   index.fixingCalendar(),
                                   BusinessDayConvention.Unadjusted, BusinessDayConvention.Unadjusted,
                                   DateGeneration.Rule.Forward, false);
            List<CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                .withCouponRates(fixedRate, fixedLegDayCounter)
                .withNotionals(nominals)
                .withPaymentAdjustment(index.businessDayConvention());

            Swap swap = new Swap(floatingLeg, fixedLeg);
            swap.setPricingEngine(new DiscountingSwapEngine(termStructure));
            double bp = 1.0e-4;
            double fairRate = fixedRate - (double)(swap.NPV()/(swap.legBPS(1) / bp));
            List<double> exerciceRate = new InitializedList<double>(1,fairRate);
            cap_ = new Cap(floatingLeg, exerciceRate);
            marketValue_ = blackPrice(volatility_.link.value());
        }
示例#36
0
        public CommercialPaper(Loan.Type type, double nominal, Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount, Schedule principalSchedule, BusinessDayConvention? paymentConvention)
            : base(2)
        {
            type_ = type;
            nominal_ = nominal;
            fixedSchedule_ = fixedSchedule;
            fixedRate_ = fixedRate;
            fixedDayCount_ = fixedDayCount;
            principalSchedule_ = principalSchedule;

            if (paymentConvention.HasValue)
                paymentConvention_ = paymentConvention.Value;
            else
                paymentConvention_ = fixedSchedule_.businessDayConvention();

            List<CashFlow> principalLeg = new PricipalLeg(principalSchedule, fixedDayCount)
                .withNotionals(nominal)
                .withPaymentAdjustment(paymentConvention_)
                .withSign(type == Type.Loan ? -1 : 1);

            // temporary
            for (int i = 0; i < principalLeg.Count - 1; i++)
            {
                Principal p = (Principal)principalLeg[i];
                notionals_.Add(p.nominal());
            }

            List<CashFlow> fixedLeg = new FixedRateLeg(fixedSchedule)
                .withCouponRates(fixedRate, fixedDayCount)
                .withPaymentAdjustment(paymentConvention_)
                .withNotionals(notionals_);

            // Discounting Pricipal
            notionals_.Clear();
            double n;
            for (int i = 0; i < fixedLeg.Count; i++)
            {
                FixedRateCoupon c = (FixedRateCoupon)fixedLeg[i];
                n = i > 0 ? notionals_.Last() : c.nominal();
                notionals_.Add(n / (1 + (c.rate() * c.dayCounter().yearFraction(c.refPeriodStart, c.refPeriodEnd))));
            }

            // New Leg
            List<CashFlow> discountedFixedLeg = new FixedRateLeg(fixedSchedule)
                .withCouponRates(fixedRate, fixedDayCount)
                .withPaymentAdjustment(paymentConvention_)
                .withNotionals(notionals_);
            // Adjust Principal
            Principal p0 = (Principal)principalLeg[0];
            p0.setAmount(notionals_.Last());

            legs_[0] = discountedFixedLeg;
            legs_[1] = principalLeg;
            if (type_ == Type.Loan)
            {
                payer_[0] = +1;
                payer_[1] = -1;
            }
            else
            {
                payer_[0] = -1;
                payer_[1] = +1;
            }
        }
示例#37
0
      public AmortizingFixedRateBond(
                          int settlementDays,
                          Calendar calendar,
                          double faceAmount,
                          Date startDate,
                          Period bondTenor,
                          Frequency sinkingFrequency,
                          double coupon,
                          DayCounter accrualDayCounter,
                          BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                          Date issueDate = null)
         :base(settlementDays, calendar, issueDate)
      {
         frequency_ = sinkingFrequency;
         dayCounter_ = accrualDayCounter;

         Utils.QL_REQUIRE( bondTenor.length() > 0,() =>
                  "bond tenor must be positive. "
                  + bondTenor + " is not allowed." );

         maturityDate_ = startDate + bondTenor;
         maturityDate_ = startDate + bondTenor;
         schedule_ = sinkingSchedule(startDate, bondTenor, sinkingFrequency, calendar);
         cashflows_ = new FixedRateLeg(schedule_)
                        .withCouponRates(coupon, accrualDayCounter)
                        .withNotionals(sinkingNotionals(bondTenor, sinkingFrequency, coupon, faceAmount))        
                        .withPaymentAdjustment(paymentConvention).value();

         addRedemptionsToCashflows();

      }
示例#38
0
      /*! simple annual compounding coupon rates
          with internal schedule calculation */
      public FixedRateBond(int settlementDays, 
                           Calendar calendar,
                           double faceAmount,
                           Date startDate,
                           Date maturityDate,
                           Period tenor,
                           List<double> coupons,
                           DayCounter accrualDayCounter,
                           BusinessDayConvention accrualConvention = BusinessDayConvention.Following,
                           BusinessDayConvention paymentConvention = BusinessDayConvention.Following,
                           double redemption = 100,
                           Date issueDate = null,
                           Date stubDate = null,
                           DateGeneration.Rule rule = DateGeneration.Rule.Backward,
                           bool endOfMonth = false,
                           Calendar paymentCalendar = null,
									Period exCouponPeriod = null,
                           Calendar exCouponCalendar = null,
									BusinessDayConvention exCouponConvention = BusinessDayConvention.Unadjusted,
                           bool exCouponEndOfMonth = false)
         : base(settlementDays, paymentCalendar == null ? calendar : paymentCalendar, 
                issueDate) 
      {

         frequency_ = tenor.frequency();
         dayCounter_ = accrualDayCounter;
         maturityDate_     = maturityDate;

         Date firstDate, nextToLastDate;

         switch (rule) 
         {
         
            case DateGeneration.Rule.Backward:
               firstDate = null;
               nextToLastDate = stubDate;
               break;

            case DateGeneration.Rule.Forward:
               firstDate = stubDate;
               nextToLastDate = null;
               break;

            case DateGeneration.Rule.Zero:
            case DateGeneration.Rule.ThirdWednesday:
            case DateGeneration.Rule.Twentieth:
            case DateGeneration.Rule.TwentiethIMM:
               throw new ApplicationException("stub date (" + stubDate + ") not allowed with " + rule + " DateGeneration::Rule");
              
            default:
               throw new ApplicationException("unknown DateGeneration::Rule (" + rule + ")");
         }


         Schedule schedule = new Schedule(startDate, maturityDate_, tenor,
                                          calendar, accrualConvention, accrualConvention,
                                          rule, endOfMonth,
                                          firstDate, nextToLastDate);

            
         cashflows_ = new FixedRateLeg(schedule)
            .withCouponRates(coupons, accrualDayCounter)
				.withExCouponPeriod(exCouponPeriod,
										  exCouponCalendar,
										  exCouponConvention,
										  exCouponEndOfMonth)
            .withPaymentCalendar(calendar_)
            .withNotionals(faceAmount)
            .withPaymentAdjustment(paymentConvention);

         addRedemptionsToCashflows(new List<double>() { redemption });


         if (cashflows().Count == 0)
            throw new ApplicationException("bond with no cashflows!");
         
         if (redemptions_.Count != 1)
            throw new ApplicationException("multiple redemptions created");
      }
示例#39
0
        // constructor
        public VanillaSwap(Type type, double nominal,
                         Schedule fixedSchedule, double fixedRate, DayCounter fixedDayCount,
                         Schedule floatSchedule, IborIndex iborIndex, double spread, DayCounter floatingDayCount,
                         BusinessDayConvention? paymentConvention = null)
            : base(2)
        {
            type_ = type;
             nominal_ = nominal;
             fixedSchedule_ = fixedSchedule;
             fixedRate_ = fixedRate;
             fixedDayCount_ = fixedDayCount;
             floatingSchedule_ = floatSchedule;
             iborIndex_ = iborIndex;
             spread_ = spread;
             floatingDayCount_ = floatingDayCount;

             if (paymentConvention.HasValue)
            paymentConvention_ = paymentConvention.Value;
             else
            paymentConvention_ = floatingSchedule_.businessDayConvention();

             legs_[0] = new FixedRateLeg(fixedSchedule)
                                     .withCouponRates(fixedRate, fixedDayCount)
                                     .withPaymentAdjustment(paymentConvention_)
                                     .withNotionals(nominal);

             legs_[1] = new IborLeg(floatSchedule, iborIndex)
                                     .withPaymentDayCounter(floatingDayCount)
            //.withFixingDays(iborIndex.fixingDays())
                                     .withSpreads(spread)
                                     .withNotionals(nominal)
                                     .withPaymentAdjustment(paymentConvention_);

             foreach (var cf in legs_[1])
            cf.registerWith(update);

             switch (type_)
             {
            case Type.Payer:
               payer_[0] = -1.0;
               payer_[1] = +1.0;
               break;
            case Type.Receiver:
               payer_[0] = +1.0;
               payer_[1] = -1.0;
               break;
            default:
               throw new ApplicationException("Unknown vanilla-swap type");
             }
        }