forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompoundoption.cpp
346 lines (274 loc) · 17.8 KB
/
compoundoption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 Dimitri Reiswich
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/instruments/compoundoption.hpp>
#include <ql/pricingengines/exotic/analyticcompoundoptionengine.hpp>
#include <ql/instruments/europeanoption.hpp>
#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/utilities/dataformatters.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(CompoundOptionTests)
#undef REPORT_FAILURE
#define REPORT_FAILURE(greekName, payoffM, payoffD, exerciseM, \
exerciseD, s, q, r, today, \
v, expected, calculated, error, tolerance) \
BOOST_FAIL(\
"\nmother option type: " << payoffM->optionType() << \
"\ndaughter option type: " << payoffD->optionType() << \
"\nspot value: " << s << \
"\nstrike mother: " << payoffM->strike() << \
"\nstrike daughter: " << payoffD->strike() << \
"\ndividend yield: " << io::rate(q) << \
"\nrisk-free rate: " << io::rate(r) << \
"\nreference date: " << today << \
"\nmaturity mother: " << exerciseM->lastDate() << \
"\nmaturity daughter: " << exerciseD->lastDate() << \
"\nvolatility: " << io::volatility(v) << \
"\n expected " << greekName << ": " << expected << \
"\ncalculated " << greekName << ": " << calculated << \
"\nerror: " << error << \
"\ntolerance: " << tolerance);
struct CompoundOptionData {
Option::Type typeMother;
Option::Type typeDaughter;
Real strikeMother;
Real strikeDaughter;
Real s; // spot
Rate q; // dividend
Rate r; // risk-free rate
Time tMother; // time to maturity
Time tDaughter;// time to maturity
Volatility v; // volatility
Real npv; // expected result
Real tol; // tolerance
Real delta;
Real gamma;
Real vega;
Real theta;
};
BOOST_AUTO_TEST_CASE(testPutCallParity){
BOOST_TEST_MESSAGE("Testing compound-option put-call parity...");
// Test Put Call Parity for compound options.
// Formula taken from: "Foreign Exchange Risk", Wystup, Risk 2002
// Page 81, Equation 9.5
CompoundOptionData values[] = {
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol
{ Option::Put, Option::Call, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35},
{ Option::Call, Option::Call, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35},
{ Option::Call, Option::Put, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35},
{ Option::Call, Option::Call, 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11},
{ Option::Call, Option::Put , 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11},
{ Option::Call, Option::Call, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22},
{ Option::Call, Option::Put, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22},
{ Option::Call, Option::Call, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08},
{ Option::Call, Option::Put, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08},
{ Option::Call, Option::Call, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17},
{ Option::Call, Option::Put, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17},
};
Calendar calendar = TARGET();
DayCounter dc = Actual360();
Date todaysDate = Settings::instance().evaluationDate();
ext::shared_ptr<SimpleQuote> spot(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> qRate(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> vol(new SimpleQuote(0.0));
ext::shared_ptr<YieldTermStructure> rTS(
new FlatForward(0, NullCalendar(), Handle<Quote>(rRate), dc));
ext::shared_ptr<YieldTermStructure> qTS(
new FlatForward(0, NullCalendar(), Handle<Quote>(qRate), dc));
ext::shared_ptr<BlackVolTermStructure> volTS(
new BlackConstantVol(todaysDate, NullCalendar(),
Handle<Quote>(vol), dc));
for (auto& value : values) {
ext::shared_ptr<StrikedTypePayoff> payoffMotherCall(
new PlainVanillaPayoff(Option::Call, value.strikeMother));
ext::shared_ptr<StrikedTypePayoff> payoffMotherPut(
new PlainVanillaPayoff(Option::Put, value.strikeMother));
ext::shared_ptr<StrikedTypePayoff> payoffDaughter(
new PlainVanillaPayoff(value.typeDaughter, value.strikeDaughter));
Date matDateMom = todaysDate + timeToDays(value.tMother);
Date matDateDaughter = todaysDate + timeToDays(value.tDaughter);
ext::shared_ptr<Exercise> exerciseCompound(
new EuropeanExercise(matDateMom));
ext::shared_ptr<Exercise> exerciseDaughter(
new EuropeanExercise(matDateDaughter));
spot->setValue(value.s);
qRate->setValue(value.q);
rRate->setValue(value.r);
vol->setValue(value.v);
CompoundOption compoundOptionCall(payoffMotherCall,exerciseCompound,
payoffDaughter, exerciseDaughter);
CompoundOption compoundOptionPut(payoffMotherPut,exerciseCompound,
payoffDaughter, exerciseDaughter);
VanillaOption vanillaOption(EuropeanOption(payoffDaughter,
exerciseDaughter));
ext::shared_ptr<BlackScholesMertonProcess> stochProcess(
new BlackScholesMertonProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(qTS),
Handle<YieldTermStructure>(rTS),
Handle<BlackVolTermStructure>(volTS)));
ext::shared_ptr<PricingEngine> engineCompound(
new AnalyticCompoundOptionEngine(stochProcess));
ext::shared_ptr<PricingEngine> engineEuropean(
new AnalyticEuropeanEngine(stochProcess));
compoundOptionCall.setPricingEngine(engineCompound);
compoundOptionPut.setPricingEngine(engineCompound);
vanillaOption.setPricingEngine(engineEuropean);
Real discFact=rTS->discount(matDateMom);
Real discStrike = value.strikeMother * discFact;
Real calculated =
compoundOptionCall.NPV() + discStrike - compoundOptionPut.NPV()
- vanillaOption.NPV();
Real expected=0.0;
Real error=std::abs(calculated-expected);
Real tolerance=1.0e-8;
if(error>tolerance){
REPORT_FAILURE("put call parity", payoffMotherCall, payoffDaughter, exerciseCompound,
exerciseDaughter, value.s, value.q, value.r, todaysDate, value.v,
value.delta, calculated, error, tolerance);
}
}
}
BOOST_AUTO_TEST_CASE(testValues){
BOOST_TEST_MESSAGE("Testing compound-option values and greeks...");
CompoundOptionData values[] = {
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol, value, tol, delta, gamma, vega, theta
// Tolerance is taken to be pretty high with 1.0e-3, since the price/theta is very sensitive with respect to
// the implementation of the bivariate normal - which differs in the various implementations.
// Option Value Taken from Haug 2007, Greeks from www.sitmo.com
{ Option::Put, Option::Call, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35, 21.1965, 1.0e-3, -0.1966,0.0007, -32.1241, -3.3837},
//*********************************************************
// Option Values and Greeks taken from www.sitmo.com
{ Option::Call, Option::Call, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35, 17.5945, 1.0e-3, 0.3219,0.0038, 106.5185, -65.1614},
{ Option::Call, Option::Put, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35, 18.7128, 1.0e-3, -0.2906,0.0036, 103.3856, -46.6982},
{ Option::Put, Option::Put, 50.0, 520.0 , 500.0, 0.03, 0.08, 0.25, 0.5, 0.35, 15.2601, 1.0e-3, 0.1760,0.0005, -35.2570, -10.1126},
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol, value, tol, delta, gamma, vega, theta
{ Option::Call, Option::Call, 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11, 0.0729, 1.0e-3, 0.6614,2.5762, 0.5812, -0.0297},
{ Option::Call, Option::Put , 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11, 0.0074, 1.0e-3, -0.1334,1.9681, 0.2933, -0.0155},
{ Option::Put ,Option::Call, 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11, 0.0021, 1.0e-3, -0.0426,0.7252, -0.0052, -0.0058},
{ Option::Put, Option::Put , 0.05, 1.14 , 1.20, 0.0, 0.01, 0.5, 2.0, 0.11, 0.0192, 1.0e-3, 0.1626,0.1171, -0.2931, -0.0028},
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol, value, tol, delta, gamma, vega, theta
{ Option::Call, Option::Call, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22, 0.4419, 1.0e-3, 0.1049,0.0195, 11.3368, -6.2871},
{ Option::Call, Option::Put, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22, 2.6112, 1.0e-3, -0.3618,0.0337, 28.4843, -13.4124},
{ Option::Put, Option::Call, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22, 4.1616, 1.0e-3, -0.3174,0.0024, -26.6403, -2.2720},
{ Option::Put, Option::Put, 10.0, 122.0 , 120.0, 0.06, 0.02, 0.1, 0.7, 0.22, 1.0914, 1.0e-3, 0.1748,0.0165, -9.4928, -4.8995},
//*********************************************************
//*********************************************************
// Option Values and Greeks taken from mathfinance VBA implementation
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol, value, tol, delta, gamma, vega, theta
{ Option::Call, Option::Call, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08, 0.0099, 1.0e-3, 0.0285,0.0688, 0.7764, -0.0027},
{ Option::Call, Option::Put, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08, 0.9826, 1.0e-3, -0.7224,0.2158, 2.7279, -0.3332},
{ Option::Put, Option::Call, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08, 0.3585, 1.0e-3, -0.0720,-0.0835, -1.5633, -0.0117},
{ Option::Put, Option::Put, 0.4, 8.2 , 8.0, 0.05, 0.00, 2.0, 3.0, 0.08, 0.0168, 1.0e-3, 0.0378, 0.0635, 0.3882, 0.0021},
// type Mother, typeDaughter, strike Mother, strike Daughter, spot, q, r, t Mother, t Daughter, vol, value, tol, delta, gamma, vega, theta
{ Option::Call, Option::Call, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17, 0.0680, 1.0e-3, 0.4937,2.1271, 0.4418, -0.0843},
{ Option::Call, Option::Put, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17, 0.0605, 1.0e-3, -0.4169,2.0836, 0.4330, -0.0697},
{ Option::Put, Option::Call, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17, 0.0081, 1.0e-3, -0.0417,0.0761, -0.0045, -0.0020},
{ Option::Put, Option::Put, 0.02, 1.6 , 1.6, 0.013, 0.022, 0.45, 0.5, 0.17, 0.0078, 1.0e-3, 0.0413,0.0326, -0.0133, -0.0016}
};
Calendar calendar = TARGET();
DayCounter dc = Actual360();
Date todaysDate = Settings::instance().evaluationDate();
ext::shared_ptr<SimpleQuote> spot(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> qRate(new SimpleQuote(0.0));
ext::shared_ptr<SimpleQuote> vol(new SimpleQuote(0.0));
ext::shared_ptr<YieldTermStructure> rTS(
new FlatForward(0, NullCalendar(), Handle<Quote>(rRate), dc));
ext::shared_ptr<YieldTermStructure> qTS(
new FlatForward(0, NullCalendar(), Handle<Quote>(qRate), dc));
ext::shared_ptr<BlackVolTermStructure> volTS(
new BlackConstantVol(todaysDate, NullCalendar(),
Handle<Quote>(vol), dc));
for (auto& value : values) {
ext::shared_ptr<StrikedTypePayoff> payoffMother(
new PlainVanillaPayoff(value.typeMother, value.strikeMother));
ext::shared_ptr<StrikedTypePayoff> payoffDaughter(
new PlainVanillaPayoff(value.typeDaughter, value.strikeDaughter));
Date matDateMom = todaysDate + timeToDays(value.tMother);
Date matDateDaughter = todaysDate + timeToDays(value.tDaughter);
ext::shared_ptr<Exercise> exerciseMother(
new EuropeanExercise(matDateMom));
ext::shared_ptr<Exercise> exerciseDaughter(
new EuropeanExercise(matDateDaughter));
spot->setValue(value.s);
qRate->setValue(value.q);
rRate->setValue(value.r);
vol->setValue(value.v);
CompoundOption compoundOption(payoffMother,exerciseMother,
payoffDaughter, exerciseDaughter);
ext::shared_ptr<BlackScholesMertonProcess> stochProcess(
new BlackScholesMertonProcess(
Handle<Quote>(spot),
Handle<YieldTermStructure>(qTS),
Handle<YieldTermStructure>(rTS),
Handle<BlackVolTermStructure>(volTS)));
ext::shared_ptr<PricingEngine> engineCompound(
new AnalyticCompoundOptionEngine(stochProcess));
compoundOption.setPricingEngine(engineCompound);
Real calculated = compoundOption.NPV();
Real error = std::fabs(calculated - value.npv); //-values[i].npv
Real tolerance = value.tol;
if (error>tolerance) {
REPORT_FAILURE("value", payoffMother, payoffDaughter, exerciseMother, exerciseDaughter,
value.s, value.q, value.r, todaysDate, value.v, value.npv, calculated,
error, tolerance);
}
calculated = compoundOption.delta();
error = std::fabs(calculated - value.delta);
tolerance = value.tol;
if (error>tolerance) {
REPORT_FAILURE("delta", payoffMother, payoffDaughter, exerciseMother, exerciseDaughter,
value.s, value.q, value.r, todaysDate, value.v, value.delta, calculated,
error, tolerance);
}
calculated = compoundOption.gamma();
error = std::fabs(calculated - value.gamma);
tolerance = value.tol;
if (error>tolerance) {
REPORT_FAILURE("gamma", payoffMother, payoffDaughter, exerciseMother, exerciseDaughter,
value.s, value.q, value.r, todaysDate, value.v, value.gamma, calculated,
error, tolerance);
}
calculated = compoundOption.vega();
error = std::fabs(calculated - value.vega);
tolerance = value.tol;
if (error>tolerance) {
REPORT_FAILURE("vega", payoffMother, payoffDaughter, exerciseMother, exerciseDaughter,
value.s, value.q, value.r, todaysDate, value.v, value.vega, calculated,
error, tolerance);
}
calculated = compoundOption.theta();
error = std::fabs(calculated - value.theta);
tolerance = value.tol;
if (error>tolerance) {
REPORT_FAILURE("theta", payoffMother, payoffDaughter, exerciseMother, exerciseDaughter,
value.s, value.q, value.r, todaysDate, value.v, value.theta, calculated,
error, tolerance);
}
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()