Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equitybarrieroption.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7 ORE is free software: you can redistribute it and/or modify it
8 under the terms of the Modified BSD License. You should have received a
9 copy of the license along with this program.
10 The license is also available online at <http://opensourcerisk.org>
11 This program is distributed on the basis that it will form a useful
12 contribution to risk analytics and model standardisation, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
15*/
16
17/*! \file ored/portfolio/builders/equitybarrieroption.hpp
18 \brief
19 \ingroup portfolio
20*/
21
22#pragma once
23
24#include <boost/make_shared.hpp>
29#include <ql/pricingengines/barrier/analyticbarrierengine.hpp>
30#include <ql/pricingengines/barrier/fdblackscholesbarrierengine.hpp>
31#include <ql/processes/blackscholesprocess.hpp>
33
34namespace ore {
35namespace data {
36using namespace std;
37using namespace QuantLib;
38
39//! Engine Builder for Equity Barrier Options
40/*! Pricing engines are cached by asset name / currency
41
42 \ingroup portfolio
43 */
45 : public ore::data::CachingPricingEngineBuilder<string, const string&, const Currency&, const Date&> {
46
47protected:
48 EquityBarrierOptionEngineBuilder(const string& model, const string& engine)
49 : CachingEngineBuilder(model, engine, {"EquityBarrierOption"}) {}
50
51 virtual string keyImpl(const string& assetName, const Currency& ccy, const Date& expiryDate) override {
52 return assetName + "/" + ccy.code() + "/" + ore::data::to_string(expiryDate);
53 }
54
55 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> getBlackScholesProcess(const string& assetName, const Currency& ccy,
56 const std::vector<Time>& timePoints = {}) {
57
58 Handle<BlackVolTermStructure> vol = this->market_->equityVol(assetName, configuration(ore::data::MarketContext::pricing));
59 if (!timePoints.empty()) {
60 vol = Handle<BlackVolTermStructure>(
61 QuantLib::ext::make_shared<QuantExt::BlackMonotoneVarVolTermStructure>(vol, timePoints));
62 vol->enableExtrapolation();
63 }
64 return QuantLib::ext::make_shared<GeneralizedBlackScholesProcess>(
65 this->market_->equitySpot(assetName, configuration(ore::data::MarketContext::pricing)),
66 this->market_->equityDividendCurve(assetName, configuration(ore::data::MarketContext::pricing)),
67 this->market_->equityForecastCurve(assetName, configuration(ore::data::MarketContext::pricing)),
68 vol);
69 }
70};
71
74public:
76 : EquityBarrierOptionEngineBuilder("BlackScholesMerton", "AnalyticBarrierEngine") {}
77
78protected:
79
80 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const string& assetName, const Currency& ccy, const Date& expiryDate) override {
81 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> gbsp = getBlackScholesProcess(assetName, ccy);
82 return QuantLib::ext::make_shared<QuantLib::AnalyticBarrierEngine>(gbsp);
83 }
84
85};
86
89public:
91 : EquityBarrierOptionEngineBuilder("BlackScholesMerton", "FdBlackScholesBarrierEngine") {}
92
93protected:
94
95 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const string& assetName, const Currency& ccy, const Date& expiryDate) override {
96 // We follow the way FdBlackScholesBarrierEngine determines maturity for time grid generation
97 Handle<YieldTermStructure> riskFreeRate =
98 market_->discountCurve(ccy.code(), configuration(ore::data::MarketContext::pricing));
99 Time expiry = riskFreeRate->dayCounter().yearFraction(riskFreeRate->referenceDate(),
100 std::max(riskFreeRate->referenceDate(), expiryDate));
101
102 FdmSchemeDesc scheme = ore::data::parseFdmSchemeDesc(engineParameter("Scheme"));
103 Size tGrid = std::max<Size>(1, (Size)(ore::data::parseInteger(engineParameter("TimeGridPerYear")) * expiry));
104 Size xGrid = ore::data::parseInteger(engineParameter("XGrid"));
105 Size dampingSteps = ore::data::parseInteger(engineParameter("DampingSteps"));
106 bool monotoneVar = ore::data::parseBool(engineParameter("EnforceMonotoneVariance", {}, false, "true"));
107
108 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> gbsp;
109
110 if(monotoneVar) {
111 // Replicate the construction of time grid in FiniteDifferenceModel::rollbackImpl
112 // This time grid is required to build a BlackMonotoneVarVolTermStructure which
113 // ensures monotonic variance along the time grid
114 std::vector<Time> timePoints(tGrid + 1);
115 Array timePointsArray(tGrid, expiry, -expiry / tGrid);
116 timePoints[0] = 0.0;
117 for(Size i = 0; i < tGrid; i++)
118 timePoints[timePoints.size() - i - 1] = timePointsArray[i];
119 timePoints.insert(std::upper_bound(timePoints.begin(), timePoints.end(), 0.99 / 365), 0.99 / 365);
120 gbsp = getBlackScholesProcess(assetName, ccy, timePoints);
121 } else {
122 gbsp = getBlackScholesProcess(assetName, ccy);
123 }
124 return QuantLib::ext::make_shared<FdBlackScholesBarrierEngine>(gbsp, tGrid, xGrid,
125 dampingSteps, scheme);
126 }
127
128};
129
130} // namespace data
131} // namespace ore
Abstract template engine builder class.
Abstract template EngineBuilder class that can cache engines and coupon pricers.
QuantLib::ext::shared_ptr< Market > market_
const string & engine() const
Return the engine name.
const string & model() const
Return the model name.
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const string &assetName, const Currency &ccy, const Date &expiryDate) override
Engine Builder for Equity Barrier Options.
EquityBarrierOptionEngineBuilder(const string &model, const string &engine)
virtual string keyImpl(const string &assetName, const Currency &ccy, const Date &expiryDate) override
QuantLib::ext::shared_ptr< GeneralizedBlackScholesProcess > getBlackScholesProcess(const string &assetName, const Currency &ccy, const std::vector< Time > &timePoints={})
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const string &assetName, const Currency &ccy, const Date &expiryDate) override
Pricing Engine Factory.
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
FdmSchemeDesc parseFdmSchemeDesc(const std::string &s)
Convert string to fdm scheme desc.
Definition: parsers.cpp:692
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities