QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
swap.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) 2006, 2011 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2007, 2008 StatPro Italia srl
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
26#include <ostream>
27
28namespace QuantLib {
29
30 Swap::Swap(const Leg& firstLeg,
31 const Leg& secondLeg)
32 : legs_(2), payer_(2),
33 legNPV_(2, 0.0), legBPS_(2, 0.0),
34 startDiscounts_(2, 0.0), endDiscounts_(2, 0.0),
35 npvDateDiscount_(0.0) {
36 legs_[0] = firstLeg;
37 legs_[1] = secondLeg;
38 payer_[0] = -1.0;
39 payer_[1] = 1.0;
40 for (auto& i : legs_[0])
41 registerWith(i);
42 for (auto& i : legs_[1])
43 registerWith(i);
44 }
45
46 Swap::Swap(const std::vector<Leg>& legs,
47 const std::vector<bool>& payer)
48 : legs_(legs), payer_(legs.size(), 1.0),
49 legNPV_(legs.size(), 0.0), legBPS_(legs.size(), 0.0),
50 startDiscounts_(legs.size(), 0.0), endDiscounts_(legs.size(), 0.0),
51 npvDateDiscount_(0.0) {
52 QL_REQUIRE(payer.size()==legs_.size(),
53 "size mismatch between payer (" << payer.size() <<
54 ") and legs (" << legs_.size() << ")");
55 for (Size j=0; j<legs_.size(); ++j) {
56 if (payer[j]) payer_[j]=-1.0;
57 for (auto& i : legs_[j])
58 registerWith(i);
59 }
60 }
61
63 : legs_(legs), payer_(legs),
64 legNPV_(legs, 0.0), legBPS_(legs, 0.0),
65 startDiscounts_(legs, 0.0), endDiscounts_(legs, 0.0),
66 npvDateDiscount_(0.0) {}
67
68 bool Swap::isExpired() const {
69 for (const auto& leg : legs_) {
70 Leg::const_iterator i;
71 for (i = leg.begin(); i != leg.end(); ++i)
72 if (!(*i)->hasOccurred())
73 return false;
74 }
75 return true;
76 }
77
78 void Swap::setupExpired() const {
80 std::fill(legBPS_.begin(), legBPS_.end(), 0.0);
81 std::fill(legNPV_.begin(), legNPV_.end(), 0.0);
82 std::fill(startDiscounts_.begin(), startDiscounts_.end(), 0.0);
83 std::fill(endDiscounts_.begin(), endDiscounts_.end(), 0.0);
84 npvDateDiscount_ = 0.0;
85 }
86
88 auto* arguments = dynamic_cast<Swap::arguments*>(args);
89 QL_REQUIRE(arguments != nullptr, "wrong argument type");
90
93 }
94
97
98 const auto* results = dynamic_cast<const Swap::results*>(r);
99 QL_REQUIRE(results != nullptr, "wrong result type");
100
101 if (!results->legNPV.empty()) {
102 QL_REQUIRE(results->legNPV.size() == legNPV_.size(),
103 "wrong number of leg NPV returned");
105 } else {
106 std::fill(legNPV_.begin(), legNPV_.end(), Null<Real>());
107 }
108
109 if (!results->legBPS.empty()) {
110 QL_REQUIRE(results->legBPS.size() == legBPS_.size(),
111 "wrong number of leg BPS returned");
113 } else {
114 std::fill(legBPS_.begin(), legBPS_.end(), Null<Real>());
115 }
116
117 if (!results->startDiscounts.empty()) {
119 "wrong number of leg start discounts returned");
121 } else {
122 std::fill(startDiscounts_.begin(), startDiscounts_.end(),
124 }
125
126 if (!results->endDiscounts.empty()) {
128 "wrong number of leg end discounts returned");
130 } else {
131 std::fill(endDiscounts_.begin(), endDiscounts_.end(),
133 }
134
137 } else {
139 }
140 }
141
142 Size Swap::numberOfLegs() const { return legs_.size(); }
143
144 const std::vector<Leg>& Swap::legs() const { return legs_; }
145
147 QL_REQUIRE(!legs_.empty(), "no legs given");
149 for (Size j=1; j<legs_.size(); ++j)
150 d = std::min(d, CashFlows::startDate(legs_[j]));
151 return d;
152 }
153
155 QL_REQUIRE(!legs_.empty(), "no legs given");
157 for (Size j=1; j<legs_.size(); ++j)
158 d = std::max(d, CashFlows::maturityDate(legs_[j]));
159 return d;
160 }
161
163 for (auto& leg : legs_) {
164 for (auto& k : leg) {
165 k->deepUpdate();
166 }
167 }
168 update();
169 }
170
172 QL_REQUIRE(legs.size() == payer.size(),
173 "number of legs and multipliers differ");
174 }
175
178 legNPV.clear();
179 legBPS.clear();
180 startDiscounts.clear();
181 endDiscounts.clear();
183 }
184
185 std::ostream& operator<<(std::ostream& out, Swap::Type t) {
186 switch (t) {
187 case Swap::Payer:
188 return out << "Payer";
189 case Swap::Receiver:
190 return out << "Receiver";
191 default:
192 QL_FAIL("unknown Swap::Type(" << Integer(t) << ")");
193 }
194 }
195
196}
Cash-flow analysis functions.
static Date maturityDate(const Leg &leg)
Definition: cashflows.cpp:52
static Date startDate(const Leg &leg)
Definition: cashflows.cpp:38
Concrete date class.
Definition: date.hpp:125
virtual void fetchResults(const PricingEngine::results *) const
Definition: instrument.hpp:155
virtual void setupExpired() const
Definition: instrument.hpp:140
void update() override
Definition: lazyobject.hpp:188
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
void validate() const override
Definition: swap.cpp:171
std::vector< Real > payer
Definition: swap.hpp:145
std::vector< Leg > legs
Definition: swap.hpp:144
std::vector< Real > legBPS
Definition: swap.hpp:152
DiscountFactor npvDateDiscount
Definition: swap.hpp:154
std::vector< DiscountFactor > endDiscounts
Definition: swap.hpp:153
std::vector< DiscountFactor > startDiscounts
Definition: swap.hpp:153
std::vector< Real > legNPV
Definition: swap.hpp:151
void reset() override
Definition: swap.cpp:176
DiscountFactor endDiscounts(Size j) const
Definition: swap.hpp:100
DiscountFactor npvDateDiscount_
Definition: swap.hpp:138
const std::vector< Leg > & legs() const
Definition: swap.cpp:144
void setupArguments(PricingEngine::arguments *) const override
Definition: swap.cpp:87
Size numberOfLegs() const
Definition: swap.cpp:142
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Definition: swap.cpp:68
Real legBPS(Size j) const
Definition: swap.hpp:82
void deepUpdate() override
Definition: swap.cpp:162
virtual Date startDate() const
Definition: swap.cpp:146
std::vector< Leg > legs_
Definition: swap.hpp:133
std::vector< Real > legNPV_
Definition: swap.hpp:135
const Leg & leg(Size j) const
Definition: swap.hpp:111
DiscountFactor npvDateDiscount() const
Definition: swap.hpp:106
std::vector< Real > legBPS_
Definition: swap.hpp:136
Swap(const Leg &firstLeg, const Leg &secondLeg)
Definition: swap.cpp:30
virtual Date maturityDate() const
Definition: swap.cpp:154
std::vector< DiscountFactor > startDiscounts_
Definition: swap.hpp:137
Real legNPV(Size j) const
Definition: swap.hpp:88
void setupExpired() const override
Definition: swap.cpp:78
DiscountFactor startDiscounts(Size j) const
Definition: swap.hpp:94
void fetchResults(const PricingEngine::results *) const override
Definition: swap.cpp:95
std::vector< DiscountFactor > endDiscounts_
Definition: swap.hpp:137
std::vector< Real > payer_
Definition: swap.hpp:134
bool payer(Size j) const
Definition: swap.hpp:115
const DefaultType & t
#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
Date d
Coupon paying a variable index-based rate.
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
ext::shared_ptr< YieldTermStructure > r
Interest rate swap.
Interest-rate term structure.