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

lballabio / QuantLib / 22997032504

12 Mar 2026 10:16AM UTC coverage: 74.309% (+0.04%) from 74.266%
22997032504

Pull #2479

github

web-flow
Merge e585cfef3 into 0a02cced0
Pull Request #2479: Bring `MakeCreditDefaultSwap` up to date with CDS instrument

51 of 62 new or added lines in 1 file covered. (82.26%)

57806 of 77791 relevant lines covered (74.31%)

8756024.3 hits per line

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

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

3
/*
4
 Copyright (C) 2014 Jose Aparicio
5
 Copyright (C) 2014 Peter Caspers
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
 <https://www.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/makecds.hpp>
22
#include <ql/time/daycounters/actual360.hpp>
23
#include <ql/time/calendars/weekendsonly.hpp>
24

25

26
namespace QuantLib {
27

28
    MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Period& tenor,
12✔
29
                                                 Rate runningSpread)
12✔
30
    : tenor_(tenor), runningSpread_(runningSpread) {}
12✔
31

32
    MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Date& termDate,
95✔
33
                                                 Rate runningSpread)
95✔
34
    : termDate_(termDate), runningSpread_(runningSpread) {}
95✔
35

36
    MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Schedule& schedule,
1✔
37
                                                 Rate runningSpread)
1✔
38
    : schedule_(schedule), runningSpread_(runningSpread) {}
1✔
39

40

41
    MakeCreditDefaultSwap::operator CreditDefaultSwap() const {
10✔
42
        ext::shared_ptr<CreditDefaultSwap> swap = *this;
10✔
43
        return *swap;
20✔
44
    }
45

46
    MakeCreditDefaultSwap::operator ext::shared_ptr<CreditDefaultSwap>() const {
108✔
47

48
        Date tradeDate = tradeDate_ != Date() ? tradeDate_ : Settings::instance().evaluationDate();
108✔
49
        Date upfrontDate = upfrontDate_ != Date() ? upfrontDate_ : WeekendsOnly().advance(tradeDate, cashSettlementDays_, Days);
108✔
50

51
        Date protectionStart = protectionStart_;
108✔
52
        if (protectionStart == Date()) {
108✔
53
            if (schedule_) { // NOLINT(readability-implicit-bool-conversion)
107✔
54
                protectionStart = schedule_->at(0);
1✔
55
            } else {
56
                if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS) {
106✔
57
                    protectionStart = tradeDate;
106✔
58
                } else {
NEW
59
                    protectionStart = tradeDate + 1;
×
60
                }
61
            }
62
        }
63

64
        // schedule, tenor and term date come from different constructors; exactly one of them is not null.
65
        Schedule schedule;
108✔
66
        if (schedule_) { // NOLINT(readability-implicit-bool-conversion)
108✔
67
            schedule = *schedule_;
1✔
68
        } else {
69
            Date end;
107✔
70
            if (tenor_) { // NOLINT(readability-implicit-bool-conversion)
107✔
71
                if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS || rule_ == DateGeneration::OldCDS) {
12✔
72
                    end = cdsMaturity(tradeDate, *tenor_, rule_);
12✔
73
                } else {
NEW
74
                    end = tradeDate + *tenor_;
×
75
                }
76
            } else {
77
                end = *termDate_; // NOLINT(bugprone-unchecked-optional-access)
95✔
78
            }
79

80
            schedule = Schedule(protectionStart, end, couponTenor_, WeekendsOnly(), convention_,
321✔
81
                                Unadjusted, rule_, false);
214✔
82
        }
83

84
        auto cds = ext::make_shared<CreditDefaultSwap>(
85
            side_, nominal_, upfrontRate_, runningSpread_, schedule, convention_,
108✔
86
            dayCounter_, settlesAccrual_, paysAtDefaultTime_, protectionStart, upfrontDate,
108✔
87
            claim_, lastPeriodDayCounter_, rebatesAccrual_, tradeDate, cashSettlementDays_);
108✔
88

89
        cds->setPricingEngine(engine_);
108✔
90
        return cds;
108✔
91
    }
108✔
92

93

94
    MakeCreditDefaultSwap &
95
    MakeCreditDefaultSwap::withSide(Protection::Side side) {
40✔
96
        side_ = side;
40✔
97
        return *this;
40✔
98
    }
99

100
    MakeCreditDefaultSwap &MakeCreditDefaultSwap::withNominal(Real nominal) {
95✔
101
        nominal_ = nominal;
95✔
102
        return *this;
95✔
103
    }
104

105
    MakeCreditDefaultSwap &
106
    MakeCreditDefaultSwap::withUpfrontRate(Real upfrontRate) {
41✔
107
        upfrontRate_ = upfrontRate;
41✔
108
        return *this;
41✔
109
    }
110

111
    MakeCreditDefaultSwap &
112
    MakeCreditDefaultSwap::withCouponTenor(Period couponTenor) {
1✔
113
        couponTenor_ = couponTenor;
1✔
114
        return *this;
1✔
115
    }
116

NEW
117
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withDateGenerationRule(DateGeneration::Rule rule) {
×
NEW
118
        rule_ = rule;
×
NEW
119
        return *this;
×
120
    }
121

NEW
122
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withConvention(BusinessDayConvention convention) {
×
NEW
123
        convention_ = convention;
×
NEW
124
        return *this;
×
125
    }
126

127
    MakeCreditDefaultSwap &
128
    MakeCreditDefaultSwap::withDayCounter(const DayCounter& dayCounter) {
1✔
129
        dayCounter_ = dayCounter;
130
        return *this;
1✔
131
    }
132

133
    MakeCreditDefaultSwap &
134
    MakeCreditDefaultSwap::settleAccrual(bool b) {
1✔
135
        settlesAccrual_ = b;
1✔
136
        return *this;
1✔
137
    }
138

139
    MakeCreditDefaultSwap &
140
    MakeCreditDefaultSwap::payAtDefaultTime(bool b) {
1✔
141
        paysAtDefaultTime_ = b;
1✔
142
        return *this;
1✔
143
    }
144

145
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withProtectionStart(Date d) {
1✔
146
        protectionStart_ = d;
1✔
147
        return *this;
1✔
148
    }
149

150
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withUpfrontDate(Date d) {
1✔
151
        upfrontDate_ = d;
1✔
152
        return *this;
1✔
153
    }
154

NEW
155
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withClaim(ext::shared_ptr<Claim> claim) {
×
NEW
156
        claim_ = claim;
×
NEW
157
        return *this;
×
158
    }
159

160
    MakeCreditDefaultSwap &MakeCreditDefaultSwap::withLastPeriodDayCounter(
1✔
161
        const DayCounter& lastPeriodDayCounter) {
162
        lastPeriodDayCounter_ = lastPeriodDayCounter;
163
        return *this;
1✔
164
    }
165

166
    MakeCreditDefaultSwap &
167
    MakeCreditDefaultSwap::rebateAccrual(bool b) {
1✔
168
        rebatesAccrual_ = b;
1✔
169
        return *this;
1✔
170
    }
171

172
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withTradeDate(Date tradeDate) {
2✔
173
        tradeDate_ = tradeDate;
2✔
174
        return *this;
2✔
175
    }
176

177
    MakeCreditDefaultSwap& MakeCreditDefaultSwap::withCashSettlementDays(Natural cashSettlementDays) {
2✔
178
        cashSettlementDays_ = cashSettlementDays;
2✔
179
        return *this;
2✔
180
    }
181

182
    MakeCreditDefaultSwap &MakeCreditDefaultSwap::withPricingEngine(
62✔
183
                               const ext::shared_ptr<PricingEngine> &engine) {
184
        engine_ = engine;
62✔
185
        return *this;
62✔
186
    }
187

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