32 for (
auto const n : c) {
56 DLOG(
"CommodityPosition::build() called for " <<
id());
57 QL_REQUIRE(!
data_.
underlyings().empty(),
"CommodityPosition::build(): no underlyings given");
62 std::vector<std::string> currencies;
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();
71 if (u.priceType() ==
"FutureSettlement" && p.first) {
72 auto convention = QuantLib::ext::dynamic_pointer_cast<CommodityFutureConvention>(p.second);
74 Date expiry = Settings::instance().evaluationDate();
75 Size nOffset = u.futureMonthOffset() == Null<Size>() ? 0 : u.futureMonthOffset();
76 if (u.deliveryRollDays() != Null<Size>()) {
78 u.deliveryRollCalendar().empty() ? convention->calendar() :
parseCalendar(u.deliveryRollCalendar());
79 expiry = cal.advance(expiry, u.deliveryRollDays() * Days, convention->businessDayConvention());
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()) {
92 expiry = feCalc.
nextExpiry(
true, expiry, nOffset,
false);
94 index = index->clone(expiry, pts);
98 currencies.push_back(pts->currency().code());
105 for (
auto const& c : currencies) {
117 qlInstr->setPricingEngine(QuantLib::ext::make_shared<CommodityPositionInstrumentWrapperEngine>());
119 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(qlInstr);
131 QuantLib::ext::static_pointer_cast<CommodityPositionInstrumentWrapper>(
instrument_->qlInstrument())
132 ->setNpvCurrencyConversion(conversion);
146std::map<AssetClass, std::set<std::string>>
148 std::map<AssetClass, std::set<std::string>> result;
156 const Real quantity,
const std::vector<QuantLib::ext::shared_ptr<QuantExt::CommodityIndex>>& commodities,
157 const std::vector<Real>& weights,
const std::vector<Handle<Quote>>& fxConversion)
158 : quantity_(quantity), commodities_(commodities), weights_(weights), fxConversion_(fxConversion) {
159 QL_REQUIRE(
commodities_.size() ==
weights_.size(),
"CommodityPositionInstrumentWrapper: commodities size ("
163 "CommodityPositionInstrumentWrapper: fxConversion size ("
182 QL_REQUIRE(a !=
nullptr,
"wrong argument type in CommodityPositionInstrumentWrapper");
191 Instrument::fetchResults(r);
196 Date today = Settings::instance().evaluationDate();
197 for (Size i = 0; i <
arguments_.commodities_.size(); ++i) {
207 result *=
arguments_.npvCcyConversion_->value();
const Instrument::results * results_
const std::vector< CommodityUnderlying > & underlyings() const
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
std::vector< CommodityUnderlying > underlyings_
std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > indices_
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
void fromXML(XMLNode *node) override
std::vector< Handle< Quote > > fxConversion_
void setNpvCurrencyConversion(const std::string &ccy, const Handle< Quote > &conversion)
XMLNode * toXML(XMLDocument &doc) const override
std::vector< Real > weights_
void build(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &) override
CommodityPositionData data_
std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > commodities_
std::vector< Handle< Quote > > fxConversion_
std::vector< Real > weights_
Handle< Quote > npvCcyConversion_
void calculate() const override
void fetchResults(const QuantLib::PricingEngine::results *) const override
bool isExpired() const override
std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > commodities_
std::vector< Handle< Quote > > fxConversion_
std::vector< Real > weights_
Handle< Quote > npvCcyConversion_
void setupExpired() const override
void setupArguments(QuantLib::PricingEngine::arguments *) const override
CommodityPositionInstrumentWrapper(const Real quantity, const std::vector< QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > > &commodities, const std::vector< Real > &weights, const std::vector< Handle< Quote > > &fxConversion={})
void setNpvCurrencyConversion(const Handle< Quote > &npvCcyConversion)
Perform date calculations for future contracts based on conventions.
QuantLib::Date nextExpiry(bool includeExpiry=true, const QuantLib::Date &referenceDate=QuantLib::Date(), QuantLib::Natural offset=0, bool forOption=false) override
QuantLib::Date expiryDate(const QuantLib::Date &contractDate, QuantLib::Natural monthOffset=0, bool forOption=false) override
virtual void fromXML(XMLNode *node) override
void setSensitivityTemplate(const EngineBuilder &builder)
virtual XMLNode * toXML(XMLDocument &doc) const override
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
std::map< std::string, boost::any > additionalData_
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
static void checkNode(XMLNode *n, const string &expectedName)
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
Commodity Position trade data model and serialization.
Base class for classes that perform date calculations for future contracts.
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Month parseMonth(const string &s)
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Map text representations to QuantLib/QuantExt types.
#define DLOG(text)
Logging Macro (Level = Debug)
QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > parseCommodityIndex(const string &name, bool hasPrefix, const Handle< PriceTermStructure > &ts, const Calendar &cal, const bool enforceFutureIndex)
Serializable Credit Default Swap.
Swap::arguments * arguments_