QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fixedvsfloatingswap.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) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
6 Copyright (C) 2007 Ferdinando Ametrano
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
29#include <utility>
30
31namespace QuantLib {
32
34 std::vector<Real> fixedNominals,
35 Schedule fixedSchedule,
36 Rate fixedRate,
37 DayCounter fixedDayCount,
38 std::vector<Real> floatingNominals,
39 Schedule floatingSchedule,
40 ext::shared_ptr<IborIndex> iborIndex,
41 Spread spread,
42 DayCounter floatingDayCount,
43 ext::optional<BusinessDayConvention> paymentConvention,
44 Integer paymentLag,
45 const Calendar& paymentCalendar)
46 : Swap(2), type_(type), fixedNominals_(std::move(fixedNominals)), fixedSchedule_(std::move(fixedSchedule)),
47 fixedRate_(fixedRate), fixedDayCount_(std::move(fixedDayCount)),
48 floatingNominals_(std::move(floatingNominals)), floatingSchedule_(std::move(floatingSchedule)),
49 iborIndex_(std::move(iborIndex)), spread_(spread), floatingDayCount_(std::move(floatingDayCount)) {
50
51 QL_REQUIRE(iborIndex_, "null floating index provided");
52
54 fixedDayCount_ = iborIndex_->dayCounter();
55
56 if (paymentConvention) // NOLINT(readability-implicit-bool-conversion)
58 else
60
65 .withPaymentLag(paymentLag)
66 .withPaymentCalendar(paymentCalendar.empty() ?
68 paymentCalendar);
69
70 // legs_[1] to be built by derived class constructor
71
72 switch (type_) {
73 case Payer:
74 payer_[0] = -1.0;
75 payer_[1] = +1.0;
76 break;
77 case Receiver:
78 payer_[0] = +1.0;
79 payer_[1] = -1.0;
80 break;
81 default:
82 QL_FAIL("Unknown vanilla-swap type");
83 }
84
85
86 // These bools tell us if we can support the old methods nominal() and nominals().
87 // There might be false negatives (i.e., if we pass constant vectors of different lengths
88 // as fixedNominals and floatingNominals) but we're going to assume that whoever uses the
89 // constructor with two vectors is mostly going to use the new methods instead.
90 sameNominals_ = std::equal(fixedNominals_.begin(), fixedNominals_.end(),
92 if (!sameNominals_) {
93 constantNominals_ = false;
94 } else {
95 constantNominals_ = true;
96 Real front = fixedNominals_[0];
97 for (auto x : fixedNominals_) {
98 if (x != front) {
99 constantNominals_ = false;
100 break;
101 }
102 }
103 }
104 }
105
107
109
110 auto* arguments = dynamic_cast<FixedVsFloatingSwap::arguments*>(args);
111
112 if (arguments == nullptr) // it's a swap engine...
113 return;
114
116
119 else
121
122 const Leg& fixedCoupons = fixedLeg();
123 Size n = fixedCoupons.size();
124
125 arguments->fixedResetDates = arguments->fixedPayDates = std::vector<Date>(n);
126 arguments->fixedNominals = arguments->fixedCoupons = std::vector<Real>(n);
127
128 for (Size i=0; i<n; ++i) {
129 auto coupon = ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
130
131 arguments->fixedPayDates[i] = coupon->date();
132 arguments->fixedResetDates[i] = coupon->accrualStartDate();
133 arguments->fixedCoupons[i] = coupon->amount();
134 arguments->fixedNominals[i] = coupon->nominal();
135 }
136
138 }
139
141 calculate();
142 QL_REQUIRE(fairRate_ != Null<Rate>(), "result not available");
143 return fairRate_;
144 }
145
147 calculate();
148 QL_REQUIRE(fairSpread_ != Null<Spread>(), "result not available");
149 return fairSpread_;
150 }
151
153 calculate();
154 QL_REQUIRE(legBPS_[0] != Null<Real>(), "result not available");
155 return legBPS_[0];
156 }
157
159 calculate();
160 QL_REQUIRE(legBPS_[1] != Null<Real>(), "result not available");
161 return legBPS_[1];
162 }
163
165 calculate();
166 QL_REQUIRE(legNPV_[0] != Null<Real>(), "result not available");
167 return legNPV_[0];
168 }
169
171 calculate();
172 QL_REQUIRE(legNPV_[1] != Null<Real>(), "result not available");
173 return legNPV_[1];
174 }
175
178 legBPS_[0] = legBPS_[1] = 0.0;
181 }
182
184 static const Spread basisPoint = 1.0e-4;
185
187
188 const auto* results = dynamic_cast<const FixedVsFloatingSwap::results*>(r);
189 if (results != nullptr) { // might be a swap engine, so no error is thrown
192 } else {
195 }
196
197 if (fairRate_ == Null<Rate>()) {
198 // calculate it from other results
199 if (legBPS_[0] != Null<Real>())
200 fairRate_ = fixedRate_ - NPV_/(legBPS_[0]/basisPoint);
201 }
202 if (fairSpread_ == Null<Spread>()) {
203 // ditto
204 if (legBPS_[1] != Null<Real>())
205 fairSpread_ = spread_ - NPV_/(legBPS_[1]/basisPoint);
206 }
207 }
208
211 QL_REQUIRE(fixedNominals.size() == fixedPayDates.size(),
212 "number of fixed nominals different from "
213 "number of fixed payment dates");
215 "number of fixed start dates different from "
216 "number of fixed payment dates");
217 QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
218 "number of fixed payment dates different from "
219 "number of fixed coupon amounts");
221 "number of floating nominals different from "
222 "number of floating payment dates");
224 "number of floating start dates different from "
225 "number of floating payment dates");
227 "number of floating fixing dates different from "
228 "number of floating payment dates");
230 "number of floating accrual Times different from "
231 "number of floating payment dates");
233 "number of floating spreads different from "
234 "number of floating payment dates");
236 "number of floating payment dates different from "
237 "number of floating coupon amounts");
238 }
239
244 }
245
246}
Cash-flow analysis functions.
Cash flow vector builders.
calendar class
Definition: calendar.hpp:61
bool empty() const
Returns whether or not the calendar is initialized.
Definition: calendar.hpp:202
day counter class
Definition: daycounter.hpp:44
helper class building a sequence of fixed rate coupons
FixedRateLeg & withNotionals(Real)
FixedRateLeg & withPaymentLag(Integer lag)
FixedRateLeg & withPaymentCalendar(const Calendar &)
FixedRateLeg & withPaymentAdjustment(BusinessDayConvention)
FixedRateLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
Arguments for simple swap calculation
Results from simple swap calculation
BusinessDayConvention paymentConvention() const
virtual void setupFloatingArguments(arguments *args) const =0
ext::shared_ptr< IborIndex > iborIndex_
void setupArguments(PricingEngine::arguments *args) const override
BusinessDayConvention paymentConvention_
void fetchResults(const PricingEngine::results *) const override
FixedVsFloatingSwap(Type type, std::vector< Real > fixedNominals, Schedule fixedSchedule, Rate fixedRate, DayCounter fixedDayCount, std::vector< Real > floatingNominals, Schedule floatingSchedule, ext::shared_ptr< IborIndex > iborIndex, Spread spread, DayCounter floatingDayCount, ext::optional< BusinessDayConvention > paymentConvention=ext::nullopt, Integer paymentLag=0, const Calendar &paymentCalendar=Calendar())
void calculate() const override
Definition: instrument.hpp:129
template class providing a null value for a given type.
Definition: null.hpp:76
Payment schedule.
Definition: schedule.hpp:40
const Calendar & calendar() const
Definition: schedule.hpp:176
BusinessDayConvention businessDayConvention() const
Definition: schedule.hpp:196
void validate() const override
Definition: swap.cpp:171
void reset() override
Definition: swap.cpp:176
Interest rate swap.
Definition: swap.hpp:41
void setupArguments(PricingEngine::arguments *) const override
Definition: swap.cpp:87
std::vector< Leg > legs_
Definition: swap.hpp:133
std::vector< Real > legNPV_
Definition: swap.hpp:135
std::vector< Real > legBPS_
Definition: swap.hpp:136
void setupExpired() const override
Definition: swap.cpp:78
void fetchResults(const PricingEngine::results *) const override
Definition: swap.cpp:95
std::vector< Real > payer_
Definition: swap.hpp:134
Coupon pricers.
#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.
Fixed-rate vs floating-rate swap.
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
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.
ext::shared_ptr< YieldTermStructure > r
Interest-rate term structure.