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

Commodity digital option trade representation as call spread. More...

#include <ored/portfolio/commoditydigitaloption.hpp>

+ Inheritance diagram for CommodityDigitalOption:
+ Collaboration diagram for CommodityDigitalOption:

Public Member Functions

 CommodityDigitalOption ()
 Default constructor. More...
 
 CommodityDigitalOption (const Envelope &env, const OptionData &optionData, const std::string &commodityName, const std::string &currency, QuantLib::Real strike, QuantLib::Real payoff, const boost::optional< bool > &isFuturePrice=boost::none, const QuantLib::Date &futureExpiryDate=QuantLib::Date())
 Detailed constructor. More...
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory) override
 Build underlying instrument and link pricing engine. More...
 
std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 Add underlying Commodity names. More...
 
Serialisation
virtual void fromXML (XMLNode *node) override
 
virtual XMLNodetoXML (XMLDocument &doc) const override
 
Trade
bool hasCashflows () 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...
 
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

OptionData optionData_
 
std::string name_
 
string currency_
 
Real strike_
 
Real payoff_
 
boost::optional< boolisFuturePrice_
 
QuantLib::Date futureExpiryDate_
 
QuantLib::ext::shared_ptr< QuantLib::Index > index_
 An index is needed if the option is to be automatically exercised on expiry. More...
 
std::string indexName_
 Hold the external index name if needed e.g. in the case of an FX index. More...
 
QuantLib::Date expiryDate_
 Store the option expiry date. More...
 
Date forwardDate_
 
const boost::optional< bool > & isFuturePrice () const
 
const QuantLib::Date & futureExpiryDate () const
 

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

Commodity digital option trade representation as call spread.

Definition at line 35 of file commoditydigitaloption.hpp.

Constructor & Destructor Documentation

◆ CommodityDigitalOption() [1/2]

Default constructor.

Definition at line 41 of file commoditydigitaloption.cpp.

41{ tradeType_ = "CommodityDigitalOption"; }
string tradeType_
Definition: trade.hpp:196

◆ CommodityDigitalOption() [2/2]

CommodityDigitalOption ( const Envelope env,
const OptionData optionData,
const std::string &  commodityName,
const std::string &  currency,
QuantLib::Real  strike,
QuantLib::Real  payoff,
const boost::optional< bool > &  isFuturePrice = boost::none,
const QuantLib::Date &  futureExpiryDate = QuantLib::Date() 
)

Detailed constructor.

Member Function Documentation

◆ build()

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

Build underlying instrument and link pricing engine.

Implements Trade.

Definition at line 51 of file commoditydigitaloption.cpp.

51 {
52
53 // ISDA taxonomy, assuming Commodity follows the Equity template
54 additionalData_["isdaAssetClass"] = std::string("Commodity");
55 additionalData_["isdaBaseProduct"] = std::string("Option");
56 additionalData_["isdaSubProduct"] = std::string("Price Return Basic Performance");
57 // skip the transaction level mapping for now
58 additionalData_["isdaTransaction"] = std::string("");
59
60 // Checks
61 QL_REQUIRE((strike_ > 0 || close_enough(strike_, 0.0)), "Commodity digital option requires a positive strike");
62 QL_REQUIRE(optionData_.exerciseDates().size() == 1, "Invalid number of exercise dates");
63
65
66 // Populate the index_ in case the option is automatic exercise.
67 // Intentionally use null calendar because we will ask for index value on the expiry date without adjustment.
68 const QuantLib::ext::shared_ptr<Market>& market = engineFactory->market();
69 index_ = *market->commodityIndex(name_, engineFactory->configuration(MarketContext::pricing));
71
72 // Assume future price if isFuturePrice_ is not explicitly set or if it is and true.
73 auto index = *market->commodityIndex(name_, engineFactory->configuration(MarketContext::pricing));
74
75 // If we are given an explicit future contract expiry date, use it, otherwise use option's expiry.
76 Date expiryDate;
77 if (futureExpiryDate_ != Date()) {
78 expiryDate = futureExpiryDate_;
79 } else {
80 // Get the expiry date of the option. This is the expiry date of the commodity future index.
81 const vector<string>& expiryDates = optionData_.exerciseDates();
82 QL_REQUIRE(expiryDates.size() == 1,
83 "Expected exactly one expiry date for CommodityDigitalOption but got " << expiryDates.size() << ".");
84 expiryDate = parseDate(expiryDates[0]);
85 }
86
87 // Clone the index with the relevant expiry date.
88 index_ = index->clone(expiryDate);
89
90 // Set the VanillaOptionTrade forwardDate_ if the index is a CommodityFuturesIndex - we possibly still have a
91 // CommoditySpotIndex at this point so check. Also, will only work for European exercise.
93 QL_REQUIRE(et == Exercise::European, "European style expected for CommodityDigitalOption");
94 if (et == Exercise::European && QuantLib::ext::dynamic_pointer_cast<CommodityFuturesIndex>(index_)) {
95 forwardDate_ = expiryDate;
96 }
97 }
98
99 // Build digital as call or put spread
100 Real strikeSpread = strike_ * 0.01; // FIXME, what is a usual spread here, and where should we put it?
101 Real strike1 = strike_ - strikeSpread/2;
102 Real strike2 = strike_ + strikeSpread/2;
103 CommodityOption opt1(envelope(), optionData_, name_, currency_, 1.0, TradeStrike(strike1, currency_), isFuturePrice_, futureExpiryDate_);
104 CommodityOption opt2(envelope(), optionData_, name_, currency_, 1.0, TradeStrike(strike2, currency_),
106 opt1.build(engineFactory);
107 opt2.build(engineFactory);
108 QuantLib::ext::shared_ptr<Instrument> inst1 = opt1.instrument()->qlInstrument();
109 QuantLib::ext::shared_ptr<Instrument> inst2 = opt2.instrument()->qlInstrument();
110
111 setSensitivityTemplate(opt1.sensitivityTemplate());
112
113 QuantLib::ext::shared_ptr<CompositeInstrument> composite = QuantLib::ext::make_shared<CompositeInstrument>();
114 // add and subtract such that the long call spread and long put spread have positive values
115 if (optionData_.callPut() == "Call") {
116 composite->add(inst1);
117 composite->subtract(inst2);
118 }
119 else if (optionData_.callPut() == "Put") {
120 composite->add(inst2);
121 composite->subtract(inst1);
122 }
123 else {
124 QL_FAIL("OptionType Call or Put required in CommodityDigitalOption " << id());
125 }
126
127 Position::Type positionType = parsePositionType(optionData_.longShort());
128 Real bsIndicator = (positionType == QuantLib::Position::Long ? 1.0 : -1.0);
129 Real multiplier = payoff_ * bsIndicator / strikeSpread;
130 std::vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
131 std::vector<Real> additionalMultipliers;
132 // FIXME: Do we need to retrieve the engine builder's configuration
133 string configuration = Market::defaultConfiguration;
134 Currency ccy = parseCurrencyWithMinors(currency_);
135 maturity_ = std::max(expiryDate_, addPremiums(additionalInstruments, additionalMultipliers, multiplier,
136 optionData_.premiumData(), -bsIndicator, ccy, engineFactory, configuration));
137
138 instrument_ = QuantLib::ext::shared_ptr<InstrumentWrapper>(
139 new VanillaInstrument(composite, multiplier, additionalInstruments, additionalMultipliers));
140
144
145 // LOG the volatility if the trade expiry date is in the future.
146 if (expiryDate_ > Settings::instance().evaluationDate()) {
147 DLOG("Implied vol for " << tradeType_ << " on " << name_ << " with expiry " << expiryDate_
148 << " and strike " << strike_ << " is "
149 << market->commodityVolatility(name_)->blackVol(expiryDate_, strike_));
150 }
151
152 additionalData_["payoff"] = payoff_;
153 additionalData_["strike"] = strike_;
154 additionalData_["optionType"] = optionData_.callPut();
155 additionalData_["strikeCurrency"] = currency_;
156}
QuantLib::Date expiryDate_
Store the option expiry date.
QuantLib::ext::shared_ptr< QuantLib::Index > index_
An index is needed if the option is to be automatically exercised on expiry.
static const string defaultConfiguration
Default configuration label.
Definition: market.hpp:296
const string & callPut() const
Definition: optiondata.hpp:71
const string & longShort() const
Definition: optiondata.hpp:70
const string & style() const
Definition: optiondata.hpp:74
const PremiumData & premiumData() const
Definition: optiondata.hpp:83
const vector< string > & exerciseDates() const
Definition: optiondata.hpp:76
string npvCurrency_
Definition: trade.hpp:201
QuantLib::Real notional_
Definition: trade.hpp:202
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
const Envelope & envelope() const
Definition: trade.hpp:135
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
string notionalCurrency_
Definition: trade.hpp:203
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 parseCurrencyWithMinors(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:310
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function:

◆ underlyingIndices()

std::map< 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 159 of file commoditydigitaloption.cpp.

159 {
160 return {{AssetClass::COM, std::set<std::string>({name_})}};
161}

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 163 of file commoditydigitaloption.cpp.

163 {
164
165 Trade::fromXML(node);
166
167 XMLNode* commodityNode = XMLUtils::getChildNode(node, "CommodityDigitalOptionData");
168 QL_REQUIRE(commodityNode, "A commodity option needs a 'CommodityDigitalOptionData' node");
169
170 optionData_.fromXML(XMLUtils::getChildNode(commodityNode, "OptionData"));
171
172 name_ = XMLUtils::getChildValue(commodityNode, "Name", true);
173 currency_ = XMLUtils::getChildValue(commodityNode, "Currency", true);
174 strike_ = XMLUtils::getChildValueAsDouble(commodityNode, "Strike", true);
175 payoff_ = XMLUtils::getChildValueAsDouble(commodityNode, "Payoff", true);
176
177 isFuturePrice_ = boost::none;
178 if (XMLNode* n = XMLUtils::getChildNode(commodityNode, "IsFuturePrice"))
180
181 futureExpiryDate_ = Date();
182 if (XMLNode* n = XMLUtils::getChildNode(commodityNode, "FutureExpiryDate"))
184}
virtual void fromXML(XMLNode *node) override
Definition: optiondata.cpp:32
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
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 string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
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 186 of file commoditydigitaloption.cpp.

186 {
187
188 XMLNode* node = Trade::toXML(doc);
189
190 XMLNode* commodityNode = doc.allocNode("CommodityDigitalOptionData");
191 XMLUtils::appendNode(node, commodityNode);
192
193 XMLUtils::appendNode(commodityNode, optionData_.toXML(doc));
194
195 XMLUtils::addChild(doc, commodityNode, "Name", name_);
196 XMLUtils::addChild(doc, commodityNode, "Currency", currency_);
197 XMLUtils::addChild(doc, commodityNode, "Strike", strike_);
198 XMLUtils::addChild(doc, commodityNode, "Payoff", payoff_);
199
200 if (isFuturePrice_)
201 XMLUtils::addChild(doc, commodityNode, "IsFuturePrice", *isFuturePrice_);
202
203 if (futureExpiryDate_ != Date())
204 XMLUtils::addChild(doc, commodityNode, "FutureExpiryDate", to_string(futureExpiryDate_));
205
206 return node;
207}
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: optiondata.cpp:86
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
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
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:

◆ hasCashflows()

bool hasCashflows ( ) const
overridevirtual

Utility method indicating if the trade has cashflows for the cashflow report. The default implementation returns true so that a trade is automatically considered when cashflows are being written. To prevent a trade from being asked for its cashflows, the method can be overridden to return false.

Reimplemented from Trade.

Definition at line 61 of file commoditydigitaloption.hpp.

61{ return false; }

◆ isFuturePrice()

const boost::optional< bool > & isFuturePrice ( ) const

Definition at line 66 of file commoditydigitaloption.hpp.

66{ return isFuturePrice_; }

◆ futureExpiryDate()

const QuantLib::Date & futureExpiryDate ( ) const

Definition at line 67 of file commoditydigitaloption.hpp.

67{ return futureExpiryDate_; }

Member Data Documentation

◆ optionData_

OptionData optionData_
private

Definition at line 71 of file commoditydigitaloption.hpp.

◆ name_

std::string name_
private

Definition at line 72 of file commoditydigitaloption.hpp.

◆ currency_

string currency_
private

Definition at line 73 of file commoditydigitaloption.hpp.

◆ strike_

Real strike_
private

Definition at line 74 of file commoditydigitaloption.hpp.

◆ payoff_

Real payoff_
private

Definition at line 75 of file commoditydigitaloption.hpp.

◆ isFuturePrice_

boost::optional<bool> isFuturePrice_
private

Indicates if the option underlying is a commodity future settlement price, true, or a spot price false. If not explicitly set, it is assumed to be true.

Definition at line 80 of file commoditydigitaloption.hpp.

◆ futureExpiryDate_

QuantLib::Date futureExpiryDate_
private

An explicit expiry date for the underlying future contract. This can be used if the option trade references a future contract settlement price and the option's expiry date does not match the future contract expiry date.

Definition at line 85 of file commoditydigitaloption.hpp.

◆ index_

QuantLib::ext::shared_ptr<QuantLib::Index> index_
private

An index is needed if the option is to be automatically exercised on expiry.

Definition at line 88 of file commoditydigitaloption.hpp.

◆ indexName_

std::string indexName_
private

Hold the external index name if needed e.g. in the case of an FX index.

Definition at line 91 of file commoditydigitaloption.hpp.

◆ expiryDate_

QuantLib::Date expiryDate_
private

Store the option expiry date.

Definition at line 94 of file commoditydigitaloption.hpp.

◆ forwardDate_

Date forwardDate_
private

Definition at line 96 of file commoditydigitaloption.hpp.