Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equityswap.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
22
24
25namespace ore {
26namespace data {
27
28EquitySwap::EquitySwap(const Envelope& env, const vector<LegData>& legData) : Swap(env, legData, "EquitySwap") {}
29
30EquitySwap::EquitySwap(const Envelope& env, const LegData& leg0, const LegData& leg1)
31 : Swap(env, leg0, leg1, "EquitySwap") {}
32
33void EquitySwap::checkEquitySwap(const vector<LegData>& legData) {
34 // An Equity Swap must have 2 legs - 1 an Equity Leg and the other an IR Leg either Fixed or Floating
35 equityLegIndex_ = Null<Size>();
36 irLegIndex_ = Null<Size>();
37 for (Size i = 0; i < legData.size(); ++i) {
38 if (legData[i].legType() == "Equity") {
40 } else {
41 irLegIndex_ = i;
42 }
43 }
44 QL_REQUIRE((legData.size() == 2) && equityLegIndex_ != Null<Size>() && irLegIndex_ != Null<Size>(),
45 "An Equity Swap must have 2 legs, an Equity Leg and an IR Leg - Trade: " + id());
46}
47
48void EquitySwap::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
49
50 DLOG("EquitySwap::build() called for " << id());
51
53
54 QL_REQUIRE(equityLegIndex_ < legData_.size(), "equityLegIndex (" << equityLegIndex_
55 << ") out of range, legData has size "
56 << legData_.size() << ", this is unexpected");
57 QL_REQUIRE(irLegIndex_ < legData_.size(), "equityLegIndex (" << irLegIndex_ << ") out of range, legData has size "
58 << legData_.size() << ", this is unexpected");
59
60 // 1 add indexing data from the equity leg, if this is desired
61
62 auto eqLegData = QuantLib::ext::dynamic_pointer_cast<EquityLegData>(legData_[equityLegIndex_].concreteLegData());
63 QL_REQUIRE(eqLegData, "could not cast to EquityLegData for equity leg in equity swap, this is unexpected");
64
65 if (legData_[irLegIndex_].indexingFromAssetLeg() && eqLegData->notionalReset()) {
66 DLOG("adding indexing information from equity leg to funding leg");
67
68 // build valuation schedule
69 std::vector<string> valuationDates;
70 auto legBuilder = engineFactory->legBuilder(legData_[equityLegIndex_].legType());
71 RequiredFixings dummy;
72 Leg tmpEqLeg = legBuilder->buildLeg(legData_[equityLegIndex_], engineFactory, dummy,
73 engineFactory->configuration(MarketContext::pricing));
74 for (Size i = 0; i < tmpEqLeg.size(); ++i) {
75 auto eqCpn = QuantLib::ext::dynamic_pointer_cast<QuantExt::EquityCoupon>(tmpEqLeg[i]);
76 QL_REQUIRE(eqCpn, "EquitySwap::build(): expected EquityCoupon on equity leg, this is unexpected");
77 valuationDates.push_back(ore::data::to_string(eqCpn->fixingStartDate()));
78 if (i == tmpEqLeg.size() - 1)
79 valuationDates.push_back(ore::data::to_string(eqCpn->fixingEndDate()));
80 }
81 ScheduleData valuationSchedule(ScheduleDates("", "", "", valuationDates, ""));
82
83 // add equity indexing
84 QL_REQUIRE(eqLegData->quantity() != Null<Real>(),
85 "indexing can only be added to funding leg, if quantity is given on equity leg");
86 Indexing eqIndexing("EQ-" + eqLegData->eqName(), "", false, false, false, eqLegData->quantity(),
87 eqLegData->initialPrice(), Null<Real>(), valuationSchedule, 0, "", "U", false);
88 legData_[irLegIndex_].indexing().push_back(eqIndexing);
89
90 // add fx indexing, if applicable
91 if (!eqLegData->fxIndex().empty()) {
92 Real initialFxFixing = Null<Real>();
93 // if the eq leg has an initial price and this is in the eq leg currency, we do not want an FX conversion
94 // for this price
95 if (!eqLegData->initialPriceCurrency().empty() &&
96 eqLegData->initialPriceCurrency() == legData_[equityLegIndex_].currency() &&
97 eqLegData->initialPrice() != Null<Real>())
98 initialFxFixing = 1.0;
99 Indexing fxIndexing(eqLegData->fxIndex(), "", false, false, false, 1.0, initialFxFixing, Null<Real>(),
100 valuationSchedule, 0, "", "U", false);
101 legData_[irLegIndex_].indexing().push_back(fxIndexing);
102 }
103
104 // set notional node to 1.0
105 legData_[irLegIndex_].notionals() = std::vector<Real>(1, 1.0);
106 legData_[irLegIndex_].notionalDates() = std::vector<std::string>();
107
108 // reset flag that told us to pull the indexing information from the equity leg
109 legData_[irLegIndex_].indexingFromAssetLeg() = false;
110 }
111
112 // just underlying security; notionals and currencies are covered by the Swap class already
113 additionalData_["underlyingSecurityId"] = eqLegData->eqName();
114
115 // 2 now build the swap using the updated leg data
116
117 Swap::build(engineFactory);
118}
119
122
123 // ISDA taxonomy
124 additionalData_["isdaAssetClass"] = string("Equity");
125 additionalData_["isdaBaseProduct"] = string("Swap");
126 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
127 // skip the transaction level mapping for now
128 additionalData_["isdaTransaction"] = string("");
129}
130
131QuantLib::Real EquitySwap::notional() const {
132 Date asof = Settings::instance().evaluationDate();
133 for (auto const& c : legs_[equityLegIndex_]) {
134 if (auto cpn = QuantLib::ext::dynamic_pointer_cast<QuantExt::EquityCoupon>(c)) {
135 if (c->date() > asof)
136 return cpn->nominal();
137 }
138 }
139 ALOG("Error retrieving current notional for equity swap " << id() << " as of " << io::iso_date(asof));
140 return Null<Real>();
141}
142
143std::string EquitySwap::notionalCurrency() const {
144 // try to get the notional ccy from the additional results of the instrument
145 if (legCurrencies_.size() > equityLegIndex_)
147 else
148 return Swap::notionalCurrency();
149}
150
151} // namespace data
152} // namespace ore
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
std::string notionalCurrency() const override
Definition: equityswap.cpp:143
void setIsdaTaxonomyFields() override
Definition: equityswap.cpp:120
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
Definition: equityswap.cpp:131
void checkEquitySwap(const vector< LegData > &legData)
Definition: equityswap.cpp:33
EquitySwap()
Default constructor.
Definition: equityswap.hpp:40
virtual void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Trade interface.
Definition: equityswap.cpp:48
Serializable object holding indexing data.
Definition: indexing.hpp:39
Serializable object holding leg data.
Definition: legdata.hpp:844
Serializable schedule data.
Definition: schedule.hpp:202
Serializable object holding schedule Dates data.
Definition: schedule.hpp:110
Serializable Swap, Single and Cross Currency.
Definition: swap.hpp:36
const vector< LegData > & legData() const
Definition: swap.hpp:74
std::string notionalCurrency() const override
Definition: swap.cpp:286
vector< LegData > legData_
Definition: swap.hpp:82
virtual void setIsdaTaxonomyFields()
Definition: swap.cpp:220
virtual void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
Definition: swap.cpp:45
std::vector< string > legCurrencies_
Definition: trade.hpp:199
std::vector< QuantLib::Leg > legs_
Definition: trade.hpp:198
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
Equity Swap data model and serialization.
Logic for calculating required fixing dates on legs.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities