• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 10286624206

07 Aug 2024 02:50PM UTC coverage: 72.651% (+0.01%) from 72.639%
10286624206

push

github

web-flow
Allow passing a pricer to OISRateHelper, deprecate superseded experimental classes (#2052)

7 of 8 new or added lines in 1 file covered. (87.5%)

3 existing lines in 3 files now uncovered.

55085 of 75821 relevant lines covered (72.65%)

8638462.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.2
/ql/termstructures/yield/oisratehelper.cpp
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

21
#include <ql/instruments/makeois.hpp>
22
#include <ql/instruments/simplifynotificationgraph.hpp>
23
#include <ql/cashflows/couponpricer.hpp>
24
#include <ql/pricingengines/swap/discountingswapengine.hpp>
25
#include <ql/termstructures/yield/oisratehelper.hpp>
26
#include <ql/utilities/null_deleter.hpp>
27
#include <utility>
28

29
namespace QuantLib {
30

31
    OISRateHelper::OISRateHelper(Natural settlementDays,
410✔
32
                                 const Period& tenor, // swap maturity
33
                                 const Handle<Quote>& fixedRate,
34
                                 const ext::shared_ptr<OvernightIndex>& overnightIndex,
35
                                 Handle<YieldTermStructure> discount,
36
                                 bool telescopicValueDates,
37
                                 Integer paymentLag,
38
                                 BusinessDayConvention paymentConvention,
39
                                 Frequency paymentFrequency,
40
                                 Calendar paymentCalendar,
41
                                 const Period& forwardStart,
42
                                 const Spread 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)
410✔
53
    : RelativeDateRateHelper(fixedRate), pillarChoice_(pillar), settlementDays_(settlementDays), tenor_(tenor),
410✔
54
      discountHandle_(std::move(discount)), telescopicValueDates_(telescopicValueDates),
410✔
55
      paymentLag_(paymentLag), paymentConvention_(paymentConvention),
410✔
56
      paymentFrequency_(paymentFrequency), paymentCalendar_(std::move(paymentCalendar)),
410✔
57
      forwardStart_(forwardStart), overnightSpread_(overnightSpread),
410✔
58
      averagingMethod_(averagingMethod), endOfMonth_(endOfMonth),
410✔
59
      fixedPaymentFrequency_(fixedPaymentFrequency), fixedCalendar_(std::move(fixedCalendar)),
410✔
60
      lookbackDays_(lookbackDays), lockoutDays_(lockoutDays), applyObservationShift_(applyObservationShift),
410✔
61
      pricer_(std::move(pricer)) {
410✔
62

63
        overnightIndex_ =
64
            ext::dynamic_pointer_cast<OvernightIndex>(overnightIndex->clone(termStructureHandle_));
820✔
65
        // We want to be notified of changes of fixings, but we don't
66
        // want notifications from termStructureHandle_ (they would
67
        // interfere with bootstrapping.)
68
        overnightIndex_->unregisterWith(termStructureHandle_);
410✔
69

70
        registerWith(overnightIndex_);
820✔
71
        registerWith(discountHandle_);
412✔
72

73
        pillarDate_ = customPillarDate;
410✔
74
        OISRateHelper::initializeDates();
410✔
75
    }
408✔
76

77
    void OISRateHelper::initializeDates() {
410✔
78

79
        // input discount curve Handle might be empty now but it could
80
        //    be assigned a curve later; use a RelinkableHandle here
81
        MakeOIS tmp = MakeOIS(tenor_, overnightIndex_, 0.0, forwardStart_)
820✔
82
            .withDiscountingTermStructure(discountRelinkableHandle_)
410✔
83
            .withSettlementDays(settlementDays_)
410✔
84
            .withTelescopicValueDates(telescopicValueDates_)
410✔
85
            .withPaymentLag(paymentLag_)
410✔
86
            .withPaymentAdjustment(paymentConvention_)
410✔
87
            .withPaymentFrequency(paymentFrequency_)
410✔
88
            .withPaymentCalendar(paymentCalendar_)
410✔
89
            .withOvernightLegSpread(overnightSpread_)
410✔
90
            .withAveragingMethod(averagingMethod_)
410✔
91
            .withLookbackDays(lookbackDays_)
410✔
92
            .withLockoutDays(lockoutDays_)
410✔
93
            .withObservationShift(applyObservationShift_);
410✔
94
        if (endOfMonth_) {
410✔
95
            tmp.withEndOfMonth(*endOfMonth_);
×
96
        }
97
        if (fixedPaymentFrequency_) {
410✔
98
            tmp.withFixedLegPaymentFrequency(*fixedPaymentFrequency_);
×
99
        }
100
        if (!fixedCalendar_.empty()) {
410✔
101
            tmp.withFixedLegCalendar(fixedCalendar_);
×
102
        }
103
        swap_ = tmp;
818✔
104

105
        if (pricer_)
408✔
106
            setCouponPricer(swap_->overnightLeg(), pricer_);
33✔
107

108
        simplifyNotificationGraph(*swap_, true);
408✔
109

110
        earliestDate_ = swap_->startDate();
408✔
111
        maturityDate_ = swap_->maturityDate();
408✔
112

113
        Date lastPaymentDate = std::max(swap_->overnightLeg().back()->date(),
408✔
114
                                        swap_->fixedLeg().back()->date());
408✔
115
        latestRelevantDate_ = std::max(maturityDate_, lastPaymentDate);
408✔
116

117
        switch (pillarChoice_) {
408✔
118
          case Pillar::MaturityDate:
22✔
119
            pillarDate_ = maturityDate_;
22✔
120
            break;
22✔
121
          case Pillar::LastRelevantDate:
386✔
122
            pillarDate_ = latestRelevantDate_;
386✔
123
            break;
386✔
124
          case Pillar::CustomDate:
×
125
            // pillarDate_ already assigned at construction time
126
            QL_REQUIRE(pillarDate_ >= earliestDate_,
×
127
                       "pillar date (" << pillarDate_ << ") must be later "
128
                       "than or equal to the instrument's earliest date (" <<
129
                       earliestDate_ << ")");
130
            QL_REQUIRE(pillarDate_ <= latestRelevantDate_,
×
131
                       "pillar date (" << pillarDate_ << ") must be before "
132
                       "or equal to the instrument's latest relevant date (" <<
133
                       latestRelevantDate_ << ")");
134
            break;
135
          default:
×
136
            QL_FAIL("unknown Pillar::Choice(" << Integer(pillarChoice_) << ")");
×
137
        }
138

139
        latestDate_ = std::max(swap_->maturityDate(), lastPaymentDate);
408✔
140
    }
410✔
141

142
    void OISRateHelper::setTermStructure(YieldTermStructure* t) {
408✔
143
        // do not set the relinkable handle as an observer -
144
        // force recalculation when needed
145
        bool observer = false;
146

147
        ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
148
        termStructureHandle_.linkTo(temp, observer);
408✔
149

150
        if (discountHandle_.empty())
408✔
151
            discountRelinkableHandle_.linkTo(temp, observer);
408✔
152
        else
153
            discountRelinkableHandle_.linkTo(*discountHandle_, observer);
×
154

155
        RelativeDateRateHelper::setTermStructure(t);
408✔
156
    }
408✔
157

158
    Real OISRateHelper::impliedQuote() const {
4,213✔
159
        QL_REQUIRE(termStructure_ != nullptr, "term structure not set");
4,213✔
160
        // we didn't register as observers - force calculation
161
        swap_->deepUpdate();
4,213✔
162
        return swap_->fairRate();
4,213✔
163
    }
164

165
    void OISRateHelper::accept(AcyclicVisitor& v) {
×
166
        auto* v1 = dynamic_cast<Visitor<OISRateHelper>*>(&v);
×
167
        if (v1 != nullptr)
×
168
            v1->visit(*this);
×
169
        else
170
            RateHelper::accept(v);
×
171
    }
×
172

173
    DatedOISRateHelper::DatedOISRateHelper(const Date& startDate,
6✔
174
                                           const Date& endDate,
175
                                           const Handle<Quote>& fixedRate,
176
                                           const ext::shared_ptr<OvernightIndex>& overnightIndex,
177
                                           Handle<YieldTermStructure> discount,
178
                                           bool telescopicValueDates,
179
                                           RateAveraging::Type averagingMethod,
180
                                           Integer paymentLag,
181
                                           BusinessDayConvention paymentConvention,
182
                                           Frequency paymentFrequency,
183
                                           const Calendar& paymentCalendar,
184
                                           Spread overnightSpread,
185
                                           ext::optional<bool> endOfMonth,
186
                                           ext::optional<Frequency> fixedPaymentFrequency,
187
                                           const Calendar& fixedCalendar,
188
                                           Natural lookbackDays,
189
                                           Natural lockoutDays,
190
                                           bool applyObservationShift,
191
                                           ext::shared_ptr<FloatingRateCouponPricer> pricer)
6✔
192
    : RateHelper(fixedRate), discountHandle_(std::move(discount)),
193
      telescopicValueDates_(telescopicValueDates), averagingMethod_(averagingMethod) {
12✔
194

195
        auto clonedOvernightIndex =
196
            ext::dynamic_pointer_cast<OvernightIndex>(overnightIndex->clone(termStructureHandle_));
6✔
197
        // We want to be notified of changes of fixings, but we don't
198
        // want notifications from termStructureHandle_ (they would
199
        // interfere with bootstrapping.)
200
        clonedOvernightIndex->unregisterWith(termStructureHandle_);
6✔
201

202
        registerWith(clonedOvernightIndex);
12✔
203
        registerWith(discountHandle_);
6✔
204

205
        // input discount curve Handle might be empty now but it could
206
        //    be assigned a curve later; use a RelinkableHandle here
207
        auto tmp = MakeOIS(Period(), clonedOvernightIndex, 0.0)
12✔
208
            .withDiscountingTermStructure(discountRelinkableHandle_)
6✔
209
            .withEffectiveDate(startDate)
6✔
210
            .withTerminationDate(endDate)
6✔
211
            .withTelescopicValueDates(telescopicValueDates_)
6✔
212
            .withPaymentLag(paymentLag)
6✔
213
            .withPaymentAdjustment(paymentConvention)
6✔
214
            .withPaymentFrequency(paymentFrequency)
6✔
215
            .withPaymentCalendar(paymentCalendar)
6✔
216
            .withOvernightLegSpread(overnightSpread)
6✔
217
            .withAveragingMethod(averagingMethod_)
6✔
218
            .withLookbackDays(lookbackDays)
6✔
219
            .withLockoutDays(lockoutDays)
6✔
220
            .withObservationShift(applyObservationShift);
6✔
221
        if (endOfMonth) {
6✔
222
            tmp.withEndOfMonth(*endOfMonth);
×
223
        }
224
        if (fixedPaymentFrequency) {
6✔
225
            tmp.withFixedLegPaymentFrequency(*fixedPaymentFrequency);
×
226
        }
227
        if (!fixedCalendar.empty()) {
6✔
228
            tmp.withFixedLegCalendar(fixedCalendar);
×
229
        }
230
        swap_ = tmp;
12✔
231

232
        if (pricer)
6✔
NEW
233
            setCouponPricer(swap_->overnightLeg(), pricer);
×
234

235
        earliestDate_ = swap_->startDate();
6✔
236
        Date lastPaymentDate = std::max(swap_->overnightLeg().back()->date(),
6✔
237
                                        swap_->fixedLeg().back()->date());
6✔
238
        latestDate_ = std::max(swap_->maturityDate(), lastPaymentDate);
6✔
239
    }
12✔
240

241
    DatedOISRateHelper::DatedOISRateHelper(const Date& startDate,
×
242
                                           const Date& endDate,
243
                                           const Handle<Quote>& fixedRate,
244
                                           const ext::shared_ptr<OvernightIndex>& overnightIndex,
245
                                           Handle<YieldTermStructure> discount,
246
                                           bool telescopicValueDates,
247
                                           RateAveraging::Type averagingMethod,
248
                                           Integer paymentLag,
249
                                           BusinessDayConvention paymentConvention,
250
                                           Frequency paymentFrequency,
251
                                           const Calendar& paymentCalendar,
252
                                           const Period&,
253
                                           Spread overnightSpread,
254
                                           ext::optional<bool> endOfMonth,
255
                                           ext::optional<Frequency> fixedPaymentFrequency,
256
                                           const Calendar& fixedCalendar)
×
257
    : DatedOISRateHelper(startDate, endDate, fixedRate, overnightIndex, std::move(discount), telescopicValueDates,
258
                         averagingMethod, paymentLag, paymentConvention, paymentFrequency, paymentCalendar,
259
                         overnightSpread, endOfMonth, fixedPaymentFrequency, fixedCalendar) {}
×
260

261
    void DatedOISRateHelper::setTermStructure(YieldTermStructure* t) {
6✔
262
        // do not set the relinkable handle as an observer -
263
        // force recalculation when needed
264
        bool observer = false;
265

266
        ext::shared_ptr<YieldTermStructure> temp(t, null_deleter());
267
        termStructureHandle_.linkTo(temp, observer);
6✔
268

269
        if (discountHandle_.empty())
6✔
270
            discountRelinkableHandle_.linkTo(temp, observer);
6✔
271
        else
272
            discountRelinkableHandle_.linkTo(*discountHandle_, observer);
×
273

274
        RateHelper::setTermStructure(t);
6✔
275
    }
6✔
276

277
    Real DatedOISRateHelper::impliedQuote() const {
133✔
278
        QL_REQUIRE(termStructure_ != nullptr, "term structure not set");
133✔
279
        // we didn't register as observers - force calculation
280
        swap_->deepUpdate();
133✔
281
        return swap_->fairRate();
133✔
282
    }
283

284
    void DatedOISRateHelper::accept(AcyclicVisitor& v) {
×
285
        auto* v1 = dynamic_cast<Visitor<DatedOISRateHelper>*>(&v);
×
286
        if (v1 != nullptr)
×
287
            v1->visit(*this);
×
288
        else
289
            RateHelper::accept(v);
×
290
    }
×
291

292
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc