Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Classes | List of all members
TenorBasisSwap Class Reference

Single currency tenor basis swap. More...

#include <qle/instruments/tenorbasisswap.hpp>

+ Inheritance diagram for TenorBasisSwap:
+ Collaboration diagram for TenorBasisSwap:

Classes

class  engine
 
class  results
 

Public Member Functions

Constructors
 TenorBasisSwap (const Date &effectiveDate, Real nominal, const Period &swapTenor, const QuantLib::ext::shared_ptr< IborIndex > &payIndex, Spread paySpread, const Period &payFrequency, const QuantLib::ext::shared_ptr< IborIndex > &recIndex, Spread recSpread, const Period &recFrequency, DateGeneration::Rule rule=DateGeneration::Backward, bool includeSpread=false, bool spreadOnRec=true, QuantExt::SubPeriodsCoupon1::Type type=QuantExt::SubPeriodsCoupon1::Compounding, const bool telescopicValueDates=false)
 Constructor with conventions deduced from the indices. More...
 
 TenorBasisSwap (Real nominal, const Schedule &paySchedule, const QuantLib::ext::shared_ptr< IborIndex > &payIndex, Spread paySpread, const Schedule &recSchedule, const QuantLib::ext::shared_ptr< IborIndex > &recIndex, Spread recSpread, bool includeSpread=false, bool spreadOnRec=true, QuantExt::SubPeriodsCoupon1::Type type=QuantExt::SubPeriodsCoupon1::Compounding, const bool telescopicValueDates=false)
 Constructor using Schedules with a full interface. More...
 
 TenorBasisSwap (const std::vector< Real > &nominals, const Schedule &paySchedule, const QuantLib::ext::shared_ptr< IborIndex > &payIndex, Spread paySpread, const Schedule &recSchedule, const QuantLib::ext::shared_ptr< IborIndex > &recIndex, Spread recSpread, bool includeSpread=false, bool spreadOnRec=true, QuantExt::SubPeriodsCoupon1::Type type=QuantExt::SubPeriodsCoupon1::Compounding, const bool telescopicValueDates=false)
 
Inspectors
Real nominal () const
 
const std::vector< Real > & nominals () const
 
const Schedule & paySchedule () const
 
const QuantLib::ext::shared_ptr< IborIndex > & payIndex () const
 
Spread paySpread () const
 
const Leg & payLeg () const
 
const Schedule & recSchedule () const
 
const QuantLib::ext::shared_ptr< IborIndex > & recIndex () const
 
Spread recSpread () const
 
const Leg & recLeg () const
 
const Period & recFrequency () const
 
const Period & payFrequency () const
 
bool includeSpread () const
 
bool spreadOnRec () const
 
QuantExt::SubPeriodsCoupon1::Type type () const
 

Results

std::vector< Real > nominals_
 
Schedule paySchedule_
 
QuantLib::ext::shared_ptr< IborIndex > payIndex_
 
Spread paySpread_
 
Period payFrequency_
 
Schedule recSchedule_
 
QuantLib::ext::shared_ptr< IborIndex > recIndex_
 
Spread recSpread_
 
Period recFrequency_
 
bool includeSpread_
 
bool spreadOnRec_
 
QuantExt::SubPeriodsCoupon1::Type type_
 
bool telescopicValueDates_
 
bool noSubPeriod_
 
std::vector< Spread > fairSpread_
 
Calendar recIndexCalendar_
 
Calendar payIndexCalendar_
 
Size idxRec_
 
Size idxPay_
 
Real payLegBPS () const
 
Real payLegNPV () const
 
Rate fairPayLegSpread () const
 
Real recLegBPS () const
 
Real recLegNPV () const
 
Spread fairRecLegSpread () const
 
void fetchResults (const PricingEngine::results *) const override
 
void initializeLegs ()
 
void setupExpired () const override
 

Detailed Description

Single currency tenor basis swap.

Definition at line 38 of file tenorbasisswap.hpp.

Constructor & Destructor Documentation

◆ TenorBasisSwap() [1/3]

TenorBasisSwap ( const Date &  effectiveDate,
Real  nominal,
const Period &  swapTenor,
const QuantLib::ext::shared_ptr< IborIndex > &  payIndex,
Spread  paySpread,
const Period &  payFrequency,
const QuantLib::ext::shared_ptr< IborIndex > &  recIndex,
Spread  recSpread,
const Period &  recFrequency,
DateGeneration::Rule  rule = DateGeneration::Backward,
bool  includeSpread = false,
bool  spreadOnRec = true,
QuantExt::SubPeriodsCoupon1::Type  type = QuantExt::SubPeriodsCoupon1::Compounding,
const bool  telescopicValueDates = false 
)

Constructor with conventions deduced from the indices.

Definition at line 64 of file tenorbasisswap.cpp.

70 : Swap(2), nominals_(std::vector<Real>(1, nominal)), payIndex_(payIndex), paySpread_(paySpread),
73 telescopicValueDates_(telescopicValueDates) {
74
75 // Create the default pay and rec schedules
76 Date terminationDate = effectiveDate + swapTenor;
77
78 QuantLib::ext::shared_ptr<Libor> payIndexAsLibor = QuantLib::ext::dynamic_pointer_cast<Libor>(payIndex_);
79 payIndexCalendar_ = payIndexAsLibor != NULL ? payIndexAsLibor->jointCalendar() : payIndex_->fixingCalendar();
80 QuantLib::ext::shared_ptr<Libor> recIndexAsLibor = QuantLib::ext::dynamic_pointer_cast<Libor>(recIndex_);
82 recIndexAsLibor != NULL ? recIndexAsLibor->jointCalendar() : recIndex_->fixingCalendar();
83
84 paySchedule_ = MakeSchedule()
85 .from(effectiveDate)
86 .to(terminationDate)
87 .withTenor(payFrequency_)
88 .withCalendar(payIndexCalendar_)
89 .withConvention(payIndex_->businessDayConvention())
90 .withTerminationDateConvention(payIndex_->businessDayConvention())
91 .withRule(rule)
92 .endOfMonth(payIndex_->endOfMonth());
93
94 recSchedule_ = MakeSchedule()
95 .from(effectiveDate)
96 .to(terminationDate)
97 .withTenor(recFrequency_)
98 .withCalendar(recIndexCalendar_)
99 .withConvention(recIndex_->businessDayConvention())
100 .withTerminationDateConvention(recIndex_->businessDayConvention())
101 .withRule(rule)
102 .endOfMonth(recIndex_->endOfMonth());
103
104 // Create legs
106}
QuantLib::ext::shared_ptr< IborIndex > payIndex_
const QuantLib::ext::shared_ptr< IborIndex > & recIndex() const
QuantLib::ext::shared_ptr< IborIndex > recIndex_
const Period & payFrequency() const
const Period & recFrequency() const
QuantExt::SubPeriodsCoupon1::Type type() const
const QuantLib::ext::shared_ptr< IborIndex > & payIndex() const
QuantExt::SubPeriodsCoupon1::Type type_
std::vector< Real > nominals_
+ Here is the call graph for this function:

◆ TenorBasisSwap() [2/3]

TenorBasisSwap ( Real  nominal,
const Schedule &  paySchedule,
const QuantLib::ext::shared_ptr< IborIndex > &  payIndex,
Spread  paySpread,
const Schedule &  recSchedule,
const QuantLib::ext::shared_ptr< IborIndex > &  recIndex,
Spread  recSpread,
bool  includeSpread = false,
bool  spreadOnRec = true,
QuantExt::SubPeriodsCoupon1::Type  type = QuantExt::SubPeriodsCoupon1::Compounding,
const bool  telescopicValueDates = false 
)

