Example #1
0
        public SwaptionHelper(Period maturity,
                              Period length,
                              Handle <Quote> volatility,
                              IborIndex index,
                              Period fixedLegTenor,
                              DayCounter fixedLegDayCounter,
                              DayCounter floatingLegDayCounter,
                              Handle <YieldTermStructure> termStructure,
                              bool calibrateVolatility /*= false*/)
            : base(volatility, termStructure, calibrateVolatility)
        {
            Calendar calendar   = index.fixingCalendar();
            Period   indexTenor = index.tenor();
            int      fixingDays = index.fixingDays();

            Date exerciseDate = calendar.advance(termStructure.link.referenceDate(),
                                                 maturity,
                                                 index.businessDayConvention());
            Date startDate = calendar.advance(exerciseDate,
                                              fixingDays, TimeUnit.Days,
                                              index.businessDayConvention());
            Date endDate = calendar.advance(startDate, length,
                                            index.businessDayConvention());

            Schedule fixedSchedule = new Schedule(startDate, endDate, fixedLegTenor, calendar,
                                                  index.businessDayConvention(),
                                                  index.businessDayConvention(),
                                                  DateGeneration.Rule.Forward, false);
            Schedule floatSchedule = new Schedule(startDate, endDate, index.tenor(), calendar,
                                                  index.businessDayConvention(),
                                                  index.businessDayConvention(),
                                                  DateGeneration.Rule.Forward, false);

            IPricingEngine swapEngine = new DiscountingSwapEngine(termStructure);

            VanillaSwap temp = new VanillaSwap(VanillaSwap.Type.Receiver, 1.0,
                                               fixedSchedule, 0.0, fixedLegDayCounter,
                                               floatSchedule, index, 0.0, floatingLegDayCounter);

            temp.setPricingEngine(swapEngine);
            exerciseRate_ = temp.fairRate();
            swap_         = new VanillaSwap(VanillaSwap.Type.Receiver, 1.0,
                                            fixedSchedule, exerciseRate_, fixedLegDayCounter,
                                            floatSchedule, index, 0.0, floatingLegDayCounter);
            swap_.setPricingEngine(swapEngine);

            Exercise exercise = new EuropeanExercise(exerciseDate);

            swaption_    = new Swaption(swap_, exercise);
            marketValue_ = blackPrice(volatility_.link.value());
        }
Example #2
0
        public SwaptionHelper(Period maturity,
            Period length,
            Handle<Quote> volatility,
            IborIndex index,
            Period fixedLegTenor,
            DayCounter fixedLegDayCounter,
            DayCounter floatingLegDayCounter,
            Handle<YieldTermStructure> termStructure,
            bool calibrateVolatility /*= false*/)
            : base(volatility,termStructure, calibrateVolatility)
        {
            Calendar calendar = index.fixingCalendar();
            Period indexTenor = index.tenor();
            int fixingDays = index.fixingDays();

            Date exerciseDate   = calendar.advance(termStructure.link.referenceDate(),
                                                maturity,
                                                index.businessDayConvention());
            Date startDate      = calendar.advance(exerciseDate,
                                                fixingDays, TimeUnit.Days,
                                                index.businessDayConvention());
            Date endDate        = calendar.advance(startDate, length,
                                                index.businessDayConvention());

            Schedule fixedSchedule=new Schedule(startDate, endDate, fixedLegTenor, calendar,
                                            index.businessDayConvention(),
                                            index.businessDayConvention(),
                                            DateGeneration.Rule.Forward, false);
            Schedule floatSchedule=new Schedule(startDate, endDate, index.tenor(), calendar,
                                            index.businessDayConvention(),
                                            index.businessDayConvention(),
                                            DateGeneration.Rule.Forward, false);

            IPricingEngine swapEngine=new DiscountingSwapEngine(termStructure);

            VanillaSwap temp=new VanillaSwap(VanillaSwap.Type.Receiver, 1.0,
                                        fixedSchedule, 0.0, fixedLegDayCounter,
                                        floatSchedule, index, 0.0, floatingLegDayCounter);
            temp.setPricingEngine(swapEngine);
            exerciseRate_ = temp.fairRate();
            swap_ = new VanillaSwap(VanillaSwap.Type.Receiver, 1.0,
                            fixedSchedule, exerciseRate_, fixedLegDayCounter,
                            floatSchedule, index, 0.0, floatingLegDayCounter);
            swap_.setPricingEngine(swapEngine);

            Exercise exercise=new EuropeanExercise(exerciseDate);
            swaption_ = new Swaption(swap_, exercise);
            marketValue_ = blackPrice(volatility_.link.value());
        }
Example #3
0
        protected OptionletStripper(CapFloorTermVolSurface termVolSurface, IborIndex iborIndex,
                                    Handle <YieldTermStructure> discount = null,
                                    VolatilityType type = VolatilityType.ShiftedLognormal,
                                    double displacement = 0.0)
        {
            termVolSurface_ = termVolSurface;
            iborIndex_      = iborIndex;
            discount_       = discount ?? new Handle <YieldTermStructure>();
            nStrikes_       = termVolSurface.strikes().Count;
            volatilityType_ = type;
            displacement_   = displacement;


            if (volatilityType_ == VolatilityType.Normal)
            {
                Utils.QL_REQUIRE(displacement_.IsEqual(0.0), () =>
                                 "non-null displacement is not allowed with Normal model");
            }

            termVolSurface.registerWith(update);
            iborIndex_.registerWith(update);
            discount_.registerWith(update);
            Settings.registerWith(update);

            Period indexTenor       = iborIndex_.tenor();
            Period maxCapFloorTenor = termVolSurface.optionTenors().Last();

            // optionlet tenors and capFloor lengths
            optionletTenors_.Add(indexTenor);
            capFloorLengths_.Add(optionletTenors_.Last() + indexTenor);
            Utils.QL_REQUIRE(maxCapFloorTenor >= capFloorLengths_.Last(), () =>
                             "too short (" + maxCapFloorTenor + ") capfloor term vol termVolSurface");
            Period nextCapFloorLength = capFloorLengths_.Last() + indexTenor;

            while (nextCapFloorLength <= maxCapFloorTenor)
            {
                optionletTenors_.Add(capFloorLengths_.Last());
                capFloorLengths_.Add(nextCapFloorLength);
                nextCapFloorLength += indexTenor;
            }
            nOptionletTenors_ = optionletTenors_.Count;

            optionletVolatilities_ = new InitializedList <List <double> >(nOptionletTenors_);
            for (int x = 0; x < nOptionletTenors_; x++)
            {
                optionletVolatilities_[x] = new InitializedList <double>(nStrikes_);
            }
            optionletStrikes_ = new InitializedList <List <double> >(nOptionletTenors_);
            for (int x = 0; x < nOptionletTenors_; x++)
            {
                optionletStrikes_[x] = new List <double>(termVolSurface.strikes());
            }

            optionletDates_          = new InitializedList <Date>(nOptionletTenors_);
            optionletTimes_          = new InitializedList <double>(nOptionletTenors_);
            atmOptionletRate_        = new InitializedList <double>(nOptionletTenors_);
            optionletPaymentDates_   = new InitializedList <Date>(nOptionletTenors_);
            optionletAccrualPeriods_ = new InitializedList <double>(nOptionletTenors_);
        }
Example #4
0
 public DepositRateHelper(double rate, IborIndex i)
     : base(rate)
 {
     iborIndex_ = new IborIndex("no-fix", // never take fixing into account
                                i.tenor(), i.fixingDays(), new Currency(),
                                i.fixingCalendar(), i.businessDayConvention(),
                                i.endOfMonth(), i.dayCounter(), termStructureHandle_);
     initializeDates();
 }
Example #5
0
 public DepositRateHelper(double rate, IborIndex i)
     : base(rate)
 {
     iborIndex_ = new IborIndex("no-fix", // never take fixing into account
                                i.tenor(), i.fixingDays(), new Currency(),
                                i.fixingCalendar(), i.businessDayConvention(),
                                i.endOfMonth(), i.dayCounter(), termStructureHandle_);
     initializeDates();
 }
Example #6
0
        public List <CashFlow> cashFlows(double amount)
        {
            Date refDate = index_.forwardingTermStructure().link.referenceDate();

            Schedule schedule = new Schedule(refDate,
                                             refDate + new Period(index_.tenor().length() * size_,
                                                                  index_.tenor().units()),
                                             index_.tenor(), index_.fixingCalendar(),
                                             index_.businessDayConvention(),
                                             index_.businessDayConvention(),
                                             DateGeneration.Rule.Forward, false);

            IborLeg cashflows = (IborLeg) new IborLeg(schedule, index_)
                                .withFixingDays(index_.fixingDays())
                                .withPaymentDayCounter(index_.dayCounter())
                                .withNotionals(amount)
                                .withPaymentAdjustment(index_.businessDayConvention());

            return(cashflows.value());
        }
Example #7
0
        public FraRateHelper(double rate, int monthsToStart, IborIndex i) : base(rate)
        {
            periodToStart_ = new Period(monthsToStart, TimeUnit.Months);

            iborIndex_ = new IborIndex("no-fix",  // never take fixing into account
                                       i.tenor(), i.fixingDays(), new Currency(),
                                       i.fixingCalendar(), i.businessDayConvention(),
                                       i.endOfMonth(), i.dayCounter(), termStructureHandle_);

            initializeDates();
        }
Example #8
0
        public FRARateHelper(double rate, int monthsToStart, IborIndex i)
            : base(rate)
        {
            periodToStart_ = new Period(monthsToStart, TimeUnit.Months);

            iborIndex_ = new IborIndex("no-fix",  // never take fixing into account
                                       i.tenor(), i.fixingDays(), new Currency(),
                                       i.fixingCalendar(), i.businessDayConvention(),
                                       i.endOfMonth(), i.dayCounter(), termStructureHandle_);

            initializeDates();
        }
Example #9
0
        public FuturesRateHelper(double price, Date immDate, IborIndex i, double convAdj)
            : base(price)
        {
            convAdj_ = new Handle<Quote>(new SimpleQuote(convAdj));

            if (!IMM.isIMMdate(immDate, false)) throw new ArgumentException(immDate + "is not a valid IMM date");
            earliestDate_ = immDate;

            Calendar cal = i.fixingCalendar();
            latestDate_ = cal.advance(immDate, i.tenor(), i.businessDayConvention());
            yearFraction_ = i.dayCounter().yearFraction(earliestDate_, latestDate_);
        }
Example #10
0
        public FuturesRateHelper(double price, Date immDate, IborIndex i, double convAdj)
            : base(price)
        {
            convAdj_ = new Handle <Quote>(new SimpleQuote(convAdj));

            if (!IMM.isIMMdate(immDate, false))
            {
                throw new ArgumentException(immDate + "is not a valid IMM date");
            }
            earliestDate_ = immDate;

            Calendar cal = i.fixingCalendar();

            latestDate_   = cal.advance(immDate, i.tenor(), i.businessDayConvention());
            yearFraction_ = i.dayCounter().yearFraction(earliestDate_, latestDate_);
        }
Example #11
0
 public MakeCms(Period swapTenor,
                SwapIndex swapIndex,
                IborIndex iborIndex,
                double iborSpread   = 0.0,
                Period forwardStart = null,
                Date maturityDate   = null)
 {
     swapTenor_     = swapTenor;
     swapIndex_     = swapIndex;
     iborIndex_     = iborIndex;
     iborSpread_    = iborSpread;
     iborCap_       = null;
     iborFloor_     = null;
     useAtmSpread_  = false;
     forwardStart_  = forwardStart ?? new Period(0, TimeUnit.Days);
     cmsSpread_     = 0.0;
     cmsGearing_    = 1.0;
     cmsCap_        = null;
     cmsFloor_      = null;
     effectiveDate_ = null;
     cmsCalendar_   = swapIndex.fixingCalendar();
     floatCalendar_ = iborIndex.fixingCalendar();
     payCms_        = true;
     nominal_       = 1.0;
     maturityDate_  = maturityDate;
     cmsTenor_      = new Period(3, TimeUnit.Months);
     floatTenor_    = iborIndex.tenor();
     cmsConvention_ = BusinessDayConvention.ModifiedFollowing;
     cmsTerminationDateConvention_ = BusinessDayConvention.ModifiedFollowing;
     floatConvention_ = iborIndex.businessDayConvention();
     floatTerminationDateConvention_ = iborIndex.businessDayConvention();
     cmsRule_             = DateGeneration.Rule.Backward;
     floatRule_           = DateGeneration.Rule.Backward;
     cmsEndOfMonth_       = false;
     floatEndOfMonth_     = false;
     cmsFirstDate_        = null;
     cmsNextToLastDate_   = null;
     floatFirstDate_      = null;
     floatNextToLastDate_ = null;
     cmsDayCount_         = new Actual360();
     floatDayCount_       = iborIndex.dayCounter();
     engine_ = new DiscountingSwapEngine(swapIndex.forwardingTermStructure());
 }
Example #12
0
        public MakeVanillaSwap(Period swapTenor, IborIndex index, double? fixedRate, Period forwardStart) {
            swapTenor_ = swapTenor;
            iborIndex_ = index;
            fixedRate_ = fixedRate;
            forwardStart_ = forwardStart;
            effectiveDate_ = null;
            fixedCalendar_ = floatCalendar_ = index.fixingCalendar();
            
            type_ = VanillaSwap.Type.Payer;
            nominal_ = 1.0;
            fixedTenor_ = new Period(1, TimeUnit.Years);
            floatTenor_ = index.tenor();
            fixedConvention_ = fixedTerminationDateConvention_ = BusinessDayConvention.ModifiedFollowing;
            floatConvention_ = floatTerminationDateConvention_ = index.businessDayConvention();
            fixedRule_ = floatRule_ = DateGeneration.Rule.Backward;
            fixedEndOfMonth_ = floatEndOfMonth_ = false;
            fixedFirstDate_ = fixedNextToLastDate_ = floatFirstDate_ = floatNextToLastDate_ = null;
            floatSpread_ = 0.0;
            fixedDayCount_ = new Thirty360(Thirty360.Thirty360Convention.BondBasis);
            floatDayCount_ = index.dayCounter();

            engine_ = new DiscountingSwapEngine(index.forwardingTermStructure());
        }
