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 449
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>QuantLib: Replication.cpp</title>
<link href='https://fonts.googleapis.com/css?family=Merriweather+Sans:800' rel='stylesheet' type='text/css'>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname"><a href="http://quantlib.org">
<img alt="QuantLib" src="QL-title.jpg"></a>
<div id="projectbrief">A free/open-source library for quantitative finance</div>
<div id="projectnumber">Reference manual - version 1.20</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</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"><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">#ifdef 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" href="namespace_quant_lib.html">QuantLib</a>;</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span></div>
<div class="line"><span class="keyword">namespace </span><a class="code" href="namespace_quant_lib.html">QuantLib</a> {</div>
<div class="line"> </div>
<div class="line"> ThreadKey sessionId() { <span class="keywordflow">return</span> 0; }</div>
<div class="line"> </div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></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 name="_a0"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> today(29, May, 2006);</div>
<div class="line"> <a name="a1"></a><a class="code" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">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"> <a name="a2"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> barrier = 70.0;</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> rebate = 0.0;</div>
<div class="line"> Option::Type type = Option::Put;</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> underlyingValue = 100.0;</div>
<div class="line"> ext::shared_ptr<SimpleQuote> underlying(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a3"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(underlyingValue));</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike = 100.0;</div>
<div class="line"> ext::shared_ptr<SimpleQuote> riskFreeRate(<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(0.04));</div>
<div class="line"> ext::shared_ptr<SimpleQuote> <a name="a4"></a><a class="code" href="group__manips.html#gac402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a>(<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(0.20));</div>
<div class="line"> <a class="code" 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"> <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> widths[] = { 45, 15, 15 };</div>
<div class="line"> <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> 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 name="_a5"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dayCounter = <a name="_a6"></a><a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>();</div>
<div class="line"> <a name="_a7"></a><a class="code" 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" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> h2(<a class="code" href="group__manips.html#gac402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a>);</div>
<div class="line"> <a name="_a8"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle<YieldTermStructure></a> flatRate(</div>
<div class="line"> ext::shared_ptr<YieldTermStructure>(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(0, <a name="_a10"></a><a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(),</div>
<div class="line"> h1, dayCounter)));</div>
<div class="line"> <a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<BlackVolTermStructure></a> flatVol(</div>
<div class="line"> ext::shared_ptr<BlackVolTermStructure>(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a11"></a><a class="code" href="class_quant_lib_1_1_black_constant_vol.html" title="Constant Black volatility, no time-strike dependence.">BlackConstantVol</a>(0, <a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(),</div>
<div class="line"> h2, dayCounter)));</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// instantiate the option</span></div>
<div class="line"> ext::shared_ptr<Exercise> exercise(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a12"></a><a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(maturity));</div>
<div class="line"> ext::shared_ptr<StrikedTypePayoff> payoff(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a13"></a><a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(type, strike));</div>
<div class="line"> </div>
<div class="line"> ext::shared_ptr<BlackScholesProcess> bsProcess(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_black_scholes_process.html" title="Black-Scholes (1973) stochastic process.">BlackScholesProcess</a>(<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a>(underlying),</div>
<div class="line"> flatRate, flatVol));</div>
<div class="line"> </div>
<div class="line"> ext::shared_ptr<PricingEngine> barrierEngine(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_analytic_barrier_engine.html" title="Pricing engine for barrier options using analytical formulae.">AnalyticBarrierEngine</a>(bsProcess));</div>
<div class="line"> ext::shared_ptr<PricingEngine> europeanEngine(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_analytic_european_engine.html" title="Pricing engine for European vanilla options using analytical formulae.">AnalyticEuropeanEngine</a>(bsProcess));</div>
<div class="line"> </div>
<div class="line"> <a name="_a17"></a><a class="code" href="class_quant_lib_1_1_barrier_option.html" title="Barrier option on a single asset.">BarrierOption</a> referenceOption(barrierType, barrier, rebate,</div>
<div class="line"> payoff, exercise);</div>
<div class="line"> referenceOption.setPricingEngine(barrierEngine);</div>
<div class="line"> </div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> referenceValue = referenceOption.NPV();</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 name="_a18"></a><a class="code" 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"> ext::shared_ptr<Instrument> put1(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(payoff, exercise));</div>
<div class="line"> put1->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a name="a20"></a><a class="code" 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" 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" 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"> ext::shared_ptr<StrikedTypePayoff> digitalPayoff(</div>
<div class="line"> <span class="keyword">new</span> <a name="_a21"></a><a class="code" href="class_quant_lib_1_1_cash_or_nothing_payoff.html" title="Binary cash-or-nothing payoff.">CashOrNothingPayoff</a>(Option::Put, barrier, 1.0));</div>
<div class="line"> ext::shared_ptr<Instrument> digitalPut(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(digitalPayoff, exercise));</div>
<div class="line"> digitalPut->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a name="a22"></a><a class="code" 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" 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" 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"> ext::shared_ptr<StrikedTypePayoff> lowerPayoff(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));</div>
<div class="line"> ext::shared_ptr<Instrument> put2(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(lowerPayoff, exercise));</div>
<div class="line"> put2->setPricingEngine(europeanEngine);</div>
<div class="line"> portfolio1.<a class="code" 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" 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" 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"> <a name="a23"></a><a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> 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" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Months;</div>
<div class="line"> ext::shared_ptr<Exercise> innerExercise(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));</div>
<div class="line"> ext::shared_ptr<StrikedTypePayoff> innerPayoff(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));</div>
<div class="line"> ext::shared_ptr<Instrument> putn(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(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" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio1.<a name="a24"></a><a class="code" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> 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"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> 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" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">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"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio1.<a class="code" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> 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" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;</div>
<div class="line"> ext::shared_ptr<Exercise> innerExercise(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));</div>
<div class="line"> ext::shared_ptr<StrikedTypePayoff> innerPayoff(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));</div>
<div class="line"> ext::shared_ptr<Instrument> putn(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(innerPayoff, innerExercise));</div>
<div class="line"> putn->setPricingEngine(europeanEngine);</div>
<div class="line"> <a class="code" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio2.<a class="code" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> putValue = putn->NPV();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> notional = portfolioValue/putValue;</div>
<div class="line"> portfolio2.<a class="code" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> underlying->setValue(underlyingValue);</div>
<div class="line"> portfolioValue = portfolio2.<a class="code" 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" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;</div>
<div class="line"> ext::shared_ptr<Exercise> innerExercise(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));</div>
<div class="line"> ext::shared_ptr<StrikedTypePayoff> innerPayoff(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));</div>
<div class="line"> ext::shared_ptr<Instrument> putn(</div>
<div class="line"> <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(innerPayoff, innerExercise));</div>
<div class="line"> putn->setPricingEngine(europeanEngine);</div>
<div class="line"> <a class="code" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().evaluationDate() = killDate;</div>
<div class="line"> underlying->setValue(barrier);</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio3.<a class="code" href="class_quant_lib_1_1_instrument.html#a4f791921f063f3ea19bd6efa9ceb4892" title="returns the net present value of the instrument.">NPV</a>();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> putValue = putn->NPV();</div>
<div class="line"> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> notional = portfolioValue/putValue;</div>
<div class="line"> portfolio3.<a class="code" 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" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().evaluationDate() = today;</div>
<div class="line"> underlying->setValue(underlyingValue);</div>
<div class="line"> portfolioValue = portfolio3.<a class="code" 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.NPV();</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" 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" 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" 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.NPV();</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" 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" 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" 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.8.9.1-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="http://www.doxygen.org/index.html">Doxygen</a>
1.8.20
</small></address>
</body>
</html>
|