| 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) 2006, 2008 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 | |
| 22 | /*! \file fixedvsfloatingswap.hpp |
| 23 | \brief Fixed-rate vs floating-rate swap |
| 24 | */ |
| 25 | |
| 26 | #ifndef quantlib_fixed_vs_floating_swap_hpp |
| 27 | #define quantlib_fixed_vs_floating_swap_hpp |
| 28 | |
| 29 | #include <ql/instruments/swap.hpp> |
| 30 | #include <ql/time/daycounter.hpp> |
| 31 | #include <ql/time/schedule.hpp> |
| 32 | #include <ql/optional.hpp> |
| 33 | |
| 34 | namespace QuantLib { |
| 35 | |
| 36 | class IborIndex; |
| 37 | |
| 38 | //! Fixed vs floating swap |
| 39 | /*! \ingroup instruments |
| 40 | |
| 41 | If no payment convention is passed, the convention of the |
| 42 | floating-rate schedule is used. |
| 43 | |
| 44 | \warning if <tt>Settings::includeReferenceDateCashFlows()</tt> |
| 45 | is set to <tt>true</tt>, payments occurring at the |
| 46 | settlement date of the swap might be included in the |
| 47 | NPV and therefore affect the fair-rate and |
| 48 | fair-spread calculation. This might not be what you |
| 49 | want. |
| 50 | */ |
| 51 | class FixedVsFloatingSwap : public Swap { |
| 52 | public: |
| 53 | class arguments; |
| 54 | class results; |
| 55 | class engine; |
| 56 | FixedVsFloatingSwap(Type type, |
| 57 | std::vector<Real> fixedNominals, |
| 58 | Schedule fixedSchedule, |
| 59 | Rate fixedRate, |
| 60 | DayCounter fixedDayCount, |
| 61 | std::vector<Real> floatingNominals, |
| 62 | Schedule floatingSchedule, |
| 63 | ext::shared_ptr<IborIndex> iborIndex, |
| 64 | Spread spread, |
| 65 | DayCounter floatingDayCount, |
| 66 | ext::optional<BusinessDayConvention> paymentConvention = ext::nullopt, |
| 67 | Natural paymentLag = 0, |
| 68 | const Calendar& paymentCalendar = Calendar()); |
| 69 | //! \name Inspectors |
| 70 | //@{ |
| 71 | Type type() const; |
| 72 | |
| 73 | /*! This throws if the nominal is not constant across coupons. */ |
| 74 | Real nominal() const; |
| 75 | /*! This throws if the nominals are not the same for the two legs. */ |
| 76 | const std::vector<Real>& nominals() const; |
| 77 | |
| 78 | const std::vector<Real>& fixedNominals() const; |
| 79 | const Schedule& fixedSchedule() const; |
| 80 | Rate fixedRate() const; |
| 81 | const DayCounter& fixedDayCount() const; |
| 82 | |
| 83 | const std::vector<Real>& floatingNominals() const; |
| 84 | const Schedule& floatingSchedule() const; |
| 85 | const ext::shared_ptr<IborIndex>& iborIndex() const; |
| 86 | Spread spread() const; |
| 87 | const DayCounter& floatingDayCount() const; |
| 88 | |
| 89 | BusinessDayConvention paymentConvention() const; |
| 90 | |
| 91 | const Leg& fixedLeg() const; |
| 92 | const Leg& floatingLeg() const; |
| 93 | //@} |
| 94 | |
| 95 | //! \name Results |
| 96 | //@{ |
| 97 | Real fixedLegBPS() const; |
| 98 | Real fixedLegNPV() const; |
| 99 | Rate fairRate() const; |
| 100 | |
| 101 | Real floatingLegBPS() const; |
| 102 | Real floatingLegNPV() const; |
| 103 | Spread fairSpread() const; |
| 104 | //@} |
| 105 | // other |
| 106 | void setupArguments(PricingEngine::arguments* args) const override; |
| 107 | void fetchResults(const PricingEngine::results*) const override; |
| 108 | |
| 109 | private: |
| 110 | void setupExpired() const override; |
| 111 | virtual void setupFloatingArguments(arguments* args) const = 0; |
| 112 | Type type_; |
| 113 | std::vector<Real> fixedNominals_; |
| 114 | Schedule fixedSchedule_; |
| 115 | Rate fixedRate_; |
| 116 | DayCounter fixedDayCount_; |
| 117 | std::vector<Real> floatingNominals_; |
| 118 | Schedule floatingSchedule_; |
| 119 | ext::shared_ptr<IborIndex> iborIndex_; |
| 120 | Spread spread_; |
| 121 | DayCounter floatingDayCount_; |
| 122 | BusinessDayConvention paymentConvention_; |
| 123 | // results |
| 124 | mutable Rate fairRate_; |
| 125 | mutable Spread fairSpread_; |
| 126 | |
| 127 | bool constantNominals_, sameNominals_; |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | //! %Arguments for simple swap calculation |
| 132 | class FixedVsFloatingSwap::arguments : public Swap::arguments { |
| 133 | public: |
| 134 | arguments() : nominal(Null<Real>()) {} |
| 135 | Type type = Receiver; |
| 136 | Real nominal; |
| 137 | |
| 138 | std::vector<Real> fixedNominals; |
| 139 | std::vector<Date> fixedResetDates; |
| 140 | std::vector<Date> fixedPayDates; |
| 141 | std::vector<Real> floatingNominals; |
| 142 | std::vector<Time> floatingAccrualTimes; |
| 143 | std::vector<Date> floatingResetDates; |
| 144 | std::vector<Date> floatingFixingDates; |
| 145 | std::vector<Date> floatingPayDates; |
| 146 | |
| 147 | std::vector<Real> fixedCoupons; |
| 148 | std::vector<Spread> floatingSpreads; |
| 149 | std::vector<Real> floatingCoupons; |
| 150 | void validate() const override; |
| 151 | }; |
| 152 | |
| 153 | //! %Results from simple swap calculation |
| 154 | class FixedVsFloatingSwap::results : public Swap::results { |
| 155 | public: |
| 156 | Rate fairRate; |
| 157 | Spread fairSpread; |
| 158 | void reset() override; |
| 159 | }; |
| 160 | |
| 161 | class FixedVsFloatingSwap::engine : public GenericEngine<FixedVsFloatingSwap::arguments, |
| 162 | FixedVsFloatingSwap::results> {}; |
| 163 | |
| 164 | |
| 165 | // inline definitions |
| 166 | |
| 167 | inline Swap::Type FixedVsFloatingSwap::type() const { |
| 168 | return type_; |
| 169 | } |
| 170 | |
| 171 | inline Real FixedVsFloatingSwap::nominal() const { |
| 172 | QL_REQUIRE(constantNominals_, "nominal is not constant" ); |
| 173 | return fixedNominals_[0]; |
| 174 | } |
| 175 | |
| 176 | inline const std::vector<Real>& FixedVsFloatingSwap::nominals() const { |
| 177 | QL_REQUIRE(sameNominals_, "different nominals on fixed and floating leg" ); |
| 178 | return fixedNominals_; |
| 179 | } |
| 180 | |
| 181 | inline const std::vector<Real>& FixedVsFloatingSwap::fixedNominals() const { |
| 182 | return fixedNominals_; |
| 183 | } |
| 184 | |
| 185 | inline const Schedule& FixedVsFloatingSwap::fixedSchedule() const { |
| 186 | return fixedSchedule_; |
| 187 | } |
| 188 | |
| 189 | inline Rate FixedVsFloatingSwap::fixedRate() const { |
| 190 | return fixedRate_; |
| 191 | } |
| 192 | |
| 193 | inline const DayCounter& FixedVsFloatingSwap::fixedDayCount() const { |
| 194 | return fixedDayCount_; |
| 195 | } |
| 196 | |
| 197 | inline const std::vector<Real>& FixedVsFloatingSwap::floatingNominals() const { |
| 198 | return floatingNominals_; |
| 199 | } |
| 200 | |
| 201 | inline const Schedule& FixedVsFloatingSwap::floatingSchedule() const { |
| 202 | return floatingSchedule_; |
| 203 | } |
| 204 | |
| 205 | inline const ext::shared_ptr<IborIndex>& FixedVsFloatingSwap::iborIndex() const { |
| 206 | return iborIndex_; |
| 207 | } |
| 208 | |
| 209 | inline Spread FixedVsFloatingSwap::spread() const { |
| 210 | return spread_; |
| 211 | } |
| 212 | |
| 213 | inline const DayCounter& FixedVsFloatingSwap::floatingDayCount() const { |
| 214 | return floatingDayCount_; |
| 215 | } |
| 216 | |
| 217 | inline BusinessDayConvention FixedVsFloatingSwap::paymentConvention() const { |
| 218 | return paymentConvention_; |
| 219 | } |
| 220 | |
| 221 | inline const Leg& FixedVsFloatingSwap::fixedLeg() const { |
| 222 | return legs_[0]; |
| 223 | } |
| 224 | |
| 225 | inline const Leg& FixedVsFloatingSwap::floatingLeg() const { |
| 226 | return legs_[1]; |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | |
| 231 | #endif |
| 232 | |