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

lballabio / QuantLib / 18089177727

29 Sep 2025 07:23AM UTC coverage: 73.914% (+0.1%) from 73.816%
18089177727

Pull #2265

github

web-flow
Merge 13a7b3b53 into 787317697
Pull Request #2265: Fix potential dangling reference in MultiCubicSpline

2 of 2 new or added lines in 1 file covered. (100.0%)

219 existing lines in 29 files now uncovered.

57063 of 77202 relevant lines covered (73.91%)

8763673.17 hits per line

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

62.03
/ql/instruments/makevanillaswap.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2

3
/*
4
 Copyright (C) 2006, 2007, 2010, 2014, 2015 Ferdinando Ametrano
5
 Copyright (C) 2006 Katiuscia Manzoni
6
 Copyright (C) 2006 StatPro Italia srl
7
 Copyright (C) 2015 Paolo Mazzocchi
8
 Copyright (C) 2018 Matthias Groncki
9

10
 This file is part of QuantLib, a free-software/open-source library
11
 for financial quantitative analysts and developers - http://quantlib.org/
12

13
 QuantLib is free software: you can redistribute it and/or modify it
14
 under the terms of the QuantLib license.  You should have received a
15
 copy of the license along with this program; if not, please email
16
 <quantlib-dev@lists.sf.net>. The license is also available online at
17
 <https://www.quantlib.org/license.shtml>.
18

19
 This program is distributed in the hope that it will be useful, but WITHOUT
20
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21
 FOR A PARTICULAR PURPOSE.  See the license for more details.
22
*/
23

24
#include <ql/instruments/makevanillaswap.hpp>
25
#include <ql/pricingengines/swap/discountingswapengine.hpp>
26
#include <ql/time/daycounters/thirty360.hpp>
27
#include <ql/time/daycounters/actual360.hpp>
28
#include <ql/time/daycounters/actual365fixed.hpp>
29
#include <ql/indexes/iborindex.hpp>
30
#include <ql/time/schedule.hpp>
31
#include <ql/currencies/america.hpp>
32
#include <ql/currencies/asia.hpp>
33
#include <ql/currencies/europe.hpp>
34
#include <ql/currencies/oceania.hpp>
35
#include <ql/utilities/null.hpp>
36
#include <ql/optional.hpp>
37

38
namespace QuantLib {
39

40
    MakeVanillaSwap::MakeVanillaSwap(const Period& swapTenor,
34,601✔
41
                                     const ext::shared_ptr<IborIndex>& index,
42
                                     Rate fixedRate,
43
                                     const Period& forwardStart)
34,601✔
44
    : swapTenor_(swapTenor), iborIndex_(index), fixedRate_(fixedRate), forwardStart_(forwardStart),
34,601✔
45
      fixedCalendar_(index->fixingCalendar()), floatCalendar_(index->fixingCalendar()),
34,601✔
46
      floatTenor_(index->tenor()),
34,601✔
47
      floatConvention_(index->businessDayConvention()),
69,202✔
48
      floatTerminationDateConvention_(index->businessDayConvention()),
34,601✔
49

50
      floatDayCount_(index->dayCounter()) {}
138,404✔
51

52
    MakeVanillaSwap::operator VanillaSwap() const {
8,034✔
53
        ext::shared_ptr<VanillaSwap> swap = *this;
8,034✔
54
        return *swap;
16,068✔
55
    }
56

57
    MakeVanillaSwap::operator ext::shared_ptr<VanillaSwap>() const {
34,601✔
58

59
        Date startDate;
34,601✔
60
        if (effectiveDate_ != Date())
34,601✔
61
            startDate = effectiveDate_;
25,459✔
62
        else {
63
            Date refDate = Settings::instance().evaluationDate();
9,142✔
64
            // if the evaluation date is not a business day
65
            // then move to the next business day
66
            refDate = floatCalendar_.adjust(refDate);
9,142✔
67
            // use index valueDate interface wherever possible to estimate spot date.
68
            // Unless we pass an explicit settlementDays_ which does not match the index-defined number of fixing days.
69
            Date spotDate;
9,142✔
70
            if (settlementDays_ == Null<Natural>())
9,142✔
71
                spotDate = iborIndex_->valueDate(refDate);
9,136✔
72
            else
73
                spotDate = floatCalendar_.advance(refDate, settlementDays_ * Days);
6✔
74
            startDate = spotDate+forwardStart_;
9,142✔
75
            if (forwardStart_.length()<0)
9,142✔
UNCOV
76
                startDate = floatCalendar_.adjust(startDate,
×
77
                                                  Preceding);
78
            else if (forwardStart_.length()>0)
9,142✔
79
                startDate = floatCalendar_.adjust(startDate,
×
80
                                                  Following);
81
            // no explicit date adjustment needed for forwardStart_.length()==0 (already handled by spotDate arithmetic above)
82
        }
83

84
        Date endDate = terminationDate_;
34,601✔
85
        if (endDate == Date()) {
34,601✔
86
            if (floatEndOfMonth_)
32,071✔
UNCOV
87
                endDate = floatCalendar_.advance(startDate,
×
UNCOV
88
                                                 swapTenor_,
×
89
                                                 ModifiedFollowing,
90
                                                 floatEndOfMonth_);
91
            else
92
                endDate = startDate + swapTenor_;
32,071✔
93
        }
94

95
        const Currency& curr = iborIndex_->currency();
34,601✔
96
        Period fixedTenor;
34,601✔
97
        if (fixedTenor_ != Period())
34,601✔
98
            fixedTenor = fixedTenor_;
31,505✔
99
        else {
100
            if ((curr == EURCurrency()) ||
6,192✔
101
                (curr == USDCurrency()) ||
3,096✔
102
                (curr == CHFCurrency()) ||
3,096✔
103
                (curr == SEKCurrency()) ||
6,192✔
104
                (curr == GBPCurrency() && swapTenor_ <= 1 * Years))
3,096✔
105
                fixedTenor = Period(1, Years);
3,096✔
UNCOV
106
            else if ((curr == GBPCurrency() && swapTenor_ > 1 * Years) ||
×
UNCOV
107
                (curr == JPYCurrency()) ||
×
UNCOV
108
                (curr == AUDCurrency() && swapTenor_ >= 4 * Years))
×
109
                fixedTenor = Period(6, Months);
×
110
            else if ((curr == HKDCurrency() ||
×
111
                     (curr == AUDCurrency() && swapTenor_ < 4 * Years)))
×
112
                fixedTenor = Period(3, Months);
×
113
            else
114
                QL_FAIL("unknown fixed leg default tenor for " << curr);
×
115
        }
116

117
        Schedule fixedSchedule(startDate, endDate,
118
                               fixedTenor, fixedCalendar_,
119
                               fixedConvention_,
34,601✔
120
                               fixedTerminationDateConvention_,
34,601✔
121
                               fixedRule_, fixedEndOfMonth_,
34,601✔
122
                               fixedFirstDate_, fixedNextToLastDate_);
34,601✔
123

124
        Schedule floatSchedule(startDate, endDate,
125
                               floatTenor_, floatCalendar_,
34,601✔
126
                               floatConvention_,
34,601✔
127
                               floatTerminationDateConvention_,
34,601✔
128
                               floatRule_, floatEndOfMonth_,
34,601✔
129
                               floatFirstDate_, floatNextToLastDate_);
34,601✔
130

131
        DayCounter fixedDayCount;
34,601✔
132
        if (fixedDayCount_ != DayCounter())
69,202✔
133
            fixedDayCount = fixedDayCount_;
134
        else {
135
            if (curr == USDCurrency())
6,192✔
UNCOV
136
                fixedDayCount = Actual360();
×
137
            else if (curr == EURCurrency() || curr == CHFCurrency() ||
9,288✔
138
                     curr == SEKCurrency())
3,096✔
139
                fixedDayCount = Thirty360(Thirty360::BondBasis);
6,192✔
UNCOV
140
            else if (curr == GBPCurrency() || curr == JPYCurrency() ||
×
UNCOV
141
                     curr == AUDCurrency() || curr == HKDCurrency() ||
×
UNCOV
142
                     curr == THBCurrency())
×
143
                fixedDayCount = Actual365Fixed();
×
144
            else
145
                QL_FAIL("unknown fixed leg day counter for " << curr);
×
146
        }
147

148
        Rate usedFixedRate = fixedRate_;
34,601✔
149
        if (fixedRate_ == Null<Rate>()) {
34,601✔
UNCOV
150
            VanillaSwap temp(type_, 100.00, fixedSchedule,
×
151
                             0.0, // fixed rate
UNCOV
152
                             fixedDayCount, floatSchedule, iborIndex_, floatSpread_, floatDayCount_,
×
153
                             paymentConvention_, useIndexedCoupons_);
×
UNCOV
154
            if (engine_ == nullptr) {
×
155
                Handle<YieldTermStructure> disc =
156
                                        iborIndex_->forwardingTermStructure();
×
157
                QL_REQUIRE(!disc.empty(),
×
158
                           "null term structure set to this instance of " <<
159
                           iborIndex_->name());
160
                bool includeSettlementDateFlows = false;
161
                ext::shared_ptr<PricingEngine> engine(new
UNCOV
162
                    DiscountingSwapEngine(disc, includeSettlementDateFlows));
×
UNCOV
163
                temp.setPricingEngine(engine);
×
164
            } else
165
                temp.setPricingEngine(engine_);
×
166

UNCOV
167
            usedFixedRate = temp.fairRate();
×
168
        }
×
169

170
        ext::shared_ptr<VanillaSwap> swap(new VanillaSwap(
171
            type_, nominal_, fixedSchedule, usedFixedRate, fixedDayCount, floatSchedule, iborIndex_,
34,601✔
172
            floatSpread_, floatDayCount_, paymentConvention_, useIndexedCoupons_));
103,803✔
173

174
        if (engine_ == nullptr) {
34,601✔
175
            Handle<YieldTermStructure> disc =
176
                                    iborIndex_->forwardingTermStructure();
30,227✔
177
            bool includeSettlementDateFlows = false;
178
            ext::shared_ptr<PricingEngine> engine(new
179
                DiscountingSwapEngine(disc, includeSettlementDateFlows));
30,227✔
180
            swap->setPricingEngine(engine);
30,227✔
181
        } else
182
            swap->setPricingEngine(engine_);
4,374✔
183

184
        return swap;
34,601✔
185
    }
34,601✔
186

187
    MakeVanillaSwap& MakeVanillaSwap::receiveFixed(bool flag) {
561✔
188
        type_ = flag ? Swap::Receiver : Swap::Payer ;
561✔
189
        return *this;
561✔
190
    }
191

192
    MakeVanillaSwap& MakeVanillaSwap::withType(Swap::Type type) {
11,382✔
193
        type_ = type;
11,382✔
194
        return *this;
11,382✔
195
    }
196

197
    MakeVanillaSwap& MakeVanillaSwap::withNominal(Real n) {
2,645✔
198
        nominal_ = n;
2,645✔
199
        return *this;
2,645✔
200
    }
201

202
    MakeVanillaSwap& MakeVanillaSwap::withSettlementDays(Natural settlementDays) {
1,301✔
203
        settlementDays_ = settlementDays;
1,301✔
204
        effectiveDate_ = Date();
1,301✔
205
        return *this;
1,301✔
206
    }
207

208
    MakeVanillaSwap&
209
    MakeVanillaSwap::withEffectiveDate(const Date& effectiveDate) {
26,749✔
210
        effectiveDate_ = effectiveDate;
26,749✔
211
        return *this;
26,749✔
212
    }
213

214
    MakeVanillaSwap&
215
    MakeVanillaSwap::withTerminationDate(const Date& terminationDate) {
3,820✔
216
        terminationDate_ = terminationDate;
3,820✔
217
        if (terminationDate != Date())
3,820✔
218
            swapTenor_ = Period();
2,530✔
219
        return *this;
3,820✔
220
    }
221

UNCOV
222
    MakeVanillaSwap& MakeVanillaSwap::withRule(DateGeneration::Rule r) {
×
UNCOV
223
        fixedRule_ = r;
×
UNCOV
224
        floatRule_ = r;
×
225
        return *this;
×
226
    }
227

228
    MakeVanillaSwap& MakeVanillaSwap::withPaymentConvention(BusinessDayConvention bdc) {
×
UNCOV
229
        paymentConvention_ = bdc;
×
UNCOV
230
        return *this;
×
231
    }
232

233
    MakeVanillaSwap& MakeVanillaSwap::withDiscountingTermStructure(
4,374✔
234
                                        const Handle<YieldTermStructure>& d) {
235
        bool includeSettlementDateFlows = false;
236
        engine_ = ext::shared_ptr<PricingEngine>(new
13,122✔
237
            DiscountingSwapEngine(d, includeSettlementDateFlows));
8,748✔
238
        return *this;
4,374✔
239
    }
240

UNCOV
241
    MakeVanillaSwap& MakeVanillaSwap::withPricingEngine(
×
242
                             const ext::shared_ptr<PricingEngine>& engine) {
UNCOV
243
        engine_ = engine;
×
244
        return *this;
×
245
    }
246

247
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegTenor(const Period& t) {
31,505✔
248
        fixedTenor_ = t;
31,505✔
249
        return *this;
31,505✔
250
    }
251

252
    MakeVanillaSwap&
253
    MakeVanillaSwap::withFixedLegCalendar(const Calendar& cal) {
14,754✔
254
        fixedCalendar_ = cal;
255
        return *this;
14,754✔
256
    }
257

258
    MakeVanillaSwap&
259
    MakeVanillaSwap::withFixedLegConvention(BusinessDayConvention bdc) {
14,927✔
260
        fixedConvention_ = bdc;
14,927✔
261
        return *this;
14,927✔
262
    }
263

264
    MakeVanillaSwap&
265
    MakeVanillaSwap::withFixedLegTerminationDateConvention(BusinessDayConvention bdc) {
14,921✔
266
        fixedTerminationDateConvention_ = bdc;
14,921✔
267
        return *this;
14,921✔
268
    }
269

UNCOV
270
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegRule(DateGeneration::Rule r) {
×
UNCOV
271
        fixedRule_ = r;
×
UNCOV
272
        return *this;
×
273
    }
274

275
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegEndOfMonth(bool flag) {
1,295✔
276
        fixedEndOfMonth_ = flag;
1,295✔
277
        return *this;
1,295✔
278
    }
279

UNCOV
280
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegFirstDate(const Date& d) {
×
UNCOV
281
        fixedFirstDate_ = d;
×
UNCOV
282
        return *this;
×
283
    }
284

285
    MakeVanillaSwap&
UNCOV
286
    MakeVanillaSwap::withFixedLegNextToLastDate(const Date& d) {
×
UNCOV
287
        fixedNextToLastDate_ = d;
×
UNCOV
288
        return *this;
×
289
    }
290

291
    MakeVanillaSwap&
292
    MakeVanillaSwap::withFixedLegDayCount(const DayCounter& dc) {
31,505✔
293
        fixedDayCount_ = dc;
294
        return *this;
31,505✔
295
    }
296

UNCOV
297
    MakeVanillaSwap& MakeVanillaSwap::withFloatingLegTenor(const Period& t) {
×
UNCOV
298
        floatTenor_ = t;
×
UNCOV
299
        return *this;
×
300
    }
301

302
    MakeVanillaSwap&
303
    MakeVanillaSwap::withFloatingLegCalendar(const Calendar& cal) {
1,316✔
304
        floatCalendar_ = cal;
305
        return *this;
1,316✔
306
    }
307

308
    MakeVanillaSwap&
UNCOV
309
    MakeVanillaSwap::withFloatingLegConvention(BusinessDayConvention bdc) {
×
UNCOV
310
        floatConvention_ = bdc;
×
UNCOV
311
        return *this;
×
312
    }
313

314
    MakeVanillaSwap&
UNCOV
315
    MakeVanillaSwap::withFloatingLegTerminationDateConvention(BusinessDayConvention bdc) {
×
UNCOV
316
        floatTerminationDateConvention_ = bdc;
×
UNCOV
317
        return *this;
×
318
    }
319

320
    MakeVanillaSwap& MakeVanillaSwap::withFloatingLegRule(DateGeneration::Rule r) {
×
UNCOV
321
        floatRule_ = r;
×
UNCOV
322
        return *this;
×
323
    }
324

325
    MakeVanillaSwap& MakeVanillaSwap::withFloatingLegEndOfMonth(bool flag) {
1,295✔
326
        floatEndOfMonth_ = flag;
1,295✔
327
        return *this;
1,295✔
328
    }
329

330
    MakeVanillaSwap&
UNCOV
331
    MakeVanillaSwap::withFloatingLegFirstDate(const Date& d) {
×
UNCOV
332
        floatFirstDate_ = d;
×
UNCOV
333
        return *this;
×
334
    }
335

336
    MakeVanillaSwap&
UNCOV
337
    MakeVanillaSwap::withFloatingLegNextToLastDate(const Date& d) {
×
UNCOV
338
        floatNextToLastDate_ = d;
×
UNCOV
339
        return *this;
×
340
    }
341

342
    MakeVanillaSwap&
UNCOV
343
    MakeVanillaSwap::withFloatingLegDayCount(const DayCounter& dc) {
×
344
        floatDayCount_ = dc;
UNCOV
345
        return *this;
×
346
    }
347

348
    MakeVanillaSwap& MakeVanillaSwap::withFloatingLegSpread(Spread sp) {
8,737✔
349
        floatSpread_ = sp;
8,737✔
350
        return *this;
8,737✔
351
    }
352

353
    MakeVanillaSwap& MakeVanillaSwap::withIndexedCoupons(const ext::optional<bool>& b) {
1,399✔
354
        useIndexedCoupons_ = b;
1,399✔
355
        return *this;
1,399✔
356
    }
357

UNCOV
358
    MakeVanillaSwap& MakeVanillaSwap::withAtParCoupons(bool b) {
×
UNCOV
359
        useIndexedCoupons_ = !b;
×
UNCOV
360
        return *this;
×
361
    }
362

363
}
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