Constructor using Schedules with a full interface.

Definition at line 108 of file tenorbasisswap.cpp.

113 : Swap(2), nominals_(std::vector<Real>(1, nominal)), paySchedule_(paySchedule), payIndex_(payIndex),
116
117 // Create legs
119}
const Schedule & recSchedule() const
const Schedule & paySchedule() const
+ Here is the call graph for this function:

◆ TenorBasisSwap() [3/3]

TenorBasisSwap ( const std::vector< Real > &  nominals,
const Schedule &  paySchedule,
const QuantLib::ext::shared_ptr< IborIndex > &  payIndex,
Spread  paySpread,
const Schedule &  recSchedule,
const QuantLib::ext::shared_ptr< IborIndex > &  recIndex,
Spread  recSpread,
bool  includeSpread = false,
bool  spreadOnRec = true,
QuantExt::SubPeriodsCoupon1::Type  type = QuantExt::SubPeriodsCoupon1::Compounding,
const bool  telescopicValueDates = false 
)

Definition at line 121 of file tenorbasisswap.cpp.

+ Here is the call graph for this function:

Member Function Documentation

◆ nominal()

Real nominal ( ) const

Definition at line 139 of file tenorbasisswap.hpp.

139 {
140 QL_REQUIRE(nominals_.size() == 1, "varying nominals");
141 return nominals_[0];
142}

◆ nominals()

const std::vector< Real > & nominals ( ) const

Definition at line 68 of file tenorbasisswap.hpp.

68{ return nominals_; }

◆ paySchedule()

const Schedule & paySchedule ( ) const

Definition at line 143 of file tenorbasisswap.hpp.

143{ return paySchedule_; }

◆ payIndex()

const QuantLib::ext::shared_ptr< IborIndex > & payIndex ( ) const

Definition at line 145 of file tenorbasisswap.hpp.

145{ return payIndex_; }

◆ paySpread()

Spread paySpread ( ) const

Definition at line 147 of file tenorbasisswap.hpp.

147{ return paySpread_; }

◆ payLeg()

const Leg & payLeg ( ) const

Definition at line 149 of file tenorbasisswap.hpp.

149{ return legs_[idxPay_]; }
+ Here is the caller graph for this function:

◆ recSchedule()

const Schedule & recSchedule ( ) const

Definition at line 151 of file tenorbasisswap.hpp.

151{ return recSchedule_; }

◆ recIndex()

const QuantLib::ext::shared_ptr< IborIndex > & recIndex ( ) const

Definition at line 153 of file tenorbasisswap.hpp.

153{ return recIndex_; }

◆ recSpread()

Spread recSpread ( ) const

Definition at line 155 of file tenorbasisswap.hpp.

155{ return recSpread_; }

◆ recLeg()

const Leg & recLeg ( ) const

Definition at line 165 of file tenorbasisswap.hpp.

165{ return legs_[idxRec_]; }
+ Here is the caller graph for this function:

◆ recFrequency()

const Period & recFrequency ( ) const

Definition at line 157 of file tenorbasisswap.hpp.

157{ return recFrequency_; }

◆ payFrequency()

const Period & payFrequency ( ) const

Definition at line 159 of file tenorbasisswap.hpp.

159{ return payFrequency_; }

◆ includeSpread()

bool includeSpread ( ) const

Definition at line 161 of file tenorbasisswap.hpp.

161{ return includeSpread_; }

◆ spreadOnRec()

bool spreadOnRec ( ) const

Definition at line 83 of file tenorbasisswap.hpp.

83{ return spreadOnRec_; }

◆ type()

Definition at line 163 of file tenorbasisswap.hpp.

163{ return type_; }

◆ payLegBPS()

Real payLegBPS ( ) const

Definition at line 237 of file tenorbasisswap.cpp.

237 {
238 calculate();
239 QL_REQUIRE(legBPS_[idxPay_] != Null<Real>(), "Pay leg BPS not available");
240 return legBPS_[idxPay_];
241}

