This example uses the CompositeInstrument class to statically replicate a down-and-out barrier options.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <iostream>
#include <iomanip>
int main(int, char* []) {
try {
std::cout << std::endl;
Date today(29, May, 2006);
Settings::instance().evaluationDate() = today;
Real underlyingValue = 100.0;
auto underlying = ext::make_shared<SimpleQuote>(underlyingValue);
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 ;
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;
ext::make_shared<FlatForward>(0,
NullCalendar(), h1, dayCounter));
ext::make_shared<BlackConstantVol>(0,
NullCalendar(), h2, dayCounter));
auto exercise = ext::make_shared<EuropeanExercise>(maturity);
auto payoff = ext::make_shared<PlainVanillaPayoff>(type, strike);
auto bsProcess = ext::make_shared<BlackScholesProcess>(
auto barrierEngine = ext::make_shared<AnalyticBarrierEngine>(bsProcess);
auto europeanEngine = ext::make_shared<AnalyticEuropeanEngine>(bsProcess);
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;
auto put1 = ext::make_shared<EuropeanOption>(
payoff, exercise);
put1->setPricingEngine(europeanEngine);
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);
auto lowerPayoff = ext::make_shared<PlainVanillaPayoff>(Option::Put, barrier);
auto put2 = ext::make_shared<EuropeanOption>(lowerPayoff, exercise);
put2->setPricingEngine(europeanEngine);
for (i=12; i>=1; i--) {
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);
Date killDate = today + (i-1)*Months;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
Real portfolioValue = portfolio1.
NPV();
Real putValue = putn->NPV();
Real notional = portfolioValue/putValue;
}
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
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 (i=52; i>=2; i-=2) {
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;
}
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 (i=52; i>=1; i--) {
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;
}
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;
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;
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;
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;
}
}
Analytic barrier option engines.
Analytic European engine.
Barrier option on a single asset.
Black constant volatility, no time dependence, no strike dependence.
Actual/365 (Fixed) day count convention.
Barrier option on a single asset.
void add(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0)
adds an instrument to the composite
void subtract(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0)
shorts an instrument from the composite
Shared handle to an observable.
Real NPV() const
returns the net present value of the instrument.
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Calendar for reproducing theoretical calculations.
Composite instrument class.
European option on a single asset.
Option exercise classes and payoff function.
flat forward rate term structure
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
ext::shared_ptr< QuantLib::Payoff > payoff
Calendar for reproducing theoretical calculations.
Global definitions and compiler switches.