QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
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 <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/makecds.hpp>
22#include <ql/time/daycounters/actual360.hpp>
23#include <ql/time/calendars/weekendsonly.hpp>
24
25
26namespace QuantLib {
27
29 const Real couponRate)
30 : side_(Protection::Buyer), nominal_(1.0), tenor_(tenor),
31 couponTenor_(3 * Months), couponRate_(couponRate), upfrontRate_(0.0),
32 dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)),
33 rule_(DateGeneration::CDS), cashSettlementDays_(3) {}
34
36 const Real couponRate)
37 : side_(Protection::Buyer), nominal_(1.0), termDate_(termDate),
38 couponTenor_(3 * Months), couponRate_(couponRate), upfrontRate_(0.0),
39 dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)),
40 rule_(DateGeneration::CDS), cashSettlementDays_(3) {}
41
42 MakeCreditDefaultSwap::operator CreditDefaultSwap() const {
43 ext::shared_ptr<CreditDefaultSwap> swap = *this;
44 return *swap;
45 }
46
47 MakeCreditDefaultSwap::operator ext::shared_ptr<CreditDefaultSwap>() const {
48
49 Date tradeDate = (tradeDate_ != Null<Date>()) ? tradeDate_ : Settings::instance().evaluationDate();
50 Date upfrontDate = WeekendsOnly().advance(tradeDate, cashSettlementDays_, Days);
51
52 Date protectionStart;
53 if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS) {
54 protectionStart = tradeDate;
55 } else {
56 protectionStart = tradeDate + 1;
57 }
58
59 Date end;
60 if (tenor_) { // NOLINT(readability-implicit-bool-conversion)
61 if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS || rule_ == DateGeneration::OldCDS) {
62 end = cdsMaturity(tradeDate, *tenor_, rule_);
63 } else {
64 end = tradeDate + *tenor_;
65 }
66 } else {
67 end = *termDate_;
68 }
69
70 Schedule schedule(protectionStart, end, couponTenor_, WeekendsOnly(), Following,
71 Unadjusted, rule_, false);
72
73 ext::shared_ptr<CreditDefaultSwap> cds =
74 ext::make_shared<CreditDefaultSwap>(
75 side_, nominal_, upfrontRate_, couponRate_, schedule, Following,
76 dayCounter_, true, true, protectionStart, upfrontDate,
77 ext::shared_ptr<Claim>(), lastPeriodDayCounter_, true, tradeDate, cashSettlementDays_);
78
79 cds->setPricingEngine(engine_);
80 return cds;
81
82 }
83
86 upfrontRate_ = upfrontRate;
87 return *this;
88 }
89
92 side_ = side;
93 return *this;
94 }
95
97 nominal_ = nominal;
98 return *this;
99 }
100
103 couponTenor_ = couponTenor;
104 return *this;
105 }
106
109 dayCounter_ = dayCounter;
110 return *this;
111 }
112
114 DayCounter &lastPeriodDayCounter) {
115 lastPeriodDayCounter_ = lastPeriodDayCounter;
116 return *this;
117 }
118
120 rule_ = rule;
121 return *this;
122 }
123
125 cashSettlementDays_ = cashSettlementDays;
126 return *this;
127 }
128
130 const ext::shared_ptr<PricingEngine> &engine) {
131 engine_ = engine;
132 return *this;
133 }
134
136 tradeDate_ = tradeDate;
137 return *this;
138 }
139
140}
Actual/360 day count convention.
Definition: actual360.hpp:37
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Definition: calendar.cpp:130
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
MakeCreditDefaultSwap & withLastPeriodDayCounter(DayCounter &)
Definition: makecds.cpp:113
MakeCreditDefaultSwap & withTradeDate(const Date &tradeDate)
Definition: makecds.cpp:135
MakeCreditDefaultSwap & withDayCounter(DayCounter &)
Definition: makecds.cpp:108
MakeCreditDefaultSwap & withCouponTenor(Period)
Definition: makecds.cpp:102
MakeCreditDefaultSwap & withNominal(Real)
Definition: makecds.cpp:96
MakeCreditDefaultSwap(const Period &tenor, Real couponRate)
Definition: makecds.cpp:28
MakeCreditDefaultSwap & withDateGenerationRule(DateGeneration::Rule rule)
Definition: makecds.cpp:119
DateGeneration::Rule rule_
Definition: makecds.hpp:68
MakeCreditDefaultSwap & withCashSettlementDays(Natural cashSettlementDays)
Definition: makecds.cpp:124
MakeCreditDefaultSwap & withPricingEngine(const ext::shared_ptr< PricingEngine > &)
Definition: makecds.cpp:129
ext::shared_ptr< PricingEngine > engine_
Definition: makecds.hpp:72
MakeCreditDefaultSwap & withSide(Protection::Side)
Definition: makecds.cpp:91
MakeCreditDefaultSwap & withUpfrontRate(Real)
Definition: makecds.cpp:85
template class providing a null value for a given type.
Definition: null.hpp:76
Payment schedule.
Definition: schedule.hpp:40
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Weekends-only calendar.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Definition: any.hpp:35
Date cdsMaturity(const Date &tradeDate, const Period &tenor, DateGeneration::Rule rule)
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
Date-generation rule.
information on a default-protection contract
Definition: default.hpp:32