Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
varianceswap.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/portfolio/builders/varianceswap.hpp
20 \brief variance swap engine builder
21 \ingroup builders
22*/
23
24#pragma once
25
30
31#include <string>
32
33#include <boost/make_shared.hpp>
41#include <ql/processes/blackscholesprocess.hpp>
42
43namespace ore {
44namespace data {
45using namespace std;
46using namespace QuantLib;
47
48//! Engine Builder for Variance Swaps
49/*! Pricing engines are cached by equity/currency
50
51 \ingroup builders
52 */
53class VarSwapEngineBuilder : public ore::data::CachingPricingEngineBuilder<string, const string&, const Currency&,
54 const AssetClass&, const MomentType&> {
55public:
57 : CachingEngineBuilder("BlackScholesMerton", "ReplicatingVarianceSwapEngine",
58 {"EquityVarianceSwap", "FxVarianceSwap", "CommodityVarianceSwap"}) {}
59
60protected:
61 virtual string keyImpl(const string& underlyingName, const Currency& ccy, const AssetClass& assetClassUnderlying,
62 const MomentType& momentType) override {
63 return underlyingName + "/" + ccy.code() + "/" +
64 (momentType == MomentType::Variance ? "Variance" : "Volatility");
65 }
66
67 virtual QuantLib::ext::shared_ptr<PricingEngine> engineImpl(const string& underlyingName, const Currency& ccy,
68 const AssetClass& assetClassUnderlying,
69 const MomentType& momentType) override {
70 QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess> gbsp;
71 QuantLib::ext::shared_ptr<Index> index;
72 if (assetClassUnderlying == AssetClass::EQ) {
73 gbsp = QuantLib::ext::make_shared<GeneralizedBlackScholesProcess>(
74 market_->equitySpot(underlyingName, configuration(ore::data::MarketContext::pricing)),
75 market_->equityDividendCurve(underlyingName, configuration(ore::data::MarketContext::pricing)),
76 market_->equityForecastCurve(underlyingName, configuration(ore::data::MarketContext::pricing)),
77 market_->equityVol(underlyingName, configuration(ore::data::MarketContext::pricing)));
78 index = market_->equityCurve(underlyingName).currentLink();
79 } else if (assetClassUnderlying == AssetClass::FX) {
80 const auto fxIndex = parseFxIndex("FX-" + underlyingName);
81 const string& sourceCurrencyCode = fxIndex->sourceCurrency().code();
82 const string& targetCurrencyCode = fxIndex->targetCurrency().code();
83 const string& ccyPairCode = sourceCurrencyCode + targetCurrencyCode;
84 gbsp = QuantLib::ext::make_shared<GeneralizedBlackScholesProcess>(
86 market_->discountCurve(targetCurrencyCode, configuration(ore::data::MarketContext::pricing)),
87 market_->discountCurve(sourceCurrencyCode, configuration(ore::data::MarketContext::pricing)),
89
90 index = buildFxIndex("FX-" + underlyingName, targetCurrencyCode, sourceCurrencyCode, market_,
92 } else if (assetClassUnderlying == AssetClass::COM) {
93 Handle<BlackVolTermStructure> vol = market_->commodityVolatility(underlyingName, configuration(ore::data::MarketContext::pricing));
94 Handle<QuantExt::PriceTermStructure> priceCurve = market_->commodityPriceCurve(underlyingName, configuration(ore::data::MarketContext::pricing));
95 Handle<Quote> commoditySpot(QuantLib::ext::make_shared<QuantExt::DerivedPriceQuote>(priceCurve));
96 Handle<YieldTermStructure> discount = market_->discountCurve(ccy.code(), configuration(ore::data::MarketContext::pricing));
97 Handle<YieldTermStructure> yield(QuantLib::ext::make_shared<QuantExt::PriceTermStructureAdapter>(*priceCurve, *discount));
98 yield->enableExtrapolation();
99 gbsp = QuantLib::ext::make_shared<GeneralizedBlackScholesProcess>(commoditySpot, yield, discount, vol);
100 index = market_->commodityIndex(underlyingName).currentLink();
101 } else {
102 QL_FAIL("Asset class of " + underlyingName + " not recognized.");
103 }
104
106
107 std::string schemeStr = engineParameter("Scheme", {}, false, "GaussLobatto");
108 std::string boundsStr = engineParameter("Bounds", {}, false, "PriceThreshold");
109
110 if (schemeStr == "GaussLobatto")
111 settings.scheme = QuantExt::GeneralisedReplicatingVarianceSwapEngine::VarSwapSettings::Scheme::GaussLobatto;
112 else if (schemeStr == "Segment")
113 settings.scheme = QuantExt::GeneralisedReplicatingVarianceSwapEngine::VarSwapSettings::Scheme::Segment;
114 else {
115 QL_FAIL("invalid var swap pricing engine parameter Scheme (" << schemeStr
116 << "), expected GaussLobatto, Segment");
117 }
118
119 if (boundsStr == "Fixed")
120 settings.bounds = QuantExt::GeneralisedReplicatingVarianceSwapEngine::VarSwapSettings::Bounds::Fixed;
121 else if (boundsStr == "PriceThreshold")
122 settings.bounds = QuantExt::GeneralisedReplicatingVarianceSwapEngine::VarSwapSettings::Bounds::PriceThreshold;
123 else {
124 QL_FAIL("invalid var swap pricing engine parameter Bounds (" << boundsStr
125 << "), expected Fixed, PriceThreshold");
126 }
127
128 settings.accuracy = parseReal(engineParameter("Accuracy", {}, false, "1E-5"));
129 settings.maxIterations = parseInteger(engineParameter("MaxIterations", {}, false, "1000"));
130 settings.steps = parseInteger(engineParameter("Steps", {}, false, "100"));
131 settings.priceThreshold = parseReal(engineParameter("PriceThreshold", {}, false, "1E-10"));
132 settings.maxPriceThresholdSteps = parseInteger(engineParameter("MaxPriceThresholdSteps", {}, false, "100"));
133 settings.priceThresholdStep = parseReal(engineParameter("PriceThresholdStep", {}, false, "0.1"));
134 settings.fixedMinStdDevs = parseReal(engineParameter("FixedMinStdDevs", {}, false, "-5.0"));
135 settings.fixedMaxStdDevs = parseReal(engineParameter("FixedMaxStdDevs", {}, false, "5.0"));
136
137 bool staticTodaysSpot = false;
138 if (auto it = globalParameters_.find("RunType");
139 it != globalParameters_.end() && it->second != "Exposure") {
140 staticTodaysSpot = parseBool(modelParameter("StaticTodaysSpot", {}, false, "false"));
141 }
142
143 if (momentType == MomentType::Variance)
144 return QuantLib::ext::make_shared<QuantExt::GeneralisedReplicatingVarianceSwapEngine>(
145 index, gbsp, market_->discountCurve(ccy.code(), configuration(ore::data::MarketContext::pricing)),
146 settings, staticTodaysSpot);
147 else
148 return QuantLib::ext::make_shared<QuantExt::VolatilityFromVarianceSwapEngine>(
149 index, gbsp, market_->discountCurve(ccy.code(), configuration(ore::data::MarketContext::pricing)),
150 settings, staticTodaysSpot);
151 }
152};
153
154} // namespace data
155} // namespace ore
Abstract template engine builder class.
Abstract template EngineBuilder class that can cache engines and coupon pricers.
QuantLib::ext::shared_ptr< Market > market_
std::string modelParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
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)
std::map< std::string, std::string > globalParameters_
Engine Builder for Variance Swaps.
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const string &underlyingName, const Currency &ccy, const AssetClass &assetClassUnderlying, const MomentType &momentType) override
virtual string keyImpl(const string &underlyingName, const Currency &ccy, const AssetClass &assetClassUnderlying, const MomentType &momentType) override
Pricing Engine Factory.
QuantLib::ext::shared_ptr< FxIndex > parseFxIndex(const string &s, const Handle< Quote > &fxSpot, const Handle< YieldTermStructure > &sourceYts, const Handle< YieldTermStructure > &targetYts, const bool useConventions)
Convert std::string to QuantExt::FxIndex.
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
Map text representations to QuantLib/QuantExt types.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
market data related utilties
QuantLib::ext::shared_ptr< QuantExt::FxIndex > buildFxIndex(const string &fxIndex, const string &domestic, const string &foreign, const QuantLib::ext::shared_ptr< Market > &market, const string &configuration, bool useXbsCurves)
Definition: marketdata.cpp:137
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities