/src/quantlib/ql/instruments/nonstandardswap.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2013, 2016 Peter Caspers |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | /*! \file nonstandardswap.hpp |
21 | | \brief vanilla swap but possibly with period dependent nominal and strike |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_nonstandard_swap_hpp |
25 | | #define quantlib_nonstandard_swap_hpp |
26 | | |
27 | | #include <ql/instruments/swap.hpp> |
28 | | #include <ql/instruments/fixedvsfloatingswap.hpp> |
29 | | #include <ql/time/daycounter.hpp> |
30 | | #include <ql/time/schedule.hpp> |
31 | | #include <ql/optional.hpp> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | class IborIndex; |
36 | | class SwapIndex; |
37 | | |
38 | | //! nonstandard swap |
39 | | |
40 | | class NonstandardSwap : public Swap { |
41 | | public: |
42 | | class arguments; |
43 | | class results; |
44 | | class engine; |
45 | | explicit NonstandardSwap(const FixedVsFloatingSwap &fromVanilla); |
46 | | NonstandardSwap(Swap::Type type, |
47 | | std::vector<Real> fixedNominal, |
48 | | const std::vector<Real>& floatingNominal, |
49 | | Schedule fixedSchedule, |
50 | | std::vector<Real> fixedRate, |
51 | | DayCounter fixedDayCount, |
52 | | Schedule floatingSchedule, |
53 | | ext::shared_ptr<IborIndex> iborIndex, |
54 | | Real gearing, |
55 | | Spread spread, |
56 | | DayCounter floatingDayCount, |
57 | | bool intermediateCapitalExchange = false, |
58 | | bool finalCapitalExchange = false, |
59 | | ext::optional<BusinessDayConvention> paymentConvention = ext::nullopt); |
60 | | NonstandardSwap(Swap::Type type, |
61 | | std::vector<Real> fixedNominal, |
62 | | std::vector<Real> floatingNominal, |
63 | | Schedule fixedSchedule, |
64 | | std::vector<Real> fixedRate, |
65 | | DayCounter fixedDayCount, |
66 | | Schedule floatingSchedule, |
67 | | ext::shared_ptr<IborIndex> iborIndex, |
68 | | std::vector<Real> gearing, |
69 | | std::vector<Spread> spread, |
70 | | DayCounter floatingDayCount, |
71 | | bool intermediateCapitalExchange = false, |
72 | | bool finalCapitalExchange = false, |
73 | | ext::optional<BusinessDayConvention> paymentConvention = ext::nullopt); |
74 | | //! \name Inspectors |
75 | | //@{ |
76 | | Swap::Type type() const; |
77 | | const std::vector<Real> &fixedNominal() const; |
78 | | const std::vector<Real> &floatingNominal() const; |
79 | | |
80 | | const Schedule &fixedSchedule() const; |
81 | | const std::vector<Real> &fixedRate() const; |
82 | | const DayCounter &fixedDayCount() const; |
83 | | |
84 | | const Schedule &floatingSchedule() const; |
85 | | const ext::shared_ptr<IborIndex> &iborIndex() const; |
86 | | Spread spread() const; |
87 | | Real gearing() const; |
88 | | const std::vector<Spread>& spreads() const; |
89 | | const std::vector<Real>& gearings() const; |
90 | | const DayCounter &floatingDayCount() const; |
91 | | |
92 | | BusinessDayConvention paymentConvention() const; |
93 | | |
94 | | const Leg &fixedLeg() const; |
95 | | const Leg &floatingLeg() const; |
96 | | //@} |
97 | | |
98 | | //! \name Results |
99 | | //@{ |
100 | | //@} |
101 | | // other |
102 | | void setupArguments(PricingEngine::arguments* args) const override; |
103 | | void fetchResults(const PricingEngine::results*) const override; |
104 | | |
105 | | private: |
106 | | void init(); |
107 | | void setupExpired() const override; |
108 | | Swap::Type type_; |
109 | | std::vector<Real> fixedNominal_, floatingNominal_; |
110 | | Schedule fixedSchedule_; |
111 | | std::vector<Real> fixedRate_; |
112 | | DayCounter fixedDayCount_; |
113 | | Schedule floatingSchedule_; |
114 | | ext::shared_ptr<IborIndex> iborIndex_; |
115 | | std::vector<Spread> spread_; |
116 | | std::vector<Real> gearing_; |
117 | | bool singleSpreadAndGearing_; |
118 | | DayCounter floatingDayCount_; |
119 | | BusinessDayConvention paymentConvention_; |
120 | | const bool intermediateCapitalExchange_; |
121 | | const bool finalCapitalExchange_; |
122 | | // results |
123 | | }; |
124 | | |
125 | | //! %Arguments for nonstandard swap calculation |
126 | | class NonstandardSwap::arguments : public Swap::arguments { |
127 | | public: |
128 | | arguments() = default; |
129 | | Swap::Type type = Swap::Receiver; |
130 | | std::vector<Real> fixedNominal, floatingNominal; |
131 | | |
132 | | std::vector<Date> fixedResetDates; |
133 | | std::vector<Date> fixedPayDates; |
134 | | std::vector<Time> floatingAccrualTimes; |
135 | | std::vector<Date> floatingResetDates; |
136 | | std::vector<Date> floatingFixingDates; |
137 | | std::vector<Date> floatingPayDates; |
138 | | |
139 | | std::vector<Real> fixedCoupons; |
140 | | std::vector<Real> fixedRate; |
141 | | std::vector<Spread> floatingSpreads; |
142 | | std::vector<Real> floatingGearings; |
143 | | std::vector<Real> floatingCoupons; |
144 | | |
145 | | ext::shared_ptr<IborIndex> iborIndex; |
146 | | |
147 | | std::vector<bool> fixedIsRedemptionFlow; |
148 | | std::vector<bool> floatingIsRedemptionFlow; |
149 | | |
150 | | void validate() const override; |
151 | | }; |
152 | | |
153 | | //! %Results from nonstandard swap calculation |
154 | | class NonstandardSwap::results : public Swap::results { |
155 | | public: |
156 | | void reset() override; |
157 | | }; |
158 | | |
159 | | class NonstandardSwap::engine |
160 | | : public GenericEngine<NonstandardSwap::arguments, |
161 | | NonstandardSwap::results> {}; |
162 | | |
163 | | // inline definitions |
164 | | |
165 | 0 | inline Swap::Type NonstandardSwap::type() const { return type_; } |
166 | | |
167 | 0 | inline const std::vector<Real> &NonstandardSwap::fixedNominal() const { |
168 | 0 | return fixedNominal_; |
169 | 0 | } |
170 | | |
171 | 0 | inline const std::vector<Real> &NonstandardSwap::floatingNominal() const { |
172 | 0 | return floatingNominal_; |
173 | 0 | } |
174 | | |
175 | 0 | inline const Schedule &NonstandardSwap::fixedSchedule() const { |
176 | 0 | return fixedSchedule_; |
177 | 0 | } |
178 | | |
179 | 0 | inline const std::vector<Real> &NonstandardSwap::fixedRate() const { |
180 | 0 | return fixedRate_; |
181 | 0 | } |
182 | | |
183 | 0 | inline const DayCounter &NonstandardSwap::fixedDayCount() const { |
184 | 0 | return fixedDayCount_; |
185 | 0 | } |
186 | | |
187 | 0 | inline const Schedule &NonstandardSwap::floatingSchedule() const { |
188 | 0 | return floatingSchedule_; |
189 | 0 | } |
190 | | |
191 | | inline const ext::shared_ptr<IborIndex> & |
192 | 0 | NonstandardSwap::iborIndex() const { |
193 | 0 | return iborIndex_; |
194 | 0 | } |
195 | | |
196 | 0 | inline Spread NonstandardSwap::spread() const { |
197 | 0 | QL_REQUIRE(singleSpreadAndGearing_, |
198 | 0 | "spread is a vector, use spreads inspector instead"); |
199 | 0 | return spread_.front(); |
200 | 0 | } |
201 | | |
202 | 0 | inline Real NonstandardSwap::gearing() const { |
203 | 0 | QL_REQUIRE(singleSpreadAndGearing_, |
204 | 0 | "gearing is a vector, use gearings inspector instead"); |
205 | 0 | return gearing_.front(); |
206 | 0 | } |
207 | | |
208 | 0 | inline const std::vector<Spread> &NonstandardSwap::spreads() const { |
209 | 0 | return spread_; |
210 | 0 | } |
211 | | |
212 | 0 | inline const std::vector<Real> &NonstandardSwap::gearings() const { |
213 | 0 | return gearing_; |
214 | 0 | } |
215 | | |
216 | 0 | inline const DayCounter &NonstandardSwap::floatingDayCount() const { |
217 | 0 | return floatingDayCount_; |
218 | 0 | } |
219 | | |
220 | 0 | inline BusinessDayConvention NonstandardSwap::paymentConvention() const { |
221 | 0 | return paymentConvention_; |
222 | 0 | } |
223 | | |
224 | 0 | inline const Leg &NonstandardSwap::fixedLeg() const { return legs_[0]; } |
225 | | |
226 | 0 | inline const Leg &NonstandardSwap::floatingLeg() const { return legs_[1]; } |
227 | | } |
228 | | |
229 | | #endif |