Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equityforward.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
19/*!
20 \file qlw/trades/equityforward.cpp
21 \brief Equity Forward data model
22 \ingroup portfolio
23*/
24
25#include <boost/make_shared.hpp>
30#include <ql/errors.hpp>
32
33using namespace QuantLib;
34using namespace std;
35
36namespace ore {
37namespace data {
38
39void EquityForward::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
40
41 // ISDA taxonomy
42 additionalData_["isdaAssetClass"] = string("Equity");
43 additionalData_["isdaBaseProduct"] = string("Forward");
44 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
45 // skip the transaction level mapping for now
46 additionalData_["isdaTransaction"] = string("");
47
48 additionalData_["strikeCurrency"] = strikeCurrency_;
49 additionalData_["quantity"] = quantity_;
50
51 Currency ccy = parseCurrencyWithMinors(currency_);
52
53 // convert strike to the major currency if needed
54 Real strike;
55 if (!strikeCurrency_.empty()) {
56 strike = convertMinorToMajorCurrency(strikeCurrency_, strike_);
57 // ensure strike currency matches equity currency
58 } else {
59 WLOG("No Strike Currency provide for trade " << id() << ", assuming trade currency " << ccy);
61 }
62 additionalData_["strike"] = strike;
63
64 QuantLib::Position::Type longShort = parsePositionType(longShort_);
66
67 string name = eqName();
68 additionalData_["underlyingSecurityId"] = name;
69
70 QuantLib::ext::shared_ptr<Instrument> inst =
71 QuantLib::ext::make_shared<QuantExt::EquityForward>(name, ccy, longShort, quantity_, maturity, strike);
72
73 // set up other Trade details
74 instrument_ = QuantLib::ext::shared_ptr<InstrumentWrapper>(new VanillaInstrument(inst));
75 npvCurrency_ = ccy.code();
76 maturity_ = maturity;
77 // Notional - we really need todays spot to get the correct notional.
78 // But rather than having it move around we use strike * quantity
79 notional_ = strike * quantity_;
80 notionalCurrency_ = ccy.code();
81
82 // We check the ccys here at the end of the build to ensure that the rest of the build above
83 // (which does not require the market) runs first
84
85 // get the equity currency from the market
86 Currency equityCcy = engineFactory->market()->equityCurve(eqName())->currency();
87
88 // ensure forward currency matches the equity currency
89 QL_REQUIRE(!equityCcy.empty(), "No equity currency in equityCurve for equity " << eqName());
90 QL_REQUIRE(ccy == equityCcy, "EquityForward currency " << ccy << " does not match equity currency " << equityCcy << " for trade " << id());
91
92 if (!strikeCurrency_.empty()) {
93 Currency strikeCcy = parseCurrencyWithMinors(strikeCurrency_);
94 QL_REQUIRE(strikeCcy == equityCcy, "Strike currency " << ccy << " does not match equity currency " << equityCcy << " for trade " << id());
95 }
96
97 // Pricing engine
98 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
99 QL_REQUIRE(builder, "No builder found for " << tradeType_);
100 QuantLib::ext::shared_ptr<EquityForwardEngineBuilder> eqFwdBuilder =
101 QuantLib::ext::dynamic_pointer_cast<EquityForwardEngineBuilder>(builder);
102 inst->setPricingEngine(eqFwdBuilder->engine(name, ccy));
103 setSensitivityTemplate(*eqFwdBuilder);
104}
105
106void EquityForward::fromXML(XMLNode* node) {
107 Trade::fromXML(node);
108 XMLNode* eNode = XMLUtils::getChildNode(node, "EquityForwardData");
109
110 longShort_ = XMLUtils::getChildValue(eNode, "LongShort", true);
111 maturityDate_ = XMLUtils::getChildValue(eNode, "Maturity", true);
112 XMLNode* tmp = XMLUtils::getChildNode(eNode, "Underlying");
113 if (!tmp)
114 tmp = XMLUtils::getChildNode(eNode, "Name");
115 equityUnderlying_.fromXML(tmp);
116 currency_ = XMLUtils::getChildValue(eNode, "Currency", true);
117 strike_ = XMLUtils::getChildValueAsDouble(eNode, "Strike", true);
118 strikeCurrency_ = XMLUtils::getChildValue(eNode, "StrikeCurrency", false);
119 quantity_ = XMLUtils::getChildValueAsDouble(eNode, "Quantity", true);
120}
121
122XMLNode* EquityForward::toXML(XMLDocument& doc) const {
123 XMLNode* node = Trade::toXML(doc);
124 XMLNode* eNode = doc.allocNode("EquityForwardData");
125 XMLUtils::appendNode(node, eNode);
126
127 XMLUtils::addChild(doc, eNode, "LongShort", longShort_);
128 XMLUtils::addChild(doc, eNode, "Maturity", maturityDate_);
129 XMLUtils::appendNode(eNode, equityUnderlying_.toXML(doc));
130 XMLUtils::addChild(doc, eNode, "Currency", currency_);
131 XMLUtils::addChild(doc, eNode, "Strike", strike_);
132 if (!strikeCurrency_.empty())
133 XMLUtils::addChild(doc, eNode, "StrikeCurrency", strikeCurrency_);
134 XMLUtils::addChild(doc, eNode, "Quantity", quantity_);
135 return node;
136}
137
138std::map<AssetClass, std::set<std::string>>
139EquityForward::underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager) const {
140 return {{AssetClass::EQ, std::set<std::string>({eqName()})}};
141}
142
143} // namespace data
144} // namespace ore
Builder that returns an engine to price an equity forward.
const std::string & name() const
Position::Type longShort_
Position::Type longShort() const
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Vanilla Instrument Wrapper.
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
Pricing Engine Factory.
Equity Forward data model and serialization.
Currency parseCurrencyWithMinors(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:310
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
QuantLib::Real convertMinorToMajorCurrency(const std::string &s, QuantLib::Real value)
Convert a value from a minor ccy to major.
Definition: parsers.cpp:324
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
Time maturity
Definition: utilities.cpp:66
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Reference data model and serialization.