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

#include <ored/portfolio/commodityapo.hpp>

+ Inheritance diagram for CommodityAveragePriceOption:
+ Collaboration diagram for CommodityAveragePriceOption:

Public Member Functions

 CommodityAveragePriceOption ()
 
 CommodityAveragePriceOption (const ore::data::Envelope &envelope, const ore::data::OptionData &optionData, QuantLib::Real quantity, QuantLib::Real strike, const std::string &currency, const std::string &name, CommodityPriceType priceType, const std::string &startDate, const std::string &endDate, const std::string &paymentCalendar, const std::string &paymentLag, const std::string &paymentConvention, const std::string &pricingCalendar, const std::string &paymentDate="", QuantLib::Real gearing=1.0, QuantLib::Spread spread=0.0, QuantExt::CommodityQuantityFrequency commodityQuantityFrequency=QuantExt::CommodityQuantityFrequency::PerCalculationPeriod, CommodityPayRelativeTo commodityPayRelativeTo=CommodityPayRelativeTo::CalculationPeriodEndDate, QuantLib::Natural futureMonthOffset=0, QuantLib::Natural deliveryRollDays=0, bool includePeriodEnd=true, const BarrierData &barrierData={}, const std::string &fxIndex="")
 
void build (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory) override
 
std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 Add underlying Commodity names. More...
 
Inspectors
const ore::data::OptionDataoptionData ()
 
const ore::data::BarrierDatabarrierData ()
 
QuantLib::Real quantity () const
 
QuantLib::Real strike () const
 
const std::string & currency () const
 
const std::string & name () const
 
CommodityPriceType priceType () const
 
const std::string & startDate () const
 
const std::string & endDate () const
 
const std::string & paymentCalendar () const
 
const std::string & paymentLag () const
 
const std::string & paymentConvention () const
 
const std::string & pricingCalendar () const
 
const std::string & paymentDate () const
 
QuantLib::Real gearing () const
 
QuantLib::Spread spread () const
 
QuantExt::CommodityQuantityFrequency commodityQuantityFrequency () const
 
CommodityPayRelativeTo commodityPayRelativeTo () const
 
QuantLib::Natural futureMonthOffset () const
 
QuantLib::Natural deliveryRollDays () const
 
bool includePeriodEnd () const
 
const std::string & fxIndex () const
 
Serialisation
virtual void fromXML (ore::data::XMLNode *node) override
 
virtual ore::data::XMLNodetoXML (ore::data::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...
 
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...
 

Trade

ore::data::OptionData optionData_
 
ore::data::BarrierData barrierData_
 
QuantLib::Real quantity_
 
QuantLib::Real strike_
 
std::string currency_
 
std::string name_
 
CommodityPriceType priceType_
 
std::string startDate_
 
std::string endDate_
 
std::string paymentCalendar_
 
std::string paymentLag_
 
std::string paymentConvention_
 
std::string pricingCalendar_
 
std::string paymentDate_
 
QuantLib::Real gearing_
 
QuantLib::Spread spread_
 
QuantExt::CommodityQuantityFrequency commodityQuantityFrequency_
 
CommodityPayRelativeTo commodityPayRelativeTo_
 
QuantLib::Natural futureMonthOffset_
 
QuantLib::Natural deliveryRollDays_
 
bool includePeriodEnd_
 
std::string fxIndex_
 
bool allAveraging_
 
bool hasCashflows () const override
 
QuantLib::Leg buildLeg (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const std::string &configuration)
 
void buildStandardOption (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const QuantLib::Leg &leg, QuantLib::Date exerciseDate)
 Build a standard option. More...
 
void buildApo (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const QuantLib::Leg &leg, QuantLib::Date exerciseDate, const QuantLib::ext::shared_ptr< ore::data::EngineBuilder > &builder)
 Build an average price option. 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

Serializable Commodity Average Price Option

Definition at line 40 of file commodityapo.hpp.

Constructor & Destructor Documentation

◆ CommodityAveragePriceOption() [1/2]

Definition at line 42 of file commodityapo.hpp.

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

◆ CommodityAveragePriceOption() [2/2]

CommodityAveragePriceOption ( const ore::data::Envelope envelope,
const ore::data::OptionData optionData,
QuantLib::Real  quantity,
QuantLib::Real  strike,
const std::string &  currency,
const std::string &  name,
CommodityPriceType  priceType,
const std::string &  startDate,
const std::string &  endDate,
const std::string &  paymentCalendar,
const std::string &  paymentLag,
const std::string &  paymentConvention,
const std::string &  pricingCalendar,
const std::string &  paymentDate = "",
QuantLib::Real  gearing = 1.0,
QuantLib::Spread  spread = 0.0,
QuantExt::CommodityQuantityFrequency  commodityQuantityFrequency = QuantExt::CommodityQuantityFrequency::PerCalculationPeriod,
CommodityPayRelativeTo  commodityPayRelativeTo = CommodityPayRelativeTo::CalculationPeriodEndDate,
QuantLib::Natural  futureMonthOffset = 0,
QuantLib::Natural  deliveryRollDays = 0,
bool  includePeriodEnd = true,
const BarrierData barrierData = {},
const std::string &  fxIndex = "" 
)

Member Function Documentation

◆ build()

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

Definition at line 63 of file commodityapo.cpp.

63 {
64
65 reset();
66
67
68 DLOG("CommodityAveragePriceOption::build() called for trade " << id());
69
70 // ISDA taxonomy, assuming Commodity follows the Equity template
71 additionalData_["isdaAssetClass"] = std::string("Commodity");
72 additionalData_["isdaBaseProduct"] = std::string("Option");
73 additionalData_["isdaSubProduct"] = std::string("Price Return Basic Performance");
74 // skip the transaction level mapping for now
75 additionalData_["isdaTransaction"] = std::string();
76
77 QL_REQUIRE(gearing_ > 0.0, "Gearing (" << gearing_ << ") should be positive.");
78 QL_REQUIRE(spread_ < strike_ || QuantLib::close_enough(spread_, strike_),
79 "Spread (" << spread_ << ") should be less than strike (" << strike_ << ").");
80
81 additionalData_["quantity"] = quantity_;
82 additionalData_["strike"] = strike_;
83 additionalData_["strikeCurrency"] = currency_;
84
85 // Notional = effective_quantity * effective_strike = (G x Q) x ((K - s) / G) = Q x (K - s)
88
89 // Allow exercise dates not to be specified for an APO. In this case, the exercise date is deduced below when
90 // building the APO or a standard option.
91 Date exDate;
92 if (!optionData_.exerciseDates().empty()) {
93 QL_REQUIRE(optionData_.exerciseDates().size() == 1, "Commodity average price option must be European");
94 exDate = parseDate(optionData_.exerciseDates().front());
95 }
96
97 // Just to get the configuration string for now
98 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(
99 barrierData_.initialized() ? "CommodityAveragePriceBarrierOption" : "CommodityAveragePriceOption");
100 string configuration = builder->configuration(MarketContext::pricing);
101
102 // Build the leg. In this method the allAveraging_ flag is set to true if the future itself is averaging and we
103 // can then use a standard commodity option pricer below.
104 Leg leg = buildLeg(engineFactory, configuration);
105
106 // Based on allAveraging_ flag, set up a standard or averaging commodity option
107 if (allAveraging_) {
108 buildStandardOption(engineFactory, leg, exDate);
109 } else {
110 buildApo(engineFactory, leg, exDate, builder);
111 }
112
113 // Add leg to legs_ so that fixings method can work.
114 legs_.push_back(leg);
115 legPayers_.push_back(false);
116 legCurrencies_.push_back(currency_);
117}
bool initialized() const
Definition: barrierdata.hpp:52
void buildApo(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const QuantLib::Leg &leg, QuantLib::Date exerciseDate, const QuantLib::ext::shared_ptr< ore::data::EngineBuilder > &builder)
Build an average price option.
void buildStandardOption(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const QuantLib::Leg &leg, QuantLib::Date exerciseDate)
Build a standard option.
QuantLib::Leg buildLeg(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory, const std::string &configuration)
const vector< string > & exerciseDates() const
Definition: optiondata.hpp:76
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
QuantLib::Real notional_
Definition: trade.hpp:202
void reset()
Reset trade, clear all base class data. This does not reset accumulated timings for this trade.
Definition: trade.cpp:130
string notionalCurrency_
Definition: trade.hpp:203
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
+ 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 119 of file commodityapo.cpp.

120 {
121 return {{AssetClass::COM, std::set<std::string>({name_})}};
122}

◆ optionData()

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

Definition at line 64 of file commodityapo.hpp.

64{ return optionData_; }

◆ barrierData()

const ore::data::BarrierData & barrierData ( )

Definition at line 65 of file commodityapo.hpp.

65{ return barrierData_; }

◆ quantity()

QuantLib::Real quantity ( ) const

Definition at line 66 of file commodityapo.hpp.

66{ return quantity_; }

◆ strike()

QuantLib::Real strike ( ) const

Definition at line 67 of file commodityapo.hpp.

67{ return strike_; }

◆ currency()

const std::string & currency ( ) const

Definition at line 68 of file commodityapo.hpp.

68{ return currency_; }

◆ name()

const std::string & name ( ) const

Definition at line 69 of file commodityapo.hpp.

69{ return name_; }

◆ priceType()

CommodityPriceType priceType ( ) const

Definition at line 70 of file commodityapo.hpp.

70{ return priceType_; }

◆ startDate()

const std::string & startDate ( ) const

Definition at line 71 of file commodityapo.hpp.

◆ endDate()

const std::string & endDate ( ) const

Definition at line 72 of file commodityapo.hpp.

◆ paymentCalendar()

const std::string & paymentCalendar ( ) const

Definition at line 73 of file commodityapo.hpp.

◆ paymentLag()

const std::string & paymentLag ( ) const

Definition at line 74 of file commodityapo.hpp.

◆ paymentConvention()

const std::string & paymentConvention ( ) const

Definition at line 75 of file commodityapo.hpp.

◆ pricingCalendar()

const std::string & pricingCalendar ( ) const

Definition at line 76 of file commodityapo.hpp.

◆ paymentDate()

const std::string & paymentDate ( ) const

Definition at line 77 of file commodityapo.hpp.

◆ gearing()

QuantLib::Real gearing ( ) const

Definition at line 78 of file commodityapo.hpp.

78{ return gearing_; }

◆ spread()

QuantLib::Spread spread ( ) const

Definition at line 79 of file commodityapo.hpp.

79{ return spread_; }

◆ commodityQuantityFrequency()

QuantExt::CommodityQuantityFrequency commodityQuantityFrequency ( ) const

Definition at line 80 of file commodityapo.hpp.

QuantExt::CommodityQuantityFrequency commodityQuantityFrequency_

◆ commodityPayRelativeTo()

CommodityPayRelativeTo commodityPayRelativeTo ( ) const

Definition at line 81 of file commodityapo.hpp.

CommodityPayRelativeTo commodityPayRelativeTo_

◆ futureMonthOffset()

QuantLib::Natural futureMonthOffset ( ) const

Definition at line 82 of file commodityapo.hpp.

82{ return futureMonthOffset_; }

◆ deliveryRollDays()

QuantLib::Natural deliveryRollDays ( ) const

Definition at line 83 of file commodityapo.hpp.

83{ return deliveryRollDays_; }

◆ includePeriodEnd()

bool includePeriodEnd ( ) const

Definition at line 84 of file commodityapo.hpp.

◆ fxIndex()

const std::string & fxIndex ( ) const

Definition at line 85 of file commodityapo.hpp.

85{ return fxIndex_; }
boost::shared_ptr< FxIndex > fxIndex_

◆ fromXML()

void fromXML ( ore::data::XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 124 of file commodityapo.cpp.

124 {
125
126 Trade::fromXML(node);
127
128 XMLNode* apoNode = XMLUtils::getChildNode(node, "CommodityAveragePriceOptionData");
129 QL_REQUIRE(apoNode, "No CommodityAveragePriceOptionData Node");
130
131 optionData_.fromXML(XMLUtils::getChildNode(apoNode, "OptionData"));
132 if(auto b = XMLUtils::getChildNode(apoNode, "BarrierData")) {
134 }
135 name_ = XMLUtils::getChildValue(apoNode, "Name", true);
136 currency_ = XMLUtils::getChildValue(apoNode, "Currency", true);
137 quantity_ = XMLUtils::getChildValueAsDouble(apoNode, "Quantity", true);
138 strike_ = XMLUtils::getChildValueAsDouble(apoNode, "Strike", true);
139 priceType_ = parseCommodityPriceType(XMLUtils::getChildValue(apoNode, "PriceType", true));
140 startDate_ = XMLUtils::getChildValue(apoNode, "StartDate", true);
141 endDate_ = XMLUtils::getChildValue(apoNode, "EndDate", true);
142 paymentCalendar_ = XMLUtils::getChildValue(apoNode, "PaymentCalendar", true);
143 paymentLag_ = XMLUtils::getChildValue(apoNode, "PaymentLag", true);
144 paymentConvention_ = XMLUtils::getChildValue(apoNode, "PaymentConvention", true);
145 pricingCalendar_ = XMLUtils::getChildValue(apoNode, "PricingCalendar", true);
146
147 paymentDate_ = XMLUtils::getChildValue(apoNode, "PaymentDate", false);
148
149 gearing_ = 1.0;
150 if (XMLNode* n = XMLUtils::getChildNode(apoNode, "Gearing")) {
151 gearing_ = parseReal(XMLUtils::getNodeValue(n));
152 }
153
154 spread_ = XMLUtils::getChildValueAsDouble(apoNode, "Spread", false);
155
156 commodityQuantityFrequency_ = CommodityQuantityFrequency::PerCalculationPeriod;
157 if (XMLNode* n = XMLUtils::getChildNode(apoNode, "CommodityQuantityFrequency")) {
159 }
160
161 commodityPayRelativeTo_ = CommodityPayRelativeTo::CalculationPeriodEndDate;
162 if (XMLNode* n = XMLUtils::getChildNode(apoNode, "CommodityPayRelativeTo")) {
163 commodityPayRelativeTo_ = parseCommodityPayRelativeTo(XMLUtils::getNodeValue(n));
164 }
165
166 futureMonthOffset_ = XMLUtils::getChildValueAsInt(apoNode, "FutureMonthOffset", false);
167 deliveryRollDays_ = XMLUtils::getChildValueAsInt(apoNode, "DeliveryRollDays", false);
168
169 includePeriodEnd_ = true;
170 if (XMLNode* n = XMLUtils::getChildNode(apoNode, "IncludePeriodEnd")) {
171 includePeriodEnd_ = parseBool(XMLUtils::getNodeValue(n));
172 }
173
174 if (XMLNode* n = XMLUtils::getChildNode(apoNode, "FXIndex")){
175 fxIndex_ = XMLUtils::getNodeValue(n);
176 }
177}
virtual void fromXML(ore::data::XMLNode *node) override
Definition: barrierdata.cpp:25
virtual void fromXML(XMLNode *node) override
Definition: optiondata.cpp:32
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
CommodityPriceType parseCommodityPriceType(const string &s)
CommodityPayRelativeTo parseCommodityPayRelativeTo(const string &s)
CommodityQuantityFrequency parseCommodityQuantityFrequency(const string &s)
Convert text to QuantExt::CommodityQuantityFrequency.
Definition: parsers.cpp:1192
+ Here is the call graph for this function:

◆ toXML()

XMLNode * toXML ( ore::data::XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 179 of file commodityapo.cpp.

179 {
180
181 XMLNode* node = Trade::toXML(doc);
182
183 XMLNode* apoNode = doc.allocNode("CommodityAveragePriceOptionData");
184 XMLUtils::appendNode(node, apoNode);
185
186 XMLUtils::appendNode(apoNode, optionData_.toXML(doc));
188 XMLUtils::appendNode(apoNode, barrierData_.toXML(doc));
189 XMLUtils::addChild(doc, apoNode, "Name", name_);
190 XMLUtils::addChild(doc, apoNode, "Currency", currency_);
191 XMLUtils::addChild(doc, apoNode, "Quantity", quantity_);
192 XMLUtils::addChild(doc, apoNode, "Strike", strike_);
193 XMLUtils::addChild(doc, apoNode, "PriceType", to_string(priceType_));
194 XMLUtils::addChild(doc, apoNode, "StartDate", startDate_);
195 XMLUtils::addChild(doc, apoNode, "EndDate", endDate_);
196 XMLUtils::addChild(doc, apoNode, "PaymentCalendar", paymentCalendar_);
197 XMLUtils::addChild(doc, apoNode, "PaymentLag", paymentLag_);
198 XMLUtils::addChild(doc, apoNode, "PaymentConvention", paymentConvention_);
199 XMLUtils::addChild(doc, apoNode, "PricingCalendar", pricingCalendar_);
200 XMLUtils::addChild(doc, apoNode, "PaymentDate", paymentDate_);
201 XMLUtils::addChild(doc, apoNode, "Gearing", gearing_);
202 XMLUtils::addChild(doc, apoNode, "Spread", spread_);
203 XMLUtils::addChild(doc, apoNode, "CommodityQuantityFrequency", to_string(commodityQuantityFrequency_));
204 XMLUtils::addChild(doc, apoNode, "CommodityPayRelativeTo", to_string(commodityPayRelativeTo_));
205 XMLUtils::addChild(doc, apoNode, "FutureMonthOffset", static_cast<int>(futureMonthOffset_));
206 XMLUtils::addChild(doc, apoNode, "DeliveryRollDays", static_cast<int>(deliveryRollDays_));
207 XMLUtils::addChild(doc, apoNode, "IncludePeriodEnd", includePeriodEnd_);
208 if(!fxIndex_.empty()){
209 XMLUtils::addChild(doc, apoNode, "FXIndex", fxIndex_);
210 }
211
212 return node;
213}
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: barrierdata.cpp:49
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: optiondata.cpp:86
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
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 96 of file commodityapo.hpp.

96{ return false; }

◆ buildLeg()

Leg buildLeg ( const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &  engineFactory,
const std::string &  configuration 
)
private

Build a commodity floating leg to extract the single commodity averaging flow

Definition at line 215 of file commodityapo.cpp.

216 {
217
218 // Create the ScheduleData for use in the LegData. Tenor is not needed.
219 ScheduleData scheduleData(ScheduleDates("NullCalendar", "Unadjusted", "", {startDate_, endDate_}));
220
221 // Create the CommodityFloatingLegData. Want to generate a single averaging commodity coupon.
222 vector<Real> quantities{quantity_};
223 vector<string> quantityDates;
224 vector<Real> spreads{spread_};
225 vector<string> spreadDates;
226 vector<Real> gearings{gearing_};
227 vector<string> gearingDates;
228 vector<string> pricingDates;
229 bool isAveraged = true;
230 bool isInArrears = false;
231 QuantLib::ext::shared_ptr<CommodityFloatingLegData> commLegData = QuantLib::ext::make_shared<CommodityFloatingLegData>(
232 name_, priceType_, quantities, quantityDates, commodityQuantityFrequency_, commodityPayRelativeTo_, spreads,
233 spreadDates, gearings, gearingDates, CommodityPricingDateRule::FutureExpiryDate, pricingCalendar_, 0,
234 pricingDates, isAveraged, isInArrears, futureMonthOffset_, deliveryRollDays_, includePeriodEnd_, true,
235 QuantLib::Null<QuantLib::Natural>(), true, "", QuantLib::Null<QuantLib::Natural>(), false, QuantLib::Null<QuantLib::Natural>(),
236 fxIndex_);
237
238 // Create the LegData. All defaults are as in the LegData ctor.
239 vector<string> paymentDates = paymentDate_.empty() ? vector<string>() : vector<string>(1, paymentDate_);
240 LegData legData(commLegData, true, currency_, scheduleData, "", vector<Real>(), vector<string>(),
241 paymentConvention_, false, false, false, true, "", 0, "", vector<AmortizationData>(),
242 paymentLag_, "", paymentCalendar_, paymentDates);
243
244 // Get the leg builder, set the allAveraging_ flag and build the leg
245 auto legBuilder = engineFactory->legBuilder(legData.legType());
246 QuantLib::ext::shared_ptr<CommodityFloatingLegBuilder> cflb =
247 QuantLib::ext::dynamic_pointer_cast<CommodityFloatingLegBuilder>(legBuilder);
248 QL_REQUIRE(cflb, "Expected a CommodityFloatingLegBuilder for leg type " << legData.legType());
249 Leg leg = cflb->buildLeg(legData, engineFactory, requiredFixings_, configuration);
250 allAveraging_ = cflb->allAveraging();
251
252 return leg;
253}
set< Date > pricingDates(const Date &s, const Date &e, const Calendar &pricingCalendar, bool excludeStart, bool includeEnd, bool useBusinessDays)
+ Here is the call graph for this function:

◆ buildStandardOption()

void buildStandardOption ( const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &  engineFactory,
const QuantLib::Leg &  leg,
QuantLib::Date  exerciseDate 
)
private

Build a standard option.

Definition at line 255 of file commodityapo.cpp.

256 {
257
258 QL_REQUIRE(!barrierData_.initialized(), "Commodity APO: standard option does not support barriers");
259
260 // Expect the leg to hold a single commodity averaging cashflow on which the option is written
261 QL_REQUIRE(leg.size() == 1, "Single flow expected but found " << leg.size());
262 auto flow = QuantLib::ext::dynamic_pointer_cast<CommodityIndexedCashFlow>(leg[0]);
263 QL_REQUIRE(flow, "Expected a cashflow of type CommodityIndexedCashFlow");
264
265 // If exercise date is given use it. If not given, take the cashflow's pricing date.
266 if (exerciseDate != Date()) {
267 QL_REQUIRE(exerciseDate >= flow->pricingDate(),
268 "Exercise date, " << io::iso_date(exerciseDate) << ", should be on or after the pricing date, "
269 << io::iso_date(flow->pricingDate()));
270 DLOG("buildStandardOption: explicit exercise date given for APO " << io::iso_date(exerciseDate) << ".");
271 } else {
272 exerciseDate = flow->pricingDate();
273 optionData_.setExerciseDates({to_string(exerciseDate)});
274 DLOG("buildStandardOption: set exercise date on APO to cashflow's pricing date " << io::iso_date(exerciseDate)
275 << ".");
276 }
277 DLOG("buildStandardOption: pricing date on APO is " << io::iso_date(flow->pricingDate()) << ".");
278
279 // Update the optionData_ if necessary
282 DLOG("buildStandardOption: setting automatic exercise to true on APO.");
283 }
284
285 if (!optionData_.paymentData()) {
286 QL_REQUIRE(exerciseDate <= flow->date(), "Exercise date, " << io::iso_date(exerciseDate)
287 << ", should be on or before payment date, "
288 << io::iso_date(flow->date()));
289 string strDate = to_string(flow->date());
290 optionData_.setPaymentData(OptionPaymentData({strDate}));
291 DLOG("buildStandardOption: setting payment date to " << strDate << " on APO.");
292 } else {
293 DLOG("buildStandardOption: using explicitly provided payment data on APO.");
294 }
295
296 // Build the commodity option.
297 TradeStrike effectiveStrike((strike_ - spread_) / gearing_, currency_);
298 Real effectiveQuantity = gearing_ * quantity_;
299 CommodityOption commOption(envelope(), optionData_, name_, currency_, effectiveQuantity, effectiveStrike,
300 flow->index()->isFuturesIndex(), flow->pricingDate());
301 commOption.build(engineFactory);
302 setSensitivityTemplate(commOption.sensitivityTemplate());
303 instrument_ = commOption.instrument();
304 maturity_ = commOption.maturity();
305}
void setPaymentData(const OptionPaymentData &paymentData)
Definition: optiondata.hpp:101
void setAutomaticExercise(bool automaticExercise)
Definition: optiondata.hpp:100
boost::optional< bool > automaticExercise() const
Definition: optiondata.hpp:91
void setExerciseDates(const std::vector< std::string > &exerciseDates)
Definition: optiondata.hpp:98
const boost::optional< OptionPaymentData > & paymentData() const
Definition: optiondata.hpp:93
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
+ Here is the call graph for this function:

◆ buildApo()

void buildApo ( const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &  engineFactory,
const QuantLib::Leg &  leg,
QuantLib::Date  exerciseDate,
const QuantLib::ext::shared_ptr< ore::data::EngineBuilder > &  builder 
)
private

Build an average price option.

Definition at line 307 of file commodityapo.cpp.

308 {
309
310 // Expect the leg to hold a single commodity averaging cashflow on which the option is written
311 QL_REQUIRE(leg.size() == 1, "Single flow expected but found " << leg.size());
312 auto apoFlow = QuantLib::ext::dynamic_pointer_cast<CommodityIndexedAverageCashFlow>(leg[0]);
313 QL_REQUIRE(apoFlow, "Expected a cashflow of type CommodityIndexedAverageCashFlow");
314
315 // Populate relevant Trade members
316 maturity_ = std::max(optionData_.premiumData().latestPremiumDate(), apoFlow->date());
317
318 Date lastApoFixingDate = apoFlow->indices().rbegin()->first;
319
320 // If exercise date is given use it. If not given, take the cashflow's last pricing date.
321 if (exerciseDate != Date()) {
322 QL_REQUIRE(exerciseDate >= lastApoFixingDate, "Exercise date, "
323 << io::iso_date(exerciseDate)
324 << ", should be on or after the last APO fixing date, "
325 << io::iso_date(lastApoFixingDate));
326 DLOG("buildApo: explicit exercise date given for APO " << io::iso_date(exerciseDate) << ".");
327 } else {
328 exerciseDate = lastApoFixingDate;
329 string strDate = to_string(lastApoFixingDate);
330 optionData_.setExerciseDates({strDate});
331 DLOG("buildApo: set exercise date on APO to cashflow's last pricing date " << io::iso_date(lastApoFixingDate)
332 << ".");
333 }
334 DLOG("buildApo: pricing date on APO is " << io::iso_date(lastApoFixingDate) << ".");
335
336 QL_REQUIRE(exerciseDate <= apoFlow->date(), "Exercise date, " << io::iso_date(exerciseDate)
337 << ", should be on or before payment date, "
338 << io::iso_date(apoFlow->date()));
339
340 // If the apo is payout and the underlying are quoted in different currencies, handle the fxIndex
341 QuantLib::ext::shared_ptr<FxIndex> fxIndex;
342 if(!fxIndex_.empty()) {
343 auto underlyingCcy =
344 QuantLib::ext::dynamic_pointer_cast<CommodityIndexedAverageCashFlow>(leg[0])->index()->priceCurve()->currency();
345 // check currencies consistency
346 QL_REQUIRE(npvCurrency_ == underlyingCcy.code() || npvCurrency_ == currency_, "Commodity cross-currency APO: inconsistent currencies in trade.");
347
348 // only add an fx index if underlying currency differs from trade currency
349 if (npvCurrency_ != underlyingCcy.code()) {
350 fxIndex = buildFxIndex(fxIndex_, npvCurrency_, underlyingCcy.code(), engineFactory->market(),
351 engineFactory->configuration(MarketContext::pricing));
352 for (auto cf : leg) { // request appropriate fixings
353 auto cacf = QuantLib::ext::dynamic_pointer_cast<CommodityIndexedAverageCashFlow>(cf);
354 for (auto kv : cacf->indices()) {
355 if (!fxIndex->fixingCalendar().isBusinessDay(
356 kv.first)) { // If fx index is not available for the commodity pricing day,
357 // this ensures to require the previous valid one which will be used in pricing
358 // from fxIndex()->fixing(...)
359 Date adjustedFixingDate = fxIndex->fixingCalendar().adjust(kv.first, Preceding);
360 requiredFixings_.addFixingDate(adjustedFixingDate, fxIndex_);
361
362 } else {
364 }
365 }
366 }
367 }
368 }
369
370 // get barrier info
371 Real barrierLevel = Null<Real>();
372 Barrier::Type barrierType = Barrier::DownIn;
373 Exercise::Type barrierStyle = Exercise::American;
375 QL_REQUIRE(barrierData_.levels().size() == 1, "Commodity APO: Expected exactly one barrier level.");
376 barrierLevel = barrierData_.levels().front().value();
378 if (!barrierData_.style().empty()) {
380 QL_REQUIRE(barrierStyle == Exercise::European || barrierStyle == Exercise::American,
381 "Commodity APO: Expected 'European' or 'American' as barrier style");
382 }
383 }
384
385 // Create the APO instrument
386 QuantLib::ext::shared_ptr<QuantLib::Exercise> exercise = QuantLib::ext::make_shared<EuropeanExercise>(exerciseDate);
387 auto apo = QuantLib::ext::make_shared<QuantExt::CommodityAveragePriceOption>(
388 apoFlow, exercise, apoFlow->periodQuantity(), strike_, parseOptionType(optionData_.callPut()),
389 Settlement::Physical, Settlement::PhysicalOTC, barrierLevel, barrierType, barrierStyle, fxIndex);
390
391 // Set the pricing engine
392 Currency ccy = parseCurrency(currency_);
393 auto engineBuilder = QuantLib::ext::dynamic_pointer_cast<CommodityApoBaseEngineBuilder>(builder);
394 QuantLib::ext::shared_ptr<PricingEngine> engine = engineBuilder->engine(ccy, name_, id(), apo);
395 apo->setPricingEngine(engine);
396 setSensitivityTemplate(*engineBuilder);
397
398 // position type and trade multiplier
399 Position::Type positionType = parsePositionType(optionData_.longShort());
400 Real multiplier = positionType == Position::Long ? 1.0 : -1.0;
401
402 // Take care of fees
403 vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
404 vector<Real> additionalMultipliers;
405 addPremiums(additionalInstruments, additionalMultipliers, multiplier, optionData_.premiumData(),
406 positionType == Position::Long ? -1.0 : 1.0, ccy, engineFactory,
407 engineBuilder->configuration(MarketContext::pricing));
408
409 // Populate instrument wrapper
410 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(apo, multiplier, additionalInstruments, additionalMultipliers);
411}
const boost::shared_ptr< FxIndex > & fxIndex() const
Exercise::Type barrierStyle() const
const std::string & type() const
Definition: barrierdata.hpp:46
std::vector< ore::data::TradeBarrier > levels() const
Definition: barrierdata.hpp:50
const std::string & style() const
Definition: barrierdata.hpp:51
const string & callPut() const
Definition: optiondata.hpp:71
const string & longShort() const
Definition: optiondata.hpp:70
const PremiumData & premiumData() const
Definition: optiondata.hpp:83
QuantLib::Date latestPremiumDate() const
Definition: premiumdata.cpp:28
void addFixingDate(const QuantLib::Date &fixingDate, const std::string &indexName, const QuantLib::Date &payDate=Date::maxDate(), const bool alwaysAddIfPaysOnSettlement=false, const bool mandatoryFixing=true)
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
RequiredFixings requiredFixings_
Definition: trade.hpp:223
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
Barrier::Type parseBarrierType(const std::string &s)
Convert std::string to QuantLib::BarrierType.
Definition: parsers.cpp:1042
Option::Type parseOptionType(const std::string &s)
Convert text to QuantLib::Option::Type.
Definition: parsers.cpp:481
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:

Member Data Documentation

◆ optionData_

ore::data::OptionData optionData_
private

Definition at line 100 of file commodityapo.hpp.

◆ barrierData_

ore::data::BarrierData barrierData_
private

Definition at line 101 of file commodityapo.hpp.

◆ quantity_

QuantLib::Real quantity_
private

Definition at line 102 of file commodityapo.hpp.

◆ strike_

QuantLib::Real strike_
private

Definition at line 103 of file commodityapo.hpp.

◆ currency_

std::string currency_
private

Definition at line 104 of file commodityapo.hpp.

◆ name_

std::string name_
private

Definition at line 105 of file commodityapo.hpp.

◆ priceType_

CommodityPriceType priceType_
private

Definition at line 106 of file commodityapo.hpp.

◆ startDate_

std::string startDate_
private

Definition at line 107 of file commodityapo.hpp.

◆ endDate_

std::string endDate_
private

Definition at line 108 of file commodityapo.hpp.

◆ paymentCalendar_

std::string paymentCalendar_
private

Definition at line 109 of file commodityapo.hpp.

◆ paymentLag_

std::string paymentLag_
private

Definition at line 110 of file commodityapo.hpp.

◆ paymentConvention_

std::string paymentConvention_
private

Definition at line 111 of file commodityapo.hpp.

◆ pricingCalendar_

std::string pricingCalendar_
private

Definition at line 112 of file commodityapo.hpp.

◆ paymentDate_

std::string paymentDate_
private

Definition at line 113 of file commodityapo.hpp.

◆ gearing_

QuantLib::Real gearing_
private

Definition at line 114 of file commodityapo.hpp.

◆ spread_

QuantLib::Spread spread_
private

Definition at line 115 of file commodityapo.hpp.

◆ commodityQuantityFrequency_

QuantExt::CommodityQuantityFrequency commodityQuantityFrequency_
private

Definition at line 116 of file commodityapo.hpp.

◆ commodityPayRelativeTo_

CommodityPayRelativeTo commodityPayRelativeTo_
private

Definition at line 117 of file commodityapo.hpp.

◆ futureMonthOffset_

QuantLib::Natural futureMonthOffset_
private

Definition at line 118 of file commodityapo.hpp.

◆ deliveryRollDays_

QuantLib::Natural deliveryRollDays_
private

Definition at line 119 of file commodityapo.hpp.

◆ includePeriodEnd_

bool includePeriodEnd_
private

Definition at line 120 of file commodityapo.hpp.

◆ fxIndex_

std::string fxIndex_
private

Definition at line 121 of file commodityapo.hpp.

◆ allAveraging_

bool allAveraging_
private

Flag indicating if the commodity contract itself is averaging. This is used to decide if we build a standard non-averaging commodity option or an averaging commodity option.

Definition at line 126 of file commodityapo.hpp.