Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
BasketVarianceSwap Class Reference

#include <ored/portfolio/basketvarianceswap.hpp>

+ Inheritance diagram for BasketVarianceSwap:
+ Collaboration diagram for BasketVarianceSwap:

Public Member Functions

 BasketVarianceSwap (const string &tradeType="BasketVarianceSwap")
 
 BasketVarianceSwap (const Envelope &env, const string &longShort, const string &notional, const string &strike, const string &currency, const string &cap, const string &floor, const string &settlementDate, const ScheduleData &valuationSchedule, bool squaredPayoff, const vector< QuantLib::ext::shared_ptr< Underlying > > underlyings)
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 
void setIsdaTaxonomyFields () override
 
void fromXML (XMLNode *node) override
 
XMLNodetoXML (XMLDocument &doc) const override
 
- Public Member Functions inherited from ScriptedTrade
 ScriptedTrade (const std::string &tradeType="ScriptedTrade", const Envelope &env=Envelope())
 
 ScriptedTrade (const Envelope &env, const std::vector< ScriptedTradeEventData > &events, const std::vector< ScriptedTradeValueTypeData > &numbers, const std::vector< ScriptedTradeValueTypeData > &indices, const std::vector< ScriptedTradeValueTypeData > &currencies, const std::vector< ScriptedTradeValueTypeData > &daycounters, const std::map< std::string, ScriptedTradeScriptData > &script, const std::string &productTag, const std::string &tradeType="ScriptedTrade")
 
 ScriptedTrade (const Envelope &env, const std::vector< ScriptedTradeEventData > &events, const std::vector< ScriptedTradeValueTypeData > &numbers, const std::vector< ScriptedTradeValueTypeData > &indices, const std::vector< ScriptedTradeValueTypeData > &currencies, const std::vector< ScriptedTradeValueTypeData > &daycounters, const std::string &scriptName, const std::string &tradeType="ScriptedTrade")
 
void clear ()
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 
QuantLib::Real notional () const override
 Return the current notional in npvCurrency. See individual sub-classes for the precise definition. More...
 
std::string notionalCurrency () const override
 
void fromXML (XMLNode *node) override
 
XMLNodetoXML (ore::data::XMLDocument &doc) const override
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, const PremiumData &premiumData, const Real premiumMultiplier)
 
std::map< ore::data::AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 
virtual void setIsdaTaxonomyFields ()
 
const std::vector< ScriptedTradeEventData > & events () const
 
const std::vector< ScriptedTradeValueTypeData > & numbers () const
 
const std::vector< ScriptedTradeValueTypeData > & indices () const
 
const std::vector< ScriptedTradeValueTypeData > & currencies () const
 
const std::vector< ScriptedTradeValueTypeData > & daycounters () const
 
const std::map< std::string, ScriptedTradeScriptData > & script () const
 
const std::string & productTag () const
 
const std::string & scriptName () const
 
const std::string & simmProductClass () const
 
const std::string & scheduleProductClass () const
 
const ScriptedTradeScriptDatascript (const std::string &purpose, const bool fallBackOnEmptyPurpose=true) const
 
- Public Member Functions inherited from Trade
 Trade ()
 Default constructor. More...
 
 Trade (const string &tradeType, const Envelope &env=Envelope(), const TradeActions &ta=TradeActions())
 Base class constructor. More...
 
virtual ~Trade ()
 Default destructor. More...
 
virtual void build (const QuantLib::ext::shared_ptr< EngineFactory > &)=0
 
virtual std::map< std::string, RequiredFixings::FixingDatesfixings (const QuantLib::Date &settlementDate=QuantLib::Date()) const
 
const RequiredFixingsrequiredFixings () const
 
virtual std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const
 
void reset ()
 Reset trade, clear all base class data. This does not reset accumulated timings for this trade. More...
 
void resetPricingStats (const std::size_t numberOfPricings=0, const boost::timer::nanosecond_type cumulativePricingTime=0)
 Reset accumulated timings to given values. More...
 
string & id ()
 Set the trade id. More...
 
void setEnvelope (const Envelope &envelope)
 Set the envelope with counterparty and portfolio info. More...
 
void setAdditionalData (const std::map< std::string, boost::any > &additionalData)
 
TradeActionstradeActions ()
 Set the trade actions. More...
 
const string & id () const
 
const string & tradeType () const
 
const Envelopeenvelope () const
 
const set< string > & portfolioIds () const
 
const TradeActionstradeActions () const
 
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument () const
 
const std::vector< QuantLib::Leg > & legs () const
 
const std::vector< string > & legCurrencies () const
 
const std::vector< bool > & legPayers () const
 
const string & npvCurrency () const
 
const Date & maturity () const
 
virtual bool isExpired (const Date &d)
 
const string & issuer () const
 
template<typename T >
additionalDatum (const std::string &tag) const
 returns any additional datum. More...
 
virtual const std::map< std::string, boost::any > & additionalData () const
 returns all additional data returned by the trade once built More...
 
const std::string & sensitivityTemplate () const
 
void validate () const
 Utility to validate that everything that needs to be set in this base class is actually set. More...
 
virtual bool hasCashflows () const
 
boost::timer::nanosecond_type getCumulativePricingTime () const
 Get cumulative timing spent on pricing. More...
 
std::size_t getNumberOfPricings () const
 Get number of pricings. More...
 
- Public Member Functions inherited from XMLSerializable
virtual ~XMLSerializable ()
 
virtual void fromXML (XMLNode *node)=0
 
virtual XMLNodetoXML (XMLDocument &doc) const =0
 
void fromFile (const std::string &filename)
 
void toFile (const std::string &filename) const
 
void fromXMLString (const std::string &xml)
 Parse from XML string. More...
 
std::string toXMLString () const
 Parse from XML string. More...
 

Private Member Functions

void initIndices ()
 

Private Attributes

string longShort_
 
string notional_
 
string strike_
 
string currency_
 
string cap_
 
string floor_
 
string settlementDate_
 
ScheduleData valuationSchedule_
 
bool squaredPayoff_
 
vector< QuantLib::ext::shared_ptr< Underlying > > underlyings_
 

Additional Inherited Members

- Protected Member Functions inherited from Trade
Date addPremiums (std::vector< QuantLib::ext::shared_ptr< Instrument > > &instruments, std::vector< Real > &multipliers, const Real tradeMultiplier, const PremiumData &premiumData, const Real premiumMultiplier, const Currency &tradeCurrency, const QuantLib::ext::shared_ptr< EngineFactory > &factory, const string &configuration)
 
void setLegBasedAdditionalData (const Size legNo, Size resultLegId=Null< Size >()) const
 
void setSensitivityTemplate (const EngineBuilder &builder)
 
void setSensitivityTemplate (const std::string &id)
 
- Protected Attributes inherited from ScriptedTrade
std::vector< ScriptedTradeEventDataevents_
 
std::vector< ScriptedTradeValueTypeDatanumbers_
 
std::vector< ScriptedTradeValueTypeDataindices_
 
std::vector< ScriptedTradeValueTypeDatacurrencies_
 
std::vector< ScriptedTradeValueTypeDatadaycounters_
 
std::map< std::string, ScriptedTradeScriptDatascript_
 
std::string productTag_
 
std::string scriptName_
 
std::string simmProductClass_
 
std::string scheduleProductClass_
 
- Protected Attributes inherited from Trade
string tradeType_
 
QuantLib::ext::shared_ptr< InstrumentWrapperinstrument_
 
std::vector< QuantLib::Leg > legs_
 
std::vector< string > legCurrencies_
 
std::vector< boollegPayers_
 
string npvCurrency_
 
QuantLib::Real notional_
 
string notionalCurrency_
 
Date maturity_
 
string issuer_
 
string sensitivityTemplate_
 
bool sensitivityTemplateSet_ = false
 
std::size_t savedNumberOfPricings_ = 0
 
boost::timer::nanosecond_type savedCumulativePricingTime_ = 0
 
RequiredFixings requiredFixings_
 
std::map< std::string, boost::any > additionalData_
 

Detailed Description

Definition at line 34 of file basketvarianceswap.hpp.

Constructor & Destructor Documentation

◆ BasketVarianceSwap() [1/2]

BasketVarianceSwap ( const string &  tradeType = "BasketVarianceSwap")
explicit

Definition at line 36 of file basketvarianceswap.hpp.

ScriptedTrade(const std::string &tradeType="ScriptedTrade", const Envelope &env=Envelope())
const string & tradeType() const
Definition: trade.hpp:133

◆ BasketVarianceSwap() [2/2]

BasketVarianceSwap ( const Envelope env,
const string &  longShort,
const string &  notional,
const string &  strike,
const string &  currency,
const string &  cap,
const string &  floor,
const string &  settlementDate,
const ScheduleData valuationSchedule,
bool  squaredPayoff,
const vector< QuantLib::ext::shared_ptr< Underlying > >  underlyings 
)

