24#include <boost/lexical_cast.hpp>
31 auto builder = QuantLib::ext::dynamic_pointer_cast<ScriptedTradeEngineBuilder>(factory->builder(
"ScriptedTrade"));
39 QL_REQUIRE(
underlyings_.size() > 0,
"No underlyings were provided");
42 QL_REQUIRE(u->type() == type,
"All of Underlyings must be from the same asset class.");
63 if (type ==
"InterestRate") {
75 "REQUIRE {Notional >= 0} AND {Strike >= 0};\n"
76 "REQUIRE {Cap >= 0} AND {Floor >= 0};\n"
79 "n = SIZE(Underlyings);\n"
81 "NUMBER sumOfWeights;\n"
82 "FOR i IN (1, n, 1) DO\n"
83 " sumOfWeights = sumOfWeights + Weights[i];\n"
85 "REQUIRE sumOfWeights == 1;\n"
87 "NUMBER d, expectedN, currPrice[n], prevPrice[n];\n"
88 "NUMBER realisedVariance, basketVariation, realisedVariation;\n"
89 "NUMBER strike, cap, floor, currentNotional, payoff;\n"
91 "FOR d IN (2, SIZE(ValuationSchedule), 1) DO\n"
92 " basketVariation = 0;\n"
93 " FOR i IN (1, n, 1) DO\n"
94 " currPrice[i] = Underlyings[i](ValuationSchedule[d]);\n"
95 " prevPrice[i] = Underlyings[i](ValuationSchedule[d-1]);\n"
96 " basketVariation = basketVariation + Weights[i] * ln(currPrice[i]/prevPrice[i]);\n"
98 " realisedVariance = realisedVariance + pow(basketVariation, 2);\n"
101 "expectedN = SIZE(ValuationSchedule) - 1;\n"
102 "realisedVariance = (252/expectedN) * realisedVariance;\n"
104 "IF SquaredPayoff == 1 THEN\n"
105 " realisedVariation = realisedVariance;\n"
106 " currentNotional = pow(100, 2) * Notional / (2 * 100 * Strike);\n"
107 " strike = pow(Strike, 2);\n"
109 " realisedVariation = sqrt(realisedVariance);\n"
110 " currentNotional = 100 * Notional;\n"
111 " strike = Strike;\n"
114 "IF Floor > 0 THEN\n"
115 " IF SquaredPayoff == 1 THEN\n"
116 " floor = pow(Floor, 2);\n"
120 " realisedVariation = max(floor * strike, realisedVariation);\n"
123 " IF SquaredPayoff == 1 THEN\n"
124 " cap = pow(Cap, 2);\n"
128 " realisedVariation = min(cap * strike, realisedVariation);\n"
131 "payoff = LongShort * currentNotional * (realisedVariation - strike);\n"
133 "Swap = PAY(payoff, ValuationSchedule[SIZE(ValuationSchedule)],\n"
134 " SettlementDate, PayCcy);\n",
136 {{
"RealisedVariance",
"realisedVariance"},
137 {
"currentNotional",
"currentNotional"},
138 {
"notionalCurrency",
"PayCcy"}},
152 std::string assetClass = boost::any_cast<std::string>(
additionalData_[
"isdaAssetClass"]);
153 if (assetClass ==
"Equity") {
155 additionalData_[
"isdaSubProduct"] = string(
"Parameter Return Variance");
156 }
else if (assetClass ==
"Foreign Exchange") {
159 }
else if (assetClass ==
"Commodity") {
162 additionalData_[
"isdaSubProduct"] = string(
"Parameter Return Variance");
164 WLOG(
"ISDA taxonomy incomplete for trade " <<
id());
170 std::vector<string> underlyings, weights;
173 QL_REQUIRE(u->weight() != Null<Real>(),
"underlying '" << u->name() <<
"' has no weight");
174 weights.push_back(boost::lexical_cast<std::string>(u->weight()));
176 indices_.emplace_back(
"Index",
"Underlyings", underlyings);
177 numbers_.emplace_back(
"Number",
"Weights", weights);
183 QL_REQUIRE(tradeDataNode,
"BasketVarianceSwapData node not found");
191 QL_REQUIRE(valuationSchedule,
"No valuation schedule provided");
195 QL_REQUIRE(underlyingsNode,
"Could not find an Underlyings node.");
197 for (
auto const& u : underlyings) {
vector< QuantLib::ext::shared_ptr< Underlying > > underlyings_
ScheduleData valuationSchedule_
void setIsdaTaxonomyFields() override
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
std::vector< ScriptedTradeEventData > events_
std::vector< ScriptedTradeValueTypeData > currencies_
std::vector< ScriptedTradeValueTypeData > indices_
virtual void setIsdaTaxonomyFields()
std::vector< ScriptedTradeValueTypeData > numbers_
std::map< std::string, ScriptedTradeScriptData > script_
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
const string & tradeType() const
std::map< std::string, boost::any > additionalData_
const QuantLib::ext::shared_ptr< Underlying > & underlying()
void fromXML(XMLNode *node) override
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 string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static void setNodeName(XMLDocument &doc, XMLNode *node, const string &name)
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
bool parseBool(const string &s)
Convert text to bool.
#define LOG(text)
Logging Macro (Level = Notice)
#define WLOG(text)
Logging Macro (Level = Warning)
QL_DEPRECATED_ENABLE_WARNING std::string scriptedIndexName(const QuantLib::ext::shared_ptr< Underlying > &underlying)
Serializable Credit Default Swap.
trade schedule data model and serialization
string conversion utilities