QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
oisratehelper.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009, 2012 Roland Lichters
5 Copyright (C) 2009, 2012 Ferdinando Ametrano
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
27#include <utility>
28
29namespace QuantLib {
30
32 const Period& tenor, // swap maturity
33 const Handle<Quote>& fixedRate,
34 const ext::shared_ptr<OvernightIndex>& overnightIndex,
36 bool telescopicValueDates,
37 Integer paymentLag,
38 BusinessDayConvention paymentConvention,
39 Frequency paymentFrequency,
40 Calendar paymentCalendar,
41 const Period& forwardStart,
42 const std::variant<Spread, Handle<Quote>>& overnightSpread,
43 Pillar::Choice pillar,
44 Date customPillarDate,
45 RateAveraging::Type averagingMethod,
46 ext::optional<bool> endOfMonth,
47 ext::optional<Frequency> fixedPaymentFrequency,
48 Calendar fixedCalendar,
49 Natural lookbackDays,
50 Natural lockoutDays,
51 bool applyObservationShift,
52 ext::shared_ptr<FloatingRateCouponPricer> pricer,
54 Calendar overnightCalendar)
55 : RelativeDateRateHelper(fixedRate), settlementDays_(settlementDays), tenor_(tenor),
56 discountHandle_(std::move(discount)), telescopicValueDates_(telescopicValueDates),
57 paymentLag_(paymentLag), paymentConvention_(paymentConvention),
58 paymentFrequency_(paymentFrequency), paymentCalendar_(std::move(paymentCalendar)),
59 forwardStart_(forwardStart), overnightSpread_(handleFromVariant(overnightSpread)), pillarChoice_(pillar),
60 averagingMethod_(averagingMethod), endOfMonth_(endOfMonth),
61 fixedPaymentFrequency_(fixedPaymentFrequency), fixedCalendar_(std::move(fixedCalendar)),
62 overnightCalendar_(std::move(overnightCalendar)), lookbackDays_(lookbackDays), lockoutDays_(lockoutDays),
63 applyObservationShift_(applyObservationShift), pricer_(std::move(pricer)), rule_(rule) {
64 initialize(overnightIndex, customPillarDate);
65 }
66
68 const Date& endDate,
69 const Handle<Quote>& fixedRate,
70 const ext::shared_ptr<OvernightIndex>& overnightIndex,
72 bool telescopicValueDates,
73 Integer paymentLag,
74 BusinessDayConvention paymentConvention,
75 Frequency paymentFrequency,
76 Calendar paymentCalendar,
77 const std::variant<Spread, Handle<Quote>>& overnightSpread,
78 Pillar::Choice pillar,
79 Date customPillarDate,
80 RateAveraging::Type averagingMethod,
81 ext::optional<bool> endOfMonth,
82 ext::optional<Frequency> fixedPaymentFrequency,
83 Calendar fixedCalendar,
84 Natural lookbackDays,
85 Natural lockoutDays,
86 bool applyObservationShift,
87 ext::shared_ptr<FloatingRateCouponPricer> pricer,
89 Calendar overnightCalendar)
90 : RelativeDateRateHelper(fixedRate, false), startDate_(startDate), endDate_(endDate),
91 discountHandle_(std::move(discount)), telescopicValueDates_(telescopicValueDates),
92 paymentLag_(paymentLag), paymentConvention_(paymentConvention),
93 paymentFrequency_(paymentFrequency), paymentCalendar_(std::move(paymentCalendar)),
94 overnightSpread_(handleFromVariant(overnightSpread)), pillarChoice_(pillar),
95 averagingMethod_(averagingMethod), endOfMonth_(endOfMonth),
96 fixedPaymentFrequency_(fixedPaymentFrequency), fixedCalendar_(std::move(fixedCalendar)),
97 overnightCalendar_(std::move(overnightCalendar)), lookbackDays_(lookbackDays), lockoutDays_(lockoutDays),
98 applyObservationShift_(applyObservationShift), pricer_(std::move(pricer)), rule_(rule) {
99 initialize(overnightIndex, customPillarDate);
100 }
101
102 void OISRateHelper::initialize(const ext::shared_ptr<OvernightIndex>& overnightIndex,
103 Date customPillarDate) {
105 ext::dynamic_pointer_cast<OvernightIndex>(overnightIndex->clone(termStructureHandle_));
106 // We want to be notified of changes of fixings, but we don't
107 // want notifications from termStructureHandle_ (they would
108 // interfere with bootstrapping.)
109 overnightIndex_->unregisterWith(termStructureHandle_);
110
114
115 pillarDate_ = customPillarDate;
117 }
118
120
121 // 1. do not pass the spread here, as it might be a Quote
122 // i.e. it can dynamically change
123 // 2. input discount curve Handle might be empty now but it could
124 // be assigned a curve later; use a RelinkableHandle here
127 .withSettlementDays(settlementDays_) // resets effectiveDate
140 if (endOfMonth_) {
142 }
145 }
146 if (!fixedCalendar_.empty()) {
148 }
149 if (!overnightCalendar_.empty()) {
151 }
152 swap_ = tmp;
153
154 if (pricer_)
155 setCouponPricer(swap_->overnightLeg(), pricer_);
156
158
159 earliestDate_ = swap_->startDate();
160 maturityDate_ = swap_->maturityDate();
161
162 Date lastPaymentDate = std::max(swap_->overnightLeg().back()->date(),
163 swap_->fixedLeg().back()->date());
164 latestRelevantDate_ = latestDate_ = std::max(maturityDate_, lastPaymentDate);
165
166 switch (pillarChoice_) {
169 break;
172 break;
174 // pillarDate_ already assigned at construction time
176 "pillar date (" << pillarDate_ << ") must be later "
177 "than or equal to the instrument's earliest date (" <<
178 earliestDate_ << ")");
180 "pillar date (" << pillarDate_ << ") must be before "
181 "or equal to the instrument's latest relevant date (" <<
182 latestRelevantDate_ << ")");
183 break;
184 default:
185 QL_FAIL("unknown Pillar::Choice(" << Integer(pillarChoice_) << ")");
186 }
187 }
188
190 // do not set the relinkable handle as an observer -
191 // force recalculation when needed
192 bool observer = false;
193
194 ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
195 termStructureHandle_.linkTo(temp, observer);
196
197 if (discountHandle_.empty())
198 discountRelinkableHandle_.linkTo(temp, observer);
199 else
201
203 }
204
206 QL_REQUIRE(termStructure_ != nullptr, "term structure not set");
207 // we didn't register as observers - force calculation
208 swap_->deepUpdate();
209 // weak implementation... to be improved
210 static const Spread basisPoint = 1.0e-4;
211 Real floatingLegNPV = swap_->overnightLegNPV();
212 Spread spread = overnightSpread_.empty() ? 0.0 : overnightSpread_->value();
213 Real spreadNPV = swap_->overnightLegBPS()/basisPoint*spread;
214 Real totNPV = - (floatingLegNPV+spreadNPV);
215 Real result = totNPV/(swap_->fixedLegBPS()/basisPoint);
216 return result;
217 }
218
220 auto* v1 = dynamic_cast<Visitor<OISRateHelper>*>(&v);
221 if (v1 != nullptr)
222 v1->visit(*this);
223 else
225 }
226
228 const Date& endDate,
229 const Handle<Quote>& fixedRate,
230 const ext::shared_ptr<OvernightIndex>& overnightIndex,
232 bool telescopicValueDates,
233 RateAveraging::Type averagingMethod,
234 Integer paymentLag,
235 BusinessDayConvention paymentConvention,
236 Frequency paymentFrequency,
237 const Calendar& paymentCalendar,
238 Spread overnightSpread,
239 ext::optional<bool> endOfMonth,
240 ext::optional<Frequency> fixedPaymentFrequency,
241 const Calendar& fixedCalendar,
242 Natural lookbackDays,
243 Natural lockoutDays,
244 bool applyObservationShift,
245 const ext::shared_ptr<FloatingRateCouponPricer>& pricer)
246 : OISRateHelper(startDate, endDate, fixedRate, overnightIndex, std::move(discount), telescopicValueDates,
247 paymentLag, paymentConvention, paymentFrequency, paymentCalendar,
248 std::variant<Spread, Handle<Quote>>(overnightSpread),
249 Pillar::LastRelevantDate, Date(), averagingMethod, endOfMonth, fixedPaymentFrequency,
250 fixedCalendar, lookbackDays, lockoutDays, applyObservationShift, pricer) {}
251
253 const Date& endDate,
254 const Handle<Quote>& fixedRate,
255 const ext::shared_ptr<OvernightIndex>& overnightIndex,
257 bool telescopicValueDates,
258 RateAveraging::Type averagingMethod,
259 Integer paymentLag,
260 BusinessDayConvention paymentConvention,
261 Frequency paymentFrequency,
262 const Calendar& paymentCalendar,
263 const Period&,
264 Spread overnightSpread,
265 ext::optional<bool> endOfMonth,
266 ext::optional<Frequency> fixedPaymentFrequency,
267 const Calendar& fixedCalendar)
268 : DatedOISRateHelper(startDate, endDate, fixedRate, overnightIndex, std::move(discount), telescopicValueDates,
269 averagingMethod, paymentLag, paymentConvention, paymentFrequency, paymentCalendar,
270 overnightSpread, endOfMonth, fixedPaymentFrequency, fixedCalendar) {}
271
272}
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
bool empty() const
Returns whether or not the calendar is initialized.
Definition: calendar.hpp:208
Concrete date class.
Definition: date.hpp:125
DatedOISRateHelper(const Date &startDate, const Date &endDate, const Handle< Quote > &fixedRate, const ext::shared_ptr< OvernightIndex > &overnightIndex, Handle< YieldTermStructure > discountingCurve={}, bool telescopicValueDates=false, RateAveraging::Type averagingMethod=RateAveraging::Compound, Integer paymentLag=0, BusinessDayConvention paymentConvention=Following, Frequency paymentFrequency=Annual, const Calendar &paymentCalendar=Calendar(), Spread overnightSpread={}, ext::optional< bool > endOfMonth=ext::nullopt, ext::optional< Frequency > fixedPaymentFrequency=ext::nullopt, const Calendar &fixedCalendar=Calendar(), Natural lookbackDays=Null< Natural >(), Natural lockoutDays=0, bool applyObservationShift=false, const ext::shared_ptr< FloatingRateCouponPricer > &pricer={})
Shared handle to an observable.
Definition: handle.hpp:41
helper class
Definition: makeois.hpp:39
MakeOIS & withPaymentAdjustment(BusinessDayConvention convention)
Definition: makeois.cpp:214
MakeOIS & withPaymentLag(Integer lag)
Definition: makeois.cpp:219
MakeOIS & withDiscountingTermStructure(const Handle< YieldTermStructure > &discountingTermStructure)
Definition: makeois.cpp:257
MakeOIS & withLockoutDays(Natural lockoutDays)
Definition: makeois.cpp:341
MakeOIS & withSettlementDays(Natural settlementDays)
Definition: makeois.cpp:182
MakeOIS & withPaymentFrequency(Frequency f)
Definition: makeois.cpp:200
MakeOIS & withRule(DateGeneration::Rule r)
Definition: makeois.cpp:243
MakeOIS & withLookbackDays(Natural lookbackDays)
Definition: makeois.cpp:336
MakeOIS & withFixedLegCalendar(const Calendar &cal)
Definition: makeois.cpp:233
MakeOIS & withPaymentCalendar(const Calendar &cal)
Definition: makeois.cpp:224
MakeOIS & withTerminationDate(const Date &)
Definition: makeois.cpp:193
MakeOIS & withEffectiveDate(const Date &)
Definition: makeois.cpp:188
MakeOIS & withEndOfMonth(bool flag=true)
Definition: makeois.cpp:305
MakeOIS & withFixedLegPaymentFrequency(Frequency f)
Definition: makeois.cpp:204
MakeOIS & withAveragingMethod(RateAveraging::Type averagingMethod)
Definition: makeois.cpp:331
MakeOIS & withTelescopicValueDates(bool telescopicValueDates)
Definition: makeois.cpp:326
MakeOIS & withObservationShift(bool applyObservationShift=true)
Definition: makeois.cpp:346
MakeOIS & withOvernightLegCalendar(const Calendar &cal)
Definition: makeois.cpp:238
Rate helper for bootstrapping over Overnight Indexed Swap rates.
ext::shared_ptr< OvernightIndex > overnightIndex_
RelinkableHandle< YieldTermStructure > discountRelinkableHandle_
void setTermStructure(YieldTermStructure *) override
void initialize(const ext::shared_ptr< OvernightIndex > &overnightIndex, Date customPillarDate)
RelinkableHandle< YieldTermStructure > termStructureHandle_
Pillar::Choice pillarChoice_
Handle< YieldTermStructure > discountHandle_
OISRateHelper(Natural settlementDays, const Period &tenor, const Handle< Quote > &fixedRate, const ext::shared_ptr< OvernightIndex > &overnightIndex, Handle< YieldTermStructure > discountingCurve={}, bool telescopicValueDates=false, Integer paymentLag=0, BusinessDayConvention paymentConvention=Following, Frequency paymentFrequency=Annual, Calendar paymentCalendar=Calendar(), const Period &forwardStart=0 *Days, const std::variant< Spread, Handle< Quote > > &overnightSpread=Spread(0.0), Pillar::Choice pillar=Pillar::LastRelevantDate, Date customPillarDate=Date(), RateAveraging::Type averagingMethod=RateAveraging::Compound, ext::optional< bool > endOfMonth=ext::nullopt, ext::optional< Frequency > fixedPaymentFrequency=ext::nullopt, Calendar fixedCalendar=Calendar(), Natural lookbackDays=Null< Natural >(), Natural lockoutDays=0, bool applyObservationShift=false, ext::shared_ptr< FloatingRateCouponPricer > pricer={}, DateGeneration::Rule rule=DateGeneration::Backward, Calendar overnightCalendar=Calendar())
ext::shared_ptr< FloatingRateCouponPricer > pricer_
void accept(AcyclicVisitor &) override
ext::optional< bool > endOfMonth_
DateGeneration::Rule rule_
RateAveraging::Type averagingMethod_
void initializeDates() override
ext::shared_ptr< OvernightIndexedSwap > swap_
BusinessDayConvention paymentConvention_
Real impliedQuote() const override
Handle< Quote > overnightSpread_
ext::optional< Frequency > fixedPaymentFrequency_
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
purely virtual base class for market observables
Definition: quote.hpp:38
Bootstrap helper with date schedule relative to global evaluation date.
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Interest-rate term structure.
ext::shared_ptr< FloatingRateCouponPricer > pricer_
Coupon pricers.
Calendar paymentCalendar_
Integer paymentLag_
const DefaultType & t
discounting swap engine
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Spread
spreads on interest rates
Definition: types.hpp:74
Helper class to instantiate overnight indexed swaps.
Definition: any.hpp:37
Handle< Quote > handleFromVariant(const std::variant< Real, Handle< Quote > > &value)
Definition: quote.cpp:27
void setCouponPricer(const Leg &leg, const ext::shared_ptr< FloatingRateCouponPricer > &pricer)
void simplifyNotificationGraph(Instrument &instrument, const Leg &leg, bool unregisterCoupons)
Utility function to optimize the observability graph of an instrument.
STL namespace.
empty deleter for shared_ptr
Overnight Indexed Swap (aka OIS) rate helpers.
ext::shared_ptr< BlackVolTermStructure > v
utility functions to reduce number of notifications sent by observables
Choice
Alternatives ways of determining the pillar date.
@ CustomDate
last date relevant for instrument pricing
@ LastRelevantDate
instruments maturity date