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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*!
Copyright (C) 2006, 2007 StatPro Italia srl
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.
*/
/* This example showcases the CompositeInstrument class. Such class
is used to build a static replication of a down-and-out barrier
option, as outlined in Section 10.2 of Mark Joshi's "The Concepts
and Practice of Mathematical Finance" to which we refer the
reader.
*/
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
# include <ql/auto_link.hpp>
#endif
#include <ql/instruments/compositeinstrument.hpp>
#include <ql/instruments/barrieroption.hpp>
#include <ql/instruments/europeanoption.hpp>
#include <ql/pricingengines/barrier/analyticbarrierengine.hpp>
#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
#include <ql/exercise.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <iostream>
#include <iomanip>
using namespace QuantLib;
int main(int, char* []) {
try {
std::cout << std::endl;
Date today(29, May, 2006);
Settings::instance().evaluationDate() = today;
// the option to replicate
Barrier::Type barrierType = Barrier::DownOut;
Real barrier = 70.0;
Real rebate = 0.0;
Option::Type type = Option::Put;
Real underlyingValue = 100.0;
auto underlying = ext::make_shared<SimpleQuote>(underlyingValue);
Real strike = 100.0;
auto riskFreeRate = ext::make_shared<SimpleQuote>(0.04);
auto volatility = ext::make_shared<SimpleQuote>(0.20);
Date maturity = today + 1*Years;
std::cout << std::endl ;
// write column headings
Size widths[] = { 45, 15, 15 };
Size totalWidth = widths[0]+widths[1]+widths[2];
std::string rule(totalWidth, '-'), dblrule(totalWidth, '=');
std::cout << dblrule << std::endl;
std::cout << "Initial market conditions" << std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << "Option"
<< std::setw(widths[1]) << std::left << "NPV"
<< std::setw(widths[2]) << std::left << "Error"
<< std::endl;
std::cout << rule << std::endl;
// bootstrap the yield/vol curves
DayCounter dayCounter = Actual365Fixed();
Handle<Quote> h1(riskFreeRate);
Handle<Quote> h2(volatility);
Handle<YieldTermStructure> flatRate(
ext::make_shared<FlatForward>(0, NullCalendar(), h1, dayCounter));
Handle<BlackVolTermStructure> flatVol(
ext::make_shared<BlackConstantVol>(0, NullCalendar(), h2, dayCounter));
// instantiate the option
auto exercise = ext::make_shared<EuropeanExercise>(maturity);
auto payoff = ext::make_shared<PlainVanillaPayoff>(type, strike);
auto bsProcess = ext::make_shared<BlackScholesProcess>(
Handle<Quote>(underlying), flatRate, flatVol);
auto barrierEngine = ext::make_shared<AnalyticBarrierEngine>(bsProcess);
auto europeanEngine = ext::make_shared<AnalyticEuropeanEngine>(bsProcess);
BarrierOption referenceOption(barrierType, barrier, rebate,
payoff, exercise);
referenceOption.setPricingEngine(barrierEngine);
Real referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< "Original barrier option"
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << "N/A"
<< std::endl;
// Replicating portfolios
CompositeInstrument portfolio1, portfolio2, portfolio3;
// Final payoff first (the same for all portfolios):
// as shown in Joshi, a put struck at K...
auto put1 = ext::make_shared<EuropeanOption>(payoff, exercise);
put1->setPricingEngine(europeanEngine);
portfolio1.add(put1);
portfolio2.add(put1);
portfolio3.add(put1);
// ...minus a digital put struck at B of notional K-B...
auto digitalPayoff = ext::make_shared<CashOrNothingPayoff>(Option::Put, barrier, 1.0);
auto digitalPut = ext::make_shared<EuropeanOption>(digitalPayoff, exercise);
digitalPut->setPricingEngine(europeanEngine);
portfolio1.subtract(digitalPut, strike-barrier);
portfolio2.subtract(digitalPut, strike-barrier);
portfolio3.subtract(digitalPut, strike-barrier);
// ...minus a put option struck at B.
auto lowerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);
auto put2 = ext::make_shared<EuropeanOption>(lowerPayoff, exercise);
put2->setPricingEngine(europeanEngine);
portfolio1.subtract(put2);
portfolio2.subtract(put2);
portfolio3.subtract(put2);
// Now we use puts struck at B to kill the value of the
// portfolio on a number of points (B,t). For the first
// portfolio, we'll use 12 dates at one-month's distance.
Integer i;
for (i=12; i>=1; i--) {
// First, we instantiate the option...
Date innerMaturity = today + i*Months;
auto innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);
auto innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);
auto putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);
putn->setPricingEngine(europeanEngine);
// ...second, we evaluate the current portfolio and the
// latest put at (B,t)...
Date killDate = today + (i-1)*Months;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
Real portfolioValue = portfolio1.NPV();
Real putValue = putn->NPV();
// ...finally, we estimate the notional that kills the
// portfolio value at that point...
Real notional = portfolioValue/putValue;
// ...and we subtract from the portfolio a put with such
// notional.
portfolio1.subtract(putn, notional);
}
// The portfolio being complete, we return to today's market...
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
// ...and output the value.
Real portfolioValue = portfolio1.NPV();
Real error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (12 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
// For the second portfolio, we'll use 26 dates at two-weeks'
// distance.
for (i=52; i>=2; i-=2) {
// Same as above.
Date innerMaturity = today + i*Weeks;
auto innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);
auto innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);
auto putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);
putn->setPricingEngine(europeanEngine);
Date killDate = today + (i-2)*Weeks;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
Real portfolioValue = portfolio2.NPV();
Real putValue = putn->NPV();
Real notional = portfolioValue/putValue;
portfolio2.subtract(putn, notional);
}
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
portfolioValue = portfolio2.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (26 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
// For the third portfolio, we'll use 52 dates at one-week's
// distance.
for (i=52; i>=1; i--) {
// Same as above.
Date innerMaturity = today + i*Weeks;
auto innerExercise = ext::make_shared<EuropeanExercise>(innerMaturity);
auto innerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);
auto putn = ext::make_shared<EuropeanOption>(innerPayoff, innerExercise);
putn->setPricingEngine(europeanEngine);
Date killDate = today + (i-1)*Weeks;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
Real portfolioValue = portfolio3.NPV();
Real putValue = putn->NPV();
Real notional = portfolioValue/putValue;
portfolio3.subtract(putn, notional);
}
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
portfolioValue = portfolio3.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (52 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
// Now we modify the market condition to see whether the
// replication holds. First, we change the underlying value so
// that the option is out of the money.
std::cout << dblrule << std::endl;
std::cout << "Modified market conditions: out of the money"
<< std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << "Option"
<< std::setw(widths[1]) << std::left << "NPV"
<< std::setw(widths[2]) << std::left << "Error"
<< std::endl;
std::cout << rule << std::endl;
underlying->setValue(110.0);
referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< "Original barrier option"
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << "N/A"
<< std::endl;
portfolioValue = portfolio1.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (12 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio2.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (26 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio3.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (52 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
// Next, we change the underlying value so that the option is
// in the money.
std::cout << dblrule << std::endl;
std::cout << "Modified market conditions: in the money" << std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << "Option"
<< std::setw(widths[1]) << std::left << "NPV"
<< std::setw(widths[2]) << std::left << "Error"
<< std::endl;
std::cout << rule << std::endl;
underlying->setValue(90.0);
referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< "Original barrier option"
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << "N/A"
<< std::endl;
portfolioValue = portfolio1.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (12 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio2.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (26 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio3.NPV();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< "Replicating portfolio (52 dates)"
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
// Finally, a word of warning for those (shame on them) who
// run the example but do not read the code.
std::cout << dblrule << std::endl;
std::cout
<< std::endl
<< "The replication seems to be less robust when volatility and \n"
<< "risk-free rate are changed. Feel free to experiment with \n"
<< "the example and contribute a patch if you spot any errors."
<< std::endl;
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "unknown error" << std::endl;
return 1;
}
}
|