/src/quantlib/ql/cashflows/indexedcashflow.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2009 Chris Kenyon |
5 | | Copyright (C) 2022 Quaternion Risk Management Ltd |
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 indexedcashflow.hpp |
22 | | \brief Cash flow dependent on an index ratio (NOT a coupon, i.e. no accruals) |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_indexed_cash_flow_hpp |
26 | | #define quantlib_indexed_cash_flow_hpp |
27 | | |
28 | | #include <ql/patterns/visitor.hpp> |
29 | | #include <ql/cashflow.hpp> |
30 | | #include <ql/index.hpp> |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | //! Cash flow dependent on an index ratio. |
35 | | |
36 | | /*! This cash flow is not a coupon, i.e., there's no accrual. The |
37 | | amount is either i(T)/i(0) or i(T)/i(0) - 1, depending on the |
38 | | growthOnly parameter. |
39 | | |
40 | | We expect this to be used inside an instrument that does all the date |
41 | | adjustment etc., so this takes just dates and does not change them. |
42 | | growthOnly = false means i(T)/i(0), which is a bond-type setting. |
43 | | growthOnly = true means i(T)/i(0) - 1, which is a swap-type setting. |
44 | | */ |
45 | | class IndexedCashFlow : public CashFlow { |
46 | | public: |
47 | | IndexedCashFlow(Real notional, |
48 | | ext::shared_ptr<Index> index, |
49 | | const Date& baseDate, |
50 | | const Date& fixingDate, |
51 | | const Date& paymentDate, |
52 | | bool growthOnly = false); |
53 | | //! \name Event interface |
54 | | //@{ |
55 | 0 | Date date() const override { return paymentDate_; } |
56 | | //@} |
57 | 0 | virtual Real notional() const { return notional_; } |
58 | 0 | virtual Date baseDate() const { return baseDate_; } |
59 | 0 | virtual Date fixingDate() const { return fixingDate_; } |
60 | 0 | virtual ext::shared_ptr<Index> index() const { return index_; } |
61 | 0 | virtual bool growthOnly() const { return growthOnly_; } |
62 | 0 | virtual Real baseFixing() const { return index_->fixing(baseDate()); } |
63 | 0 | virtual Real indexFixing() const { return index_->fixing(fixingDate_); } |
64 | | //! \name CashFlow interface |
65 | | //@{ |
66 | | Real amount() const override; // already virtual |
67 | | //@} |
68 | | //! \name Visitability |
69 | | //@{ |
70 | | void accept(AcyclicVisitor&) override; |
71 | | //@} |
72 | | //! \name LazyObject interface |
73 | | //@{ |
74 | | void performCalculations() const override; |
75 | | //@} |
76 | | protected: |
77 | | mutable Real amount_; |
78 | | private: |
79 | | Real notional_; |
80 | | ext::shared_ptr<Index> index_; |
81 | | Date baseDate_, fixingDate_, paymentDate_; |
82 | | bool growthOnly_; |
83 | | }; |
84 | | |
85 | | |
86 | | // inline definitions |
87 | | |
88 | 0 | inline void IndexedCashFlow::accept(AcyclicVisitor& v) { |
89 | 0 | auto* v1 = dynamic_cast<Visitor<IndexedCashFlow>*>(&v); |
90 | 0 | if (v1 != nullptr) |
91 | 0 | v1->visit(*this); |
92 | 0 | else |
93 | 0 | CashFlow::accept(v); |
94 | 0 | } |
95 | | |
96 | | } |
97 | | |
98 | | #endif |