Пример #1
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();
        }
Пример #2
0
        public Swap value()
        {
            Date startDate;

            if (effectiveDate_ != null)
            {
                startDate = effectiveDate_;
            }
            else
            {
                int  fixingDays = iborIndex_.fixingDays();
                Date refDate    = Settings.evaluationDate();
                // if the evaluation date is not a business day
                // then move to the next business day
                refDate = floatCalendar_.adjust(refDate);
                Date spotDate = floatCalendar_.advance(refDate, new Period(fixingDays, TimeUnit.Days));
                startDate = spotDate + forwardStart_;
            }

            Date terminationDate = maturityDate_ == null ? startDate + swapTenor_ : maturityDate_;

            Schedule cmsSchedule = new Schedule(startDate, terminationDate,
                                                cmsTenor_, cmsCalendar_,
                                                cmsConvention_,
                                                cmsTerminationDateConvention_,
                                                cmsRule_, cmsEndOfMonth_,
                                                cmsFirstDate_, cmsNextToLastDate_);

            Schedule floatSchedule = new Schedule(startDate, terminationDate,
                                                  floatTenor_, floatCalendar_,
                                                  floatConvention_,
                                                  floatTerminationDateConvention_,
                                                  floatRule_, floatEndOfMonth_,
                                                  floatFirstDate_, floatNextToLastDate_);

            List <CashFlow> cmsLeg = new CmsLeg(cmsSchedule, swapIndex_)
                                     .withPaymentDayCounter(cmsDayCount_)
                                     .withFixingDays(swapIndex_.fixingDays())
                                     .withGearings(cmsGearing_)
                                     .withSpreads(cmsSpread_)
                                     .withCaps(cmsCap_)
                                     .withFloors(cmsFloor_)
                                     .withNotionals(nominal_)
                                     .withPaymentAdjustment(cmsConvention_);

            if (couponPricer_ != null)
            {
                Utils.setCouponPricer(cmsLeg, couponPricer_);
            }

            double?usedSpread = iborSpread_;

            if (useAtmSpread_)
            {
                Utils.QL_REQUIRE(!iborIndex_.forwardingTermStructure().empty(), () =>
                                 "null term structure set to this instance of " + iborIndex_.name());
                Utils.QL_REQUIRE(!swapIndex_.forwardingTermStructure().empty(), () =>
                                 "null term structure set to this instance of " + swapIndex_.name());
                Utils.QL_REQUIRE(couponPricer_ != null, () => "no CmsCouponPricer set (yet)");
                List <CashFlow> fLeg = new IborLeg(floatSchedule, iborIndex_)
                                       .withPaymentDayCounter(floatDayCount_)
                                       .withFixingDays(iborIndex_.fixingDays())
                                       .withCaps(iborCap_)
                                       .withFloors(iborFloor_)
                                       .withNotionals(nominal_)
                                       .withPaymentAdjustment(floatConvention_);

                if (iborCouponPricer_ != null)
                {
                    Utils.setCouponPricer(fLeg, iborCouponPricer_);
                }

                Swap temp = new Swap(cmsLeg, fLeg);
                temp.setPricingEngine(engine_);

                double?npv = temp.legNPV(0) + temp.legNPV(1);

                usedSpread = -npv / temp.legBPS(1) * 1e-4;
            }
            else
            {
                Utils.QL_REQUIRE(usedSpread.HasValue, () => "null spread set");
            }

            List <CashFlow> floatLeg = new IborLeg(floatSchedule, iborIndex_)
                                       .withSpreads(usedSpread.Value)
                                       .withPaymentDayCounter(floatDayCount_)
                                       .withFixingDays(iborIndex_.fixingDays())
                                       .withGearings(iborGearing_)
                                       .withCaps(iborCap_)
                                       .withFloors(iborFloor_)
                                       .withPaymentAdjustment(floatConvention_)
                                       .withNotionals(nominal_);

            if (iborCouponPricer_ != null)
            {
                Utils.setCouponPricer(floatLeg, iborCouponPricer_);
            }

            Swap swap;

            if (payCms_)
            {
                swap = new Swap(cmsLeg, floatLeg);
            }
            else
            {
                swap = new Swap(floatLeg, cmsLeg);
            }
            swap.setPricingEngine(engine_);
            return(swap);
        }
Пример #3
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());
        }