/src/quantlib/ql/instruments/stickyratchet.cpp
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) 2007 Marco Bianchetti |
5 | | Copyright (C) 2007 Giorgio Facchinetti |
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 | | <http://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 | | #include <ql/instruments/stickyratchet.hpp> |
22 | | |
23 | | namespace QuantLib { |
24 | | |
25 | | // Double Sticky/Ratchet payoffs |
26 | 0 | Real DoubleStickyRatchetPayoff::operator()(Real forward) const { |
27 | 0 | QL_REQUIRE((std::fabs(type1_)==1.0 || type1_==0.0), |
28 | 0 | "unknown/illegal type1 value (only 0.0 and +/-1,0 are allowed))"); |
29 | 0 | QL_REQUIRE((std::fabs(type2_)==1.0 || type2_==0.0), |
30 | 0 | "unknown/illegal type2 value(only 0.0 and +/-1,0 are allowed)"); |
31 | 0 | Real swaplet = gearing3_ * forward + spread3_; |
32 | 0 | Real effStrike1 = gearing1_ * initialValue1_ + spread1_; |
33 | 0 | Real effStrike2 = gearing2_ * initialValue2_ + spread2_; |
34 | 0 | Real effStrike3 = type1_*type2_*std::max<Real>(type2_*(swaplet-effStrike2),0.0); |
35 | 0 | Real price = accrualFactor_ * (swaplet - |
36 | 0 | type1_*std::max<Real>(type1_*(swaplet-effStrike1),effStrike3)); |
37 | 0 | return price; |
38 | 0 | } |
39 | | |
40 | 0 | std::string DoubleStickyRatchetPayoff::name() const { |
41 | 0 | return "DoubleStickyRatchetPayoff"; |
42 | 0 | } |
43 | | |
44 | 0 | std::string DoubleStickyRatchetPayoff::description() const { |
45 | 0 | std::ostringstream result; |
46 | 0 | result << name(); |
47 | 0 | return result.str(); |
48 | 0 | } |
49 | | |
50 | 0 | void DoubleStickyRatchetPayoff::accept(AcyclicVisitor& v) { |
51 | 0 | auto* v1 = dynamic_cast<Visitor<DoubleStickyRatchetPayoff>*>(&v); |
52 | 0 | if (v1 != nullptr) |
53 | 0 | v1->visit(*this); |
54 | 0 | else |
55 | 0 | Payoff::accept(v); |
56 | 0 | } |
57 | | |
58 | | /*--------------------------------------------------------------------------- |
59 | | |
60 | | // Old code for single sticky/ratchet payoffs, |
61 | | // superated by DoubleStickyRatchetPayoff class above |
62 | | |
63 | | // Single Sticky/Ratchet payoffs |
64 | | Real StickyRatchetPayoff::operator()(Real forward) const { |
65 | | QL_REQUIRE(abs(type_)==1.0, "unknown/illegal option type"); |
66 | | Real swaplet = gearing2_ * forward + spread2_; |
67 | | Real effStrike = gearing2_ * initialValue_ + spread2_; |
68 | | Real price = accrualFactor_ * (swaplet - |
69 | | type_*std::max<Real>(type_*(swaplet-effStrike),0.0)); |
70 | | return price; |
71 | | } |
72 | | |
73 | | std::string StickyRatchetPayoff::description() const { |
74 | | std::ostringstream result; |
75 | | result << name(); |
76 | | return result.str(); |
77 | | } |
78 | | |
79 | | void StickyRatchetPayoff::accept(AcyclicVisitor& v) { |
80 | | Visitor<StickyRatchetPayoff>* v1 = |
81 | | dynamic_cast<Visitor<StickyRatchetPayoff>*>(&v); |
82 | | if (v1 != 0) |
83 | | v1->visit(*this); |
84 | | else |
85 | | Payoff::accept(v); |
86 | | } |
87 | | -----------------------------------------------------------------------------*/ |
88 | | |
89 | | } |