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) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
21
22#include <ql/errors.hpp>
23#include <boost/thread/mutex.hpp>
24#include <boost/thread/lock_guard.hpp>
25#include <boost/regex.hpp>
26
27namespace ore {
28namespace data {
29
30Strike parseStrike(const std::string& s) {
31 static boost::mutex mutex_;
32 boost::lock_guard<boost::mutex> lock(mutex_);
33
34 boost::regex m1("^(ATM|atm)");
35 boost::regex m1b("^(ATMF|atmf)");
36 boost::regex m2("^(ATM|atm)(\\+|\\-)([0-9]+[.]?[0-9]*)");
37 boost::regex m3("^(\\+|\\-)?([0-9]+[.]?[0-9]*)");
38 boost::regex m4("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(d|D)");
39 boost::regex m4b("(d|D)");
40 boost::regex m5("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(c|C)");
41 boost::regex m5b("^(c|C)");
42 boost::regex m6("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(p|P)");
43 boost::regex m6b("^(p|P)");
44 boost::regex m7("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(bf|BF)");
45 boost::regex m7b("^(bf|BF)");
46 boost::regex m8("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(rr|RR)");
47 boost::regex m8b("^(rr|RR)");
48 boost::regex m9("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(ATMF|atmf)");
49 boost::regex m9b("(ATMF|atmf)");
50 boost::regex m10("^(\\+|\\-)?([0-9]+[.]?[0-9]*)(ATM|atm)");
51 boost::regex m10b("(ATM|atm)");
52 Strike result;
53 if (boost::regex_match(s, m1)) {
54 result.type = Strike::Type::ATM;
55 result.value = 0.0;
56 return result;
57 }
58 if (boost::regex_match(s, m1b)) {
59 result.type = Strike::Type::ATMF;
60 result.value = 0.0;
61 return result;
62 }
63 if (boost::regex_match(s, m2)) {
65 result.value = parseReal(regex_replace(s, m1, std::string("")));
66 return result;
67 }
68 if (boost::regex_match(s, m3)) {
70 result.value = parseReal(s);
71 return result;
72 }
73 if (boost::regex_match(s, m4)) {
75 result.value = parseReal(regex_replace(s, m4b, std::string("")));
76 return result;
77 }
78 if (boost::regex_match(s, m5)) {
80 result.value = parseReal(regex_replace(s, m5b, std::string("")));
81 return result;
82 }
83 if (boost::regex_match(s, m6)) {
85 result.value = parseReal(regex_replace(s, m6b, std::string("")));
86 return result;
87 }
88 if (boost::regex_match(s, m7)) {
89 result.type = Strike::Type::BF;
90 result.value = parseReal(regex_replace(s, m7b, std::string("")));
91 return result;
92 }
93 if (boost::regex_match(s, m8)) {
94 result.type = Strike::Type::RR;
95 result.value = parseReal(regex_replace(s, m8b, std::string("")));
96 return result;
97 }
98 if (boost::regex_match(s, m9)) {
100 result.value = parseReal(regex_replace(s, m9b, std::string("")));
101 return result;
102 }
103 if (boost::regex_match(s, m10)) {
105 result.value = parseReal(regex_replace(s, m10b, std::string("")));
106 return result;
107 }
108 QL_FAIL("could not parse strike given by " << s);
109}
110
111std::ostream& operator<<(std::ostream& out, const Strike& s) {
112 switch (s.type) {
114 out << "ATM";
115 break;
117 out << "ATMF";
118 break;
120 out << "ATM_Offset";
121 break;
123 out << "Absolute";
124 break;
126 out << "Delta";
127 break;
129 out << "ATMF_Moneyness";
130 break;
132 out << "ATM_Moneyness";
133 break;
134 default:
135 out << "UNKNOWN";
136 break;
137 }
140 if (s.value >= 0.0)
141 out << "+";
142 else
143 out << "-";
144 out << s.value;
145 }
146 return out;
147}
148
149namespace {
150Strike normaliseStrike(const Strike& s) {
151 if (s.type == Strike::Type::ATM_Offset && QuantLib::close_enough(s.value, 0.0))
152 return Strike({Strike::Type::ATM, 0.0});
153 if (s.type == Strike::Type::ATMF_Moneyness && QuantLib::close_enough(s.value, 1.0))
154 return Strike({Strike::Type::ATMF, 0.0});
155 if (s.type == Strike::Type::ATM_Moneyness && QuantLib::close_enough(s.value, 1.0))
156 return Strike({Strike::Type::ATM, 0.0});
157 return s;
158}
159} // anonymous namespace
160
161bool operator==(const Strike& s1, const Strike& s2) {
162 Strike tmp1 = normaliseStrike(s1);
163 Strike tmp2 = normaliseStrike(s2);
164 return (tmp1.type == tmp2.type && QuantLib::close_enough(tmp1.value, tmp2.value));
165}
166
167QuantLib::Real computeAbsoluteStrike(const Strike& s, const QuantLib::Real atm, const QuantLib::Real atmf) {
168 switch (s.type) {
170 return atm;
172 return atmf;
174 return atm + s.value;
176 return s.value;
178 QL_FAIL("can not compute absolute strike for type delta");
180 return atmf * s.value;
182 return atm * s.value;
183 default:
184 QL_FAIL("can not compute strike for unknown type " << s);
185 }
186}
187
188DeltaString::DeltaString(const std::string& s) {
189 QL_REQUIRE(!s.empty() && (s.back() == 'P' || s.back() == 'C' || s == "ATM"),
190 "invalid delta quote, expected ATM, 10P, 25C, ...");
191 isAtm_ = s == "ATM";
192 isPut_ = !s.empty() && s.back() == 'P';
193 isCall_ = !s.empty() && s.back() == 'C';
194 if (isPut_ || isCall_) {
195 try {
196 delta_ = ore::data::parseReal(s.substr(0, s.size() - 1)) / 100.0 * (isPut_ ? -1.0 : 1.0);
197 } catch (const std::exception& e) {
198 QL_FAIL("DeltaString: can not convert call / put delta '" << s << "' to numeric value: " << e.what());
199 }
200 }
201}
202
203} // namespace data
204} // namespace ore
DeltaString(const std::string &s)
Definition: strike.cpp:188
Strike parseStrike(const std::string &s)
Convert text to Strike.
Definition: strike.cpp:30
QuantLib::Real computeAbsoluteStrike(const Strike &s, const QuantLib::Real atm, const QuantLib::Real atmf)
Convenience function that computes an absolute strike.
Definition: strike.cpp:167
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
@ data
Definition: log.hpp:77
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
QuantLib::Real value
Definition: strike.hpp:47
strike description