QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
zerocouponswap.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2021 Marcin Rybacki
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18 */
19
25#include <utility>
26
27namespace QuantLib {
28
29 namespace {
30
31 ext::shared_ptr<CashFlow>
32 compoundedSubPeriodicCoupon(const Date& paymentDate,
33 const Date& startDate,
34 const Date& maturityDate,
35 Real nominal,
36 const ext::shared_ptr<IborIndex>& index) {
37 Schedule schedule = MakeSchedule()
38 .from(startDate)
39 .to(maturityDate)
40 .withTenor(index->tenor())
41 .withCalendar(index->fixingCalendar())
42 .withConvention(index->businessDayConvention())
43 .backwards()
44 .endOfMonth(index->endOfMonth());
45 auto floatCpn = ext::make_shared<MultipleResetsCoupon>(
46 paymentDate, nominal, schedule, index->fixingDays(), index);
47 floatCpn->setPricer(ext::make_shared<CompoundingMultipleResetsPricer>());
48 return floatCpn;
49 }
50
51 }
52
54 Real baseNominal,
55 const Date& startDate,
56 const Date& maturityDate,
57 ext::shared_ptr<IborIndex> iborIndex,
58 const Calendar& paymentCalendar,
59 BusinessDayConvention paymentConvention,
60 Natural paymentDelay)
61 : Swap(2), type_(type), baseNominal_(baseNominal), iborIndex_(std::move(iborIndex)),
62 startDate_(startDate), maturityDate_(maturityDate) {
63
64 QL_REQUIRE(!(baseNominal < 0.0), "base nominal cannot be negative");
66 "start date (" << startDate
67 << ") later than or equal to maturity date ("
68 << maturityDate << ")");
69
70 paymentDate_ = paymentCalendar.advance(maturityDate, paymentDelay, Days, paymentConvention);
71
72 legs_[1].push_back(compoundedSubPeriodicCoupon(paymentDate_, startDate, maturityDate,
74 for (auto i = legs_[1].begin(); i < legs_[1].end(); ++i)
75 registerWith(*i);
76
77 switch (type_) {
78 case Payer:
79 payer_[0] = -1.0;
80 payer_[1] = +1.0;
81 break;
82 case Receiver:
83 payer_[0] = +1.0;
84 payer_[1] = -1.0;
85 break;
86 default:
87 QL_FAIL("unknown zero coupon swap type");
88 }
89 }
90
92 Real baseNominal,
93 const Date& startDate,
94 const Date& maturityDate,
95 Real fixedPayment,
96 ext::shared_ptr<IborIndex> iborIndex,
97 const Calendar& paymentCalendar,
98 BusinessDayConvention paymentConvention,
99 Natural paymentDelay)
100 : ZeroCouponSwap(type,
101 baseNominal,
102 startDate,
103 maturityDate,
104 std::move(iborIndex),
105 paymentCalendar,
106 paymentConvention,
107 paymentDelay) {
108
109 legs_[0].push_back(
110 ext::shared_ptr<CashFlow>(new SimpleCashFlow(fixedPayment, paymentDate_)));
111 }
112
114 Real baseNominal,
115 const Date& startDate,
116 const Date& maturityDate,
117 Rate fixedRate,
118 const DayCounter& fixedDayCounter,
119 ext::shared_ptr<IborIndex> iborIndex,
120 const Calendar& paymentCalendar,
121 BusinessDayConvention paymentConvention,
122 Natural paymentDelay)
123 : ZeroCouponSwap(type,
124 baseNominal,
125 startDate,
126 maturityDate,
127 std::move(iborIndex),
128 paymentCalendar,
129 paymentConvention,
130 paymentDelay) {
131
132 InterestRate interest(fixedRate, fixedDayCounter, Compounded, Annual);
133 legs_[0].push_back(ext::shared_ptr<CashFlow>(
135 }
136
138 return legNPV(0);
139 }
140
142 return legNPV(1);
143 }
144
146 // Knowing that for the fair payment NPV = 0.0, where:
147 // NPV = (discount at fixed amount pay date) * (payer\receiver * fixed amount)
148 // + (discount at float amount pay date) * (-payer\receiver * float amount)
149 // we have:
150 // fair amount = NPV float / discount at fixed amount pay date
151 // with NPV float corrected for the payer sign.
152 Real scaling = payer(1) ? -1.0 : 1.0;
153 return floatingLegNPV() / (endDiscounts(0) * scaling);
154 }
155
157 // Given the relation between the fixed payment (N^FIX) and the fixed rate (R),
158 // N^FIX = N * [(1 + R)^T - 1],
159 // the compound factor C = (1 + R)^T
160 // can be equivalently expressed as:
161 // C = N^FIX / N + 1
162 Real compound = fairFixedPayment() / baseNominal_ + 1.0;
163 return InterestRate::impliedRate(compound, dayCounter, Compounded, Annual, startDate_,
165 }
166
167 const Leg& ZeroCouponSwap::fixedLeg() const { return leg(0); }
168
169 const Leg& ZeroCouponSwap::floatingLeg() const { return leg(1); }
170
171 Real ZeroCouponSwap::fixedPayment() const { return fixedLeg()[0]->amount(); }
172}
calendar class
Definition: calendar.hpp:61
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
Coupon paying a fixed interest rate
Concrete interest rate class.
static InterestRate impliedRate(Real compound, const DayCounter &resultDC, Compounding comp, Frequency freq, Time t)
implied interest rate for a given compound factor at a given time.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
Predetermined cash flow.
Interest rate swap.
Definition: swap.hpp:41
DiscountFactor endDiscounts(Size j) const
Definition: swap.hpp:100
std::vector< Leg > legs_
Definition: swap.hpp:133
const Leg & leg(Size j) const
Definition: swap.hpp:111
Real legNPV(Size j) const
Definition: swap.hpp:88
std::vector< Real > payer_
Definition: swap.hpp:134
bool payer(Size j) const
Definition: swap.hpp:115
Zero-coupon interest rate swap.
Rate fairFixedRate(const DayCounter &dayCounter) const
ZeroCouponSwap(Type type, Real baseNominal, const Date &startDate, const Date &maturityDate, Real fixedPayment, ext::shared_ptr< IborIndex > iborIndex, const Calendar &paymentCalendar, BusinessDayConvention paymentConvention=Following, Natural paymentDelay=0)
const Leg & floatingLeg() const
just one cashflow in each leg
Date startDate() const override
ext::shared_ptr< IborIndex > iborIndex_
const Leg & fixedLeg() const
just one cashflow in each leg
Date maturityDate() const override
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Coupon paying a fixed annual rate.
BusinessDayConvention
Business Day conventions.
@ Annual
once a year
Definition: frequency.hpp:39
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Rate
interest rates
Definition: types.hpp:70
base class for Inter-Bank-Offered-Rate indexes
Coupon compounding or averaging multiple fixings.
Definition: any.hpp:37
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
Predetermined cash flow.
Zero-coupon interest rate swap.