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

Serializable Commodity Position. More...

#include <ored/portfolio/commodityposition.hpp>

+ Inheritance diagram for CommodityPosition:
+ Collaboration diagram for CommodityPosition:

Public Member Functions

 CommodityPosition ()
 
 CommodityPosition (const Envelope &env, const CommodityPositionData &data)
 
void build (const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &) override
 
void fromXML (XMLNode *node) override
 
XMLNodetoXML (XMLDocument &doc) const override
 
std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 
const CommodityPositionDatadata () const
 
const std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > & indices () const
 
const std::vector< Real > & weights () const
 
bool isSingleCurrency () const
 
void setNpvCurrencyConversion (const std::string &ccy, const Handle< Quote > &conversion)
 
- 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

CommodityPositionData data_
 
std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > indices_
 
std::vector< Real > weights_
 
std::vector< Handle< Quote > > fxConversion_
 
bool isSingleCurrency_
 

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 Position.

Definition at line 52 of file commodityposition.hpp.

Constructor & Destructor Documentation

◆ CommodityPosition() [1/2]

Definition at line 54 of file commodityposition.hpp.

54: Trade("CommodityPosition") {}
Trade()
Default constructor.
Definition: trade.hpp:59

◆ CommodityPosition() [2/2]

CommodityPosition ( const Envelope env,
const CommodityPositionData data 
)

Definition at line 55 of file commodityposition.hpp.

55: Trade("CommodityPosition", env), data_(data) {}
const CommodityPositionData & data() const

Member Function Documentation

◆ build()

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

Definition at line 47 of file commodityposition.cpp.

47 {
48
49 // ISDA taxonomy: not a derivative, but define the asset class at least
50 // so that we can determine a TRS asset class that has an EQ position underlying
51 additionalData_["isdaAssetClass"] = string("Commodity");
52 additionalData_["isdaBaseProduct"] = string("");
53 additionalData_["isdaSubProduct"] = string("");
54 additionalData_["isdaTransaction"] = string("");
55
56 DLOG("CommodityPosition::build() called for " << id());
57 QL_REQUIRE(!data_.underlyings().empty(), "CommodityPosition::build(): no underlyings given");
58 indices_.clear();
59 weights_.clear();
60 fxConversion_.clear();
61
62 std::vector<std::string> currencies;
63 for (auto const& u : data_.underlyings()) {
64 auto pts = engineFactory->market()->commodityPriceCurve(u.name(), engineFactory->configuration(MarketContext::pricing));
65 QL_REQUIRE(!pts.empty() && *pts != nullptr, "CommodityPosition, curve missing for '"<<u.name()<<"'");
66 QL_REQUIRE(!pts->currency().empty(),
67 "CommodityPosition, Currency not set in curve config for commodity curve'" << u.name() << "'. Skip this trade.");
68 auto index = parseCommodityIndex(u.name(), false, pts, NullCalendar(), u.priceType() == "FutureSettlement");
69 QuantLib::ext::shared_ptr<Conventions> conventions = InstrumentConventions::instance().conventions();
70 pair<bool, QuantLib::ext::shared_ptr<Convention>> p = conventions->get(u.name(), Convention::Type::CommodityFuture);
71 if (u.priceType() == "FutureSettlement" && p.first) {
72 auto convention = QuantLib::ext::dynamic_pointer_cast<CommodityFutureConvention>(p.second);
73 ConventionsBasedFutureExpiry feCalc(*convention);
74 Date expiry = Settings::instance().evaluationDate();
75 Size nOffset = u.futureMonthOffset() == Null<Size>() ? 0 : u.futureMonthOffset();
76 if (u.deliveryRollDays() != Null<Size>()) {
77 auto cal =
78 u.deliveryRollCalendar().empty() ? convention->calendar() : parseCalendar(u.deliveryRollCalendar());
79 expiry = cal.advance(expiry, u.deliveryRollDays() * Days, convention->businessDayConvention());
80 }
81 expiry = feCalc.nextExpiry(true, expiry, nOffset);
82 if (!u.futureContractMonth().empty()) {
83 QL_REQUIRE(u.futureContractMonth().size() == 7,
84 "FutureContractMonth has invalid format, please use MonYYYY, where 'Mon' is a 3 letter "
85 "month abbreviation.");
86 auto month = parseMonth(u.futureContractMonth().substr(0, 3));
87 auto year = parseInteger(u.futureContractMonth().substr(3, 4));
88 Date contractDate(1, month, year);
89 expiry = feCalc.expiryDate(contractDate, nOffset, false);
90 } else if (!u.futureExpiryDate().empty()) {
91 expiry = parseDate(u.futureExpiryDate());
92 expiry = feCalc.nextExpiry(true, expiry, nOffset, false);
93 }
94 index = index->clone(expiry, pts);
95 }
96 indices_.push_back(index);
97 weights_.push_back(u.weight());
98 currencies.push_back(pts->currency().code());
99 }
100
101 // get fx quotes
102
103 isSingleCurrency_ = true;
104 npvCurrency_ = currencies.front();
105 for (auto const& c : currencies) {
106 // we use fxSpot() as opposed to fxRate() here to ensure consistency between NPV() and the fixing of an
107 // equivalent index representing the same basket
108 fxConversion_.push_back(
109 engineFactory->market()->fxSpot(c + npvCurrency_, engineFactory->configuration(MarketContext::pricing)));
110 if (npvCurrency_ != c)
111 isSingleCurrency_ = false;
112 }
113
114 // set instrument
115 auto qlInstr =
116 QuantLib::ext::make_shared<CommodityPositionInstrumentWrapper>(data_.quantity(), indices_, weights_, fxConversion_);
117 qlInstr->setPricingEngine(QuantLib::ext::make_shared<CommodityPositionInstrumentWrapperEngine>());
118 setSensitivityTemplate(std::string());
119 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(qlInstr);
120
121 // no sensible way to set these members
122 maturity_ = Date::maxDate();
123 notional_ = Null<Real>();
125
126 // leave legs empty
127}
const std::vector< CommodityUnderlying > & underlyings() const
std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > indices_
std::vector< Handle< Quote > > fxConversion_
string npvCurrency_
Definition: trade.hpp:201
QuantLib::Real notional_
Definition: trade.hpp:202
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
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
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
Month parseMonth(const string &s)
Definition: parsers.cpp:613
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > parseCommodityIndex(const string &name, bool hasPrefix, const Handle< PriceTermStructure > &ts, const Calendar &cal, const bool enforceFutureIndex)
+ Here is the call graph for this function:

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 135 of file commodityposition.cpp.

135 {
136 Trade::fromXML(node);
137 data_.fromXML(XMLUtils::getChildNode(node, "CommodityPositionData"));
138}
void fromXML(XMLNode *node) override
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
+ Here is the call graph for this function:

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 140 of file commodityposition.cpp.

140 {
141 XMLNode* node = Trade::toXML(doc);
142 XMLUtils::appendNode(node, data_.toXML(doc));
143 return node;
144}
XMLNode * toXML(XMLDocument &doc) const override
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
+ 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

Reimplemented from Trade.

Definition at line 147 of file commodityposition.cpp.

147 {
148 std::map<AssetClass, std::set<std::string>> result;
149 for (auto const& u : data_.underlyings()) {
150 result[AssetClass::COM].insert(u.name());
151 }
152 return result;
153}
+ Here is the call graph for this function:

◆ data()

const CommodityPositionData & data ( ) const

Definition at line 65 of file commodityposition.hpp.

65{ return data_; }

◆ indices()

const std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > & indices ( ) const

Definition at line 66 of file commodityposition.hpp.

66{ return indices_; }

◆ weights()

const std::vector< Real > & weights ( ) const

Definition at line 67 of file commodityposition.hpp.

67{ return weights_; }

◆ isSingleCurrency()

bool isSingleCurrency ( ) const

Definition at line 68 of file commodityposition.hpp.

68{ return isSingleCurrency_; }

◆ setNpvCurrencyConversion()

void setNpvCurrencyConversion ( const std::string &  ccy,
const Handle< Quote > &  conversion 
)

we allow to set the npv currency to a different currency than the default npv currency = first asset's currency; in this case a conversion rate from the default to the new currency has to be provided

Definition at line 129 of file commodityposition.cpp.

129 {
130 npvCurrency_ = ccy;
131 QuantLib::ext::static_pointer_cast<CommodityPositionInstrumentWrapper>(instrument_->qlInstrument())
132 ->setNpvCurrencyConversion(conversion);
133}

Member Data Documentation

◆ data_

CommodityPositionData data_
private

Definition at line 75 of file commodityposition.hpp.

◆ indices_

std::vector<QuantLib::ext::shared_ptr<QuantExt::CommodityIndex> > indices_
private

Definition at line 77 of file commodityposition.hpp.

◆ weights_

std::vector<Real> weights_
private

Definition at line 78 of file commodityposition.hpp.

◆ fxConversion_

std::vector<Handle<Quote> > fxConversion_
private

Definition at line 79 of file commodityposition.hpp.

◆ isSingleCurrency_

bool isSingleCurrency_
private

Definition at line 80 of file commodityposition.hpp.