Definition at line 37 of file basketvarianceswap.hpp.

41 : longShort_(longShort), notional_(notional), strike_(strike), currency_(currency), cap_(cap), floor_(floor),
42 settlementDate_(settlementDate), valuationSchedule_(valuationSchedule), squaredPayoff_(squaredPayoff),
43 underlyings_(underlyings) {
45 }
vector< QuantLib::ext::shared_ptr< Underlying > > underlyings_
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
+ Here is the call graph for this function:

Member Function Documentation

◆ build()

void build ( const QuantLib::ext::shared_ptr< EngineFactory > &  )
overridevirtual

Build QuantLib/QuantExt instrument, link pricing engine. If build() is called multiple times, reset() should be called between these calls.

Implements Trade.

Definition at line 29 of file basketvarianceswap.cpp.

29 {
30
31 auto builder = QuantLib::ext::dynamic_pointer_cast<ScriptedTradeEngineBuilder>(factory->builder("ScriptedTrade"));
32
33 // set script parameters
34
35 clear();
37
38 // check underlying types
39 QL_REQUIRE(underlyings_.size() > 0, "No underlyings were provided");
40 string type = underlyings_.front()->type();
41 for (auto u : underlyings_) {
42 QL_REQUIRE(u->type() == type, "All of Underlyings must be from the same asset class.");
43 }
44
45 // dates/schedules
46 events_.emplace_back("ValuationSchedule", valuationSchedule_);
47 events_.emplace_back("SettlementDate", settlementDate_);
48
49 // numbers
50 numbers_.emplace_back("Number", "Strike", strike_);
51 numbers_.emplace_back("Number", "Notional", notional_);
52 numbers_.emplace_back("Number", "Cap", cap_.empty() ? "0" : cap_);
53 numbers_.emplace_back("Number", "Floor", floor_.empty() ? "0" : floor_);
54
55 // booleans
56 numbers_.emplace_back("Number", "LongShort", parsePositionType(longShort_) == Position::Long ? "1" : "-1");
57 numbers_.emplace_back("Number", "SquaredPayoff", squaredPayoff_ ? "1" : "-1");
58
59 // currencies
60 currencies_.emplace_back("Currency", "PayCcy", currency_);
61
62 // set product tag accordingly
63 if (type == "InterestRate") {
64 productTag_ = "MultiUnderlyingIrOption";
65 } else {
66 productTag_ = "MultiAssetOptionAD({AssetClass})";
67 }
68
69 LOG("ProductTag=" << productTag_);
70
71 // set script
72
73 script_ = {// clang-format off
74 {"", ScriptedTradeScriptData(
75 "REQUIRE {Notional >= 0} AND {Strike >= 0};\n"
76 "REQUIRE {Cap >= 0} AND {Floor >= 0};\n"
77 "\n"
78 "NUMBER i, n;\n"
79 "n = SIZE(Underlyings);\n"
80 "\n"
81 "NUMBER sumOfWeights;\n"
82 "FOR i IN (1, n, 1) DO\n"
83 " sumOfWeights = sumOfWeights + Weights[i];\n"
84 "END;\n"
85 "REQUIRE sumOfWeights == 1;\n"
86 "\n"
87 "NUMBER d, expectedN, currPrice[n], prevPrice[n];\n"
88 "NUMBER realisedVariance, basketVariation, realisedVariation;\n"
89 "NUMBER strike, cap, floor, currentNotional, payoff;\n"
90 "\n"
91 "FOR d IN (2, SIZE(ValuationSchedule), 1) DO\n"
92 " basketVariation = 0;\n"
93 " FOR i IN (1, n, 1) DO\n"
94 " currPrice[i] = Underlyings[i](ValuationSchedule[d]);\n"
95 " prevPrice[i] = Underlyings[i](ValuationSchedule[d-1]);\n"
96 " basketVariation = basketVariation + Weights[i] * ln(currPrice[i]/prevPrice[i]);\n"
97 " END;\n"
98 " realisedVariance = realisedVariance + pow(basketVariation, 2);\n"
99 "END;\n"
100 "\n"
101 "expectedN = SIZE(ValuationSchedule) - 1;\n"
102 "realisedVariance = (252/expectedN) * realisedVariance;\n"
103 "\n"
104 "IF SquaredPayoff == 1 THEN\n"
105 " realisedVariation = realisedVariance;\n"
106 " currentNotional = pow(100, 2) * Notional / (2 * 100 * Strike);\n"
107 " strike = pow(Strike, 2);\n"
108 "ELSE\n"
109 " realisedVariation = sqrt(realisedVariance);\n"
110 " currentNotional = 100 * Notional;\n"
111 " strike = Strike;\n"
112 "END;\n"
113 "\n"
114 "IF Floor > 0 THEN\n"
115 " IF SquaredPayoff == 1 THEN\n"
116 " floor = pow(Floor, 2);\n"
117 " ELSE\n"
118 " floor = Floor;\n"
119 " END;\n"
120 " realisedVariation = max(floor * strike, realisedVariation);\n"
121 "END;\n"
122 "IF Cap > 0 THEN\n"
123 " IF SquaredPayoff == 1 THEN\n"
124 " cap = pow(Cap, 2);\n"
125 " ELSE\n"
126 " cap = Cap;\n"
127 " END;\n"
128 " realisedVariation = min(cap * strike, realisedVariation);\n"
129 "END;\n"
130 "\n"
131 "payoff = LongShort * currentNotional * (realisedVariation - strike);\n"
132 "\n"
133 "Swap = PAY(payoff, ValuationSchedule[SIZE(ValuationSchedule)],\n"
134 " SettlementDate, PayCcy);\n",
135 "Swap",
136 {{"RealisedVariance", "realisedVariance"},
137 {"currentNotional", "currentNotional"},
138 {"notionalCurrency", "PayCcy"}},
139 {})}};
140 // clang-format on
141
142 // build trade
143
144 ScriptedTrade::build(factory);
145}
std::vector< ScriptedTradeEventData > events_
std::vector< ScriptedTradeValueTypeData > currencies_
std::vector< ScriptedTradeValueTypeData > numbers_
std::map< std::string, ScriptedTradeScriptData > script_
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
+ Here is the call graph for this function:

◆ setIsdaTaxonomyFields()

void setIsdaTaxonomyFields ( )
overridevirtual

Reimplemented from ScriptedTrade.

Definition at line 147 of file basketvarianceswap.cpp.

147 {
149
150 // ISDA taxonomy
151 // asset class set in the base class already
152 std::string assetClass = boost::any_cast<std::string>(additionalData_["isdaAssetClass"]);
153 if (assetClass == "Equity") {
154 additionalData_["isdaBaseProduct"] = string("Swap");
155 additionalData_["isdaSubProduct"] = string("Parameter Return Variance");
156 } else if (assetClass == "Foreign Exchange") {
157 additionalData_["isdaBaseProduct"] = string("Complex Exotic");
158 additionalData_["isdaSubProduct"] = string("Generic");
159 } else if (assetClass == "Commodity") {
160 // isda taxonomy missing for this class, using the same as equity
161 additionalData_["isdaBaseProduct"] = string("Other");
162 additionalData_["isdaSubProduct"] = string("Parameter Return Variance");
163 } else {
164 WLOG("ISDA taxonomy incomplete for trade " << id());
165 }
166 additionalData_["isdaTransaction"] = string("Basket");
167}
virtual void setIsdaTaxonomyFields()
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
+ Here is the call graph for this function:

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 180 of file basketvarianceswap.cpp.

180 {
181 Trade::fromXML(node);
182 XMLNode* tradeDataNode = XMLUtils::getChildNode(node, tradeType() + "Data");
183 QL_REQUIRE(tradeDataNode, "BasketVarianceSwapData node not found");
184 longShort_ = XMLUtils::getChildValue(tradeDataNode, "LongShort", true);
185 notional_ = XMLUtils::getChildValue(tradeDataNode, "Notional", true);
186 strike_ = XMLUtils::getChildValue(tradeDataNode, "Strike", true);
187 cap_ = XMLUtils::getChildValue(tradeDataNode, "Cap", false);
188 floor_ = XMLUtils::getChildValue(tradeDataNode, "Floor", false);
189
190 XMLNode* valuationSchedule = XMLUtils::getChildNode(tradeDataNode, "ValuationSchedule");
191 QL_REQUIRE(valuationSchedule, "No valuation schedule provided");
192 valuationSchedule_.fromXML(valuationSchedule);
193
194 XMLNode* underlyingsNode = XMLUtils::getChildNode(tradeDataNode, "Underlyings");
195 QL_REQUIRE(underlyingsNode, "Could not find an Underlyings node.");
196 auto underlyings = XMLUtils::getChildrenNodes(underlyingsNode, "Underlying");
197 for (auto const& u : underlyings) {
198 UnderlyingBuilder underlyingBuilder;
199 underlyingBuilder.fromXML(u);
200 underlyings_.push_back(underlyingBuilder.underlying());
201 }
202
203 settlementDate_ = XMLUtils::getChildValue(tradeDataNode, "SettlementDate", false);
204
205 string squaredPayoff = XMLUtils::getChildValue(tradeDataNode, "SquaredPayoff", false);
206 squaredPayoff_ = squaredPayoff.empty() ? false : parseBool(squaredPayoff);
207
208 currency_ = XMLUtils::getChildValue(tradeDataNode, "Currency", true);
209
210 initIndices();
211}
virtual void fromXML(XMLNode *node) override
Definition: schedule.cpp:179
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
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
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
+ Here is the call graph for this function:

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 213 of file basketvarianceswap.cpp.

213 {
214 XMLNode* node = Trade::toXML(doc);
215 XMLNode* tradeNode = doc.allocNode(tradeType() + "Data");
216 XMLUtils::appendNode(node, tradeNode);
217 XMLUtils::addChild(doc, tradeNode, "LongShort", longShort_);
218 XMLUtils::addChild(doc, tradeNode, "Currency", currency_);
219 XMLUtils::addChild(doc, tradeNode, "Notional", notional_);
220 XMLUtils::addChild(doc, tradeNode, "Strike", strike_);
221
222 XMLNode* underlyingsNode = doc.allocNode("Underlyings");
223 for (auto const& u : underlyings_)
224 XMLUtils::appendNode(underlyingsNode, u->toXML(doc));
225 XMLUtils::appendNode(tradeNode, underlyingsNode);
226
227 XMLNode* valuationSchedule = valuationSchedule_.toXML(doc);
228 XMLUtils::setNodeName(doc, valuationSchedule, "ValuationSchedule");
229 XMLUtils::appendNode(tradeNode, valuationSchedule);
230
231 XMLUtils::addChild(doc, tradeNode, "SettlementDate", settlementDate_);
232 if (!cap_.empty()) {
233 XMLUtils::addChild(doc, tradeNode, "Cap", cap_);
234 }
235 if (!floor_.empty()) {
236 XMLUtils::addChild(doc, tradeNode, "Floor", floor_);
237 }
238 XMLUtils::addChild(doc, tradeNode, "SquaredPayoff", squaredPayoff_);
239
240 return node;
241}
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: schedule.cpp:198
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
static void setNodeName(XMLDocument &doc, XMLNode *node, const string &name)
Definition: xmlutils.cpp:478
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
+ Here is the call graph for this function:

◆ initIndices()

void initIndices ( )
private

Definition at line 169 of file basketvarianceswap.cpp.

169 {
170 std::vector<string> underlyings, weights;
171 for (auto const& u : underlyings_) {
172 underlyings.push_back(scriptedIndexName(u));
173 QL_REQUIRE(u->weight() != Null<Real>(), "underlying '" << u->name() << "' has no weight");
174 weights.push_back(boost::lexical_cast<std::string>(u->weight()));
175 }
176 indices_.emplace_back("Index", "Underlyings", underlyings);
177 numbers_.emplace_back("Number", "Weights", weights);
178}
std::vector< ScriptedTradeValueTypeData > indices_
QL_DEPRECATED_ENABLE_WARNING std::string scriptedIndexName(const QuantLib::ext::shared_ptr< Underlying > &underlying)
Definition: utilities.cpp:614
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ longShort_

string longShort_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ notional_

string notional_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ strike_

string strike_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ currency_

string currency_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ cap_

string cap_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ floor_

string floor_
private

Definition at line 55 of file basketvarianceswap.hpp.

◆ settlementDate_

string settlementDate_
private

Definition at line 56 of file basketvarianceswap.hpp.

◆ valuationSchedule_

ScheduleData valuationSchedule_
private

Definition at line 57 of file basketvarianceswap.hpp.

◆ squaredPayoff_

bool squaredPayoff_
private

Definition at line 58 of file basketvarianceswap.hpp.

◆ underlyings_

vector<QuantLib::ext::shared_ptr<Underlying> > underlyings_
private

Definition at line 59 of file basketvarianceswap.hpp.