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

#include <ored/portfolio/commodityspreadoption.hpp>

+ Inheritance diagram for CommoditySpreadOption:
+ Collaboration diagram for CommoditySpreadOption:

Public Member Functions

 CommoditySpreadOption ()
 
 CommoditySpreadOption (const CommoditySpreadOptionData &data)
 
void build (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory) override
 Implement the build method. More...
 
Serialisation
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...
 

Inspectors

CommoditySpreadOptionData csoData_
 
std::vector< std::string > fxIndex_
 
std::vector< std::string > const & fxIndex () const
 
const ore::data::OptionDataoption () const
 
QuantLib::Real strike () const
 
std::map< ore::data::AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 Add underlying Commodity names. More...
 

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 73 of file commodityspreadoption.hpp.

Constructor & Destructor Documentation

◆ CommoditySpreadOption() [1/2]

Definition at line 75 of file commodityspreadoption.hpp.

75: ore::data::Trade("CommoditySpreadOption") {}
Trade base class.
Definition: trade.hpp:55

◆ CommoditySpreadOption() [2/2]

Definition at line 76 of file commodityspreadoption.hpp.

77 : ore::data::Trade("CommoditySpreadOption"), csoData_(data) {}
@ data
Definition: log.hpp:77

Member Function Documentation

◆ build()

void build ( const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &  engineFactory)
override

Implement the build method.

Definition at line 124 of file commodityspreadoption.cpp.

