Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fxbarrieroption.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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/fxoption.hpp
18 \brief
19 \ingroup portfolio
20*/
21
22#pragma once
23
24#include <boost/make_shared.hpp>
29#include <ql/pricingengines/barrier/fdblackscholesbarrierengine.hpp>
30#include <ql/processes/blackscholesprocess.hpp>
33
34namespace ore {
35namespace data {
36using namespace std;
37using namespace QuantLib;
38
39//! Engine Builder for European FX Barrier Options
40/*! Pricing engines are cached by currency pair
41
42 \ingroup portfolio
43 */
45 : public ore::data::CachingPricingEngineBuilder<string, const Currency&, const Currency&, const Date&,
46 const Date&> {
47
48protected:
49 FxBarrierOptionEngineBuilder(const string& model, const string& engine)
50 : CachingEngineBuilder(model, engine, {"FxBarrierOption"}) {}
51
52 virtual string keyImpl(const Currency& forCcy, const Currency& domCcy, const Date& expiryDate,
53 const Date& paymentDate) override {
54 return forCcy.code() + "/" + domCcy.code() + "/" + ore::data::to_string(expiryDate) + "/" +
55 ore::data::to_string(paymentDate);
56 }
57
58 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess>
59 getBlackScholesProcess(const Currency& forCcy, const Currency& domCcy, const std::vector<Time>& timePoints = {}) {
60 const string& ccyPairCode = forCcy.code() + domCcy.code();
61 Handle<BlackVolTermStructure> vol =
63 if (!timePoints.empty()) {
64 vol = Handle<BlackVolTermStructure>(
65 QuantLib::ext::make_shared<QuantExt::BlackMonotoneVarVolTermStructure>(vol, timePoints));
66 vol->enableExtrapolation();
67 }
68 return QuantLib::ext::make_shared<GeneralizedBlackScholesProcess>(
70 market_->discountCurve(forCcy.code(), configuration(ore::data::MarketContext::pricing)),
71 market_->discountCurve(domCcy.code(), configuration(ore::data::MarketContext::pricing)), vol);
72 }
73};
74
76public:
77 FxBarrierOptionAnalyticEngineBuilder() : FxBarrierOptionEngineBuilder("GarmanKohlhagen", "AnalyticBarrierEngine") {}
78
79protected:
80 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const Currency& forCcy, const Currency& domCcy,
81 const Date& expiryDate, const Date& paymentDate) override {
82 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> gbsp = getBlackScholesProcess(forCcy, domCcy);
83 return QuantLib::ext::make_shared<QuantExt::AnalyticBarrierEngine>(gbsp, paymentDate);
84 }
85};
86
88public:
89 FxBarrierOptionFDEngineBuilder() : FxBarrierOptionEngineBuilder("GarmanKohlhagen", "FdBlackScholesBarrierEngine") {}
90
91protected:
92 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const Currency& forCcy, const Currency& domCcy,
93 const Date& expiryDate, const Date& paymentDate) override {
94 // We follow the way FdBlackScholesBarrierEngine determines maturity for time grid generation
95 Handle<YieldTermStructure> riskFreeRate =
96 market_->discountCurve(domCcy.code(), configuration(ore::data::MarketContext::pricing));
97 Time expiry = riskFreeRate->dayCounter().yearFraction(riskFreeRate->referenceDate(),
98 std::max(riskFreeRate->referenceDate(), expiryDate));
99
100 FdmSchemeDesc scheme = ore::data::parseFdmSchemeDesc(engineParameter("Scheme"));
101 Size tGrid = std::max<Size>(1, (Size)(ore::data::parseInteger(engineParameter("TimeGridPerYear")) * expiry));
102 Size xGrid = ore::data::parseInteger(engineParameter("XGrid"));
103 Size dampingSteps = ore::data::parseInteger(engineParameter("DampingSteps"));
104 bool monotoneVar = ore::data::parseBool(engineParameter("EnforceMonotoneVariance", {}, false, "true"));
105
106 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> gbsp;
107
108 if (monotoneVar) {
109 // Replicate the construction of time grid in FiniteDifferenceModel::rollbackImpl
110 // This time grid is required to build a BlackMonotoneVarVolTermStructure which
111 // ensures monotonic variance along the time grid
112 std::vector<Time> timePoints(tGrid + 1);
113 Array timePointsArray(tGrid, expiry, -expiry / tGrid);
114 timePoints[0] = 0.0;
115 for (Size i = 0; i < tGrid; i++)
116 timePoints[timePoints.size() - i - 1] = timePointsArray[i];
117 timePoints.insert(std::upper_bound(timePoints.begin(), timePoints.end(), 0.99 / 365), 0.99 / 365);
118 gbsp = getBlackScholesProcess(forCcy, domCcy, timePoints);
119 } else {
120 gbsp = getBlackScholesProcess(forCcy, domCcy);
121 }
122 return QuantLib::ext::make_shared<FdBlackScholesBarrierEngine>(gbsp, tGrid, xGrid, dampingSteps, scheme);
123 }
124};
125
126} // namespace data
127} // namespace oreplus
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 Currency &forCcy, const Currency &domCcy, const Date &expiryDate, const Date &paymentDate) override
Engine Builder for European FX Barrier Options.
virtual string keyImpl(const Currency &forCcy, const Currency &domCcy, const Date &expiryDate, const Date &paymentDate) override
QuantLib::ext::shared_ptr< GeneralizedBlackScholesProcess > getBlackScholesProcess(const Currency &forCcy, const Currency &domCcy, const std::vector< Time > &timePoints={})
FxBarrierOptionEngineBuilder(const string &model, const string &engine)
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &forCcy, const Currency &domCcy, const Date &expiryDate, const Date &paymentDate) 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