/src/quantlib/ql/instruments/capfloor.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb |
5 | | Copyright (C) 2006, 2014 Ferdinando Ametrano |
6 | | Copyright (C) 2006 François du Vignaud |
7 | | Copyright (C) 2006, 2007 StatPro Italia srl |
8 | | |
9 | | This file is part of QuantLib, a free-software/open-source library |
10 | | for financial quantitative analysts and developers - http://quantlib.org/ |
11 | | |
12 | | QuantLib is free software: you can redistribute it and/or modify it |
13 | | under the terms of the QuantLib license. You should have received a |
14 | | copy of the license along with this program; if not, please email |
15 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
16 | | <https://www.quantlib.org/license.shtml>. |
17 | | |
18 | | This program is distributed in the hope that it will be useful, but WITHOUT |
19 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
20 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
21 | | */ |
22 | | |
23 | | /*! \file capfloor.hpp |
24 | | \brief cap and floor class |
25 | | */ |
26 | | |
27 | | #ifndef quantlib_instruments_capfloor_hpp |
28 | | #define quantlib_instruments_capfloor_hpp |
29 | | |
30 | | #include <ql/instrument.hpp> |
31 | | #include <ql/cashflows/iborcoupon.hpp> |
32 | | #include <ql/handle.hpp> |
33 | | #include <ql/termstructures/volatility/volatilitytype.hpp> |
34 | | |
35 | | namespace QuantLib { |
36 | | |
37 | | class YieldTermStructure; |
38 | | |
39 | | //! Base class for cap-like instruments |
40 | | /*! \ingroup instruments |
41 | | |
42 | | \test |
43 | | - the correctness of the returned value is tested by checking |
44 | | that the price of a cap (resp. floor) decreases |
45 | | (resp. increases) with the strike rate. |
46 | | - the relationship between the values of caps, floors and the |
47 | | resulting collars is checked. |
48 | | - the put-call parity between the values of caps, floors and |
49 | | swaps is checked. |
50 | | - the correctness of the returned implied volatility is tested |
51 | | by using it for reproducing the target value. |
52 | | - the correctness of the returned value is tested by checking |
53 | | it against a known good value. |
54 | | */ |
55 | | class CapFloor : public Instrument { |
56 | | public: |
57 | | enum Type { Cap, Floor, Collar }; |
58 | | class arguments; |
59 | | class engine; |
60 | | CapFloor(Type type, |
61 | | Leg floatingLeg, |
62 | | std::vector<Rate> capRates, |
63 | | std::vector<Rate> floorRates); |
64 | | CapFloor(Type type, Leg floatingLeg, const std::vector<Rate>& strikes); |
65 | | //! \name Observable interface |
66 | | //@{ |
67 | | void deepUpdate() override; |
68 | | //@} |
69 | | //! \name Instrument interface |
70 | | //@{ |
71 | | bool isExpired() const override; |
72 | | void setupArguments(PricingEngine::arguments*) const override; |
73 | | //@} |
74 | | //! \name Inspectors |
75 | | //@{ |
76 | 0 | Type type() const { return type_; } |
77 | 0 | const std::vector<Rate>& capRates() const { return capRates_; } |
78 | 0 | const std::vector<Rate>& floorRates() const { return floorRates_; } |
79 | 0 | const Leg& floatingLeg() const { return floatingLeg_; } |
80 | | |
81 | | Date startDate() const; |
82 | | Date maturityDate() const; |
83 | | ext::shared_ptr<FloatingRateCoupon> lastFloatingRateCoupon() const; |
84 | | //! Returns the n-th optionlet as a new CapFloor with only one cash flow. |
85 | | ext::shared_ptr<CapFloor> optionlet(Size n) const; |
86 | | //@} |
87 | | Rate atmRate(const YieldTermStructure& discountCurve) const; |
88 | | //! implied term volatility |
89 | | Volatility impliedVolatility( |
90 | | Real price, |
91 | | const Handle<YieldTermStructure>& disc, |
92 | | Volatility guess, |
93 | | Real accuracy = 1.0e-4, |
94 | | Natural maxEvaluations = 100, |
95 | | Volatility minVol = 1.0e-7, |
96 | | Volatility maxVol = 4.0, |
97 | | VolatilityType type = ShiftedLognormal, |
98 | | Real displacement = 0.0) const; |
99 | | private: |
100 | | Type type_; |
101 | | Leg floatingLeg_; |
102 | | std::vector<Rate> capRates_; |
103 | | std::vector<Rate> floorRates_; |
104 | | }; |
105 | | |
106 | | //! Concrete cap class |
107 | | /*! \ingroup instruments */ |
108 | | class Cap : public CapFloor { |
109 | | public: |
110 | | Cap(const Leg& floatingLeg, |
111 | | const std::vector<Rate>& exerciseRates) |
112 | 0 | : CapFloor(CapFloor::Cap, floatingLeg, |
113 | 0 | exerciseRates, std::vector<Rate>()) {} |
114 | | }; |
115 | | |
116 | | //! Concrete floor class |
117 | | /*! \ingroup instruments */ |
118 | | class Floor : public CapFloor { |
119 | | public: |
120 | | Floor(const Leg& floatingLeg, |
121 | | const std::vector<Rate>& exerciseRates) |
122 | | : CapFloor(CapFloor::Floor, floatingLeg, |
123 | 0 | std::vector<Rate>(), exerciseRates) {} |
124 | | }; |
125 | | |
126 | | //! Concrete collar class |
127 | | /*! \ingroup instruments */ |
128 | | class Collar : public CapFloor { |
129 | | public: |
130 | | Collar(const Leg& floatingLeg, |
131 | | const std::vector<Rate>& capRates, |
132 | | const std::vector<Rate>& floorRates) |
133 | 0 | : CapFloor(CapFloor::Collar, floatingLeg, capRates, floorRates) {} |
134 | | }; |
135 | | |
136 | | |
137 | | //! %Arguments for cap/floor calculation |
138 | | class CapFloor::arguments : public virtual PricingEngine::arguments { |
139 | | public: |
140 | 0 | arguments() : type(CapFloor::Type(-1)) {} |
141 | | CapFloor::Type type; |
142 | | std::vector<Date> startDates; |
143 | | std::vector<Date> fixingDates; |
144 | | std::vector<Date> endDates; |
145 | | std::vector<Time> accrualTimes; |
146 | | std::vector<Rate> capRates; |
147 | | std::vector<Rate> floorRates; |
148 | | std::vector<Rate> forwards; |
149 | | std::vector<Real> gearings; |
150 | | std::vector<Real> spreads; |
151 | | std::vector<Real> nominals; |
152 | | std::vector<ext::shared_ptr<InterestRateIndex> > indexes; |
153 | | void validate() const override; |
154 | | }; |
155 | | |
156 | | //! base class for cap/floor engines |
157 | | class CapFloor::engine |
158 | | : public GenericEngine<CapFloor::arguments, CapFloor::results> {}; |
159 | | |
160 | | std::ostream& operator<<(std::ostream&, CapFloor::Type); |
161 | | |
162 | | } |
163 | | |
164 | | #endif |