◆ payLegNPV()

Real payLegNPV ( ) const

Definition at line 243 of file tenorbasisswap.cpp.

243 {
244 calculate();
245 QL_REQUIRE(legNPV_[idxPay_] != Null<Real>(), "Pay leg NPV not available");
246 return legNPV_[idxPay_];
247}

◆ fairPayLegSpread()

Rate fairPayLegSpread ( ) const

Definition at line 249 of file tenorbasisswap.cpp.

249 {
250 calculate();
251 QL_REQUIRE(fairSpread_[idxPay_] != Null<Spread>(), "Pay leg fair spread not available");
252 return fairSpread_[idxPay_];
253}
std::vector< Spread > fairSpread_

◆ recLegBPS()

Real recLegBPS ( ) const

Definition at line 255 of file tenorbasisswap.cpp.

255 {
256 calculate();
257 QL_REQUIRE(legBPS_[idxRec_] != Null<Real>(), "Receive leg BPS not available");
258 return legBPS_[idxRec_];
259}

◆ recLegNPV()

Real recLegNPV ( ) const

Definition at line 261 of file tenorbasisswap.cpp.

261 {
262 calculate();
263 QL_REQUIRE(legNPV_[idxRec_] != Null<Real>(), "Receive leg NPV not available");
264 return legNPV_[idxRec_];
265}

◆ fairRecLegSpread()

Spread fairRecLegSpread ( ) const

Definition at line 267 of file tenorbasisswap.cpp.

267 {
268 calculate();
269 QL_REQUIRE(fairSpread_[idxRec_] != Null<Spread>(), "Receive leg fair spread not available");
270 return fairSpread_[idxRec_];
271}

◆ fetchResults()

void fetchResults ( const PricingEngine::results *  r) const
override

Definition at line 278 of file tenorbasisswap.cpp.

278 {
279
280 static const Spread basisPoint = 1.0e-4;
281 Swap::fetchResults(r);
282
283 const TenorBasisSwap::results* results = dynamic_cast<const TenorBasisSwap::results*>(r);
284
285 if (results) {
286 fairSpread_ = results->fairSpread;
287 } else {
288 fairSpread_ = { Null<Spread>(),Null<Spread>() };
289 }
290
291 //non spread leg (idx 1) should be fine - no averaging or compounding
292 if (fairSpread_[1] == Null<Spread>()) {
293 if (legBPS_[1] != Null<Real>()) {
294 double s = spreadOnRec_ ? recSpread_ : paySpread_;
295 fairSpread_[1] = s - NPV_ / (legBPS_[1] / basisPoint);
296 }
297 }
298
299 /* spread leg (idx 0) fair spread calculation ok if no averaging/compounding OR
300 if there is averaging/compounding and the spread is added after */
301 if (fairSpread_[0] == Null<Spread>()) {
303 if (legBPS_[0] != Null<Real>()) {
304 double s = spreadOnRec_ ? paySpread_ : recSpread_;
305 fairSpread_[0] = s - NPV_ / (legBPS_[0] / basisPoint);
306 }
307 } else {
308 // Need the discount curve
309 Handle<YieldTermStructure> discountCurve;
310 QuantLib::ext::shared_ptr<DiscountingSwapEngine> engine =
311 QuantLib::ext::dynamic_pointer_cast<DiscountingSwapEngine>(engine_);
312 if (engine) {
313 discountCurve = engine->discountCurve();
314 // Calculate a guess
315 Spread guess = 0.0;
316 if (legBPS_[0] != Null<Real>()) {
317 double s = spreadOnRec_ ? paySpread_ : recSpread_;
318 guess = s - NPV_ / (legBPS_[0] / basisPoint);
319 }
320 // Attempt to solve for fair spread
321 Spread step = 1e-4;
322 Real accuracy = 1e-8;
323 FairSpreadHelper f(*this, discountCurve, legNPV_[1]);
324 Brent solver;
325 fairSpread_[0] = solver.solve(f, accuracy, guess, step);
326 }
327 }
328 }
329}
QuantLib::ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:78

