19#include <boost/make_shared.hpp>
21#include <ql/errors.hpp>
42 const string& currency, Real quantity,
const string& maturityDate, Real strike)
43 :
Trade(
"CommodityForward", envelope), position_(position), commodityName_(commodityName), currency_(currency),
44 quantity_(quantity), maturityDate_(maturityDate), strike_(strike),
45 fixingDate_(Date()), fxIndex_(
""), payCcy_(currency){}
48 const string& currency, Real quantity,
const string& maturityDate, Real strike,
49 const Date& futureExpiryDate,
const boost::optional<bool>& physicallySettled,
50 const Date& paymentDate)
51 :
Trade(
"CommodityForward", envelope), position_(position), commodityName_(commodityName), currency_(currency),
52 quantity_(quantity), maturityDate_(maturityDate), strike_(strike), isFuturePrice_(true),
53 futureExpiryDate_(futureExpiryDate), physicallySettled_(physicallySettled), paymentDate_(paymentDate),
54 fixingDate_(Date()), fxIndex_(
""), payCcy_(currency) {}
57 const string& currency, Real quantity,
const string& maturityDate, Real strike,
58 const Period& futureExpiryOffset,
const Calendar& offsetCalendar,
59 const boost::optional<bool>& physicallySettled,
60 const Date& paymentDate)
61 : Trade(
"CommodityForward", envelope), position_(position), commodityName_(commodityName), currency_(currency),
62 quantity_(quantity), maturityDate_(maturityDate), strike_(strike), isFuturePrice_(true),
63 futureExpiryOffset_(futureExpiryOffset), offsetCalendar_(offsetCalendar), physicallySettled_(physicallySettled),
64 paymentDate_(paymentDate),
65 fixingDate_(Date()), fxIndex_(
""), payCcy_(currency){}
70 additionalData_[
"isdaAssetClass"] = string(
"Commodity");
71 additionalData_[
"isdaBaseProduct"] = string(
"Forward");
72 additionalData_[
"isdaSubProduct"] = string(
"Price Return Basic Performance");
74 additionalData_[
"isdaTransaction"] = string(
"");
77 const QuantLib::ext::shared_ptr<Market>& market = engineFactory->market();
78 QuantLib::ext::shared_ptr<QuantExt::FxIndex>
fxIndex =
nullptr;
87 additionalData_[
"strike"] =
strike_;
88 additionalData_[
"strikeCurrency"] =
currency_;
90 additionalData_[
"settlementCurrency"] =
payCcy_;
92 additionalData_[
"fxIndex"] =
fxIndex;
96 auto index = *market->commodityIndex(commodityName_, engineFactory->configuration(MarketContext::pricing));
97 bool isFutureAccordingToConventions = InstrumentConventions::instance().conventions()->has(commodityName_, Convention::Type::CommodityFuture);
100 maturity_ =
index->fixingCalendar().adjust(maturity_, Preceding);
102 if ((isFuturePrice_ && *isFuturePrice_) || isFutureAccordingToConventions) {
105 index = *market->commodityIndex(commodityName_, engineFactory->configuration(MarketContext::pricing));
108 Date expiryDate = maturity_;
109 if (futureExpiryDate_ != Date()) {
110 expiryDate = futureExpiryDate_;
111 }
else if (futureExpiryOffset_ != Period()) {
112 Calendar cal = offsetCalendar_.empty() ? NullCalendar() : offsetCalendar_;
113 expiryDate = cal.advance(maturity_, futureExpiryOffset_);
127 WLOG(
"Commodity forward " <<
id() <<
" has payment date (" << io::iso_date(
paymentDate_) <<
128 ") before the maturity date (" << io::iso_date(maturity_) <<
"). Setting payment date" <<
129 " equal to the maturity date.");
135 WLOG(
"Commodity forward " <<
id() <<
" supplies a payment date (" << io::iso_date(
paymentDate_) <<
136 ") but is physically settled. The payment date is ignored.");
142 DLOG(
"commodity forward " <<
id() <<
" paymentDate is " <<
paymentDate);
143 requiredFixings_.addFixingDate(maturity_,
index->name(),
154 engineFactory->configuration(MarketContext::pricing));
157 QuantLib::ext::shared_ptr<Instrument> commodityForward = QuantLib::ext::make_shared<QuantExt::CommodityForward>(
162 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
163 QL_REQUIRE(builder,
"No builder found for " << tradeType_);
164 QuantLib::ext::shared_ptr<CommodityForwardEngineBuilder> commodityForwardEngineBuilder =
165 QuantLib::ext::dynamic_pointer_cast<CommodityForwardEngineBuilder>(builder);
166 commodityForward->setPricingEngine(commodityForwardEngineBuilder->engine(
currency));
167 setSensitivityTemplate(*commodityForwardEngineBuilder);
170 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(commodityForward);
173Real CommodityForward::notional()
const {
return notional_; }
175std::map<AssetClass, std::set<std::string>>
176CommodityForward::underlyingIndices(
const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager)
const {
177 return {{AssetClass::COM, std::set<std::string>({commodityName_})}};
180void CommodityForward::fromXML(
XMLNode* node) {
182 Trade::fromXML(node);
183 XMLNode* commodityDataNode = XMLUtils::getChildNode(node,
"CommodityForwardData");
185 position_ = XMLUtils::getChildValue(commodityDataNode,
"Position",
true);
186 commodityName_ = XMLUtils::getChildValue(commodityDataNode,
"Name",
true);
187 currency_ = XMLUtils::getChildValue(commodityDataNode,
"Currency",
true);
188 quantity_ = XMLUtils::getChildValueAsDouble(commodityDataNode,
"Quantity",
true);
189 maturityDate_ = XMLUtils::getChildValue(commodityDataNode,
"Maturity",
true);
190 strike_ = XMLUtils::getChildValueAsDouble(commodityDataNode,
"Strike",
true);
192 isFuturePrice_ = boost::none;
193 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"IsFuturePrice"))
194 isFuturePrice_ =
parseBool(XMLUtils::getNodeValue(n));
196 futureExpiryDate_ = Date();
197 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"FutureExpiryDate"))
198 futureExpiryDate_ =
parseDate(XMLUtils::getNodeValue(n));
201 if (futureExpiryDate_ == Date()) {
202 futureExpiryOffset_ = Period();
203 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"FutureExpiryOffset"))
204 futureExpiryOffset_ =
parsePeriod(XMLUtils::getNodeValue(n));
206 offsetCalendar_ = Calendar();
207 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"FutureExpiryOffsetCalendar"))
212 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"PhysicallySettled"))
216 if (
XMLNode* n = XMLUtils::getChildNode(commodityDataNode,
"PaymentDate"))
219 if (
XMLNode* settlementDataNode = XMLUtils::getChildNode(commodityDataNode,
"SettlementData")) {
222 payCcy_ = XMLUtils::getChildValue(settlementDataNode,
"PayCurrency",
true);
223 fxIndex_ = XMLUtils::getChildValue(settlementDataNode,
"FXIndex",
true);
230 XMLNode* node = Trade::toXML(doc);
232 XMLUtils::appendNode(node, commodityDataNode);
234 XMLUtils::addChild(doc, commodityDataNode,
"Position",
position_);
235 XMLUtils::addChild(doc, commodityDataNode,
"Maturity",
maturityDate_);
236 XMLUtils::addChild(doc, commodityDataNode,
"Name", commodityName_);
237 XMLUtils::addChild(doc, commodityDataNode,
"Currency",
currency_);
238 XMLUtils::addChild(doc, commodityDataNode,
"Strike",
strike_);
239 XMLUtils::addChild(doc, commodityDataNode,
"Quantity",
quantity_);
242 XMLUtils::addChild(doc, commodityDataNode,
"IsFuturePrice", *isFuturePrice_);
244 if (futureExpiryDate_ != Date())
245 XMLUtils::addChild(doc, commodityDataNode,
"FutureExpiryDate",
to_string(futureExpiryDate_));
247 if (futureExpiryOffset_ != Period())
248 XMLUtils::addChild(doc, commodityDataNode,
"FutureExpiryOffset",
to_string(futureExpiryOffset_));
250 if (offsetCalendar_ != Calendar())
251 XMLUtils::addChild(doc, commodityDataNode,
"FutureExpiryOffsetCalendar",
to_string(offsetCalendar_));
261 XMLUtils::appendNode(commodityDataNode, settlementDataNode);
262 XMLUtils::addChild(doc, settlementDataNode,
"PayCurrency",
payCcy_);
263 XMLUtils::addChild(doc, settlementDataNode,
"FXIndex",
fxIndex_);
Engine builder for commodity forward.
const boost::shared_ptr< CommodityIndex > & index() const
QuantLib::Currency currency_
QuantLib::Date paymentDate_
QuantLib::Position::Type position() const
boost::shared_ptr< QuantExt::FxIndex > fxIndex() const
const QuantLib::Currency & currency() const
const QuantLib::Date & paymentDate() const
boost::shared_ptr< FxIndex > fxIndex_
QuantLib::Position::Type position_
QuantLib::Date maturityDate_
bool physicallySettled() const
CommodityForward()
Default constructor.
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Serializable object holding generic trade data, reporting dimensions.
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
bool parseBool(const string &s)
Convert text to bool.
#define DLOG(text)
Logging Macro (Level = Debug)
#define WLOG(text)
Logging Macro (Level = Warning)
market data related utilties
An implementation of the Market class that stores the required objects in maps.
std::string to_string(const LocationInfo &l)
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)
Serializable Credit Default Swap.
string conversion utilities