Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
strike.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019, 2021 Quaternion Risk Management Ltd
3 Copyright (C) 2021 Skandinaviska Enskilda Banken AB (publ)
4 All rights reserved.
5
6 This file is part of ORE, a free-software/open-source library
7 for transparent pricing and risk analysis - http://opensourcerisk.org
8
9 ORE is free software: you can redistribute it and/or modify it
10 under the terms of the Modified BSD License. You should have received a
11 copy of the license along with this program.
12 The license is also available online at <http://opensourcerisk.org>
13
14 This program is distributed on the basis that it will form a useful
15 contribution to risk analytics and model standardisation, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
23
24#include <boost/algorithm/string.hpp>
25#include <boost/archive/binary_iarchive.hpp>
26#include <boost/archive/binary_oarchive.hpp>
27#include <boost/serialization/export.hpp>
28
29using namespace QuantLib;
30using std::ostream;
31using std::ostringstream;
32using std::string;
33using std::vector;
34
35namespace ore {
36namespace data {
37
38bool operator==(const BaseStrike& lhs, const BaseStrike& rhs) { return lhs.equal_to(rhs); }
39
41
42AbsoluteStrike::AbsoluteStrike(Real strike) : strike_(strike) {}
43
44Real AbsoluteStrike::strike() const { return strike_; }
45
46void AbsoluteStrike::fromString(const string& strStrike) { strike_ = parseReal(strStrike); }
47
48string AbsoluteStrike::toString() const { return to_string(strike_); }
49
50bool AbsoluteStrike::equal_to(const BaseStrike& other) const {
51 if (const AbsoluteStrike* p = dynamic_cast<const AbsoluteStrike*>(&other)) {
52 return close(strike_, p->strike());
53 } else {
54 return false;
55 }
56}
57
58DeltaStrike::DeltaStrike() : deltaType_(DeltaVolQuote::Spot), optionType_(Option::Call) {}
59
60DeltaStrike::DeltaStrike(DeltaVolQuote::DeltaType deltaType, Option::Type optionType, Real delta)
61 : deltaType_(deltaType), optionType_(optionType), delta_(delta) {}
62
63DeltaVolQuote::DeltaType DeltaStrike::deltaType() const { return deltaType_; }
64
65Option::Type DeltaStrike::optionType() const { return optionType_; }
66
67Real DeltaStrike::delta() const { return delta_; }
68
69void DeltaStrike::fromString(const string& strStrike) {
70
71 // Expect strStrike of form: DEL / Spot|Fwd|PaSpot|PaFwd / Call|Put / DELTA_VALUE
72 vector<string> tokens;
73 boost::split(tokens, strStrike, boost::is_any_of("/"));
74 QL_REQUIRE(tokens.size() == 4, "DeltaStrike::fromString expects 4 tokens.");
75 QL_REQUIRE(tokens[0] == "DEL", "DeltaStrike::fromString expects 1st token to equal 'DEL'.");
76
77 deltaType_ = parseDeltaType(tokens[1]);
78 optionType_ = parseOptionType(tokens[2]);
79 delta_ = parseReal(tokens[3]);
80}
81
82string DeltaStrike::toString() const {
83
84 // Write string of form: DEL / Spot|Fwd|PaSpot|PaFwd / Call|Put / DELTA_VALUE
85 ostringstream oss;
86 oss << "DEL/" << deltaType_ << "/" << optionType_ << "/" << to_string(delta_);
87 return oss.str();
88}
89
90bool DeltaStrike::equal_to(const BaseStrike& other) const {
91 if (const DeltaStrike* p = dynamic_cast<const DeltaStrike*>(&other)) {
92 return deltaType_ == p->deltaType() && optionType_ == p->optionType() && close(delta_, p->delta());
93 } else {
94 return false;
95 }
96}
97
98AtmStrike::AtmStrike() : atmType_(DeltaVolQuote::AtmSpot) {}
99
100AtmStrike::AtmStrike(DeltaVolQuote::AtmType atmType, boost::optional<DeltaVolQuote::DeltaType> deltaType)
101 : atmType_(atmType), deltaType_(deltaType) {
102 check();
103}
104
105DeltaVolQuote::AtmType AtmStrike::atmType() const { return atmType_; }
106
107boost::optional<DeltaVolQuote::DeltaType> AtmStrike::deltaType() const { return deltaType_; }
108
109void AtmStrike::fromString(const string& strStrike) {
110
111 // Expect strStrike of form:
112 // "ATM / AtmSpot|AtmFwd|AtmDeltaNeutral|AtmVegaMax|AtmGammaMax|AtmPutCall50"
113 // with an optional following "/ DEL / Spot|Fwd|PaSpot|PaFwd"
114 vector<string> tokens;
115 boost::split(tokens, strStrike, boost::is_any_of("/"));
116 QL_REQUIRE(tokens.size() == 2 || tokens.size() == 4, "AtmStrike::fromString expects 2 or 4 tokens.");
117 QL_REQUIRE(tokens[0] == "ATM", "AtmStrike::fromString expects 1st token to equal 'ATM'.");
118
119 atmType_ = parseAtmType(tokens[1]);
120
121 deltaType_ = boost::none;
122 if (tokens.size() == 4) {
123 QL_REQUIRE(tokens[2] == "DEL", "AtmStrike::fromString expects 3rd token to equal 'DEL'.");
124 deltaType_ = parseDeltaType(tokens[3]);
125 }
126
127 check();
128}
129
130string AtmStrike::toString() const {
131
132 // Write strike of form:
133 // "ATM / AtmSpot|AtmFwd|AtmDeltaNeutral|AtmVegaMax|AtmGammaMax|AtmPutCall50"
134 // with an optional following "/ DEL / Spot|Fwd|PaSpot|PaFwd" if delta type is populated.
135 ostringstream oss;
136 oss << "ATM/" << atmType_;
137
138 if (deltaType_) {
139 oss << "/DEL/" << (*deltaType_);
140 }
141
142 return oss.str();
143}
144
145bool AtmStrike::equal_to(const BaseStrike& other) const {
146 if (const AtmStrike* p = dynamic_cast<const AtmStrike*>(&other)) {
147 return (atmType_ == p->atmType()) &&
148 ((!deltaType_ && !p->deltaType()) || (deltaType_ && p->deltaType() && (*deltaType_ == *p->deltaType())));
149 } else {
150 return false;
151 }
152}
153
154void AtmStrike::check() const {
155 QL_REQUIRE(atmType_ != DeltaVolQuote::AtmNull, "AtmStrike type must not be AtmNull.");
156 if (atmType_ == DeltaVolQuote::AtmDeltaNeutral) {
157 QL_REQUIRE(deltaType_, "If AtmStrike type is AtmDeltaNeutral, we need a delta type.");
158 } else {
159 QL_REQUIRE(!deltaType_, "If AtmStrike type is not AtmDeltaNeutral, delta type should not be given.");
160 }
161 if (atmType_ == DeltaVolQuote::AtmPutCall50) {
162 QL_REQUIRE(deltaType_ && *deltaType_ == DeltaVolQuote::Fwd,
163 "If AtmStrike type is AtmPutCall50, delta type must be AtmFwd.");
164 }
165}
166
168
169MoneynessStrike::MoneynessStrike(Type type, Real moneyness) : type_(type), moneyness_(moneyness) {}
170
172
174
175void MoneynessStrike::fromString(const string& strStrike) {
176
177 // Expect strStrike of form "MNY / Spot|Fwd / MONEYNESS_VALUE"
178 vector<string> tokens;
179 boost::split(tokens, strStrike, boost::is_any_of("/"));
180 QL_REQUIRE(tokens.size() == 3, "MoneynessStrike::fromString expects 3 tokens.");
181 QL_REQUIRE(tokens[0] == "MNY", "MoneynessStrike::fromString expects 1st token to equal 'MNY'.");
182
183 type_ = parseMoneynessType(tokens[1]);
184 moneyness_ = parseReal(tokens[2]);
185}
186
188
189 // Write strike of form "MNY / Spot|Fwd / MONEYNESS_VALUE"
190 ostringstream oss;
191 oss << "MNY/" << type_ << "/" << to_string(moneyness_);
192 return oss.str();
193}
194
195bool MoneynessStrike::equal_to(const BaseStrike& other) const {
196 if (const MoneynessStrike* p = dynamic_cast<const MoneynessStrike*>(&other)) {
197 return type_ == p->type() && close(moneyness_, p->moneyness());
198 } else {
199 return false;
200 }
201}
202
203ostream& operator<<(ostream& os, const BaseStrike& strike) { return os << strike.toString(); }
204
205ostream& operator<<(ostream& os, DeltaVolQuote::DeltaType type) {
206 switch (type) {
207 case DeltaVolQuote::Spot:
208 return os << "Spot";
209 case DeltaVolQuote::Fwd:
210 return os << "Fwd";
211 case DeltaVolQuote::PaSpot:
212 return os << "PaSpot";
213 case DeltaVolQuote::PaFwd:
214 return os << "PaFwd";
215 default:
216 QL_FAIL("Unknown delta type");
217 }
218}
219
220ostream& operator<<(ostream& os, DeltaVolQuote::AtmType type) {
221 switch (type) {
222 case DeltaVolQuote::AtmNull:
223 return os << "AtmNull";
224 case DeltaVolQuote::AtmSpot:
225 return os << "AtmSpot";
226 case DeltaVolQuote::AtmFwd:
227 return os << "AtmFwd";
228 case DeltaVolQuote::AtmDeltaNeutral:
229 return os << "AtmDeltaNeutral";
230 case DeltaVolQuote::AtmVegaMax:
231 return os << "AtmVegaMax";
232 case DeltaVolQuote::AtmGammaMax:
233 return os << "AtmGammaMax";
234 case DeltaVolQuote::AtmPutCall50:
235 return os << "AtmPutCall50";
236 default:
237 QL_FAIL("Unknown atm type");
238 }
239}
240
241ostream& operator<<(ostream& os, MoneynessStrike::Type type) {
242 switch (type) {
244 return os << "Spot";
246 return os << "Fwd";
247 default:
248 QL_FAIL("Unknown moneyness type");
249 }
250}
251
253 if (type == "Spot") {
255 } else if (type == "Fwd") {
257 } else {
258 QL_FAIL("Moneyness type '" << type << "' not recognized");
259 }
260}
261
262QuantLib::ext::shared_ptr<BaseStrike> parseBaseStrike(const string& strStrike) {
263
264 QuantLib::ext::shared_ptr<BaseStrike> strike;
265
266 // Expect strStrike to either:
267 // 1. have a single token which means that we have an absolute strike, or
268 // 2. have multiple tokens beginning with one of DEL, ATM or MNY
269 vector<string> tokens;
270 boost::split(tokens, strStrike, boost::is_any_of("/"));
271
272 if (tokens.size() == 1) {
273 strike = QuantLib::ext::make_shared<AbsoluteStrike>();
274 } else if (tokens[0] == "DEL") {
275 strike = QuantLib::ext::make_shared<DeltaStrike>();
276 } else if (tokens[0] == "ATM") {
277 strike = QuantLib::ext::make_shared<AtmStrike>();
278 } else if (tokens[0] == "MNY") {
279 strike = QuantLib::ext::make_shared<MoneynessStrike>();
280 } else {
281 QL_FAIL("Could not parse strike string '" << strStrike << "'.");
282 }
283
284 strike->fromString(strStrike);
285
286 return strike;
287}
288
289template <class Archive> void BaseStrike::serialize(Archive& ar, const unsigned int version) {}
290
291template <class Archive> void AbsoluteStrike::serialize(Archive& ar, const unsigned int version) {
292 ar& boost::serialization::base_object<BaseStrike>(*this);
293 ar& strike_;
294}
295
296template <class Archive> void DeltaStrike::serialize(Archive& ar, const unsigned int version) {
297 ar& boost::serialization::base_object<BaseStrike>(*this);
298 ar& deltaType_;
299 ar& optionType_;
300 ar& delta_;
301}
302
303template <class Archive> void AtmStrike::serialize(Archive& ar, const unsigned int version) {
304 ar& boost::serialization::base_object<BaseStrike>(*this);
305 ar& atmType_;
306 ar& deltaType_;
307}
308
309template <class Archive> void MoneynessStrike::serialize(Archive& ar, const unsigned int version) {
310 ar& boost::serialization::base_object<BaseStrike>(*this);
311 ar& type_;
312 ar& moneyness_;
313}
314
315template void BaseStrike::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
316template void BaseStrike::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
317template void DeltaStrike::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
318template void DeltaStrike::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
319template void AtmStrike::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
320template void AtmStrike::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
321template void MoneynessStrike::serialize(boost::archive::binary_oarchive& ar, const unsigned int version);
322template void MoneynessStrike::serialize(boost::archive::binary_iarchive& ar, const unsigned int version);
323
324} // namespace data
325} // namespace ore
326
bool equal_to(const BaseStrike &other) const override
Override in derived classes to compare specific Strikes.
Definition: strike.cpp:50
void serialize(Archive &ar, const unsigned int version)
Definition: strike.cpp:291
void fromString(const std::string &strStrike) override
Definition: strike.cpp:46
QuantLib::Real strike() const
Return the absolute strike level.
Definition: strike.cpp:44
std::string toString() const override
Definition: strike.cpp:48
QuantLib::Real strike_
Definition: strike.hpp:93
AbsoluteStrike()
Default constructor.
Definition: strike.cpp:40
QuantLib::DeltaVolQuote::AtmType atmType_
Definition: strike.hpp:193
void check() const
Perform validation.
Definition: strike.cpp:154
bool equal_to(const BaseStrike &other) const override
Override in derived classes to compare specific Strikes.
Definition: strike.cpp:145
void fromString(const std::string &strStrike) override
Definition: strike.cpp:109
boost::optional< QuantLib::DeltaVolQuote::DeltaType > deltaType() const
Return the delta type.
Definition: strike.cpp:107
AtmStrike()
Default constructor.
Definition: strike.cpp:98
QuantLib::DeltaVolQuote::AtmType atmType() const
Return the ATM type.
Definition: strike.cpp:105
std::string toString() const override
Definition: strike.cpp:130
void serialize(Archive &ar, const unsigned int version)
Definition: strike.cpp:303
boost::optional< QuantLib::DeltaVolQuote::DeltaType > deltaType_
Definition: strike.hpp:194
virtual bool equal_to(const BaseStrike &other) const =0
Override in derived classes to compare specific Strikes.
virtual std::string toString() const =0
Write the Strike object to string.
void serialize(Archive &ar, const unsigned int version)
Definition: strike.cpp:289
QuantLib::Real delta() const
Return the delta level.
Definition: strike.cpp:67
bool equal_to(const BaseStrike &other) const override
Override in derived classes to compare specific Strikes.
Definition: strike.cpp:90
DeltaStrike()
Default constructor.
Definition: strike.cpp:58
void fromString(const std::string &strStrike) override
Definition: strike.cpp:69
QuantLib::DeltaVolQuote::DeltaType deltaType_
Definition: strike.hpp:139
QuantLib::Option::Type optionType_
Definition: strike.hpp:140
QuantLib::DeltaVolQuote::DeltaType deltaType() const
Return the delta type.
Definition: strike.cpp:63
std::string toString() const override
Definition: strike.cpp:82
QuantLib::Option::Type optionType() const
Return the option type.
Definition: strike.cpp:65
QuantLib::Real delta_
Definition: strike.hpp:141
void serialize(Archive &ar, const unsigned int version)
Definition: strike.cpp:296
bool equal_to(const BaseStrike &other) const override
Override in derived classes to compare specific Strikes.
Definition: strike.cpp:195
void fromString(const std::string &strStrike) override
Definition: strike.cpp:175
QuantLib::Real moneyness_
Definition: strike.hpp:248
QuantLib::Real moneyness() const
Return the moneyness level.
Definition: strike.cpp:173
std::string toString() const override
Definition: strike.cpp:187
MoneynessStrike()
Default constructor.
Definition: strike.cpp:167
Type type() const
Return the moneyness type.
Definition: strike.cpp:171
void serialize(Archive &ar, const unsigned int version)
Definition: strike.cpp:309
DeltaVolQuote::AtmType parseAtmType(const std::string &s)
Convert text to QuantLib::DeltaVolQuote::AtmType.
Definition: parsers.cpp:746
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Option::Type parseOptionType(const std::string &s)
Convert text to QuantLib::Option::Type.
Definition: parsers.cpp:481
DeltaVolQuote::DeltaType parseDeltaType(const std::string &s)
Convert text to QuantLib::DeltaVolQuote::DeltaType.
Definition: parsers.cpp:763
@ data
Definition: log.hpp:77
Classes for representing a strike using various conventions.
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
MoneynessStrike::Type parseMoneynessType(const string &type)
Parse MoneynessStrike::Type from type.
Definition: strike.cpp:252
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
QuantLib::ext::shared_ptr< BaseStrike > parseBaseStrike(const string &strStrike)
Parse a Strike from its string representation, strStrike.
Definition: strike.cpp:262
Serializable Credit Default Swap.
Definition: namespaces.docs:23
BOOST_CLASS_EXPORT_IMPLEMENT(ore::data::AbsoluteStrike)
Map text representations to QuantLib/QuantExt types.
string conversion utilities