Example #13
0
        protected override void initializeDates()
        {
            earliestDate_ = calendar_.advance(evaluationDate_, new Period(settlementDays_, TimeUnit.Days),
                                              BusinessDayConvention.Following);

            Date maturity = earliestDate_ + tenor_;

            // dummy BMA index with curve/swap arguments
            BMAIndex clonedIndex = new BMAIndex(termStructureHandle_);

            Schedule bmaSchedule = new MakeSchedule().from(earliestDate_).to(maturity)
                                   .withTenor(bmaPeriod_)
                                   .withCalendar(bmaIndex_.fixingCalendar())
                                   .withConvention(bmaConvention_)
                                   .backwards()
                                   .value();

            Schedule liborSchedule = new MakeSchedule().from(earliestDate_).to(maturity)
                                     .withTenor(iborIndex_.tenor())
                                     .withCalendar(iborIndex_.fixingCalendar())
                                     .withConvention(iborIndex_.businessDayConvention())
                                     .endOfMonth(iborIndex_.endOfMonth())
                                     .backwards()
                                     .value();

            swap_ = new BMASwap(BMASwap.Type.Payer, 100.0, liborSchedule, 0.75,             // arbitrary
                                0.0, iborIndex_, iborIndex_.dayCounter(), bmaSchedule, clonedIndex, bmaDayCount_);
            swap_.setPricingEngine(new DiscountingSwapEngine(iborIndex_.forwardingTermStructure()));

            Date d             = calendar_.adjust(swap_.maturityDate(), BusinessDayConvention.Following);
            int  w             = d.weekday();
            Date nextWednesday = (w >= 4) ? d + new Period((11 - w), TimeUnit.Days) :
                                 d + new Period((4 - w), TimeUnit.Days);

            latestDate_ = clonedIndex.valueDate(clonedIndex.fixingCalendar().adjust(nextWednesday));
        }
Example #14
0
        public MakeBasisSwap(Period swapTenor, IborIndex index1, IborIndex index2, Period forwardStart)
        {
            swapTenor_      = swapTenor;
            iborIndex1_     = index1;
            iborIndex2_     = index2;
            forwardStart_   = forwardStart;
            effectiveDate_  = null;
            float1Calendar_ = float2Calendar_ = index1.fixingCalendar();

            type_             = BasisSwap.Type.Payer;
            nominal_          = 1.0;
            float1Tenor_      = index1.tenor();
            float2Tenor_      = index2.tenor();
            float1Convention_ = float1TerminationDateConvention_ = index1.businessDayConvention();
            float2Convention_ = float2TerminationDateConvention_ = index2.businessDayConvention();
            float1Rule_       = float2Rule_ = DateGeneration.Rule.Backward;
            float1EndOfMonth_ = float2EndOfMonth_ = false;
            float1FirstDate_  = float1NextToLastDate_ = float2FirstDate_ = float2NextToLastDate_ = null;
            float1Spread_     = float2Spread_ = 0.0;
            float1DayCount_   = index1.dayCounter();
            float2DayCount_   = index2.dayCounter();

            engine_ = new DiscountingBasisSwapEngine(index1.forwardingTermStructure(), index2.forwardingTermStructure());
        }
Example #15
0
        public MakeBasisSwap(Period swapTenor, IborIndex index1, IborIndex index2, Period forwardStart)
        {
            swapTenor_ = swapTenor;
             iborIndex1_ = index1;
             iborIndex2_ = index2;
             forwardStart_ = forwardStart;
             effectiveDate_ = null;
             float1Calendar_ = float2Calendar_ = index1.fixingCalendar();

             type_ = BasisSwap.Type.Payer;
             nominal_ = 1.0;
             float1Tenor_ = index1.tenor();
             float2Tenor_ = index2.tenor();
             float1Convention_ = float1TerminationDateConvention_ = index1.businessDayConvention();
             float2Convention_ = float2TerminationDateConvention_ = index2.businessDayConvention();
             float1Rule_ = float2Rule_ = DateGeneration.Rule.Backward;
             float1EndOfMonth_ = float2EndOfMonth_ = false;
             float1FirstDate_ = float1NextToLastDate_ = float2FirstDate_ = float2NextToLastDate_ = null;
             float1Spread_ = float2Spread_ = 0.0;
             float1DayCount_ = index1.dayCounter();
             float2DayCount_ = index2.dayCounter();

             engine_ = new DiscountingBasisSwapEngine(index1.forwardingTermStructure(), index2.forwardingTermStructure());
        }
Example #16
0
        public MakeVanillaSwap(Period swapTenor, IborIndex index, double?fixedRate, Period forwardStart)
        {
            swapTenor_     = swapTenor;
            iborIndex_     = index;
            fixedRate_     = fixedRate;
            forwardStart_  = forwardStart;
            effectiveDate_ = null;
            fixedCalendar_ = floatCalendar_ = index.fixingCalendar();

            type_            = VanillaSwap.Type.Payer;
            nominal_         = 1.0;
            fixedTenor_      = new Period(1, TimeUnit.Years);
            floatTenor_      = index.tenor();
            fixedConvention_ = fixedTerminationDateConvention_ = BusinessDayConvention.ModifiedFollowing;
            floatConvention_ = floatTerminationDateConvention_ = index.businessDayConvention();
            fixedRule_       = floatRule_ = DateGeneration.Rule.Backward;
            fixedEndOfMonth_ = floatEndOfMonth_ = false;
            fixedFirstDate_  = fixedNextToLastDate_ = floatFirstDate_ = floatNextToLastDate_ = null;
            floatSpread_     = 0.0;
            fixedDayCount_   = new Thirty360(Thirty360.Thirty360Convention.BondBasis);
            floatDayCount_   = index.dayCounter();

            engine_ = new DiscountingSwapEngine(index.forwardingTermStructure());
        }
Example #17
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());
        }
