35 {
36
37 Date startDate;
38 Date endDate;
39
40
41 Real spread, rate1, rate2;
42
43 std::vector<Real> nominals(1, 1.0);
44
45 {
46 QuantLib::ext::shared_ptr<PricingEngine> swapEngine(
47 new DiscountingSwapEngine(
index1_->discountingTermStructure(),
false));
48
49 Calendar calendar =
index1_->fixingCalendar();
50
51 QuantLib::ext::shared_ptr<IborIndex> index =
index1_->iborIndex();
52
53
56
57 Schedule cmsSchedule(startDate, endDate,
cmsTenor_, calendar, index->businessDayConvention(),
58 index->businessDayConvention(), DateGeneration::Forward, false);
59
60 Leg cmsLeg = CmsLeg(cmsSchedule,
index1_)
61 .withNotionals(nominals)
62 .withPaymentAdjustment(
index1_->iborIndex()->businessDayConvention())
63 .withPaymentDayCounter(
index1_->iborIndex()->dayCounter())
66
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);
73
74 rate1 = s->NPV() / (s->legBPS(0) / 1.0e-4);
75 }
76
77 QuantLib::ext::shared_ptr<PricingEngine> swapEngine(
new DiscountingSwapEngine(
index2_->discountingTermStructure(),
false));
78
79 Calendar calendar =
index2_->fixingCalendar();
80
81 QuantLib::ext::shared_ptr<IborIndex> index =
index2_->iborIndex();
82
85
86 Schedule cmsSchedule(startDate, endDate,
cmsTenor_, calendar, index->businessDayConvention(),
87 index->businessDayConvention(), DateGeneration::Forward, false);
88
89 Leg cmsLeg = CmsLeg(cmsSchedule,
index2_)
90 .withNotionals(nominals)
91 .withPaymentAdjustment(
index2_->iborIndex()->businessDayConvention())
92 .withPaymentDayCounter(
index2_->iborIndex()->dayCounter())
95
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);
102
103 rate2 = s->NPV() / (s->legBPS(0) / 1.0e-4);
104
105 spread = rate1 - rate2;
106
107
108
109 std::vector<Leg> legs;
110 std::vector<bool> legPayers;
111
112 QuantLib::ext::shared_ptr<QuantLib::SwapSpreadIndex> spreadIndex = QuantLib::ext::make_shared<QuantLib::SwapSpreadIndex>(
114
117
119 DateGeneration::Forward, false);
120 Leg cmsLeg1 = CmsSpreadLeg(cmsSpreadSchedule, spreadIndex)
121 .withNotionals(nominals)
122 .withSpreads(std::vector<Rate>(1, 0))
126 .inArrears(true)
127 .withCaps(std::vector<Rate>(1, spread));
128
129 QuantLib::setCouponPricer(cmsLeg1,
pricer_);
130
131 Leg cmsLeg1b = StrippedCappedFlooredCouponLeg(cmsLeg1);
132
133 legs.push_back(cmsLeg1b);
134 legPayers.push_back(false);
135
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);
139}
QuantLib::ext::shared_ptr< QuantLib::Swap > cap_