19#include <ql/cashflows/coupon.hpp>
20#include <ql/cashflows/simplecashflow.hpp>
21#include <ql/exercise.hpp>
33#include <boost/algorithm/string/case_conv.hpp>
38using std::lower_bound;
48 DLOG(
"CommoditySwaption::build() called for trade " <<
id());
61 Settlement::Method settleMethod = Settlement::PhysicalOTC;
63 if (settleType == Settlement::Cash) {
64 settleMethod = Settlement::CollateralizedCashPrice;
72 QL_REQUIRE(exDate >= Settings::instance().evaluationDate(),
73 "Exercise date, " << io::iso_date(exDate) <<
", should be in the future relative to the valuation date "
74 << io::iso_date(Settings::instance().evaluationDate()));
77 QuantLib::ext::shared_ptr<QuantLib::Swap> swap =
buildSwap(engineFactory);
78 QL_REQUIRE(exDate <=
startDate_,
"Expected the expiry date, " << io::iso_date(exDate)
79 <<
" to be on or before the swap start date "
83 exercise_ = QuantLib::ext::make_shared<EuropeanExercise>(exDate);
84 QuantLib::ext::shared_ptr<QuantExt::GenericSwaption> swaption =
85 QuantLib::ext::make_shared<QuantExt::GenericSwaption>(swap,
exercise_, settleType, settleMethod);
88 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(
tradeType_);
89 QuantLib::ext::shared_ptr<CommoditySwaptionEngineBuilder> engineBuilder =
90 QuantLib::ext::dynamic_pointer_cast<CommoditySwaptionEngineBuilder>(builder);
93 QuantLib::ext::shared_ptr<PricingEngine> engine = engineBuilder->engine(currency,
name_);
95 swaption->setPricingEngine(engine);
99 if (settleType == Settlement::Cash) {
100 Real multiplier = positionType == Position::Long ? 1.0 : -1.0;
101 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(swaption, multiplier);
103 instrument_ = QuantLib::ext::make_shared<EuropeanOptionWrapper>(swaption, positionType == Position::Long, exDate,
104 settleType == Settlement::Physical, swap);
117 QL_REQUIRE(
legData_.size() == 2,
"Expected two commodity legs but found " <<
legData_.size());
118 QL_REQUIRE(
legData_[0].currency() ==
legData_[1].currency(),
"Cross currency commodity swap not supported");
120 "Both commodity legs are " << (
legData_[0].isPayer() ?
"paying" :
"receiving"));
122 QL_REQUIRE(
legData_[0].legType() ==
"CommodityFixed" ||
legData_[0].legType() ==
"CommodityFloating",
123 "Leg type needs to be CommodityFixed or CommodityFloating but 1st leg has type "
125 QL_REQUIRE(
legData_[1].legType() ==
"CommodityFixed" ||
legData_[1].legType() ==
"CommodityFloating",
126 "Leg type needs to be CommodityFixed or CommodityFloating but 2nd leg has type "
129 if (
legData_[0].legType() ==
"CommodityFixed") {
130 QL_REQUIRE(
legData_[1].legType() ==
"CommodityFloating",
131 "1st leg is CommodityFixed so 2nd leg should be CommodityFloating but is " <<
legData_[1].legType());
132 auto floatLeg = QuantLib::ext::dynamic_pointer_cast<CommodityFloatingLegData>(
legData_[1].concreteLegData());
133 name_ = floatLeg->name();
135 auto floatLeg = QuantLib::ext::dynamic_pointer_cast<CommodityFloatingLegData>(
legData_[0].concreteLegData());
136 QL_REQUIRE(floatLeg,
"first leg has type " <<
legData_[0].legType() <<
", expected CommodityFloating");
137 name_ = floatLeg->name();
146 QuantLib::ext::shared_ptr<QuantLib::Swap> swap = QuantLib::ext::dynamic_pointer_cast<QuantLib::Swap>(qlInstrument);
147 QL_REQUIRE(swap,
"Expected an underlying swap instrument from CommoditySwap");
158std::map<AssetClass, std::set<std::string>>
161 std::map<AssetClass, std::set<std::string>> result;
164 set<string> indices = ld.indices();
165 for (
auto ind : indices) {
166 QuantLib::ext::shared_ptr<Index> index =
parseIndex(ind);
168 if (
auto ci = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityIndex>(index)) {
181 QL_REQUIRE(swapNode,
"No CommoditySwaptionData node");
188 QL_REQUIRE(nodes.size() == 2,
"Two commodity swap legs expected, found " << nodes.size());
190 for (Size i = 0; i < nodes.size(); i++) {
192 ld->fromXML(nodes[i]);
208 for (Size i = 0; i <
legData_.size(); i++)
Engine builder for commodity swaps.
Engine builder for commodity swaptions.
vector< ore::data::LegData > legData_
ore::data::OptionData option_
virtual void fromXML(ore::data::XMLNode *node) override
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
Add underlying Commodity names.
QuantLib::ext::shared_ptr< QuantLib::Exercise > exercise_
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
void build(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory) override
QuantLib::ext::shared_ptr< CommoditySwap > commoditySwap_
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
QuantLib::ext::shared_ptr< ore::data::LegData > createLegData() const
QuantLib::Date startDate_
QuantLib::ext::shared_ptr< QuantLib::Swap > buildSwap(const QuantLib::ext::shared_ptr< ore::data::EngineFactory > &engineFactory)
const string & settlementMethod() const
const string & longShort() const
const string & settlement() const
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
const vector< string > & exerciseDates() const
virtual void fromXML(XMLNode *node) override
void setSensitivityTemplate(const EngineBuilder &builder)
virtual XMLNode * toXML(XMLDocument &doc) const override
const Envelope & envelope() const
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
void reset()
Reset trade, clear all base class data. This does not reset accumulated timings for this trade.
std::map< std::string, boost::any > additionalData_
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static void appendNode(XMLNode *parent, XMLNode *child)
Commodity swaption data model and serialization.
Settlement::Method parseSettlementMethod(const std::string &s)
Convert text to QuantLib::Settlement::Method.
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.
QuantLib::ext::shared_ptr< Index > parseIndex(const string &s)
Convert std::string to QuantLib::Index.
Settlement::Type parseSettlementType(const std::string &s)
Convert text to QuantLib::Settlement::Type.
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
Serializable Credit Default Swap.
Wrapper for option instruments, tracks whether option has been exercised or not.
string conversion utilities