19#include <ql/experimental/coupons/cmsspreadcoupon.hpp>
20#include <ql/experimental/coupons/strippedcapflooredcoupon.hpp>
21#include <ql/experimental/coupons/swapspreadindex.hpp>
22#include <ql/instruments/vanillaswap.hpp>
23#include <ql/math/solvers1d/brent.hpp>
24#include <ql/pricingengines/swap/discountingswapengine.hpp>
25#include <ql/time/daycounters/actual360.hpp>
41 Real spread, rate1, rate2;
43 std::vector<Real> nominals(1, 1.0);
46 QuantLib::ext::shared_ptr<PricingEngine> swapEngine(
47 new DiscountingSwapEngine(
index1_->discountingTermStructure(),
false));
49 Calendar calendar =
index1_->fixingCalendar();
51 QuantLib::ext::shared_ptr<IborIndex> index =
index1_->iborIndex();
57 Schedule cmsSchedule(startDate, endDate,
cmsTenor_, calendar, index->businessDayConvention(),
58 index->businessDayConvention(), DateGeneration::Forward,
false);
60 Leg cmsLeg = CmsLeg(cmsSchedule,
index1_)
61 .withNotionals(nominals)
62 .withPaymentAdjustment(
index1_->iborIndex()->businessDayConvention())
63 .withPaymentDayCounter(
index1_->iborIndex()->dayCounter())
67 std::vector<Leg> cmsLegs;
68 std::vector<bool> cmsPayers;
69 cmsLegs.push_back(cmsLeg);
70 cmsPayers.push_back(
true);
71 QuantLib::ext::shared_ptr<QuantLib::Swap> s = QuantLib::ext::make_shared<QuantLib::Swap>(cmsLegs, cmsPayers);
72 s->setPricingEngine(swapEngine);
74 rate1 = s->NPV() / (s->legBPS(0) / 1.0e-4);
77 QuantLib::ext::shared_ptr<PricingEngine> swapEngine(
new DiscountingSwapEngine(
index2_->discountingTermStructure(),
false));
79 Calendar calendar =
index2_->fixingCalendar();
81 QuantLib::ext::shared_ptr<IborIndex> index =
index2_->iborIndex();
86 Schedule cmsSchedule(startDate, endDate,
cmsTenor_, calendar, index->businessDayConvention(),
87 index->businessDayConvention(), DateGeneration::Forward,
false);
89 Leg cmsLeg = CmsLeg(cmsSchedule,
index2_)
90 .withNotionals(nominals)
91 .withPaymentAdjustment(
index2_->iborIndex()->businessDayConvention())
92 .withPaymentDayCounter(
index2_->iborIndex()->dayCounter())
96 std::vector<Leg> cmsLegs;
97 std::vector<bool> cmsPayers;
98 cmsLegs.push_back(cmsLeg);
99 cmsPayers.push_back(
true);
100 QuantLib::ext::shared_ptr<QuantLib::Swap> s = QuantLib::ext::make_shared<QuantLib::Swap>(cmsLegs, cmsPayers);
101 s->setPricingEngine(swapEngine);
103 rate2 = s->NPV() / (s->legBPS(0) / 1.0e-4);
105 spread = rate1 - rate2;
109 std::vector<Leg> legs;
110 std::vector<bool> legPayers;
112 QuantLib::ext::shared_ptr<QuantLib::SwapSpreadIndex> spreadIndex = QuantLib::ext::make_shared<QuantLib::SwapSpreadIndex>(
119 DateGeneration::Forward,
false);
120 Leg cmsLeg1 = CmsSpreadLeg(cmsSpreadSchedule, spreadIndex)
121 .withNotionals(nominals)
122 .withSpreads(std::vector<Rate>(1, 0))
127 .withCaps(std::vector<Rate>(1, spread));
129 QuantLib::setCouponPricer(cmsLeg1,
pricer_);
131 Leg cmsLeg1b = StrippedCappedFlooredCouponLeg(cmsLeg1);
133 legs.push_back(cmsLeg1b);
134 legPayers.push_back(
false);
136 cap_ = QuantLib::ext::make_shared<QuantLib::Swap>(legs, legPayers);
137 QuantLib::ext::shared_ptr<PricingEngine> swapEngine2(
new DiscountingSwapEngine(
discountCurve_,
false));
138 cap_->setPricingEngine(swapEngine2);
QuantLib::Real modelValue() const
returns the price of the instrument according to the model
void performCalculations() const override
Handle< YieldTermStructure > discountCurve_
QuantLib::ext::shared_ptr< FloatingRateCouponPricer > pricer_
BusinessDayConvention convention_
QuantLib::ext::shared_ptr< QuantLib::CmsCouponPricer > cmsPricer_
QuantLib::ext::shared_ptr< QuantLib::Swap > cap_
QuantLib::ext::shared_ptr< SwapIndex > index1_
QuantLib::ext::shared_ptr< SwapIndex > index2_