QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
payoffs.cpp
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
25namespace QuantLib {
26
27 std::string NullPayoff::name() const {
28 return "Null";
29 }
30
31 std::string NullPayoff::description() const {
32 return name();
33 }
34
36 QL_FAIL("dummy payoff given");
37 }
38
40 auto* v1 = dynamic_cast<Visitor<NullPayoff>*>(&v);
41 if (v1 != nullptr)
42 v1->visit(*this);
43 else
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
62 QL_FAIL("floating payoff not handled");
63 }
64
66 switch (type_) {
67 case Option::Call:
68 return std::max<Real>(price - strike,0.0);
69 case Option::Put:
70 return std::max<Real>(strike - price,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
84 auto* v1 = dynamic_cast<Visitor<FloatingTypePayoff>*>(&v);
85 if (v1 != nullptr)
86 v1->visit(*this);
87 else
89 }
90
92 switch (type_) {
93 case Option::Call:
94 return std::max<Real>(price-strike_,0.0);
95 case Option::Put:
96 return std::max<Real>(strike_-price,0.0);
97 default:
98 QL_FAIL("unknown/illegal option type");
99 }
100 }
101
103 auto* v1 = dynamic_cast<Visitor<PlainVanillaPayoff>*>(&v);
104 if (v1 != nullptr)
105 v1->visit(*this);
106 else
108 }
109
111 switch (type_) {
112 case Option::Call:
113 return price*std::max<Real>(Real(1.0)-strike_,0.0);
114 case Option::Put:
115 return price*std::max<Real>(strike_-Real(1.0),0.0);
116 default:
117 QL_FAIL("unknown/illegal option type");
118 }
119 }
120
122 auto* v1 = dynamic_cast<Visitor<PercentageStrikePayoff>*>(&v);
123 if (v1 != nullptr)
124 v1->visit(*this);
125 else
127 }
128
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
141 auto* v1 = dynamic_cast<Visitor<AssetOrNothingPayoff>*>(&v);
142 if (v1 != nullptr)
143 v1->visit(*this);
144 else
146 }
147
149 std::ostringstream result;
150 result << StrikedTypePayoff::description() << ", " << cashPayoff() << " cash payoff";
151 return result.str();
152 }
153
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
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
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
190 auto* v1 = dynamic_cast<Visitor<GapPayoff>*>(&v);
191 if (v1 != nullptr)
192 v1->visit(*this);
193 else
195 }
196
198 return (price>=strike_ && price<secondStrike_) ? Real(price/strike_) : 0.0;
199 }
200
202 auto* v1 = dynamic_cast<Visitor<SuperFundPayoff>*>(&v);
203 if (v1 != nullptr)
204 v1->visit(*this);
205 else
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
215 return (price>=strike_ && price<secondStrike_) ? cashPayoff_ : 0.0;
216 }
217
219 auto* v1 = dynamic_cast<Visitor<SuperSharePayoff>*>(&v);
220 if (v1 != nullptr)
221 v1->visit(*this);
222 else
224 }
225
226}
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
Real operator()(Real price) const override
Definition: payoffs.cpp:129
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:140
Real operator()(Real price) const override
Definition: payoffs.cpp:154
std::string description() const override
Definition: payoffs.cpp:148
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:165
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:83
Real operator()(Real price, Real strike) const
Definition: payoffs.cpp:65
Real operator()(Real price) const override
Definition: payoffs.cpp:178
std::string description() const override
Definition: payoffs.cpp:172
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:189
Real secondStrike() const
Definition: payoffs.hpp:191
Real operator()(Real price) const override
Definition: payoffs.cpp:35
std::string description() const override
Definition: payoffs.cpp:31
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:39
std::string name() const override
Definition: payoffs.cpp:27
virtual void accept(AcyclicVisitor &)
Definition: payoff.hpp:70
virtual std::string name() const =0
Real operator()(Real price) const override
Definition: payoffs.cpp:110
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:121
Real operator()(Real price) const override
Definition: payoffs.cpp:91
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:102
std::string description() const override
Definition: payoffs.cpp:76
Real operator()(Real price) const override
Definition: payoffs.cpp:197
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:201
Real operator()(Real price) const override
Definition: payoffs.cpp:214
std::string description() const override
Definition: payoffs.cpp:208
void accept(AcyclicVisitor &) override
Definition: payoffs.cpp:218
Real secondStrike() const
Definition: payoffs.hpp:251
std::string description() const override
Definition: payoffs.cpp:49
Option::Type type_
Definition: payoffs.hpp:58
Option::Type optionType() const
Definition: payoffs.hpp:51
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35