| 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 | Real DoubleStickyRatchetPayoff::operator()(Real forward) const { |
| 27 | QL_REQUIRE((std::fabs(type1_)==1.0 || type1_==0.0), |
| 28 | "unknown/illegal type1 value (only 0.0 and +/-1,0 are allowed))" ); |
| 29 | QL_REQUIRE((std::fabs(type2_)==1.0 || type2_==0.0), |
| 30 | "unknown/illegal type2 value(only 0.0 and +/-1,0 are allowed)" ); |
| 31 | Real swaplet = gearing3_ * forward + spread3_; |
| 32 | Real effStrike1 = gearing1_ * initialValue1_ + spread1_; |
| 33 | Real effStrike2 = gearing2_ * initialValue2_ + spread2_; |
| 34 | Real effStrike3 = type1_*type2_*std::max<Real>(a: type2_*(swaplet-effStrike2),b: 0.0); |
| 35 | Real price = accrualFactor_ * (swaplet - |
| 36 | type1_*std::max<Real>(a: type1_*(swaplet-effStrike1),b: effStrike3)); |
| 37 | return price; |
| 38 | } |
| 39 | |
| 40 | std::string DoubleStickyRatchetPayoff::name() const { |
| 41 | return "DoubleStickyRatchetPayoff" ; |
| 42 | } |
| 43 | |
| 44 | std::string DoubleStickyRatchetPayoff::description() const { |
| 45 | std::ostringstream result; |
| 46 | result << name(); |
| 47 | return result.str(); |
| 48 | } |
| 49 | |
| 50 | void DoubleStickyRatchetPayoff::accept(AcyclicVisitor& v) { |
| 51 | auto* v1 = dynamic_cast<Visitor<DoubleStickyRatchetPayoff>*>(&v); |
| 52 | if (v1 != nullptr) |
| 53 | v1->visit(*this); |
| 54 | else |
| 55 | Payoff::accept(v); |
| 56 | } |
| 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 | } |
| 90 | |