Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
performanceoption_01.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17 */
18
21
22#include <boost/lexical_cast.hpp>
23
24namespace ore {
25namespace data {
26
27void PerformanceOption_01::build(const QuantLib::ext::shared_ptr<EngineFactory>& factory) {
28
29 // set script parameters
30
31 clear();
33
34 numbers_.emplace_back("Number", "NotionalAmount", notionalAmount_);
35 numbers_.emplace_back("Number", "ParticipationRate", participationRate_);
36
37 events_.emplace_back("ValuationDate", valuationDate_);
38 events_.emplace_back("SettlementDate", settlementDate_);
39
40 numbers_.emplace_back("Number", "StrikePrices", strikePrices_);
41
42 numbers_.emplace_back("Number", "Strike", strike_);
43
44 Position::Type position = parsePositionType(position_);
45 numbers_.emplace_back("Number", "LongShort", position == Position::Long ? "1" : "-1");
46
47 currencies_.emplace_back("Currency", "PayCcy", payCcy_);
48
49 // set product tag
50
51 productTag_ = "MultiAssetOption({AssetClass})";
52
53 // set script
54
55 // clang-format off
56 script_ = {{"", ScriptedTradeScriptData(std::string("NUMBER i, p, currentNotional;\n") +
57 "FOR i IN (1, SIZE(Underlyings), 1) DO\n" +
58 (strikeIncluded_ ? " p = p + Weights[i] * ( Underlyings[i](ValuationDate) / StrikePrices[i] - Strike );\n" :
59 " p = p + Weights[i] * Underlyings[i](ValuationDate) / StrikePrices[i];\n") +
60 "END;\n"
61 "Option = LOGPAY( LongShort * NotionalAmount * ParticipationRate *\n" +
62 (strikeIncluded_ ? " max( p, 0 ), ValuationDate, SettlementDate, PayCcy );\n" :
63 " max( p - Strike, 0 ), ValuationDate, SettlementDate, PayCcy );\n") +
64 "currentNotional = NotionalAmount * ParticipationRate;\n",
65 "Option", {{"currentNotional", "currentNotional"}, {"notionalCurrency", "PayCcy"}}, {})}};
66 // clang-format on
67
68 // build trade
69
70 ScriptedTrade::build(factory);
71}
72
75
76 // ISDA taxonomy
77 // asset class set in the base class already
78 std::string assetClass = boost::any_cast<std::string>(additionalData_["isdaAssetClass"]);
79 if (assetClass == "Equity") {
80 additionalData_["isdaBaseProduct"] = string("Other");
81 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
82 } else if (assetClass == "Commodity") {
83 // isda taxonomy missing for this class, using the same as equity
84 additionalData_["isdaBaseProduct"] = string("Other");
85 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
86 } else if (assetClass == "Foreign Exchange") {
87 additionalData_["isdaBaseProduct"] = string("Complex Exotic");
88 additionalData_["isdaSubProduct"] = string("Generic");
89 } else {
90 WLOG("ISDA taxonomy incomplete for trade " << id());
91 }
92 additionalData_["isdaTransaction"] = string("Basket");
93}
94
96 std::vector<std::string> underlyingNames, weights;
97 for (auto const& u : underlyings_) {
98 underlyingNames.push_back(scriptedIndexName(u));
99 QL_REQUIRE(u->weight() != Null<Real>(), "underlying '" << u->name() << "' has no weight");
100 weights.push_back(boost::lexical_cast<std::string>(u->weight()));
101 }
102 indices_.emplace_back("Index", "Underlyings", underlyingNames);
103 numbers_.emplace_back("Number", "Weights", weights);
104}
105
107 Trade::fromXML(node);
108 XMLNode* tradeDataNode = XMLUtils::getChildNode(node, "PerformanceOption01Data");
109 QL_REQUIRE(tradeDataNode, "PerformanceOption01Data node not found");
110 notionalAmount_ = XMLUtils::getChildValue(tradeDataNode, "NotionalAmount", true);
111 participationRate_ = XMLUtils::getChildValue(tradeDataNode, "ParticipationRate", true);
112 valuationDate_ = XMLUtils::getChildValue(tradeDataNode, "ValuationDate", true);
113 settlementDate_ = XMLUtils::getChildValue(tradeDataNode, "SettlementDate", true);
114 auto underlyingsNode = XMLUtils::getChildNode(tradeDataNode, "Underlyings");
115 QL_REQUIRE(underlyingsNode, "No Underlyings node");
116 auto underlyings = XMLUtils::getChildrenNodes(underlyingsNode, "Underlying");
117 for (auto const& n : underlyings) {
118 UnderlyingBuilder underlyingBuilder;
119 underlyingBuilder.fromXML(n);
120 underlyings_.push_back(underlyingBuilder.underlying());
121 }
122 strikePrices_ = XMLUtils::getChildrenValues(tradeDataNode, "StrikePrices", "StrikePrice", true);
123 strike_ = XMLUtils::getChildValue(tradeDataNode, "Strike", true);
124 strikeIncluded_ = XMLUtils::getChildValueAsBool(tradeDataNode, "StrikeIncluded", false, true);
125 position_ = XMLUtils::getChildValue(tradeDataNode, "Position", true);
126 payCcy_ = XMLUtils::getChildValue(tradeDataNode, "PayCcy", true);
127 initIndices();
128}
129
131 XMLNode* node = Trade::toXML(doc);
132 XMLNode* tradeNode = doc.allocNode("PerformanceOption01Data");
133 XMLUtils::appendNode(node, tradeNode);
134 XMLUtils::addChild(doc, tradeNode, "NotionalAmount", notionalAmount_);
135 XMLUtils::addChild(doc, tradeNode, "ParticipationRate", participationRate_);
136 XMLUtils::addChild(doc, tradeNode, "ValuationDate", valuationDate_);
137 XMLUtils::addChild(doc, tradeNode, "SettlementDate", settlementDate_);
138 XMLNode* underlyingsNode = doc.allocNode("Underlyings");
139 XMLUtils::appendNode(tradeNode, underlyingsNode);
140 for (auto& n : underlyings_) {
141 XMLUtils::appendNode(underlyingsNode, n->toXML(doc));
142 }
143 XMLUtils::addChildren(doc, tradeNode, "StrikePrices", "StrikePrice", strikePrices_);
144 XMLUtils::addChild(doc, tradeNode, "Strike", strike_);
145 XMLUtils::addChild(doc, tradeNode, "StrikeIncluded", strikeIncluded_);
146 XMLUtils::addChild(doc, tradeNode, "Position", position_);
147 XMLUtils::addChild(doc, tradeNode, "PayCcy", payCcy_);
148 return node;
149}
150
151} // namespace data
152} // namespace ore
vector< QuantLib::ext::shared_ptr< Underlying > > underlyings_
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) 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
Definition: trade.cpp:34
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
const QuantLib::ext::shared_ptr< Underlying > & underlying()
Definition: underlying.hpp:266
void fromXML(XMLNode *node) override
Definition: underlying.cpp:305
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void addChildren(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values)
Definition: xmlutils.cpp:502
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static bool getChildValueAsBool(XMLNode *node, const string &name, bool mandatory=false, bool defaultValue=true)
Definition: xmlutils.cpp:296
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:306
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
@ data
Definition: log.hpp:77
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
QL_DEPRECATED_ENABLE_WARNING std::string scriptedIndexName(const QuantLib::ext::shared_ptr< Underlying > &underlying)
Definition: utilities.cpp:614
Serializable Credit Default Swap.
Definition: namespaces.docs:23
performance option wrapper for scripted trade
some utility functions