QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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 ext::shared_ptr<CashFlow>
31 compoundedSubPeriodicCoupon(const Date& paymentDate,
32 const Date& startDate,
33 const Date& maturityDate,
34 Real nominal,
35 const ext::shared_ptr<IborIndex>& index) {
36 auto floatCpn = ext::make_shared<SubPeriodsCoupon>(
37 paymentDate, nominal, startDate, maturityDate, index->fixingDays(), index);
38 floatCpn->setPricer(
39 ext::shared_ptr<FloatingRateCouponPricer>(new CompoundingRatePricer));
40 return floatCpn;
41 }
42 }
43
45 Real baseNominal,
46 const Date& startDate,
47 const Date& maturityDate,
48 ext::shared_ptr<IborIndex> iborIndex,
49 const Calendar& paymentCalendar,
50 BusinessDayConvention paymentConvention,
51 Natural paymentDelay)
52 : Swap(2), type_(type), baseNominal_(baseNominal), iborIndex_(std::move(iborIndex)),
53 startDate_(startDate), maturityDate_(maturityDate) {
54
55 QL_REQUIRE(!(baseNominal < 0.0), "base nominal cannot be negative");
57 "start date (" << startDate
58 << ") later than or equal to maturity date ("
59 << maturityDate << ")");
60
61 paymentDate_ = paymentCalendar.advance(maturityDate, paymentDelay, Days, paymentConvention);
62
63 legs_[1].push_back(compoundedSubPeriodicCoupon(paymentDate_, startDate, maturityDate,
65 for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i)
66 registerWith(*i);
67
68 switch (type_) {
69 case Payer:
70 payer_[0] = -1.0;
71 payer_[1] = +1.0;
72 break;
73 case Receiver:
74 payer_[0] = +1.0;
75 payer_[1] = -1.0;
76 break;
77 default:
78 QL_FAIL("unknown zero coupon swap type");
79 }
80 }
81
83 Real baseNominal,
84 const Date& startDate,
85 const Date& maturityDate,
86 Real fixedPayment,
87 ext::shared_ptr<IborIndex> iborIndex,
88 const Calendar& paymentCalendar,
89 BusinessDayConvention paymentConvention,
90 Natural paymentDelay)
91 : ZeroCouponSwap(type,
92 baseNominal,
93 startDate,
94 maturityDate,
95 std::move(iborIndex),
96 paymentCalendar,
97 paymentConvention,
98 paymentDelay) {
99
100 legs_[0].push_back(
101 ext::shared_ptr<CashFlow>(new SimpleCashFlow(fixedPayment, paymentDate_)));
102 }
103
105 Real baseNominal,
106 const Date& startDate,
107 const Date& maturityDate,
108 Rate fixedRate,
109 const DayCounter& fixedDayCounter,
110 ext::shared_ptr<IborIndex> iborIndex,
111 const Calendar& paymentCalendar,
112 BusinessDayConvention paymentConvention,
113 Natural paymentDelay)
114 : ZeroCouponSwap(type,
115 baseNominal,
116 startDate,
117 maturityDate,
118 std::move(iborIndex),
119 paymentCalendar,
120 paymentConvention,
121 paymentDelay) {
122
123 InterestRate interest(fixedRate, fixedDayCounter, Compounded, Annual);
124 legs_[0].push_back(ext::shared_ptr<CashFlow>(
126 }
127
129 return legNPV(0);
130 }
131
133 return legNPV(1);
134 }
135
137 // Knowing that for the fair payment NPV = 0.0, where:
138 // NPV = (discount at fixed amount pay date) * (payer\receiver * fixed amount)
139 // + (discount at float amount pay date) * (-payer\receiver * float amount)
140 // we have:
141 // fair amount = NPV float / discount at fixed amount pay date
142 // with NPV float corrected for the payer sign.
143 Real scaling = payer(1) ? -1.0 : 1.0;
144 return floatingLegNPV() / (endDiscounts(0) * scaling);
145 }
146
148 // Given the relation between the fixed payment (N^FIX) and the fixed rate (R),
149 // N^FIX = N * [(1 + R)^T - 1],
150 // the compound factor C = (1 + R)^T
151 // can be equivalently expressed as:
152 // C = N^FIX / N + 1
153 Real compound = fairFixedPayment() / baseNominal_ + 1.0;
154 return InterestRate::impliedRate(compound, dayCounter, Compounded, Annual, startDate_,
156 }
157
158 const Leg& ZeroCouponSwap::fixedLeg() const { return leg(0); }
159
160 const Leg& ZeroCouponSwap::floatingLeg() const { return leg(1); }
161
162 Real ZeroCouponSwap::fixedPayment() const { return fixedLeg()[0]->amount(); }
163}
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:228
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
Definition: any.hpp:35
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
Predetermined cash flow.
averaging coupons
Zero-coupon interest rate swap.