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 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
20#include <boost/test/unit_test.hpp>
22#include <oret/toplevelfixture.hpp>
23
24using namespace QuantLib;
25using namespace ore::data;
26using namespace std;
27
28BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
29
30BOOST_AUTO_TEST_SUITE(StrikeTests)
31
32BOOST_AUTO_TEST_CASE(testAbsoluteStrike) {
33
34 BOOST_TEST_MESSAGE("Testing absolute strike...");
35
36 Real inputStrike = 2.0;
37 Real tolerance = 1e-12;
38
39 // Construct an AbsoluteStrike directly
40 AbsoluteStrike strike(inputStrike);
41 BOOST_CHECK_CLOSE(strike.strike(), inputStrike, tolerance);
42
43 // Write AbsoluteStrike to string
44 string strStrike = strike.toString();
45
46 // Parse AbsoluteStrike from string
47 QuantLib::ext::shared_ptr<BaseStrike> parsedStrike;
48 BOOST_REQUIRE_NO_THROW(parsedStrike = parseBaseStrike(strStrike));
49
50 // Check that we get back an AbsoluteStrike
51 QuantLib::ext::shared_ptr<AbsoluteStrike> castStrike = QuantLib::ext::dynamic_pointer_cast<AbsoluteStrike>(parsedStrike);
52 BOOST_CHECK(castStrike);
53
54 // Check its members
55 BOOST_CHECK_CLOSE(castStrike->strike(), inputStrike, tolerance);
56}
57
58BOOST_AUTO_TEST_CASE(testDeltaStrike) {
59
60 BOOST_TEST_MESSAGE("Testing delta strike...");
61
62 DeltaVolQuote::DeltaType inputDeltaType = DeltaVolQuote::Spot;
63 Option::Type inputOptionType = Option::Call;
64 Real inputDelta = 0.25;
65 Real tolerance = 1e-12;
66
67 // Construct a DeltaStrike directly
68 DeltaStrike strike(inputDeltaType, inputOptionType, inputDelta);
69 BOOST_CHECK_EQUAL(strike.deltaType(), inputDeltaType);
70 BOOST_CHECK_EQUAL(strike.optionType(), inputOptionType);
71 BOOST_CHECK_CLOSE(strike.delta(), inputDelta, tolerance);
72
73 // Write DeltaStrike to string
74 string strStrike = strike.toString();
75
76 // Parse DeltaStrike from string
77 QuantLib::ext::shared_ptr<BaseStrike> parsedStrike;
78 BOOST_REQUIRE_NO_THROW(parsedStrike = parseBaseStrike(strStrike));
79
80 // Check that we get back a DeltaStrike
81 QuantLib::ext::shared_ptr<DeltaStrike> castStrike = QuantLib::ext::dynamic_pointer_cast<DeltaStrike>(parsedStrike);
82 BOOST_CHECK(castStrike);
83
84 // Check its members
85 BOOST_CHECK_EQUAL(castStrike->deltaType(), inputDeltaType);
86 BOOST_CHECK_EQUAL(castStrike->optionType(), inputOptionType);
87 BOOST_CHECK_CLOSE(castStrike->delta(), inputDelta, tolerance);
88}
89
90BOOST_AUTO_TEST_CASE(testAtmStrikeNoDelta) {
91
92 BOOST_TEST_MESSAGE("Testing ATM strike without delta...");
93
94 DeltaVolQuote::AtmType inputAtmType = DeltaVolQuote::AtmFwd;
95
96 // Construct an AtmStrike directly
97 AtmStrike strike(inputAtmType);
98 BOOST_CHECK_EQUAL(strike.atmType(), inputAtmType);
99 BOOST_CHECK(!strike.deltaType());
100
101 // Write AtmStrike to string
102 string strStrike = strike.toString();
103
104 // Parse AtmStrike from string
105 QuantLib::ext::shared_ptr<BaseStrike> parsedStrike;
106 BOOST_REQUIRE_NO_THROW(parsedStrike = parseBaseStrike(strStrike));
107
108 // Check that we get back an AtmStrike
109 QuantLib::ext::shared_ptr<AtmStrike> castStrike = QuantLib::ext::dynamic_pointer_cast<AtmStrike>(parsedStrike);
110 BOOST_CHECK(castStrike);
111
112 // Check its members
113 BOOST_CHECK_EQUAL(castStrike->atmType(), inputAtmType);
114 BOOST_CHECK(!castStrike->deltaType());
115}
116
117BOOST_AUTO_TEST_CASE(testAtmStrikeNoDeltaEquality) {
118
119 BOOST_TEST_MESSAGE("Testing equality operator for two ATM strikes without delta...");
120 // Checks for failure in operator== if delta type is not given
121
122 DeltaVolQuote::AtmType atmType = DeltaVolQuote::AtmFwd;
123 boost::optional<DeltaVolQuote::DeltaType> atmDeltaType;
124
125 vector<QuantLib::ext::shared_ptr<BaseStrike>> strikes;
126 strikes.push_back(QuantLib::ext::make_shared<AtmStrike>(atmType, atmDeltaType));
127 strikes.push_back(QuantLib::ext::make_shared<AtmStrike>(DeltaVolQuote::AtmFwd));
128 BOOST_CHECK(*strikes[0] == *strikes[1]);
129}
130
131BOOST_AUTO_TEST_CASE(testAtmStrikeWithDelta) {
132
133 BOOST_TEST_MESSAGE("Testing ATM strike with delta...");
134
135 DeltaVolQuote::AtmType inputAtmType = DeltaVolQuote::AtmDeltaNeutral;
136 DeltaVolQuote::DeltaType inputDeltaType = DeltaVolQuote::Fwd;
137
138 // Construct an AtmStrike directly
139 AtmStrike strike(inputAtmType, inputDeltaType);
140 BOOST_CHECK_EQUAL(strike.atmType(), inputAtmType);
141 BOOST_CHECK(strike.deltaType());
142 BOOST_CHECK_EQUAL(*strike.deltaType(), inputDeltaType);
143
144 // Write AtmStrike to string
145 string strStrike = strike.toString();
146
147 // Parse AtmStrike from string
148 QuantLib::ext::shared_ptr<BaseStrike> parsedStrike;
149 BOOST_REQUIRE_NO_THROW(parsedStrike = parseBaseStrike(strStrike));
150
151 // Check that we get back an AtmStrike
152 QuantLib::ext::shared_ptr<AtmStrike> castStrike = QuantLib::ext::dynamic_pointer_cast<AtmStrike>(parsedStrike);
153 BOOST_CHECK(castStrike);
154
155 // Check its members
156 BOOST_CHECK_EQUAL(castStrike->atmType(), inputAtmType);
157 BOOST_CHECK(castStrike->deltaType());
158 BOOST_CHECK_EQUAL(*castStrike->deltaType(), inputDeltaType);
159}
160
161BOOST_AUTO_TEST_CASE(testMoneynessStrike) {
162
163 BOOST_TEST_MESSAGE("Testing moneyness strike ...");
164
165 MoneynessStrike::Type inputMoneynessType = MoneynessStrike::Type::Forward;
166 Real inputMoneyness = 1.10;
167 Real tolerance = 1e-12;
168
169 // Construct an MoneynessStrike directly
170 MoneynessStrike strike(inputMoneynessType, inputMoneyness);
171 BOOST_CHECK_EQUAL(strike.type(), inputMoneynessType);
172 BOOST_CHECK_CLOSE(strike.moneyness(), inputMoneyness, tolerance);
173
174 // Write MoneynessStrike to string
175 string strStrike = strike.toString();
176
177 // Parse MoneynessStrike from string
178 QuantLib::ext::shared_ptr<BaseStrike> parsedStrike;
179 BOOST_REQUIRE_NO_THROW(parsedStrike = parseBaseStrike(strStrike));
180
181 // Check that we get back an MoneynessStrike
182 QuantLib::ext::shared_ptr<MoneynessStrike> castStrike = QuantLib::ext::dynamic_pointer_cast<MoneynessStrike>(parsedStrike);
183 BOOST_CHECK(castStrike);
184
185 // Check its members
186 BOOST_CHECK_EQUAL(castStrike->type(), inputMoneynessType);
187 BOOST_CHECK_CLOSE(castStrike->moneyness(), inputMoneyness, tolerance);
188}
189
190BOOST_AUTO_TEST_CASE(testAtmStrikeExceptions) {
191
192 DeltaVolQuote::AtmType atmType = DeltaVolQuote::AtmNull;
193 BOOST_CHECK_THROW(AtmStrike tmp(atmType), Error);
194
195 atmType = DeltaVolQuote::AtmDeltaNeutral;
196 BOOST_CHECK_THROW(AtmStrike tmp(atmType), Error);
197
198 atmType = DeltaVolQuote::AtmSpot;
199 DeltaVolQuote::DeltaType deltaType = DeltaVolQuote::Spot;
200 BOOST_CHECK_THROW(AtmStrike tmp(atmType, deltaType), Error);
201
202 atmType = DeltaVolQuote::AtmPutCall50;
203 BOOST_CHECK_THROW(AtmStrike tmp(atmType, deltaType), Error);
204}
205
206BOOST_AUTO_TEST_SUITE_END()
207
208BOOST_AUTO_TEST_SUITE_END()
QuantLib::Real strike() const
Return the absolute strike level.
Definition: strike.cpp:44
std::string toString() const override
Definition: strike.cpp:48
boost::optional< QuantLib::DeltaVolQuote::DeltaType > deltaType() const
Return the delta type.
Definition: strike.cpp:107
QuantLib::DeltaVolQuote::AtmType atmType() const
Return the ATM type.
Definition: strike.cpp:105
std::string toString() const override
Definition: strike.cpp:130
QuantLib::Real delta() const
Return the delta level.
Definition: strike.cpp:67
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 moneyness() const
Return the moneyness level.
Definition: strike.cpp:173
std::string toString() const override
Definition: strike.cpp:187
Type type() const
Return the moneyness type.
Definition: strike.cpp:171
Classes for representing a strike using various conventions.
QuantLib::ext::shared_ptr< BaseStrike > parseBaseStrike(const string &strStrike)
Parse a Strike from its string representation, strStrike.
Definition: strike.cpp:262
vector< Real > strikes
BOOST_AUTO_TEST_CASE(testAbsoluteStrike)
Definition: strike.cpp:32