QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
swap.hpp
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) 2006, 2011 Ferdinando Ametrano
6 Copyright (C) 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
22/*! \file swap.hpp
23 \brief Interest rate swap
24*/
25
26#ifndef quantlib_swap_hpp
27#define quantlib_swap_hpp
28
29#include <ql/instrument.hpp>
30#include <ql/cashflow.hpp>
31#include <iosfwd>
32
33namespace QuantLib {
34
35 //! Interest rate swap
36 /*! The cash flows belonging to the first leg are paid;
37 the ones belonging to the second leg are received.
38
39 \ingroup instruments
40 */
41 class Swap : public Instrument {
42 public:
43 /*! In most cases, the swap has just two legs and can be
44 defined as receiver or payer.
45
46 Its type is usually defined with respect to the leg paying
47 a fixed rate; derived swap classes will document any
48 exceptions to the rule.
49 */
50 enum Type { Receiver = -1, Payer = 1 };
51
52 class arguments;
53 class results;
54 class engine;
55 //! \name Constructors
56 //@{
57 /*! The cash flows belonging to the first leg are paid;
58 the ones belonging to the second leg are received.
59 */
60 Swap(const Leg& firstLeg,
61 const Leg& secondLeg);
62 /*! Multi leg constructor. */
63 Swap(const std::vector<Leg>& legs,
64 const std::vector<bool>& payer);
65 //@}
66 //! \name Observable interface
67 //@{
68 void deepUpdate() override;
69 //@}
70 //! \name Instrument interface
71 //@{
72 bool isExpired() const override;
73 void setupArguments(PricingEngine::arguments*) const override;
74 void fetchResults(const PricingEngine::results*) const override;
75 //@}
76 //! \name Additional interface
77 //@{
78 Size numberOfLegs() const;
79 const std::vector<Leg>& legs() const;
80 virtual Date startDate() const;
81 virtual Date maturityDate() const;
82 Real legBPS(Size j) const {
83 QL_REQUIRE(j<legs_.size(), "leg# " << j << " doesn't exist!");
84 calculate();
85 QL_REQUIRE(legBPS_[j] != Null<Real>(), "result not available");
86 return legBPS_[j];
87 }
88 Real legNPV(Size j) const {
89 QL_REQUIRE(j<legs_.size(), "leg #" << j << " doesn't exist!");
90 calculate();
91 QL_REQUIRE(legNPV_[j] != Null<Real>(), "result not available");
92 return legNPV_[j];
93 }
95 QL_REQUIRE(j<legs_.size(), "leg #" << j << " doesn't exist!");
96 calculate();
97 QL_REQUIRE(startDiscounts_[j] != Null<Real>(), "result not available");
98 return startDiscounts_[j];
99 }
101 QL_REQUIRE(j<legs_.size(), "leg #" << j << " doesn't exist!");
102 calculate();
103 QL_REQUIRE(endDiscounts_[j] != Null<Real>(), "result not available");
104 return endDiscounts_[j];
105 }
107 calculate();
108 QL_REQUIRE(npvDateDiscount_ != Null<Real>(), "result not available");
109 return npvDateDiscount_;
110 }
111 const Leg& leg(Size j) const {
112 QL_REQUIRE(j<legs_.size(), "leg #" << j << " doesn't exist!");
113 return legs_[j];
114 }
115 bool payer(Size j) const {
116 QL_REQUIRE(j<legs_.size(), "leg #" << j << " doesn't exist!");
117 return payer_[j] < 0.0;
118 }
119 //@}
120 protected:
121 //! \name Constructors
122 //@{
123 /*! This constructor can be used by derived classes that will
124 build their legs themselves.
125 */
126 Swap(Size legs);
127 //@}
128 //! \name Instrument interface
129 //@{
130 void setupExpired() const override;
131 //@}
132 // data members
133 std::vector<Leg> legs_;
134 std::vector<Real> payer_;
135 mutable std::vector<Real> legNPV_;
136 mutable std::vector<Real> legBPS_;
137 mutable std::vector<DiscountFactor> startDiscounts_, endDiscounts_;
139 };
140
141
143 public:
144 std::vector<Leg> legs;
145 std::vector<Real> payer;
146 void validate() const override;
147 };
148
150 public:
151 std::vector<Real> legNPV;
152 std::vector<Real> legBPS;
153 std::vector<DiscountFactor> startDiscounts, endDiscounts;
155 void reset() override;
156 };
157
158 class Swap::engine : public GenericEngine<Swap::arguments,
159 Swap::results> {};
160
161 std::ostream& operator<<(std::ostream& out, Swap::Type t);
162
163}
164
165#endif
Base class for cash flows.
Concrete date class.
Definition: date.hpp:125
template base class for option pricing engines
Abstract instrument class.
Definition: instrument.hpp:44
void calculate() const override
Definition: instrument.hpp:129
template class providing a null value for a given type.
Definition: null.hpp:76
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
Interest rate swap.
Definition: swap.hpp:41
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
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
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
Abstract instrument class.
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