QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
discretizedswap.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) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2004, 2007 StatPro Italia srl
6 Copyright (C) 2022 Ralf Konrad Eckel
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
23#include <ql/settings.hpp>
24#include <utility>
25
26namespace QuantLib {
27 namespace {
28 inline bool isResetTimeInPast(const Time& resetTime,
29 const Time& payTime,
30 const bool& includeTodaysCashFlows) {
31 return (resetTime < 0.0) &&
32 ((payTime > 0.0) || (includeTodaysCashFlows && (payTime == 0.0)));
33 }
34 }
35
37 const Date& referenceDate,
38 const DayCounter& dayCounter)
40 args,
41 referenceDate,
42 dayCounter,
43 std::vector<CouponAdjustment>(args.fixedPayDates.size(), CouponAdjustment::pre),
44 std::vector<CouponAdjustment>(args.floatingPayDates.size(), CouponAdjustment::pre)) {}
45
47 const Date& referenceDate,
48 const DayCounter& dayCounter,
49 std::vector<CouponAdjustment> fixedCouponAdjustments,
50 std::vector<CouponAdjustment> floatingCouponAdjustments)
51 : arguments_(args), fixedCouponAdjustments_(std::move(fixedCouponAdjustments)),
52 floatingCouponAdjustments_(std::move(floatingCouponAdjustments)) {
55 "The fixed coupon adjustments must have the same size as the number of fixed coupons.");
57 "The floating coupon adjustments must have the same size as the number of "
58 "floating coupons.");
59
60 // NOLINTNEXTLINE(readability-implicit-bool-conversion)
61 auto includeTodaysCashFlows = Settings::instance().includeTodaysCashFlows() &&
63
64 auto nrOfFixedCoupons = args.fixedResetDates.size();
65 fixedResetTimes_.resize(nrOfFixedCoupons);
66 fixedPayTimes_.resize(nrOfFixedCoupons);
67 fixedResetTimeIsInPast_.resize(nrOfFixedCoupons);
68 for (Size i = 0; i < nrOfFixedCoupons; ++i) {
69 auto resetTime = dayCounter.yearFraction(referenceDate, args.fixedResetDates[i]);
70 auto payTime = dayCounter.yearFraction(referenceDate, args.fixedPayDates[i]);
71 auto resetIsInPast = isResetTimeInPast(resetTime, payTime, includeTodaysCashFlows);
72
73 fixedResetTimes_[i] = resetTime;
74 fixedPayTimes_[i] = payTime;
75 fixedResetTimeIsInPast_[i] = resetIsInPast;
76 if (resetIsInPast)
78 }
79
80 auto nrOfFloatingCoupons = args.floatingResetDates.size();
81 floatingResetTimes_.resize(nrOfFloatingCoupons);
82 floatingPayTimes_.resize(nrOfFloatingCoupons);
83 floatingResetTimeIsInPast_.resize(nrOfFloatingCoupons);
84 for (Size i = 0; i < nrOfFloatingCoupons; ++i) {
85 auto resetTime = dayCounter.yearFraction(referenceDate, args.floatingResetDates[i]);
86 auto payTime = dayCounter.yearFraction(referenceDate, args.floatingPayDates[i]);
87 auto resetIsInPast = isResetTimeInPast(resetTime, payTime, includeTodaysCashFlows);
88
89 floatingResetTimes_[i] = resetTime;
90 floatingPayTimes_[i] = payTime;
91 floatingResetTimeIsInPast_[i] = resetIsInPast;
92 if (resetIsInPast)
94 }
95 }
96
98 values_ = Array(size, 0.0);
100 }
101
102 std::vector<Time> DiscretizedSwap::mandatoryTimes() const {
103 std::vector<Time> times;
104 for (Real t : fixedResetTimes_) {
105 if (t >= 0.0)
106 times.push_back(t);
107 }
108 for (Real t : fixedPayTimes_) {
109 if (t >= 0.0)
110 times.push_back(t);
111 }
112 for (Real t : floatingResetTimes_) {
113 if (t >= 0.0)
114 times.push_back(t);
115 }
116 for (Real t : floatingPayTimes_) {
117 if (t >= 0.0)
118 times.push_back(t);
119 }
120 return times;
121 }
122
124 // floating payments
125 for (Size i = 0; i < floatingResetTimes_.size(); i++) {
129 }
130 }
131 // fixed payments
132 for (Size i = 0; i < fixedResetTimes_.size(); i++) {
134 if (fixedCouponAdjustments_[i] == CouponAdjustment::pre && t >= 0.0 && isOnTime(t)) {
136 }
137 }
138 }
139
141 // floating payments
142 for (Size i = 0; i < floatingResetTimes_.size(); i++) {
146 }
147 }
148 // fixed payments
149 for (Size i = 0; i < fixedResetTimes_.size(); i++) {
153 }
154 }
155
156 // fixed coupons whose reset time is in the past won't be managed
157 // in preAdjustValues()
158 for (Size i = 0; i < fixedPayTimes_.size(); i++) {
159 Time t = fixedPayTimes_[i];
161 Real fixedCoupon = arguments_.fixedCoupons[i];
163 values_ -= fixedCoupon;
164 else
165 values_ += fixedCoupon;
166 }
167 }
168
169 // the same applies to floating payments whose rate is already fixed
170 for (Size i = 0; i < floatingPayTimes_.size(); i++) {
173 Real currentFloatingCoupon = arguments_.floatingCoupons[i];
174 QL_REQUIRE(currentFloatingCoupon != Null<Real>(),
175 "current floating coupon not given");
177 values_ += currentFloatingCoupon;
178 else
179 values_ -= currentFloatingCoupon;
180 }
181 }
182 }
183
186 bond.initialize(method(), fixedPayTimes_[i]);
187 bond.rollback(time_);
188
189 Real fixedCoupon = arguments_.fixedCoupons[i];
190 for (Size j = 0; j < values_.size(); j++) {
191 Real coupon = fixedCoupon * bond.values()[j];
193 values_[j] -= coupon;
194 else
195 values_[j] += coupon;
196 }
197 }
198
202 bond.rollback(time_);
203
205 "non-constant nominals are not supported yet");
206
207 Real nominal = arguments_.nominal;
210 Real accruedSpread = nominal * T * spread;
211 for (Size j = 0; j < values_.size(); j++) {
212 Real coupon = nominal * (1.0 - bond.values()[j]) + accruedSpread * bond.values()[j];
214 values_[j] += coupon;
215 else
216 values_[j] -= coupon;
217 }
218 }
219}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
const Array & values() const
const ext::shared_ptr< Lattice > & method() const
void initialize(const ext::shared_ptr< Lattice > &, Time t)
Useful discretized discount bond asset.
std::vector< Time > floatingPayTimes_
std::vector< bool > floatingResetTimeIsInPast_
void preAdjustValuesImpl() override
std::vector< CouponAdjustment > floatingCouponAdjustments_
std::vector< Time > fixedResetTimes_
std::vector< CouponAdjustment > fixedCouponAdjustments_
std::vector< bool > fixedResetTimeIsInPast_
std::vector< Time > floatingResetTimes_
VanillaSwap::arguments arguments_
DiscretizedSwap(const VanillaSwap::arguments &args, const Date &referenceDate, const DayCounter &dayCounter)
void postAdjustValuesImpl() override
std::vector< Time > mandatoryTimes() const override
std::vector< Time > fixedPayTimes_
void reset(Size size) override
Arguments for simple swap calculation
template class providing a null value for a given type.
Definition: null.hpp:76
ext::optional< bool > & includeTodaysCashFlows()
Definition: settings.hpp:163
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
const DefaultType & t
Discretized swap class.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Spread
spreads on interest rates
Definition: types.hpp:74
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
global repository for run-time library settings