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

lballabio / QuantLib / 8467932009

28 Mar 2024 01:18PM UTC coverage: 72.497% (+0.07%) from 72.426%
8467932009

Pull #1593

github

web-flow
Merge 9b4efa33c into d6f6c13a5
Pull Request #1593: allow swaptions to take OvernightIndexedSwap

103 of 127 new or added lines in 13 files covered. (81.1%)

373 existing lines in 21 files now uncovered.

54966 of 75818 relevant lines covered (72.5%)

8708317.57 hits per line

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

62.9
/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
 <http://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,841✔
41
                                     const ext::shared_ptr<IborIndex>& index,
42
                                     Rate fixedRate,
43
                                     const Period& forwardStart)
34,841✔
44
    : swapTenor_(swapTenor), iborIndex_(index), fixedRate_(fixedRate), forwardStart_(forwardStart),
34,841✔
45
      settlementDays_(Null<Natural>()), fixedCalendar_(index->fixingCalendar()),
34,841✔
46
      floatCalendar_(index->fixingCalendar()),
34,841✔
47

48
      floatTenor_(index->tenor()),
34,841✔
49

50
      floatConvention_(index->businessDayConvention()),
69,682✔
51
      floatTerminationDateConvention_(index->businessDayConvention()),
34,841✔
52

53
      floatDayCount_(index->dayCounter()) {}
139,364✔
54

55
    MakeVanillaSwap::operator VanillaSwap() const {
7,861✔
56
        ext::shared_ptr<VanillaSwap> swap = *this;
7,861✔
57
        return *swap;
15,722✔
58
    }
59

60
    MakeVanillaSwap::operator ext::shared_ptr<VanillaSwap>() const {
34,841✔
61

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

87
        Date endDate = terminationDate_;
34,841✔
88
        if (endDate == Date()) {
34,841✔
89
            if (floatEndOfMonth_)
32,321✔
90
                endDate = floatCalendar_.advance(startDate,
×
91
                                                 swapTenor_,
×
92
                                                 ModifiedFollowing,
93
                                                 floatEndOfMonth_);
94
            else
95
                endDate = startDate + swapTenor_;
32,321✔
96
        }
97

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

120
        Schedule fixedSchedule(startDate, endDate,
121
                               fixedTenor, fixedCalendar_,
122
                               fixedConvention_,
34,841✔
123
                               fixedTerminationDateConvention_,
34,841✔
124
                               fixedRule_, fixedEndOfMonth_,
34,841✔
125
                               fixedFirstDate_, fixedNextToLastDate_);
34,841✔
126

127
        Schedule floatSchedule(startDate, endDate,
128
                               floatTenor_, floatCalendar_,
34,841✔
129
                               floatConvention_,
34,841✔
130
                               floatTerminationDateConvention_,
34,841✔
131
                               floatRule_, floatEndOfMonth_,
34,841✔
132
                               floatFirstDate_, floatNextToLastDate_);
34,841✔
133

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

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

170
            usedFixedRate = temp.fairRate();
×
171
        }
×
172

173
        ext::shared_ptr<VanillaSwap> swap(new VanillaSwap(
174
            type_, nominal_, fixedSchedule, usedFixedRate, fixedDayCount, floatSchedule, iborIndex_,
34,841✔
175
            floatSpread_, floatDayCount_, paymentConvention_, useIndexedCoupons_));
104,523✔
176

177
        if (engine_ == nullptr) {
34,841✔
178
            Handle<YieldTermStructure> disc =
179
                                    iborIndex_->forwardingTermStructure();
30,054✔
180
            bool includeSettlementDateFlows = false;
181
            ext::shared_ptr<PricingEngine> engine(new
182
                DiscountingSwapEngine(disc, includeSettlementDateFlows));
30,054✔
183
            swap->setPricingEngine(engine);
30,054✔
184
        } else
185
            swap->setPricingEngine(engine_);
4,787✔
186

187
        return swap;
34,841✔
188
    }
34,841✔
189

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

195
    MakeVanillaSwap& MakeVanillaSwap::withType(Swap::Type type) {
11,382✔
196
        type_ = type;
11,382✔
197
        return *this;
11,382✔
198
    }
199

200
    MakeVanillaSwap& MakeVanillaSwap::withNominal(Real n) {
2,645✔
201
        nominal_ = n;
2,645✔
202
        return *this;
2,645✔
203
    }
204

205
    MakeVanillaSwap& MakeVanillaSwap::withSettlementDays(Natural settlementDays) {
1,714✔
206
        settlementDays_ = settlementDays;
1,714✔
207
        effectiveDate_ = Date();
1,714✔
208
        return *this;
1,714✔
209
    }
210

211
    MakeVanillaSwap&
212
    MakeVanillaSwap::withEffectiveDate(const Date& effectiveDate) {
25,437✔
213
        effectiveDate_ = effectiveDate;
25,437✔
214
        return *this;
25,437✔
215
    }
216

217
    MakeVanillaSwap&
218
    MakeVanillaSwap::withTerminationDate(const Date& terminationDate) {
2,520✔
219
        terminationDate_ = terminationDate;
2,520✔
220
        swapTenor_ = Period();
2,520✔
221
        return *this;
2,520✔
222
    }
223

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

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

235
    MakeVanillaSwap& MakeVanillaSwap::withDiscountingTermStructure(
4,787✔
236
                                        const Handle<YieldTermStructure>& d) {
237
        bool includeSettlementDateFlows = false;
238
        engine_ = ext::shared_ptr<PricingEngine>(new
14,361✔
239
            DiscountingSwapEngine(d, includeSettlementDateFlows));
9,574✔
240
        return *this;
4,787✔
241
    }
242

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

249
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegTenor(const Period& t) {
31,745✔
250
        fixedTenor_ = t;
31,745✔
251
        return *this;
31,745✔
252
    }
253

254
    MakeVanillaSwap&
255
    MakeVanillaSwap::withFixedLegCalendar(const Calendar& cal) {
15,167✔
256
        fixedCalendar_ = cal;
257
        return *this;
15,167✔
258
    }
259

260
    MakeVanillaSwap&
261
    MakeVanillaSwap::withFixedLegConvention(BusinessDayConvention bdc) {
15,323✔
262
        fixedConvention_ = bdc;
15,323✔
263
        return *this;
15,323✔
264
    }
265

266
    MakeVanillaSwap&
267
    MakeVanillaSwap::withFixedLegTerminationDateConvention(BusinessDayConvention bdc) {
15,317✔
268
        fixedTerminationDateConvention_ = bdc;
15,317✔
269
        return *this;
15,317✔
270
    }
271

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

277
    MakeVanillaSwap& MakeVanillaSwap::withFixedLegEndOfMonth(bool flag) {
1,708✔
278
        fixedEndOfMonth_ = flag;
1,708✔
279
        return *this;
1,708✔
280
    }
281

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

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

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

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

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

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

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

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

327
    MakeVanillaSwap& MakeVanillaSwap::withFloatingLegEndOfMonth(bool flag) {
1,708✔
328
        floatEndOfMonth_ = flag;
1,708✔
329
        return *this;
1,708✔
330
    }
331

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

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

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

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

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

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

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