Repository navigation
Expand file tree
/
Copy path_replication_8cpp-example.html
More file actions
448 lines (446 loc) · 39.9 KB
/
Copy path_replication_8cpp-example.html
File metadata and controls
448 lines (446 loc) · 39.9 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!-- HTML header for doxygen 1.17.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.18.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>QuantLib: Replication.cpp</title>
<link rel="stylesheet" href="/styles/fonts.css" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="codefolding.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="quantlibextra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow" style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="https://www.quantlib.org">QuantLib</a>: a free/open-source library for quantitative finance
<div id="projectnumber">Reference manual - version 1.44</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.18.0 -->
<script type="text/javascript">
let searchBox = new SearchBox("searchBox", "search/",'.html');
</script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', codefold.init);
</script>
<script type="application/json" id="doxygen-config">
{
"relPath": "",
"generateTreeView": false,
"searchEngine": true,
"serverBasedSearch": false,
"disableIndex": false,
"dynamicMenus": true,
"fullSidebar": false
}
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<div id="main-nav-mobile">
<div class="sm sm-dox"><input id="main-menu-state" type="checkbox"/>
<label class="main-menu-btn" for="main-menu-state">
<span class="main-menu-btn-icon"></span> Toggle main menu visibility</label>
<span id="searchBoxPos1"></span>
</div>
</div><!-- main-nav-mobile -->
<div id="main-nav">
<ul class="sm sm-dox" id="main-menu">
<li id="searchBoxPos2">
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" class="search-icon"><span class="search-icon-dropdown"></span></span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><div id="MSearchCloseImg" class="close-icon"></div></a>
</span>
</div>
</li>
</ul>
</div><!-- main-nav -->
</div><!-- top -->
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">Replication.cpp</div></div>
</div><!--header-->
<div class="contents">
<p>This example uses the CompositeInstrument class to statically replicate a down-and-out barrier options.</p>
<div class="fragment"><div class="line"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span></div>
<div class="line"></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* This example showcases the CompositeInstrument class. Such class</span></div>
<div class="line"><span class="comment"> is used to build a static replication of a down-and-out barrier</span></div>
<div class="line"><span class="comment"> option, as outlined in Section 10.2 of Mark Joshi's "The Concepts</span></div>
<div class="line"><span class="comment"> and Practice of Mathematical Finance" to which we refer the</span></div>
<div class="line"><span class="comment"> reader.</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <ql/qldefines.hpp></span></div>
<div class="line"><span class="preprocessor">#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)</span></div>
<div class="line"><span class="preprocessor"># include <ql/auto_link.hpp></span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"><span class="preprocessor">#include <ql/instruments/compositeinstrument.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/instruments/barrieroption.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/instruments/europeanoption.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/pricingengines/barrier/analyticbarrierengine.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/exercise.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/termstructures/yield/flatforward.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/quotes/simplequote.hpp></span></div>
<div class="line"><span class="preprocessor">#include <ql/time/calendars/nullcalendar.hpp></span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <iostream></span></div>
<div class="line"><span class="preprocessor">#include <iomanip></span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespace_quant_lib.html">QuantLib</a>;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> </div>
<div class="line"> std::cout << std::endl;</div>
<div class="line"> </div>
<div class="line"> <a id="_a0" name="_a0"></a><a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> today(29, May, 2006);</div>
<div class="line"> <a id="_a1" name="_a1"></a><a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// the option to replicate</span></div>
<div class="line"> Barrier::Type barrierType = Barrier::DownOut;</div>
<div class="line"> Real barrier = 70.0;</div>
<div class="line"> Real rebate = 0.0;</div>
<div class="line"> Option::Type type = Option::Put;</div>
<div class="line"> Real underlyingValue = 100.0;</div>
<div class="line"> <span class="keyword">auto</span> underlying = ext::make_shared<SimpleQuote>(underlyingValue);</div>
<div class="line"> Real strike = 100.0;</div>
<div class="line"> <span class="keyword">auto</span> riskFreeRate = ext::make_shared<SimpleQuote>(0.04);</div>
<div class="line"> <span class="keyword">auto</span> volatility = ext::make_shared<SimpleQuote>(0.20);</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> maturity = today + 1*Years;</div>
<div class="line"> </div>
<div class="line"> std::cout << std::endl ;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// write column headings</span></div>
<div class="line"> Size widths[] = { 45, 15, 15 };</div>
<div class="line"> Size totalWidth = widths[0]+widths[1]+widths[2];</div>
<div class="line"> std::string rule(totalWidth, <span class="charliteral">'-'</span>), dblrule(totalWidth, <span class="charliteral">'='</span>);</div>
<div class="line"> </div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << <span class="stringliteral">"Initial market conditions"</span> << std::endl;</div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span></div>
<div class="line"> << std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span></div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> std::cout << rule << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// bootstrap the yield/vol curves</span></div>
<div class="line"> <a id="_a2" name="_a2"></a><a class="code hl_class" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dayCounter = <a id="_a3" name="_a3"></a><a class="code hl_class" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>();</div>
<div class="line"> <a id="_a4" name="_a4"></a><a class="code hl_class" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> h1(riskFreeRate);</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> h2(volatility);</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<YieldTermStructure></a> flatRate(</div>
<div class="line"> ext::make_shared<FlatForward>(0, <a id="_a5" name="_a5"></a><a class="code hl_class" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(), h1, dayCounter));</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<BlackVolTermStructure></a> flatVol(</div>
<div class="line"> ext::make_shared<BlackConstantVol>(0, <a class="code hl_class" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(), h2, dayCounter));</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// instantiate the option</span></div>
<div class="line"> <span class="keyword">auto</span> exercise = ext::make_shared<EuropeanExercise>(maturity);</div>
<div class="line"> <span class="keyword">auto</span> payoff = ext::make_shared<PlainVanillaPayoff>(type, strike);</div>
<div class="line"> </div>
<div class="line"> <span class="keyword">auto</span> bsProcess = ext::make_shared<BlackScholesProcess>(</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a>(underlying), flatRate, flatVol);</div>
<div class="line"> </div>
<div class="line"> <span class="keyword">auto</span> barrierEngine = ext::make_shared<AnalyticBarrierEngine>(bsProcess);</div>
<div class="line"> <span class="keyword">auto</span> europeanEngine = ext::make_shared<AnalyticEuropeanEngine>(bsProcess);</div>
<div class="line"> </div>
<div class="line"> BarrierOption referenceOption(barrierType, barrier, rebate,</div>
<div class="line"> payoff, exercise);</div>
<div class="line"> referenceOption.<a id="a6" name="a6"></a><a class="code hl_function" href="class_quant_lib_1_1_instrument.html#ae58e4c75062c7f3f4dedbd744f9a379f" title="set the pricing engine to be used.">setPricingEngine</a>(barrierEngine);</div>
<div class="line"> </div>
<div class="line"> Real referenceValue = referenceOption.<a id="a7" name="a7"></a><a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> </div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Original barrier option"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << referenceValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Replicating portfolios</span></div>
<div class="line"> <a id="_a8" name="_a8"></a><a class="code hl_class" href="class_quant_lib_1_1_composite_instrument.html" title="Composite instrument">CompositeInstrument</a> portfolio1, portfolio2, portfolio3;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Final payoff first (the same for all portfolios):</span></div>
<div class="line"> <span class="comment">// as shown in Joshi, a put struck at K...</span></div>
<div class="line"> <span class="keyword">auto</span> put1 = ext::make_shared<EuropeanOption>(payoff, exercise);</div>
<div class="line"> put1->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a id="a9" name="a9"></a><a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a17f4129206e9cd104423285c9bb7457e" title="adds an instrument to the composite">add</a>(put1);</div>
<div class="line"> portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a17f4129206e9cd104423285c9bb7457e" title="adds an instrument to the composite">add</a>(put1);</div>
<div class="line"> portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a17f4129206e9cd104423285c9bb7457e" title="adds an instrument to the composite">add</a>(put1);</div>
<div class="line"> <span class="comment">// ...minus a digital put struck at B of notional K-B...</span></div>
<div class="line"> <span class="keyword">auto</span> digitalPayoff = ext::make_shared<CashOrNothingPayoff>(Option::Put, barrier, 1.0);</div>
<div class="line"> <span class="keyword">auto</span> digitalPut = ext::make_shared<EuropeanOption>(digitalPayoff, exercise);</div>
<div class="line"> digitalPut->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a id="a10" name="a10"></a><a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);</div>
<div class="line"> portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);</div>
<div class="line"> portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);</div>
<div class="line"> <span class="comment">// ...minus a put option struck at B.</span></div>
<div class="line"> <span class="keyword">auto</span> lowerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);</div>
<div class="line"> <span class="keyword">auto</span> put2 = ext::make_shared<EuropeanOption>(lowerPayoff, exercise);</div>
<div class="line"> put2->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(put2);</div>
<div class="line"> portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(put2);</div>
<div class="line"> portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(put2);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Now we use puts struck at B to kill the value of the</span></div>
<div class="line"> <span class="comment">// portfolio on a number of points (B,t). For the first</span></div>
<div class="line"> <span class="comment">// portfolio, we'll use 12 dates at one-month's distance.</span></div>
<div class="line"> Integer i;</div>
<div class="line"> <span class="keywordflow">for</span> (i=12; i>=1; i--) {</div>
<div class="line"> <span class="comment">// First, we instantiate the option...</span></div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Months;</div>
<div class="line"> <span class="keyword">auto</span> innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);</div>
<div class="line"> <span class="keyword">auto</span> innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);</div>
<div class="line"> <span class="keyword">auto</span> putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);</div>
<div class="line"> putn->setPricingEngine(europeanEngine);</div>
<div class="line"> <span class="comment">// ...second, we evaluate the current portfolio and the</span></div>
<div class="line"> <span class="comment">// latest put at (B,t)...</span></div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-1)*Months;</div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> Real portfolioValue = portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> Real putValue = putn->NPV();</div>
<div class="line"> <span class="comment">// ...finally, we estimate the notional that kills the</span></div>
<div class="line"> <span class="comment">// portfolio value at that point...</span></div>
<div class="line"> Real notional = portfolioValue/putValue;</div>
<div class="line"> <span class="comment">// ...and we subtract from the portfolio a put with such</span></div>
<div class="line"> <span class="comment">// notional.</span></div>
<div class="line"> portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(putn, notional);</div>
<div class="line"> }</div>
<div class="line"> <span class="comment">// The portfolio being complete, we return to today's market...</span></div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> underlying->setValue(underlyingValue);</div>
<div class="line"> <span class="comment">// ...and output the value.</span></div>
<div class="line"> Real portfolioValue = portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> Real error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (12 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// For the second portfolio, we'll use 26 dates at two-weeks'</span></div>
<div class="line"> <span class="comment">// distance.</span></div>
<div class="line"> <span class="keywordflow">for</span> (i=52; i>=2; i-=2) {</div>
<div class="line"> <span class="comment">// Same as above.</span></div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;</div>
<div class="line"> <span class="keyword">auto</span> innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);</div>
<div class="line"> <span class="keyword">auto</span> innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);</div>
<div class="line"> <span class="keyword">auto</span> putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);</div>
<div class="line"> putn->setPricingEngine(europeanEngine);</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-2)*Weeks;</div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> Real portfolioValue = portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> Real putValue = putn->NPV();</div>
<div class="line"> Real notional = portfolioValue/putValue;</div>
<div class="line"> portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(putn, notional);</div>
<div class="line"> }</div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> underlying->setValue(underlyingValue);</div>
<div class="line"> portfolioValue = portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (26 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// For the third portfolio, we'll use 52 dates at one-week's</span></div>
<div class="line"> <span class="comment">// distance.</span></div>
<div class="line"> <span class="keywordflow">for</span> (i=52; i>=1; i--) {</div>
<div class="line"> <span class="comment">// Same as above.</span></div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;</div>
<div class="line"> <span class="keyword">auto</span> innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);</div>
<div class="line"> <span class="keyword">auto</span> innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);</div>
<div class="line"> <span class="keyword">auto</span> putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);</div>
<div class="line"> putn->setPricingEngine(europeanEngine);</div>
<div class="line"> <a class="code hl_class" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-1)*Weeks;</div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> Real portfolioValue = portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> Real putValue = putn->NPV();</div>
<div class="line"> Real notional = portfolioValue/putValue;</div>
<div class="line"> portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_composite_instrument.html#a5d1575f5d5bd36deb2aafbeb785ceeb7" title="shorts an instrument from the composite">subtract</a>(putn, notional);</div>
<div class="line"> }</div>
<div class="line"> <a class="code hl_function" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> underlying->setValue(underlyingValue);</div>
<div class="line"> portfolioValue = portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (52 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Now we modify the market condition to see whether the</span></div>
<div class="line"> <span class="comment">// replication holds. First, we change the underlying value so</span></div>
<div class="line"> <span class="comment">// that the option is out of the money.</span></div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << <span class="stringliteral">"Modified market conditions: out of the money"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span></div>
<div class="line"> << std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span></div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> std::cout << rule << std::endl;</div>
<div class="line"> </div>
<div class="line"> underlying->setValue(110.0);</div>
<div class="line"> </div>
<div class="line"> referenceValue = referenceOption.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Original barrier option"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << referenceValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (12 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (26 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (52 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Next, we change the underlying value so that the option is</span></div>
<div class="line"> <span class="comment">// in the money.</span></div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << <span class="stringliteral">"Modified market conditions: in the money"</span> << std::endl;</div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span></div>
<div class="line"> << std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span></div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> std::cout << rule << std::endl;</div>
<div class="line"> </div>
<div class="line"> underlying->setValue(90.0);</div>
<div class="line"> </div>
<div class="line"> referenceValue = referenceOption.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Original barrier option"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << referenceValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio1.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (12 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio2.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (26 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> portfolioValue = portfolio3.<a class="code hl_function" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> error = portfolioValue - referenceValue;</div>
<div class="line"> std::cout << std::setw(widths[0]) << std::left</div>
<div class="line"> << <span class="stringliteral">"Replicating portfolio (52 dates)"</span></div>
<div class="line"> << std::fixed</div>
<div class="line"> << std::setw(widths[1]) << std::left << portfolioValue</div>
<div class="line"> << std::setw(widths[2]) << std::left << error</div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Finally, a word of warning for those (shame on them) who</span></div>
<div class="line"> <span class="comment">// run the example but do not read the code.</span></div>
<div class="line"> std::cout << dblrule << std::endl;</div>
<div class="line"> std::cout</div>
<div class="line"> << std::endl</div>
<div class="line"> << <span class="stringliteral">"The replication seems to be less robust when volatility and \n"</span></div>
<div class="line"> << <span class="stringliteral">"risk-free rate are changed. Feel free to experiment with \n"</span></div>
<div class="line"> << <span class="stringliteral">"the example and contribute a patch if you spot any errors."</span></div>
<div class="line"> << std::endl;</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line"> } <span class="keywordflow">catch</span> (std::exception& e) {</div>
<div class="line"> std::cerr << e.what() << std::endl;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> } <span class="keywordflow">catch</span> (...) {</div>
<div class="line"> std::cerr << <span class="stringliteral">"unknown error"</span> << std::endl;</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.17.0-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="https://www.doxygen.org/index.html">doxygen</a> 1.18.0
</small></address>
</div><!-- doc-content -->
</body>
</html>