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