Example #18
0
        public AssetSwap(bool parAssetSwap,
                       Bond bond,
                       double bondCleanPrice,
                       double nonParRepayment,
                       double gearing,
                       IborIndex iborIndex,
                       double spread = 0.0,
                       DayCounter floatingDayCount = null,
                       Date dealMaturity = null,
                       bool payBondCoupon = false)
            : base(2)
        {
            bond_ = bond;
             bondCleanPrice_ = bondCleanPrice;
             nonParRepayment_ = nonParRepayment;
             spread_ = spread;
             parSwap_ = parAssetSwap;

             Schedule tempSch = new Schedule(bond_.settlementDate(),
                                         bond_.maturityDate(),
                                         iborIndex.tenor(),
                                         iborIndex.fixingCalendar(),
                                         iborIndex.businessDayConvention(),
                                         iborIndex.businessDayConvention(),
                                         DateGeneration.Rule.Backward,
                                         false); // endOfMonth

             if (dealMaturity == null)
            dealMaturity = bond_.maturityDate();

             Utils.QL_REQUIRE( dealMaturity <= tempSch.dates().Last(), () =>
                     "deal maturity " + dealMaturity +
                     " cannot be later than (adjusted) bond maturity " +
                     tempSch.dates().Last());
             Utils.QL_REQUIRE( dealMaturity > tempSch.dates()[0], () =>
                     "deal maturity " + dealMaturity +
                     " must be later than swap start date " +
                     tempSch.dates()[0]);

             // the following might become an input parameter
             BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following;

             Date finalDate = tempSch.calendar().adjust(dealMaturity, paymentAdjustment);
             Schedule schedule = tempSch.until(finalDate);

             // bondCleanPrice must be the (forward) clean price
             // at the floating schedule start date
             upfrontDate_ = schedule.startDate();
             double dirtyPrice = bondCleanPrice_ +
                             bond_.accruedAmount(upfrontDate_);

             double notional = bond_.notional(upfrontDate_);
             /* In the market asset swap, the bond is purchased in return for
            payment of the full price. The notional of the floating leg is
            then scaled by the full price. */
             if (!parSwap_)
            notional *= dirtyPrice / 100.0;

             if (floatingDayCount == null)
            legs_[1] = new IborLeg(schedule, iborIndex)
                  .withSpreads(spread)
                  .withGearings(gearing)
                  .withNotionals(notional)
                  .withPaymentAdjustment(paymentAdjustment);
             else
            legs_[1] = new IborLeg(schedule, iborIndex)
                  .withSpreads(spread)
                  .withGearings(gearing)
                  .withPaymentDayCounter(floatingDayCount)
                  .withNotionals(notional)
                  .withPaymentAdjustment(paymentAdjustment);

             foreach (CashFlow c in legs_[1])
            c.registerWith(update);

             List<CashFlow> bondLeg = bond_.cashflows();
             // skip bond redemption
             int i;
             for (i = 0; i < bondLeg.Count && bondLeg[i].date() <= dealMaturity; ++i)
             {
            // whatever might be the choice for the discounting engine
            // bond flows on upfrontDate_ must be discarded
            bool upfrontDateBondFlows = false;
            if (!bondLeg[i].hasOccurred(upfrontDate_, upfrontDateBondFlows))
               legs_[0].Add(bondLeg[i]);
             }
             // if the first skipped cashflow is not the redemption
             // and it is a coupon then add the accrued coupon
             if (i < bondLeg.Count - 1)
             {
            Coupon c = bondLeg[i] as Coupon;
            if (c != null)
            {
               CashFlow accruedCoupon = new SimpleCashFlow(c.accruedAmount(dealMaturity), finalDate);
               legs_[0].Add(accruedCoupon);
            }
             }
             // add the nonParRepayment_
             CashFlow nonParRepaymentFlow = new SimpleCashFlow(nonParRepayment_, finalDate);
             legs_[0].Add(nonParRepaymentFlow);

             Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg to start with" );

             // special flows
             if (parSwap_)
             {
            // upfront on the floating leg
            double upfront = (dirtyPrice - 100.0) / 100.0 * notional;
            CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_);
            legs_[1].Insert(0, upfrontCashFlow);
            // backpayment on the floating leg
            // (accounts for non-par redemption, if any)
            double backPayment = notional;
            CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate);
            legs_[1].Add(backPaymentCashFlow);
             }
             else
             {
            // final notional exchange
            CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate);
            legs_[1].Add(finalCashFlow);
             }

             Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg" );

             foreach (CashFlow c in legs_[0])
            c.registerWith(update);

             if (payBondCoupon)
             {
            payer_[0] = -1.0;
            payer_[1] = +1.0;
             }
             else
             {
            payer_[0] = +1.0;
            payer_[1] = -1.0;
             }
        }
Example #19
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());
        }
