QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
basisswapratehelpers.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2021 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/cashflows/iborcoupon.hpp>
21#include <ql/cashflows/overnightindexedcoupon.hpp>
22#include <ql/experimental/termstructures/basisswapratehelpers.hpp>
23#include <ql/pricingengines/swap/discountingswapengine.hpp>
24#include <ql/utilities/null_deleter.hpp>
25#include <utility>
26
27namespace QuantLib {
28
30 const Handle<Quote>& basis,
31 const Period& tenor,
32 Natural settlementDays,
33 Calendar calendar,
34 BusinessDayConvention convention,
35 bool endOfMonth,
36 const ext::shared_ptr<IborIndex>& baseIndex,
37 const ext::shared_ptr<IborIndex>& otherIndex,
38 Handle<YieldTermStructure> discountHandle,
39 bool bootstrapBaseCurve)
40 : RelativeDateRateHelper(basis), tenor_(tenor), settlementDays_(settlementDays),
41 calendar_(std::move(calendar)), convention_(convention), endOfMonth_(endOfMonth),
42 discountHandle_(std::move(discountHandle)), bootstrapBaseCurve_(bootstrapBaseCurve) {
43
44 // we need to clone the index whose forecast curve we want to bootstrap
45 // and copy the other one
47 baseIndex_ = baseIndex->clone(termStructureHandle_);
48 baseIndex_->unregisterWith(termStructureHandle_);
49 otherIndex_ = otherIndex;
50 } else {
51 baseIndex_ = baseIndex;
52 otherIndex_ = otherIndex->clone(termStructureHandle_);
53 otherIndex_->unregisterWith(termStructureHandle_);
54 }
55
59
61 }
62
67
68 Schedule baseSchedule =
70 .withTenor(baseIndex_->tenor())
74 .forwards();
75 Leg baseLeg = IborLeg(baseSchedule, baseIndex_).withNotionals(100.0);
76 auto lastBaseCoupon = ext::dynamic_pointer_cast<IborCoupon>(baseLeg.back());
77
78 Schedule otherSchedule =
80 .withTenor(otherIndex_->tenor())
84 .forwards();
85 Leg otherLeg = IborLeg(otherSchedule, otherIndex_).withNotionals(100.0);
86 auto lastOtherCoupon = ext::dynamic_pointer_cast<IborCoupon>(otherLeg.back());
87
89 std::max(lastBaseCoupon->fixingEndDate(),
90 lastOtherCoupon->fixingEndDate()));
92
93 swap_ = ext::make_shared<Swap>(baseLeg, otherLeg);
94 swap_->setPricingEngine(ext::make_shared<DiscountingSwapEngine>(discountHandle_));
95 }
96
98 bool observer = false;
99
100 ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
101 termStructureHandle_.linkTo(temp, observer);
102
104 }
105
107 swap_->deepUpdate();
108 return - (swap_->NPV() / swap_->legBPS(0)) * 1.0e-4;
109 }
110
112 auto* v1 = dynamic_cast<Visitor<IborIborBasisSwapRateHelper>*>(&v);
113 if (v1 != nullptr)
114 v1->visit(*this);
115 else
117 }
118
119
120
122 const Handle<Quote>& basis,
123 const Period& tenor,
124 Natural settlementDays,
125 Calendar calendar,
126 BusinessDayConvention convention,
127 bool endOfMonth,
128 const ext::shared_ptr<OvernightIndex>& baseIndex,
129 const ext::shared_ptr<IborIndex>& otherIndex,
130 Handle<YieldTermStructure> discountHandle)
131 : RelativeDateRateHelper(basis), tenor_(tenor), settlementDays_(settlementDays),
132 calendar_(std::move(calendar)), convention_(convention), endOfMonth_(endOfMonth),
133 discountHandle_(std::move(discountHandle)) {
134
135 // we need to clone the index whose forecast curve we want to bootstrap
136 // and copy the other one
137 baseIndex_ = baseIndex;
138 otherIndex_ = otherIndex->clone(termStructureHandle_);
139 otherIndex_->unregisterWith(termStructureHandle_);
140
144
146 }
147
152
153 Schedule schedule =
155 .withTenor(otherIndex_->tenor())
159 .forwards();
160
161 Leg baseLeg = OvernightLeg(schedule, baseIndex_).withNotionals(100.0);
162
163 Leg otherLeg = IborLeg(schedule, otherIndex_).withNotionals(100.0);
164 auto lastOtherCoupon = ext::dynamic_pointer_cast<IborCoupon>(otherLeg.back());
165
166 latestRelevantDate_ = std::max(maturityDate_, lastOtherCoupon->fixingEndDate());
168
169 swap_ = ext::make_shared<Swap>(baseLeg, otherLeg);
170 swap_->setPricingEngine(ext::make_shared<DiscountingSwapEngine>(
172 }
173
175 // do not set the relinkable handle as an observer -
176 // force recalculation when needed---the index is not lazy
177 bool observer = false;
178
179 ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
180 termStructureHandle_.linkTo(temp, observer);
181
183 }
184
186 swap_->deepUpdate();
187 return - (swap_->NPV() / swap_->legBPS(0)) * 1.0e-4;
188 }
189
191 auto* v1 = dynamic_cast<Visitor<OvernightIborBasisSwapRateHelper>*>(&v);
192 if (v1 != nullptr)
193 v1->visit(*this);
194 else
196 }
197
198}
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
virtual void accept(AcyclicVisitor &)
virtual void setTermStructure(TS *)
sets the term structure to be used for pricing
calendar class
Definition: calendar.hpp:61
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Concrete date class.
Definition: date.hpp:125
Shared handle to an observable.
Definition: handle.hpp:41
void setTermStructure(YieldTermStructure *) override
IborIborBasisSwapRateHelper(const Handle< Quote > &basis, const Period &tenor, Natural settlementDays, Calendar calendar, BusinessDayConvention convention, bool endOfMonth, const ext::shared_ptr< IborIndex > &baseIndex, const ext::shared_ptr< IborIndex > &otherIndex, Handle< YieldTermStructure > discountHandle, bool bootstrapBaseCurve)
RelinkableHandle< YieldTermStructure > termStructureHandle_
Handle< YieldTermStructure > discountHandle_
void accept(AcyclicVisitor &) override
ext::shared_ptr< IborIndex > otherIndex_
ext::shared_ptr< IborIndex > baseIndex_
helper class building a sequence of capped/floored ibor-rate coupons
Definition: iborcoupon.hpp:133
IborLeg & withNotionals(Real notional)
Definition: iborcoupon.cpp:158
MakeSchedule & withConvention(BusinessDayConvention)
Definition: schedule.cpp:557
MakeSchedule & to(const Date &terminationDate)
Definition: schedule.cpp:537
MakeSchedule & withTenor(const Period &)
Definition: schedule.cpp:542
MakeSchedule & from(const Date &effectiveDate)
Definition: schedule.cpp:532
MakeSchedule & endOfMonth(bool flag=true)
Definition: schedule.cpp:583
MakeSchedule & forwards()
Definition: schedule.cpp:573
MakeSchedule & withCalendar(const Calendar &)
Definition: schedule.cpp:552
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
OvernightIborBasisSwapRateHelper(const Handle< Quote > &basis, const Period &tenor, Natural settlementDays, Calendar calendar, BusinessDayConvention convention, bool endOfMonth, const ext::shared_ptr< OvernightIndex > &baseIndex, const ext::shared_ptr< IborIndex > &otherIndex, Handle< YieldTermStructure > discountHandle=Handle< YieldTermStructure >())
void setTermStructure(YieldTermStructure *) override
RelinkableHandle< YieldTermStructure > termStructureHandle_
ext::shared_ptr< OvernightIndex > baseIndex_
helper class building a sequence of overnight coupons
OvernightLeg & withNotionals(Real notional)
Bootstrap helper with date schedule relative to global evaluation date.
Payment schedule.
Definition: schedule.hpp:40
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Interest-rate term structure.
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Definition: any.hpp:35
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.