Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
varianceswap.cpp
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/varianceswap.hpp
20 \brief variance swap representation
21\ingroup tradedata
22*/
23
25
28
30
31using namespace QuantLib;
32
33namespace ore {
34namespace data {
35
36void VarSwap::build(const QuantLib::ext::shared_ptr<ore::data::EngineFactory>& engineFactory) {
37 Currency ccy = ore::data::parseCurrency(currency_);
43
44 // ISDA taxonomy
46 additionalData_["isdaAssetClass"] = string("Foreign Exchange");
47 additionalData_["isdaBaseProduct"] = string("Simple Exotic");
48 additionalData_["isdaSubProduct"] = string("Vol/Var");
50 additionalData_["isdaAssetClass"] = string("Equity");
51 additionalData_["isdaBaseProduct"] = string("Swap");
53 additionalData_["isdaSubProduct"] = string("Parameter Return Variance");
54 else
55 additionalData_["isdaSubProduct"] = string("Parameter Return Volatility");
57 // guessing, that we should treat Commodities as Equities
58 additionalData_["isdaAssetClass"] = string("Commodity");
59 additionalData_["isdaBaseProduct"] = string("Swap");
62 additionalData_["isdaSubProduct"] = string("Parameter Return Variance");
63 else
64 additionalData_["isdaSubProduct"] = string("Parameter Return Volatility");
65 } else {
66 WLOG("ISDA taxonomy not set for trade " << id());
67 }
68 // skip the transaction level mapping for now
69 additionalData_["isdaTransaction"] = string("");
70
71 if (cal_.empty()) {
72 cal_ = ore::data::parseCalendar(ccy.code());
73 }
74
75 QL_REQUIRE(strike_ > 0 && !close_enough(strike_, 0.0),
76 "VarSwap::build() strike must be positive (" << strike_ << ")");
77 QL_REQUIRE(notional_ > 0 || close_enough(notional_, 0.0),
78 "VarSwap::build() notional must be non-negative (" << notional_ << ")");
79
80 // Input strike is annualised Vol
81 // The quantlib strike and notional are in terms of variance, not volatility, so we convert here.
82 Real varianceStrike = strike_ * strike_;
83 Real varianceNotional = notional_ / (2 * 100 * strike_);
84
85 QuantLib::ext::shared_ptr<QuantExt::VarianceSwap2> varSwap(new QuantExt::VarianceSwap2(
86 longShort, varianceStrike, momentType == MomentType::Variance ? varianceNotional : notional_, start_, endDate,
88
89 // Pricing Engine
90 QuantLib::ext::shared_ptr<ore::data::EngineBuilder> builder = engineFactory->builder(tradeType_);
91 QL_REQUIRE(builder, "No builder found for " << tradeType_);
92 QuantLib::ext::shared_ptr<VarSwapEngineBuilder> varSwapBuilder = QuantLib::ext::dynamic_pointer_cast<VarSwapEngineBuilder>(builder);
93
94 varSwap->setPricingEngine(varSwapBuilder->engine(name(), ccy, assetClassUnderlying_, momentType));
95 setSensitivityTemplate(*varSwapBuilder);
96
97 // set up other Trade details
98 instrument_ = QuantLib::ext::shared_ptr<ore::data::InstrumentWrapper>(new ore::data::VanillaInstrument(varSwap));
99
103
104 // add required fixings
105 for (Date d = cal_.advance(start_, -1 * Days); d <= endDate; d = cal_.advance(d, 1 * Days)) {
106 requiredFixings_.addFixingDate(d, indexName_, varSwap->maturityDate());
107 }
108}
109
110QuantLib::Real VarSwap::notional() const {
113 Real varianceNotional = notional_ / (2 * 100 * strike_);
114 return varianceNotional * 10000;
115 } else
116 return notional_ * 100;
117}
118
120 Trade::fromXML(node);
121 XMLNode* vNode = XMLUtils::getChildNode(node, tradeType() + "Data");
122 if (!vNode) {
123 vNode = XMLUtils::getChildNode(node, "VarianceSwapData");
124 oldXml_ = true;
125 }
126 startDate_ = XMLUtils::getChildValue(vNode, "StartDate", true);
127 endDate_ = XMLUtils::getChildValue(vNode, "EndDate", true);
128 currency_ = XMLUtils::getChildValue(vNode, "Currency", true);
129
130 XMLNode* tmp = XMLUtils::getChildNode(vNode, "Underlying");
131 if (!tmp) {
132 tmp = XMLUtils::getChildNode(vNode, "Name");
133 QL_REQUIRE(tmp, "Must provide a valid Underlying or Name node");
134 }
135 UnderlyingBuilder underlyingBuilder;
136 underlyingBuilder.fromXML(tmp);
137 underlying_ = underlyingBuilder.underlying();
138
139 longShort_ = XMLUtils::getChildValue(vNode, "LongShort", true);
140 strike_ = XMLUtils::getChildValueAsDouble(vNode, "Strike", true);
141 notional_ = XMLUtils::getChildValueAsDouble(vNode, "Notional", true);
142 calendar_ = XMLUtils::getChildValue(vNode, "Calendar", true);
143 momentType_ = XMLUtils::getChildValue(vNode, "MomentType", false);
144 if (momentType_ == "")
145 momentType_ = "Variance";
146 string addPastDividendsStr = XMLUtils::getChildValue(vNode, "AddPastDividends", false);
147 if (addPastDividendsStr == "")
148 addPastDividends_ = false;
149 else
150 addPastDividends_ = parseBool(addPastDividendsStr);
152}
153
155 XMLNode* node = Trade::toXML(doc);
156 XMLNode* vNode;
157 if (oldXml_) {
158 vNode = doc.allocNode("VarianceSwapData");
159 } else {
160 vNode = doc.allocNode(tradeType() + "Data");
161 }
162 XMLUtils::appendNode(node, vNode);
163 XMLUtils::addChild(doc, vNode, "StartDate", startDate_);
164 XMLUtils::addChild(doc, vNode, "EndDate", endDate_);
165 XMLUtils::addChild(doc, vNode, "Currency", currency_);
166 XMLUtils::appendNode(vNode, underlying_->toXML(doc));
167 XMLUtils::addChild(doc, vNode, "LongShort", longShort_);
168 XMLUtils::addChild(doc, vNode, "Strike", strike_);
169 XMLUtils::addChild(doc, vNode, "Notional", notional_);
170 XMLUtils::addChild(doc, vNode, "Calendar", calendar_);
171 XMLUtils::addChild(doc, vNode, "MomentType", momentType_);
172 XMLUtils::addChild(doc, vNode, "AddPastDividends", addPastDividends_);
173 return node;
174}
175
178 indexName_ = "FX-" + name();
180 indexName_ = "EQ-" + name();
182 indexName_ = "COM-" + name();
183 else {
184 QL_FAIL("asset class " << assetClassUnderlying_ << " not supported.");
185 }
186}
187
188std::map<AssetClass, std::set<std::string>>
189EqVarSwap::underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager) const {
190 return {{AssetClass::EQ, std::set<std::string>({name()})}};
191}
192
193std::map<AssetClass, std::set<std::string>>
194ComVarSwap::underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager) const {
195 return {{AssetClass::COM, std::set<std::string>({name()})}};
196}
197
198} // namespace data
199} // namespace ore
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
Add underlying Equity names.
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
Add underlying Equity names.
void addFixingDate(const QuantLib::Date &fixingDate, const std::string &indexName, const QuantLib::Date &payDate=Date::maxDate(), const bool alwaysAddIfPaysOnSettlement=false, const bool mandatoryFixing=true)
string npvCurrency_
Definition: trade.hpp:201
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
string tradeType_
Definition: trade.hpp:196
RequiredFixings requiredFixings_
Definition: trade.hpp:223
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
string notionalCurrency_
Definition: trade.hpp:203
const string & tradeType() const
Definition: trade.hpp:133
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
const QuantLib::ext::shared_ptr< Underlying > & underlying()
Definition: underlying.hpp:266
void fromXML(XMLNode *node) override
Definition: underlying.cpp:305
Vanilla Instrument Wrapper.
QuantLib::ext::shared_ptr< ore::data::Underlying > underlying_
const std::string & name() const
AssetClass assetClassUnderlying_
const string & endDate()
QuantLib::Date start_
QuantLib::Calendar cal_
double notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
void build(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &) override
const string & momentType()
std::string indexName_
const string & longShort()
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
MomentType parseMomentType(const std::string &s)
Convert text to ore::data::MomentType.
Definition: parsers.cpp:1361
@ data
Definition: log.hpp:77
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
Reference data model and serialization.
variance swap representation