Example #20
0
        public AssetSwap(bool payBondCoupon,
                       Bond bond,
                       double bondCleanPrice,
                       IborIndex iborIndex,
                       double spread,
                       Schedule floatSchedule = null,
                       DayCounter floatingDayCount = null,
                       bool parAssetSwap = true)
            : base(2)
        {
            bond_ = bond;
             bondCleanPrice_ = bondCleanPrice;
             nonParRepayment_ = 100;
             spread_ = spread;
             parSwap_ = parAssetSwap;

             Schedule schedule = floatSchedule;
             if (floatSchedule == null)
            schedule = new Schedule(bond_.settlementDate(),
                                    bond_.maturityDate(),
                                    iborIndex.tenor(),
                                    iborIndex.fixingCalendar(),
                                    iborIndex.businessDayConvention(),
                                    iborIndex.businessDayConvention(),
                                    DateGeneration.Rule.Backward,
                                    false); // endOfMonth

             // the following might become an input parameter
             BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following;

             Date finalDate = schedule.calendar().adjust(schedule.endDate(), paymentAdjustment);
             Date adjBondMaturityDate = schedule.calendar().adjust(bond_.maturityDate(), paymentAdjustment);

             Utils.QL_REQUIRE( finalDate == adjBondMaturityDate, () =>
                          "adjusted schedule end date (" +
                          finalDate +
                          ") must be equal to adjusted bond maturity date (" +
                          adjBondMaturityDate + ")");

             // bondCleanPrice must be the (forward) clean price
             // at the floating schedule start date
             upfrontDate_ = schedule.startDate();
             double dirtyPrice = bondCleanPrice_ +
                             bond_.accruedAmount(upfrontDate_);

             double notional = bond_.notional(upfrontDate_);
             /* In the market asset swap, the bond is purchased in return for
            payment of the full price. The notional of the floating leg is
            then scaled by the full price. */
             if (!parSwap_)
            notional *= dirtyPrice / 100.0;

             if (floatingDayCount == null)
            legs_[1] = new IborLeg(schedule, iborIndex)
                .withSpreads(spread)
                .withNotionals(notional)
                .withPaymentAdjustment(paymentAdjustment);
             else
            legs_[1] = new IborLeg(schedule, iborIndex)
                .withSpreads(spread)
                .withPaymentDayCounter(floatingDayCount)
                .withNotionals(notional)
                .withPaymentAdjustment(paymentAdjustment);

             foreach (CashFlow c in legs_[1])
            c.registerWith(update);

             List<CashFlow> bondLeg = bond_.cashflows();
             foreach (CashFlow c in bondLeg)
             {
            // whatever might be the choice for the discounting engine
            // bond flows on upfrontDate_ must be discarded
            bool upfrontDateBondFlows = false;
            if (!(c.hasOccurred(upfrontDate_, upfrontDateBondFlows)))
               legs_[0].Add(c);
             }

             Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg to start with" );

             // special flows
             if (parSwap_)
             {
            // upfront on the floating leg
            double upfront = (dirtyPrice - 100.0) / 100.0 * notional;
            CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_);
            legs_[1].Insert(0, upfrontCashFlow);
            // backpayment on the floating leg
            // (accounts for non-par redemption, if any)
            double backPayment = notional;
            CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate);
            legs_[1].Add(backPaymentCashFlow);
             }
             else
             {
            // final notional exchange
            CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate);
            legs_[1].Add(finalCashFlow);
             }

             Utils.QL_REQUIRE( !legs_[0].empty(), () => "empty bond leg" );

             foreach (CashFlow c in legs_[0])
            c.registerWith(update);

             if (payBondCoupon)
             {
            payer_[0] = -1.0;
            payer_[1] = +1.0;
             }
             else
             {
            payer_[0] = +1.0;
            payer_[1] = -1.0;
             }
        }
Example #21
0
            public CommonVars()
            {
                settlementDays = 2;
                nominal = 1000000.0;
                fixedConvention = BusinessDayConvention.Unadjusted;

                fixedFrequency = Frequency.Annual;
                fixedDayCount = new Thirty360();

                index =new Euribor6M(termStructure);
                floatingConvention = index.businessDayConvention();
                floatingTenor = index.tenor();
                calendar = index.fixingCalendar();
                today = calendar.adjust(Date.Today);
                Settings.setEvaluationDate(today);
                settlement = calendar.advance(today, settlementDays, TimeUnit.Days);

                termStructure.linkTo(Utilities.flatRate(settlement, 0.05, new Actual365Fixed()));
            }
Example #22
0
        public AssetSwap(bool payBondCoupon,
                         Bond bond,
                         double bondCleanPrice,
                         IborIndex iborIndex,
                         double spread,
                         Schedule floatSchedule      = null,
                         DayCounter floatingDayCount = null,
                         bool parAssetSwap           = true)
            : base(2)
        {
            bond_            = bond;
            bondCleanPrice_  = bondCleanPrice;
            nonParRepayment_ = 100;
            spread_          = spread;
            parSwap_         = parAssetSwap;

            Schedule schedule = floatSchedule;

            if (floatSchedule == null)
            {
                schedule = new Schedule(bond_.settlementDate(),
                                        bond_.maturityDate(),
                                        iborIndex.tenor(),
                                        iborIndex.fixingCalendar(),
                                        iborIndex.businessDayConvention(),
                                        iborIndex.businessDayConvention(),
                                        DateGeneration.Rule.Backward,
                                        false); // endOfMonth
            }
            // the following might become an input parameter
            BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following;

            Date finalDate           = schedule.calendar().adjust(schedule.endDate(), paymentAdjustment);
            Date adjBondMaturityDate = schedule.calendar().adjust(bond_.maturityDate(), paymentAdjustment);

            Utils.QL_REQUIRE(finalDate == adjBondMaturityDate, () =>
                             "adjusted schedule end date (" +
                             finalDate +
                             ") must be equal to adjusted bond maturity date (" +
                             adjBondMaturityDate + ")");

            // bondCleanPrice must be the (forward) clean price
            // at the floating schedule start date
            upfrontDate_ = schedule.startDate();
            double dirtyPrice = bondCleanPrice_ +
                                bond_.accruedAmount(upfrontDate_);

            double notional = bond_.notional(upfrontDate_);

            /* In the market asset swap, the bond is purchased in return for
             * payment of the full price. The notional of the floating leg is
             * then scaled by the full price. */
            if (!parSwap_)
            {
                notional *= dirtyPrice / 100.0;
            }

            if (floatingDayCount == null)
            {
                legs_[1] = new IborLeg(schedule, iborIndex)
                           .withSpreads(spread)
                           .withNotionals(notional)
                           .withPaymentAdjustment(paymentAdjustment);
            }
            else
            {
                legs_[1] = new IborLeg(schedule, iborIndex)
                           .withSpreads(spread)
                           .withPaymentDayCounter(floatingDayCount)
                           .withNotionals(notional)
                           .withPaymentAdjustment(paymentAdjustment);
            }

            foreach (CashFlow c in legs_[1])
            {
                c.registerWith(update);
            }

            List <CashFlow> bondLeg = bond_.cashflows();

            foreach (CashFlow c in bondLeg)
            {
                // whatever might be the choice for the discounting engine
                // bond flows on upfrontDate_ must be discarded
                bool upfrontDateBondFlows = false;
                if (!(c.hasOccurred(upfrontDate_, upfrontDateBondFlows)))
                {
                    legs_[0].Add(c);
                }
            }

            Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg to start with");

            // special flows
            if (parSwap_)
            {
                // upfront on the floating leg
                double   upfront         = (dirtyPrice - 100.0) / 100.0 * notional;
                CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_);
                legs_[1].Insert(0, upfrontCashFlow);
                // backpayment on the floating leg
                // (accounts for non-par redemption, if any)
                double   backPayment         = notional;
                CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate);
                legs_[1].Add(backPaymentCashFlow);
            }
            else
            {
                // final notional exchange
                CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate);
                legs_[1].Add(finalCashFlow);
            }

            Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg");

            foreach (CashFlow c in legs_[0])
            {
                c.registerWith(update);
            }

            if (payBondCoupon)
            {
                payer_[0] = -1.0;
                payer_[1] = +1.0;
            }
            else
            {
                payer_[0] = +1.0;
                payer_[1] = -1.0;
            }
        }
Example #23
0
        // calculating swaption volatility matrix using
        // Rebonatos approx. formula. Be aware that this
        // matrix is valid only for regular fixings and
        // assumes that the fix and floating leg have the
        // same frequency
        public SwaptionVolatilityMatrix getSwaptionVolatilityMatrix()
        {
            if (swaptionVola != null)
            {
                return(swaptionVola);
            }

            IborIndex index = process_.index();
            Date      today = process_.fixingDates()[0];

            int    size         = process_.size() / 2;
            Matrix volatilities = new Matrix(size, size);

            List <Date> exercises = new InitializedList <Date>(size);

            for (int i = 0; i < size; ++i)
            {
                exercises[i] = process_.fixingDates()[i + 1];
            }

            List <Period> lengths = new InitializedList <Period>(size);

            for (int i = 0; i < size; ++i)
            {
                lengths[i] = (i + 1) * index.tenor();
            }

            Vector f = process_.initialValues();

            for (int k = 0; k < size; ++k)
            {
                int    alpha   = k;
                double t_alpha = process_.fixingTimes()[alpha + 1];

                Matrix var = new Matrix(size, size);
                for (int i = alpha + 1; i <= k + size; ++i)
                {
                    for (int j = i; j <= k + size; ++j)
                    {
                        var[i - alpha - 1, j - alpha - 1] = var[j - alpha - 1, i - alpha - 1] =
                            covarProxy_.integratedCovariance(i, j, t_alpha, null);
                    }
                }

                for (int l = 1; l <= size; ++l)
                {
                    int    beta = l + k;
                    Vector w    = w_0(alpha, beta);

                    double sum = 0.0;
                    for (int i = alpha + 1; i <= beta; ++i)
                    {
                        for (int j = alpha + 1; j <= beta; ++j)
                        {
                            sum += w[i] * w[j] * f[i] * f[j] * var[i - alpha - 1, j - alpha - 1];
                        }
                    }
                    volatilities[k, l - 1] =
                        Math.Sqrt(sum / t_alpha) / S_0(alpha, beta);
                }
            }

            return(swaptionVola = new SwaptionVolatilityMatrix(today, exercises, lengths,
                                                               volatilities, index.dayCounter()));
        }
Example #24
0
        public AssetSwap(bool parAssetSwap,
                         Bond bond,
                         double bondCleanPrice,
                         double nonParRepayment,
                         double gearing,
                         IborIndex iborIndex,
                         double spread = 0.0,
                         DayCounter floatingDayCount = null,
                         Date dealMaturity           = null,
                         bool payBondCoupon          = false)
            : base(2)
        {
            bond_            = bond;
            bondCleanPrice_  = bondCleanPrice;
            nonParRepayment_ = nonParRepayment;
            spread_          = spread;
            parSwap_         = parAssetSwap;

            Schedule tempSch = new Schedule(bond_.settlementDate(),
                                            bond_.maturityDate(),
                                            iborIndex.tenor(),
                                            iborIndex.fixingCalendar(),
                                            iborIndex.businessDayConvention(),
                                            iborIndex.businessDayConvention(),
                                            DateGeneration.Rule.Backward,
                                            false); // endOfMonth

            if (dealMaturity == null)
            {
                dealMaturity = bond_.maturityDate();
            }

            Utils.QL_REQUIRE(dealMaturity <= tempSch.dates().Last(), () =>
                             "deal maturity " + dealMaturity +
                             " cannot be later than (adjusted) bond maturity " +
                             tempSch.dates().Last());
            Utils.QL_REQUIRE(dealMaturity > tempSch.dates()[0], () =>
                             "deal maturity " + dealMaturity +
                             " must be later than swap start date " +
                             tempSch.dates()[0]);

            // the following might become an input parameter
            BusinessDayConvention paymentAdjustment = BusinessDayConvention.Following;

            Date     finalDate = tempSch.calendar().adjust(dealMaturity, paymentAdjustment);
            Schedule schedule  = tempSch.until(finalDate);

            // bondCleanPrice must be the (forward) clean price
            // at the floating schedule start date
            upfrontDate_ = schedule.startDate();
            double dirtyPrice = bondCleanPrice_ +
                                bond_.accruedAmount(upfrontDate_);

            double notional = bond_.notional(upfrontDate_);

            /* In the market asset swap, the bond is purchased in return for
             * payment of the full price. The notional of the floating leg is
             * then scaled by the full price. */
            if (!parSwap_)
            {
                notional *= dirtyPrice / 100.0;
            }

            if (floatingDayCount == null)
            {
                legs_[1] = new IborLeg(schedule, iborIndex)
                           .withSpreads(spread)
                           .withGearings(gearing)
                           .withNotionals(notional)
                           .withPaymentAdjustment(paymentAdjustment);
            }
            else
            {
                legs_[1] = new IborLeg(schedule, iborIndex)
                           .withSpreads(spread)
                           .withGearings(gearing)
                           .withPaymentDayCounter(floatingDayCount)
                           .withNotionals(notional)
                           .withPaymentAdjustment(paymentAdjustment);
            }

            foreach (CashFlow c in legs_[1])
            {
                c.registerWith(update);
            }


            List <CashFlow> bondLeg = bond_.cashflows();
            // skip bond redemption
            int i;

            for (i = 0; i < bondLeg.Count && bondLeg[i].date() <= dealMaturity; ++i)
            {
                // whatever might be the choice for the discounting engine
                // bond flows on upfrontDate_ must be discarded
                bool upfrontDateBondFlows = false;
                if (!bondLeg[i].hasOccurred(upfrontDate_, upfrontDateBondFlows))
                {
                    legs_[0].Add(bondLeg[i]);
                }
            }
            // if the first skipped cashflow is not the redemption
            // and it is a coupon then add the accrued coupon
            if (i < bondLeg.Count - 1)
            {
                Coupon c = bondLeg[i] as Coupon;
                if (c != null)
                {
                    CashFlow accruedCoupon = new SimpleCashFlow(c.accruedAmount(dealMaturity), finalDate);
                    legs_[0].Add(accruedCoupon);
                }
            }
            // add the nonParRepayment_
            CashFlow nonParRepaymentFlow = new SimpleCashFlow(nonParRepayment_, finalDate);

            legs_[0].Add(nonParRepaymentFlow);

            Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg to start with");

            // special flows
            if (parSwap_)
            {
                // upfront on the floating leg
                double   upfront         = (dirtyPrice - 100.0) / 100.0 * notional;
                CashFlow upfrontCashFlow = new SimpleCashFlow(upfront, upfrontDate_);
                legs_[1].Insert(0, upfrontCashFlow);
                // backpayment on the floating leg
                // (accounts for non-par redemption, if any)
                double   backPayment         = notional;
                CashFlow backPaymentCashFlow = new SimpleCashFlow(backPayment, finalDate);
                legs_[1].Add(backPaymentCashFlow);
            }
            else
            {
                // final notional exchange
                CashFlow finalCashFlow = new SimpleCashFlow(notional, finalDate);
                legs_[1].Add(finalCashFlow);
            }

            Utils.QL_REQUIRE(!legs_[0].empty(), () => "empty bond leg");

            foreach (CashFlow c in legs_[0])
            {
                c.registerWith(update);
            }

            if (payBondCoupon)
            {
                payer_[0] = -1.0;
                payer_[1] = +1.0;
            }
            else
            {
                payer_[0] = +1.0;
                payer_[1] = -1.0;
            }
        }