124 {
125
126 DLOG("CommoditySpreadOption::build() called for trade " << id());
127
128 // ISDA taxonomy
129 additionalData_["isdaAssetClass"] = std::string("Commodity");
130 additionalData_["isdaBaseProduct"] = std::string("Other");
131 additionalData_["isdaSubProduct"] = std::string("");
132 // skip the transaction level mapping for now
133 additionalData_["isdaTransaction"] = std::string("");
134
135 reset();
136 auto legData_ = csoData_.legData();
137 auto optionData_ = csoData_.optionData();
138 auto strike_ = csoData_.strike();
139
140 QL_REQUIRE(legData_.size() == 2, "Only two legs supported");
141 QL_REQUIRE(legData_[0].currency() == legData_[1].currency(), "Both legs must have same currency");
142 QL_REQUIRE(legData_[0].isPayer() != legData_[1].isPayer(), "Need one payer and one receiver leg");
143
144 if (!optionData_.style().empty()) {
145 QuantLib::Exercise::Type exerciseType = parseExerciseType(optionData_.style());
146 QL_REQUIRE(exerciseType == QuantLib::Exercise::Type::European, "Only European spread option supported");
147 }
148
149 maturity_ = Date();
150 npvCurrency_ = legData_[0].currency();
151 Size payerLegId = legData_[0].isPayer() ? 0 : 1;
152
153 // build the relevant fxIndexes;
154 std::vector<QuantLib::ext::shared_ptr<QuantExt::FxIndex>> fxIndexes(2, nullptr);
155 Currency ccy = parseCurrency(npvCurrency_);
156 Option::Type optionType = parseOptionType(optionData_.callPut());
157
158 // init engine factory builder
159 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
160 auto engineBuilder = QuantLib::ext::dynamic_pointer_cast<CommoditySpreadOptionEngineBuilder>(builder);
161 // get config
162 auto config = builder->configuration(MarketContext::pricing);
163
164 // set exercise date to the pricing date of the coupon
165 QL_REQUIRE(optionData_.exerciseDates().size() == 0,
166 "Only European spread option supported, expiry date is end_date of the period");
167
168 // Build the commodity legs
169
170 for (Size i = 0; i < legData_.size();
171 ++i) { // The order is important, the first leg is always the long position, the second is the short;
172 legPayers_.push_back(legData_[i].isPayer());
173
174 // build legs
175
176 auto commLegData = (QuantLib::ext::dynamic_pointer_cast<CommodityFloatingLegData>(legData_[i].concreteLegData()));
177 QL_REQUIRE(commLegData, "CommoditySpreadOption leg data should be of type CommodityFloating");
178
179 auto legBuilder = engineFactory->legBuilder(legData_[i].legType());
180 auto cflb = QuantLib::ext::dynamic_pointer_cast<CommodityFloatingLegBuilder>(legBuilder);
181
182 QL_REQUIRE(cflb, "CommoditySpreadOption: Expected a CommodityFloatingLegBuilder for leg "
183 << i << " but got " << legData_[i].legType());
184 Leg leg = cflb->buildLeg(legData_[i], engineFactory, requiredFixings_, config);
185
186 // setup the cf indexes
187 QL_REQUIRE(!leg.empty(), "CommoditySpreadOption: Leg " << i << " has no coupons");
188 auto index = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityCashFlow>(leg.front())->index();
189
190 // check ccy consistency
191 auto underlyingCcy = index->priceCurve()->currency();
192 auto tmpFx = commLegData->fxIndex();
193 if (tmpFx.empty()) {
194 QL_REQUIRE(underlyingCcy.code() == npvCurrency_,
195 "CommoditySpreadOption, inconsistent currencies: Settlement currency is "
196 << npvCurrency_ << ", leg " << i + 1 << " currency " << legData_[i].currency()
197 << ", underlying currency " << underlyingCcy << ", no FxIndex provided");
198 } else {
199 QL_REQUIRE(underlyingCcy.code() != npvCurrency_,
200 "CommoditySpreadOption, inconsistent currencies: Settlement currency is "
201 << npvCurrency_ << ", leg " << i + 1 << " currency " << legData_[i].currency()
202 << ", underlying currency " << underlyingCcy << ", FxIndex " << tmpFx << "provided");
203 auto domestic = npvCurrency_;
204 auto foreign = underlyingCcy.code();
205 fxIndexes[i] = buildFxIndex(tmpFx, domestic, foreign, engineFactory->market(),
206 engineFactory->configuration(MarketContext::pricing));
207 // update required fixings. This is handled automatically in the leg builder in the averaging case
208 if (commLegData->isAveraged()) {
209 for (auto cf : leg) {
210 auto fixingDate = cf->date();
211 if (!fxIndexes[i]->fixingCalendar().isBusinessDay(
212 fixingDate)) { // If fx index is not available for the cashflow pricing day,
213 // this ensures to require the previous valid one which will be used in pricing
214 // from fxIndex()->fixing(...)
215 Date adjustedFixingDate = fxIndexes[i]->fixingCalendar().adjust(fixingDate, Preceding);
216 requiredFixings_.addFixingDate(adjustedFixingDate, tmpFx);
217 } else {
218 requiredFixings_.addFixingDate(fixingDate, tmpFx);
219 }
220 }
221 }
222 }
223 legs_.push_back(leg);
224 legCurrencies_.push_back(legData_[i].currency()); // all legs and cf are priced with the same ccy
225 }
226
227 QL_REQUIRE(legs_[0].size() == legs_[1].size(),
228 "CommoditySpreadOption: the two legs must contain the same number of options.");
229
230 QL_REQUIRE(legs_[0].size() > 0, "CommoditySpreadOption: need at least one option, please check the trade xml");
231
232 Position::Type positionType = parsePositionType(optionData_.longShort());
233 Real bsInd = (positionType == QuantLib::Position::Long ? 1.0 : -1.0);
234
235 QuantLib::ext::shared_ptr<QuantLib::Instrument> firstInstrument;
236 double firstMultiplier = 0.0;
237 vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
238 vector<Real> additionalMultipliers;
239
240 vector<Date> expiryDates;
241 for (size_t i = 0; i < legs_[0].size(); ++i) {
242 auto longFlow = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityCashFlow>(legs_[1 - payerLegId][i]);
243 auto shortFlow = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityCashFlow>(legs_[payerLegId][i]);
244 expiryDates.push_back(std::max(longFlow->lastPricingDate(), shortFlow->lastPricingDate()));
245 }
246
247 auto paymentDateAdjuster = makeOptionPaymentDateAdjuster(csoData_, expiryDates);
248
249 for (size_t i = 0; i < legs_[0].size(); ++i) {
250 auto longFlow = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityCashFlow>(legs_[1 - payerLegId][i]);
251 auto shortFlow = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityCashFlow>(legs_[payerLegId][i]);
252
253 QuantLib::Real quantity = longFlow->periodQuantity();
254
255 QL_REQUIRE(quantity == shortFlow->periodQuantity(), "all cashflows must refer to the same quantity");
256
257 QuantLib::Date expiryDate = expiryDates[i];
258
259 QuantLib::Date paymentDate = longFlow->date();
260
261 QL_REQUIRE(paymentDate == shortFlow->date(),
262 "all cashflows must refer to the same paymentDate, its used as the settlementDate of the option");
263
264 paymentDateAdjuster->updatePaymentDate(expiryDate, paymentDate);
265
266 QL_REQUIRE(paymentDate >= expiryDate, "Payment date must be greater than or equal to expiry date.");
267
268 QuantLib::ext::shared_ptr<Exercise> exercise = QuantLib::ext::make_shared<EuropeanExercise>(expiryDate);
269
270 // maturity gets overwritten every time, and it is ok. If the last option is settled with delay, maturity is set
271 // to the settlement date.
272 maturity_ = maturity_ == Date() ? paymentDate : std::max(maturity_, paymentDate);
273
274 // build the instrument for the i-th cfs
275 QuantLib::ext::shared_ptr<QuantExt::CommoditySpreadOption> spreadOption =
276 QuantLib::ext::make_shared<QuantExt::CommoditySpreadOption>(longFlow, shortFlow, exercise, quantity, strike_,
277 optionType, paymentDate, fxIndexes[1 - payerLegId],
278 fxIndexes[1 - payerLegId]);
279
280 // build and assign the engine
281 QuantLib::ext::shared_ptr<PricingEngine> commoditySpreadOptionEngine =
282 engineBuilder->engine(ccy, longFlow->index(), shortFlow->index(), id());
283 spreadOption->setPricingEngine(commoditySpreadOptionEngine);
284 setSensitivityTemplate(*engineBuilder);
285 if (i > 0) {
286 additionalInstruments.push_back(spreadOption);
287 additionalMultipliers.push_back(bsInd);
288 } else {
289 firstInstrument = spreadOption;
290 firstMultiplier = bsInd;
291 }
292 }
293
294 // Add premium
295 auto configuration = engineBuilder->configuration(MarketContext::pricing);
296 maturity_ = std::max(maturity_, addPremiums(additionalInstruments, additionalMultipliers, firstMultiplier,
297 optionData_.premiumData(), -bsInd, ccy, engineFactory, configuration));
298
299 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(firstInstrument, firstMultiplier, additionalInstruments,
300 additionalMultipliers);
301 if (!optionData_.premiumData().premiumData().empty()) {
302 auto premium = optionData_.premiumData().premiumData().front();
303 additionalData_["premiumAmount"] = -bsInd * premium.amount;
304 additionalData_["premiumPaymentDate"] = premium.payDate;
305 additionalData_["premiumCurrency"] = premium.ccy;
306 }
307}
const std::vector< ore::data::LegData > & legData() const
const ore::data::OptionData & optionData() const
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
std::vector< bool > legPayers_
Definition: trade.hpp:200
std::vector< string > legCurrencies_
Definition: trade.hpp:199
std::vector< QuantLib::Leg > legs_
Definition: trade.hpp:198
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)
Definition: trade.cpp:58
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
RequiredFixings requiredFixings_
Definition: trade.hpp:223
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
void reset()
Reset trade, clear all base class data. This does not reset accumulated timings for this trade.
Definition: trade.cpp:130
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
Exercise::Type parseExerciseType(const std::string &s)
Convert text to QuantLib::Exercise::Type.
Definition: parsers.cpp:466
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
Option::Type parseOptionType(const std::string &s)
Convert text to QuantLib::Option::Type.
Definition: parsers.cpp:481
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
Size size(const ValueType &v)
Definition: value.cpp:145
QuantLib::ext::shared_ptr< OptionPaymentDateAdjuster > makeOptionPaymentDateAdjuster(CommoditySpreadOptionData &optionData, const std::vector< Date > &expiryDates)
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
+ Here is the call graph for this function:

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 327 of file commodityspreadoption.cpp.

327 {
328 Trade::fromXML(node);
329 XMLNode* csoNode = XMLUtils::getChildNode(node, "CommoditySpreadOptionData");
330 csoData_.fromXML(csoNode);
331}
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 332 of file commodityspreadoption.cpp.

332 {
333 XMLNode* node = Trade::toXML(doc);
334 auto csoNode = csoData_.toXML(doc);
335 XMLUtils::appendNode(node, csoNode);
336 return node;
337}
XMLNode * toXML(XMLDocument &doc) const override

◆ fxIndex()

std::vector< std::string > const & fxIndex ( ) const

Definition at line 90 of file commodityspreadoption.hpp.

90{ return fxIndex_; }

◆ option()

const ore::data::OptionData & option ( ) const

Definition at line 91 of file commodityspreadoption.hpp.

91{ return csoData_.optionData(); }
+ Here is the call graph for this function:

◆ strike()

QuantLib::Real strike ( ) const

Definition at line 92 of file commodityspreadoption.hpp.

92{ return csoData_.strike(); }
+ Here is the call graph for this function:

◆ underlyingIndices()

std::map< ore::data::AssetClass, std::set< std::string > > underlyingIndices ( const QuantLib::ext::shared_ptr< ReferenceDataManager > &  referenceDataManager = nullptr) const
overridevirtual

Add underlying Commodity names.

Reimplemented from Trade.

Definition at line 310 of file commodityspreadoption.cpp.

310 {
311 std::map<ore::data::AssetClass, std::set<std::string>> result;
312 auto legData = csoData_.legData();
313 for (const auto& leg : legData) {
314 set<string> indices = leg.indices();
315 for (auto ind : indices) {
316 QuantLib::ext::shared_ptr<Index> index = parseIndex(ind);
317 // only handle commodity
318 if (auto ci = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityIndex>(index)) {
319 result[ore::data::AssetClass::COM].insert(ci->name());
320 }
321 }
322 }
323 return result;
324}
QuantLib::ext::shared_ptr< Index > parseIndex(const string &s)
Convert std::string to QuantLib::Index.
+ Here is the call graph for this function:

Member Data Documentation

◆ csoData_

CommoditySpreadOptionData csoData_
private

Definition at line 101 of file commodityspreadoption.hpp.

◆ fxIndex_

std::vector<std::string> fxIndex_
private

Definition at line 102 of file commodityspreadoption.hpp.