Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
tradestrike.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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#include <boost/lexical_cast.hpp>
22
23namespace ore {
24namespace data {
25namespace {
26
27struct StrikeValue : public boost::static_visitor<QuantLib::Real> {
28public:
29 QuantLib::Real operator()(const TradeMonetary& s) const { return s.value(); }
30 QuantLib::Real operator()(const TradeStrike::StrikeYield& s) const { return s.yield; }
31};
32
33} // namespace
34
35TradeStrike::TradeStrike(Type type, const QuantLib::Real& value) : type_(type) {
36 if (type_ == Type::Price)
38 else
40}
41
42TradeStrike::TradeStrike(const QuantLib::Real& value, const std::string& currency) : type_(Type::Price) {
44}
45
46TradeStrike::TradeStrike(const QuantLib::Real& value, QuantLib::Compounding compounding) : type_(Type::Yield) {
48}
49
50void TradeStrike::fromXML(XMLNode* node, const bool isRequired, const bool allowYieldStrike) {
51 XMLNode* dataNode = XMLUtils::getChildNode(node, "StrikeData");
52 if (dataNode) {
53 // first look for StrikeYield node
54 if (XMLNode* yieldNode = XMLUtils::getChildNode(dataNode, "StrikeYield")) {
55 QL_REQUIRE(allowYieldStrike, "StrikeYield not supported for this trade type.");
57 strikeYield.yield = XMLUtils::getChildValueAsDouble(yieldNode, "Yield", true);
59 parseCompounding(XMLUtils::getChildValue(yieldNode, "Compounding", false, "SimpleThenCompounded"));
62 } else {
64 if (XMLNode* priceNode = XMLUtils::getChildNode(dataNode, "StrikePrice"))
65 strikePrice.fromXMLNode(priceNode);
66 else {
67 // in order to remain backward compatible we also allow to be set up
68 // without the StrikePrice node
69 strikePrice.fromXMLNode(dataNode);
70 noStrikePriceNode_ = true;
71 }
74 }
75 } else {
76 // if just a strike is present
77 string s = XMLUtils::getChildValue(node, "Strike", isRequired);
78 if (!s.empty()) {
81 onlyStrike_ = true;
82 }
83 }
84}
85
87 XMLNode* node;
88 if (onlyStrike_) {
89 // can only happen for a StrikePrice
90 auto sp = QuantLib::ext::get<StrikePrice>(strike_);
91 node = doc.allocNode("Strike", boost::lexical_cast<std::string>(sp.valueString()));
92 } else {
93 node = doc.allocNode("StrikeData");
95 // maintain backward compatibility, must be a Strike Type Price to get here
96 strikePrice().toXMLNode(doc, node);
97 } else {
98 XMLNode* subNode;
99 if (type_ == Type::Yield) {
100 auto yld = strikeYield();
101 subNode = doc.allocNode("StrikeYield");
102 XMLUtils::addChild(doc, subNode, "Yield", yld.yield);
103 XMLUtils::addChild(doc, subNode, "Compounding", to_string(yld.compounding));
104 } else {
105 auto sp = strikePrice();
106 subNode = doc.allocNode("StrikePrice");
107 sp.toXMLNode(doc, subNode);
108 }
109 XMLUtils::appendNode(node, subNode);
110 }
111 }
112 return node;
113}
114
115QuantLib::Real TradeStrike::value() const {
116 return boost::apply_visitor(StrikeValue(), strike_);
117}
118
120 QL_REQUIRE(type_ == Type::Price, "TradeStrike currency only valid when Strike type is Price");
121 return strikePrice().currency();
122}
123
124const QuantLib::Compounding& TradeStrike::compounding() {
125 QL_REQUIRE(type_ == Type::Yield, "TradeStrike currency only valid when Strike type is Yield");
126 return strikeYield().compounding;
127}
128
129void TradeStrike::setValue(const QuantLib::Real& value) {
130 if (type_ == Type::Price) {
132 } else {
134 }
135}
136
137void TradeStrike::setCurrency(const std::string& currency) {
138 QL_REQUIRE(type_ == Type::Price, "TradeStrike currency only valid when Strike type is Price");
140}
141
142const bool TradeStrike::empty() const {
143 try {
144 return value() == Null<Real>();
145 } catch (...) {
146 return true;
147 }
148}
149
150} // namespace data
151} // namespace ore
std::string currency() const
void setCurrency(const std::string &currency)
void fromXMLNode(XMLNode *node)
void setValue(const QuantLib::Real &value)
void toXMLNode(XMLDocument &doc, XMLNode *node)
const QuantLib::Compounding & compounding()
boost::variant< StrikeYield, StrikePrice > strike_
Definition: tradestrike.hpp:69
XMLNode * toXML(XMLDocument &doc) const
Definition: tradestrike.cpp:86
void setCurrency(const std::string &currency)
void fromXML(XMLNode *node, const bool isRequired=true, const bool allowYieldStrike=false)
Definition: tradestrike.cpp:50
std::string currency()
const bool empty() const
StrikeYield & strikeYield() const
Definition: tradestrike.hpp:58
QuantLib::Real value() const
void setValue(const QuantLib::Real &value)
StrikePrice & strikePrice() const
Definition: tradestrike.hpp:57
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
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
SafeStack< ValueType > value
Compounding parseCompounding(const string &s)
Convert text to QuantLib::Compounding;.
Definition: parsers.cpp:376
@ data
Definition: log.hpp:77
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
QuantLib::Compounding compounding
Definition: tradestrike.hpp:44