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

#include <ored/portfolio/multilegoption.hpp>

+ Inheritance diagram for MultiLegOption:
+ Collaboration diagram for MultiLegOption:

Public Member Functions

 MultiLegOption ()
 
 MultiLegOption (const Envelope &env, const vector< LegData > &underlyingData)
 
 MultiLegOption (const Envelope &env, const OptionData &optionData, const vector< LegData > &underlyingData)
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 
const OptionDataoption ()
 
const boolhasOption ()
 
const vector< LegData > & underlying ()
 
void fromXML (XMLNode *node) override
 
XMLNodetoXML (XMLDocument &doc) const override
 
- 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
 
virtual QuantLib::Real notional () const
 Return the current notional in npvCurrency. See individual sub-classes for the precise definition. More...
 
virtual string notionalCurrency () 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 Attributes

OptionData optionData_
 
bool hasOption_
 
vector< LegDataunderlyingData_
 

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 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 32 of file multilegoption.hpp.

Constructor & Destructor Documentation

◆ MultiLegOption() [1/3]

Definition at line 35 of file multilegoption.hpp.

35: Trade("MultiLegOption"), hasOption_(false) {}
Trade()
Default constructor.
Definition: trade.hpp:59

◆ MultiLegOption() [2/3]

MultiLegOption ( const Envelope env,
const vector< LegData > &  underlyingData 
)

Definition at line 38 of file multilegoption.hpp.

39 : Trade("MultiLegOption", env), hasOption_(false), underlyingData_(underlyingData) {}
vector< LegData > underlyingData_

◆ MultiLegOption() [3/3]

MultiLegOption ( const Envelope env,
const OptionData optionData,
const vector< LegData > &  underlyingData 
)

Definition at line 42 of file multilegoption.hpp.

43 : Trade("MultiLegOption", env), optionData_(optionData), hasOption_(true), underlyingData_(underlyingData) {}

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 37 of file multilegoption.cpp.

37 {
38
39 DLOG("Building MultiLegOption " << id());
40
41 QL_REQUIRE(!underlyingData_.empty(), "MultiLegOption: no underlying given");
42
43 Position::Type positionType = Position::Long;
44 Settlement::Type settleType = Settlement::Cash;
45 if (hasOption()) {
46 Exercise::Type exerciseType = parseExerciseType(optionData_.style());
47 QL_REQUIRE(exerciseType == Exercise::Bermudan || exerciseType == Exercise::European,
48 "MultiLegOption: exercise type must be bermudan or european");
50 positionType = parsePositionType(optionData_.longShort());
51 }
52
53 Date today = Settings::instance().evaluationDate();
54 Real multiplier = positionType == Position::Long ? 1.0 : -1.0;
55
56 auto builder =
57 QuantLib::ext::dynamic_pointer_cast<MultiLegOptionEngineBuilderBase>(engineFactory->builder("MultiLegOption"));
58 QL_REQUIRE(builder != nullptr, "wrong builder, expected multi leg option engine builder");
59
60 // build underlying legs
61
62 // TODO support notional exchanges, fx resets, more leg types
63 vector<Leg> underlyingLegs;
64 vector<bool> underlyingPayers;
65 vector<Currency> underlyingCurrencies;
66 legCurrencies_.clear();
67
68 for (Size i = 0; i < underlyingData_.size(); ++i) {
69 auto legBuilder = engineFactory->legBuilder(underlyingData_[i].legType());
70 underlyingLegs.push_back(legBuilder->buildLeg(underlyingData_[i], engineFactory, requiredFixings_,
71 builder->configuration(MarketContext::pricing)));
72 underlyingCurrencies.push_back(parseCurrency(underlyingData_[i].currency()));
73 legCurrencies_.push_back(underlyingData_[i].currency());
74 underlyingPayers.push_back(underlyingData_[i].isPayer());
75 DLOG("Added leg of type " << underlyingData_[i].legType() << " in currency " << underlyingData_[i].currency()
76 << " is payer " << underlyingData_[i].isPayer());
77 }
78
79 // build exercise option
80
81 QuantLib::ext::shared_ptr<Exercise> exercise;
82 vector<Date> exDates;
83 if (hasOption()) {
84 for (Size i = 0; i < optionData_.exerciseDates().size(); ++i) {
85 Date exDate = parseDate(optionData_.exerciseDates()[i]);
86 if (exDate > Settings::instance().evaluationDate())
87 exDates.push_back(exDate);
88 }
89 }
90 if (!exDates.empty()) {
91 std::sort(exDates.begin(), exDates.end());
92 exercise = QuantLib::ext::make_shared<BermudanExercise>(exDates);
93 DLOG("Added exercise with " << exDates.size() << " alive exercise dates.");
94 } else {
95 DLOG("No exercise added, instrument is equal to the underlying");
96 }
97
98 // build instrument
99
100 auto multiLegOption = QuantLib::ext::make_shared<QuantExt::MultiLegOption>(underlyingLegs, underlyingPayers,
101 underlyingCurrencies, exercise, settleType);
102
103 DLOG("QLE Instrument built.")
104
105 // extract underlying fixing dates and indices (needed in the engine builder for model calibration below)
106 // also add currencies from indices
107 vector<Date> underlyingFixingDates;
108 vector<QuantLib::ext::shared_ptr<InterestRateIndex>> underlyingIndices;
109 vector<Currency> allCurrencies;
110 for (auto const& c : underlyingCurrencies) {
111 if (std::find(allCurrencies.begin(), allCurrencies.end(), c) == allCurrencies.end()) {
112 allCurrencies.push_back(c);
113 }
114 }
115 for (auto const& l : underlyingLegs) {
116 for (auto const& c : l) {
117 auto flr = QuantLib::ext::dynamic_pointer_cast<QuantLib::FloatingRateCoupon>(c);
118 auto cfc = QuantLib::ext::dynamic_pointer_cast<QuantLib::CappedFlooredCoupon>(c);
119 if (cfc != nullptr)
120 flr = cfc->underlying();
121 if (flr != nullptr) {
122 if (flr->fixingDate() > today) {
123 underlyingFixingDates.push_back(flr->fixingDate());
124 underlyingIndices.push_back(flr->index());
125 if (std::find(allCurrencies.begin(), allCurrencies.end(), flr->index()->currency()) ==
126 allCurrencies.end())
127 allCurrencies.push_back(flr->index()->currency());
128 }
129 }
130 }
131 }
132
133 DLOG("Extracted underlying currencies, indices and fixing dates.")
134
135 std::vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
136 std::vector<Real> additionalMultipliers;
137 Date lastPremiumDate = addPremiums(additionalInstruments, additionalMultipliers, multiplier,
138 optionData_.premiumData(), -multiplier, parseCurrency(legCurrencies_.front()),
139 engineFactory, builder->configuration(MarketContext::pricing));
140
141 // get engine and assign it
142
143 std::sort(allCurrencies.begin(), allCurrencies.end(),
144 [](const Currency& a, const Currency& b) { return a.code() < b.code(); });
145 auto engine = builder->engine(id(), exDates, multiLegOption->maturityDate(), allCurrencies, underlyingFixingDates,
146 underlyingIndices);
147 multiLegOption->setPricingEngine(engine);
148 setSensitivityTemplate(*builder);
149
150 DLOG("Pricing engine set.")
151
152 // build instrument
153 // WARNING: we don't support an option wrapper here, i.e. the vanilla simulation will not work properly
154 instrument_ =
155 QuantLib::ext::make_shared<VanillaInstrument>(multiLegOption, multiplier, additionalInstruments, additionalMultipliers);
156
157 // popular trade members
158
159 legs_ = underlyingLegs;
160 legPayers_ = underlyingPayers;
161 notional_ = currentNotional(legs_.front());
162 maturity_ = std::max(lastPremiumDate, multiLegOption->maturityDate());
163 // npv currency is base currency of the pricing model
164 auto moe = QuantLib::ext::dynamic_pointer_cast<McMultiLegOptionEngine>(engine);
165 QL_REQUIRE(moe != nullptr, "MultiLegOption::build(): expected McMultiLegOptionEngine from engine builder");
166 npvCurrency_ = moe->model()->irlgm1f(0)->currency().code();
167
168 DLOG("Building MultiLegOption done");
169} // build
const Date & maturityDate() const
const std::vector< Leg > legs_
const string & longShort() const
Definition: optiondata.hpp:70
const string & style() const
Definition: optiondata.hpp:74
const string & settlement() const
Definition: optiondata.hpp:81
const vector< string > & exerciseDates() const
Definition: optiondata.hpp:76
std::vector< string > legCurrencies_
Definition: trade.hpp:199
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
Exercise::Type parseExerciseType(const std::string &s)
Convert text to QuantLib::Exercise::Type.
Definition: parsers.cpp:466
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
Settlement::Type parseSettlementType(const std::string &s)
Convert text to QuantLib::Settlement::Type.
Definition: parsers.cpp:434
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Size size(const ValueType &v)
Definition: value.cpp:145
+ Here is the call graph for this function:

◆ option()

const OptionData & option ( )

Definition at line 47 of file multilegoption.hpp.

47{ return optionData_; }

◆ hasOption()

const bool & hasOption ( )

Definition at line 48 of file multilegoption.hpp.

48{ return hasOption_; }

◆ underlying()

const vector< LegData > & underlying ( )

Definition at line 49 of file multilegoption.hpp.

49{ return underlyingData_; }

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 171 of file multilegoption.cpp.

171 {
172 Trade::fromXML(node);
173 underlyingData_.clear();
174 optionData_ = OptionData();
175 hasOption_ = false;
176 XMLNode* n0 = XMLUtils::getChildNode(node, "MultiLegOptionData");
177 XMLNode* n1 = XMLUtils::getChildNode(n0, "OptionData");
178 if (n1 != nullptr) {
180 hasOption_ = true;
181 }
182 vector<XMLNode*> nodes = XMLUtils::getChildrenNodes(n0, "LegData");
183 for (auto const n : nodes) {
184 LegData ld;
185 ld.fromXML(n);
186 underlyingData_.push_back(ld);
187 }
188}
virtual void fromXML(XMLNode *node) override
Definition: optiondata.cpp:32
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 190 of file multilegoption.cpp.

190 {
191 XMLNode* node = Trade::toXML(doc);
192 XMLNode* n0 = doc.allocNode("MultiLegOptionData");
193 XMLUtils::appendNode(node, n0);
194 if (hasOption_) {
195 XMLUtils::appendNode(n0, optionData_.toXML(doc));
196 for (auto& d : underlyingData_) {
197 XMLUtils::appendNode(n0, d.toXML(doc));
198 }
199 }
200 return node;
201}
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: optiondata.cpp:86
+ Here is the call graph for this function:

Member Data Documentation

◆ optionData_

OptionData optionData_
private

Definition at line 55 of file multilegoption.hpp.

◆ hasOption_

bool hasOption_
private

Definition at line 56 of file multilegoption.hpp.

◆ underlyingData_

vector<LegData> underlyingData_
private

Definition at line 57 of file multilegoption.hpp.