◆ initializeLegs()

void initializeLegs ( )
private

Definition at line 134 of file tenorbasisswap.cpp.

134 {
135
136 // Checks
137 QL_REQUIRE(paySchedule_.tenor() >= payIndex_->tenor(), "Expected paySchedule tenor to exceed/equal payIndex tenor");
138 QL_REQUIRE(recSchedule_.tenor() >= recIndex_->tenor(), "Expected recSchedule tenor to exceed/equal recIndex tenor");
139
140 noSubPeriod_ = true;
141
142 // pay leg
143 QuantLib::ext::shared_ptr<OvernightIndex> payIndexON = QuantLib::ext::dynamic_pointer_cast<OvernightIndex>(payIndex_);
144 Leg payLeg;
145
146 if (payIndexON) {
147 payLeg = OvernightLeg(paySchedule_, payIndexON)
148 .withNotionals(nominals_)
149 .withSpreads(paySpread_)
150 .withTelescopicValueDates(telescopicValueDates_);
151 } else {
152 if (paySchedule_.tenor() == payIndex_->tenor()) {
153 payLeg = IborLeg(paySchedule_, payIndex_)
154 .withNotionals(nominals_)
155 .withSpreads(paySpread_)
156 .withPaymentAdjustment(payIndex_->businessDayConvention())
157 .withPaymentDayCounter(payIndex_->dayCounter())
158 .withPaymentCalendar(payIndexCalendar_);
159 } else {
160 if (!spreadOnRec_) {
161 //if spread leg and no overnight, the leg may be a subperiod leg
165 .withPaymentAdjustment(payIndex_->businessDayConvention())
166 .withPaymentDayCounter(payIndex_->dayCounter())
169 .withType(type_);
170 noSubPeriod_ = false;
171 } else {
172 QL_FAIL(
173 "Pay Leg could not be created. Neither overnight nor schedule index tenor match nor spread leg.");
174 } // spreadOnRec
175 } // paySchedule_.tenor() == payIndex_->tenor()
176 } // payIndexON
177
178 // receive leg
179 QuantLib::ext::shared_ptr<OvernightIndex> recIndexON = QuantLib::ext::dynamic_pointer_cast<OvernightIndex>(recIndex_);
180 Leg recLeg;
181
182 if (recIndexON) {
183 recLeg = OvernightLeg(recSchedule_, recIndexON)
184 .withNotionals(nominals_)
185 .withSpreads(recSpread_)
186 .withTelescopicValueDates(telescopicValueDates_);
187 } else {
188 if (recSchedule_.tenor() == recIndex_->tenor()) {
189 recLeg = IborLeg(recSchedule_, recIndex_)
190 .withNotionals(nominals_)
191 .withSpreads(recSpread_)
192 .withPaymentAdjustment(recIndex_->businessDayConvention())
193 .withPaymentDayCounter(recIndex_->dayCounter())
194 .withPaymentCalendar(recIndexCalendar_);
195 } else {
196 if (spreadOnRec_) {
197 //if spread leg and no overnight, the leg may be a subperiod leg
201 .withPaymentAdjustment(recIndex_->businessDayConvention())
202 .withPaymentDayCounter(recIndex_->dayCounter())
205 .withType(type_);
206 noSubPeriod_ = false;
207 } else {
208 QL_FAIL(
209 "Rec Leg could not be created. Neither overnight nor schedule index tenor match nor spread leg.");
210 } //!spreadOnRec
211 } //recSchedule_.tenor() == recIndex_->tenor()
212 } //recIndexON
213
214 //Allocate leg idx : spread leg = 0
215 idxPay_ = 0;
216 idxRec_ = 1;
217 if(spreadOnRec_){
218 idxPay_ = 1;
219 idxRec_ = 0;
220 }
221
222 payer_[idxPay_] = -1.0;
223 payer_[idxRec_] = +1.0;
224 legs_[idxPay_] = payLeg;
225 legs_[idxRec_] = recLeg;
226
227 // Register this instrument with its coupons
228 Leg::const_iterator it;
229 for (it = legs_[0].begin(); it != legs_[0].end(); ++it)
230 registerWith(*it);
231 for (it = legs_[1].begin(); it != legs_[1].end(); ++it)
232 registerWith(*it);
233
234
235}
helper class building a sequence of sub-period coupons
SubPeriodsLeg1 & withPaymentDayCounter(const DayCounter &dayCounter)
SubPeriodsLeg1 & withType(SubPeriodsCoupon1::Type type)
SubPeriodsLeg1 & withSpread(Spread spread)
SubPeriodsLeg1 & includeSpread(bool includeSpread)
SubPeriodsLeg1 & withPaymentCalendar(const Calendar &calendar)
SubPeriodsLeg1 & withNotionals(const std::vector< Real > &notionals)
SubPeriodsLeg1 & withPaymentAdjustment(BusinessDayConvention convention)
const Leg & payLeg() const
const Leg & recLeg() const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setupExpired()

void setupExpired ( ) const
overrideprivate

Definition at line 273 of file tenorbasisswap.cpp.

273 {
274 Swap::setupExpired();
275 fairSpread_ = { Null<Spread>(),Null<Spread>() };
276}

Member Data Documentation

◆ nominals_

std::vector<Real> nominals_
private

Definition at line 102 of file tenorbasisswap.hpp.

◆ paySchedule_

Schedule paySchedule_
private

Definition at line 104 of file tenorbasisswap.hpp.

◆ payIndex_

QuantLib::ext::shared_ptr<IborIndex> payIndex_
private

Definition at line 105 of file tenorbasisswap.hpp.

◆ paySpread_

Spread paySpread_
private

Definition at line 106 of file tenorbasisswap.hpp.

◆ payFrequency_

Period payFrequency_
private

Definition at line 107 of file tenorbasisswap.hpp.

◆ recSchedule_

Schedule recSchedule_
private

Definition at line 109 of file tenorbasisswap.hpp.

◆ recIndex_

QuantLib::ext::shared_ptr<IborIndex> recIndex_
private

Definition at line 110 of file tenorbasisswap.hpp.

◆ recSpread_

Spread recSpread_
private

Definition at line 111 of file tenorbasisswap.hpp.

◆ recFrequency_

Period recFrequency_
private

Definition at line 112 of file tenorbasisswap.hpp.

◆ includeSpread_

bool includeSpread_
private

Definition at line 114 of file tenorbasisswap.hpp.

◆ spreadOnRec_

bool spreadOnRec_
private

Definition at line 115 of file tenorbasisswap.hpp.

◆ type_

Definition at line 116 of file tenorbasisswap.hpp.

◆ telescopicValueDates_

bool telescopicValueDates_
private

Definition at line 117 of file tenorbasisswap.hpp.

◆ noSubPeriod_

bool noSubPeriod_
private

Definition at line 119 of file tenorbasisswap.hpp.

◆ fairSpread_

std::vector<Spread> fairSpread_
mutableprivate

Definition at line 121 of file tenorbasisswap.hpp.

◆ recIndexCalendar_

Calendar recIndexCalendar_
private

Definition at line 123 of file tenorbasisswap.hpp.

◆ payIndexCalendar_

Calendar payIndexCalendar_
private

Definition at line 123 of file tenorbasisswap.hpp.

◆ idxRec_

Size idxRec_
private

Definition at line 124 of file tenorbasisswap.hpp.

◆ idxPay_

Size idxPay_
private

Definition at line 124 of file tenorbasisswap.hpp.