| 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 | |
| 33 | namespace 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 | } |
| 94 | DiscountFactor startDiscounts(Size j) const { |
| 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 | } |
| 100 | DiscountFactor endDiscounts(Size j) const { |
| 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 | } |
| 106 | DiscountFactor npvDateDiscount() const { |
| 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_; |
| 138 | mutable DiscountFactor npvDateDiscount_; |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | class Swap::arguments : public virtual PricingEngine::arguments { |
| 143 | public: |
| 144 | std::vector<Leg> legs; |
| 145 | std::vector<Real> payer; |
| 146 | void validate() const override; |
| 147 | }; |
| 148 | |
| 149 | class Swap::results : public Instrument::results { |
| 150 | public: |
| 151 | std::vector<Real> legNPV; |
| 152 | std::vector<Real> legBPS; |
| 153 | std::vector<DiscountFactor> startDiscounts, endDiscounts; |
| 154 | DiscountFactor npvDateDiscount; |
| 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 |
| 166 | |