| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2003, 2006 Ferdinando Ametrano |
| 5 | Copyright (C) 2006 Warren Chou |
| 6 | Copyright (C) 2006, 2008 StatPro Italia srl |
| 7 | Copyright (C) 2006 Chiara Fornarola |
| 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 | <http://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 | #include <ql/instruments/payoffs.hpp> |
| 24 | |
| 25 | namespace QuantLib { |
| 26 | |
| 27 | std::string NullPayoff::name() const { |
| 28 | return "Null" ; |
| 29 | } |
| 30 | |
| 31 | std::string NullPayoff::description() const { |
| 32 | return name(); |
| 33 | } |
| 34 | |
| 35 | Real NullPayoff::operator()(Real) const { |
| 36 | QL_FAIL("dummy payoff given" ); |
| 37 | } |
| 38 | |
| 39 | void NullPayoff::accept(AcyclicVisitor& v) { |
| 40 | auto* v1 = dynamic_cast<Visitor<NullPayoff>*>(&v); |
| 41 | if (v1 != nullptr) |
| 42 | v1->visit(*this); |
| 43 | else |
| 44 | Payoff::accept(v); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | std::string TypePayoff::description() const { |
| 50 | std::ostringstream result; |
| 51 | result << name() << " " << optionType(); |
| 52 | return result.str(); |
| 53 | } |
| 54 | |
| 55 | //std::string StrikedPayoff::description() const { |
| 56 | // std::ostringstream result; |
| 57 | // result << ", " << strike() << " strike"; |
| 58 | // return result.str(); |
| 59 | //} |
| 60 | |
| 61 | Real FloatingTypePayoff::operator()(Real) const { |
| 62 | QL_FAIL("floating payoff not handled" ); |
| 63 | } |
| 64 | |
| 65 | Real FloatingTypePayoff::operator()(Real price, Real strike) const { |
| 66 | switch (type_) { |
| 67 | case Option::Call: |
| 68 | return std::max<Real>(a: price - strike,b: 0.0); |
| 69 | case Option::Put: |
| 70 | return std::max<Real>(a: strike - price,b: 0.0); |
| 71 | default: |
| 72 | QL_FAIL("unknown/illegal option type" ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | std::string StrikedTypePayoff::description() const { |
| 77 | std::ostringstream result; |
| 78 | result << TypePayoff::description() << ", " << |
| 79 | strike() << " strike" ; |
| 80 | return result.str(); |
| 81 | } |
| 82 | |
| 83 | void FloatingTypePayoff::accept(AcyclicVisitor& v) { |
| 84 | auto* v1 = dynamic_cast<Visitor<FloatingTypePayoff>*>(&v); |
| 85 | if (v1 != nullptr) |
| 86 | v1->visit(*this); |
| 87 | else |
| 88 | Payoff::accept(v); |
| 89 | } |
| 90 | |
| 91 | Real PlainVanillaPayoff::operator()(Real price) const { |
| 92 | switch (type_) { |
| 93 | case Option::Call: |
| 94 | return std::max<Real>(a: price-strike_,b: 0.0); |
| 95 | case Option::Put: |
| 96 | return std::max<Real>(a: strike_-price,b: 0.0); |
| 97 | default: |
| 98 | QL_FAIL("unknown/illegal option type" ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void PlainVanillaPayoff::accept(AcyclicVisitor& v) { |
| 103 | auto* v1 = dynamic_cast<Visitor<PlainVanillaPayoff>*>(&v); |
| 104 | if (v1 != nullptr) |
| 105 | v1->visit(*this); |
| 106 | else |
| 107 | Payoff::accept(v); |
| 108 | } |
| 109 | |
| 110 | Real PercentageStrikePayoff::operator()(Real price) const { |
| 111 | switch (type_) { |
| 112 | case Option::Call: |
| 113 | return price*std::max<Real>(a: Real(1.0)-strike_,b: 0.0); |
| 114 | case Option::Put: |
| 115 | return price*std::max<Real>(a: strike_-Real(1.0),b: 0.0); |
| 116 | default: |
| 117 | QL_FAIL("unknown/illegal option type" ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void PercentageStrikePayoff::accept(AcyclicVisitor& v) { |
| 122 | auto* v1 = dynamic_cast<Visitor<PercentageStrikePayoff>*>(&v); |
| 123 | if (v1 != nullptr) |
| 124 | v1->visit(*this); |
| 125 | else |
| 126 | Payoff::accept(v); |
| 127 | } |
| 128 | |
| 129 | Real AssetOrNothingPayoff::operator()(Real price) const { |
| 130 | switch (type_) { |
| 131 | case Option::Call: |
| 132 | return (price-strike_ > 0.0 ? price : 0.0); |
| 133 | case Option::Put: |
| 134 | return (strike_-price > 0.0 ? price : 0.0); |
| 135 | default: |
| 136 | QL_FAIL("unknown/illegal option type" ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void AssetOrNothingPayoff::accept(AcyclicVisitor& v) { |
| 141 | auto* v1 = dynamic_cast<Visitor<AssetOrNothingPayoff>*>(&v); |
| 142 | if (v1 != nullptr) |
| 143 | v1->visit(*this); |
| 144 | else |
| 145 | Payoff::accept(v); |
| 146 | } |
| 147 | |
| 148 | std::string CashOrNothingPayoff::description() const { |
| 149 | std::ostringstream result; |
| 150 | result << StrikedTypePayoff::description() << ", " << cashPayoff() << " cash payoff" ; |
| 151 | return result.str(); |
| 152 | } |
| 153 | |
| 154 | Real CashOrNothingPayoff::operator()(Real price) const { |
| 155 | switch (type_) { |
| 156 | case Option::Call: |
| 157 | return (price-strike_ > 0.0 ? cashPayoff_ : 0.0); |
| 158 | case Option::Put: |
| 159 | return (strike_-price > 0.0 ? cashPayoff_ : 0.0); |
| 160 | default: |
| 161 | QL_FAIL("unknown/illegal option type" ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void CashOrNothingPayoff::accept(AcyclicVisitor& v) { |
| 166 | auto* v1 = dynamic_cast<Visitor<CashOrNothingPayoff>*>(&v); |
| 167 | if (v1 != nullptr) |
| 168 | v1->visit(*this); |
| 169 | else |
| 170 | Payoff::accept(v);} |
| 171 | |
| 172 | std::string GapPayoff::description() const { |
| 173 | std::ostringstream result; |
| 174 | result << StrikedTypePayoff::description() << ", " << secondStrike() << " strike payoff" ; |
| 175 | return result.str(); |
| 176 | } |
| 177 | |
| 178 | Real GapPayoff::operator()(Real price) const { |
| 179 | switch (type_) { |
| 180 | case Option::Call: |
| 181 | return (price-strike_ >= 0.0 ? Real(price-secondStrike_) : 0.0); |
| 182 | case Option::Put: |
| 183 | return (strike_ - price >= 0.0 ? Real(secondStrike_ - price) : 0.0); |
| 184 | default: |
| 185 | QL_FAIL("unknown/illegal option type" ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void GapPayoff::accept(AcyclicVisitor& v) { |
| 190 | auto* v1 = dynamic_cast<Visitor<GapPayoff>*>(&v); |
| 191 | if (v1 != nullptr) |
| 192 | v1->visit(*this); |
| 193 | else |
| 194 | Payoff::accept(v); |
| 195 | } |
| 196 | |
| 197 | Real SuperFundPayoff::operator()(Real price) const { |
| 198 | return (price>=strike_ && price<secondStrike_) ? Real(price/strike_) : 0.0; |
| 199 | } |
| 200 | |
| 201 | void SuperFundPayoff::accept(AcyclicVisitor& v) { |
| 202 | auto* v1 = dynamic_cast<Visitor<SuperFundPayoff>*>(&v); |
| 203 | if (v1 != nullptr) |
| 204 | v1->visit(*this); |
| 205 | else |
| 206 | Payoff::accept(v); |
| 207 | } |
| 208 | std::string SuperSharePayoff::description() const { |
| 209 | std::ostringstream result; |
| 210 | result << StrikedTypePayoff::description() << ", " << secondStrike() << " second strike" << ", " << cashPayoff() << " amount" ; |
| 211 | return result.str(); |
| 212 | } |
| 213 | |
| 214 | Real SuperSharePayoff::operator()(Real price) const { |
| 215 | return (price>=strike_ && price<secondStrike_) ? cashPayoff_ : 0.0; |
| 216 | } |
| 217 | |
| 218 | void SuperSharePayoff::accept(AcyclicVisitor& v) { |
| 219 | auto* v1 = dynamic_cast<Visitor<SuperSharePayoff>*>(&v); |
| 220 | if (v1 != nullptr) |
| 221 | v1->visit(*this); |
| 222 | else |
| 223 | Payoff::accept(v); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |