Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Swap Class Reference

Serializable Swap, Single and Cross Currency. More...

#include <ored/portfolio/swap.hpp>

+ Inheritance diagram for Swap:
+ Collaboration diagram for Swap:

Public Member Functions

 Swap (const string swapType="Swap")
 Default constructor. More...
 
 Swap (const Envelope &env, const string swapType="Swap")
 
 Swap (const Envelope &env, const vector< LegData > &legData, const string swapType="Swap", const std::string settlement="Physical")
 Constructor with vector of LegData. More...
 
 Swap (const Envelope &env, const LegData &leg0, const LegData &leg1, const string swapType="Swap", const std::string settlement="Physical")
 Constructor with two legs. More...
 
virtual void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 Build QuantLib/QuantExt instrument, link pricing engine. More...
 
virtual void setIsdaTaxonomyFields ()
 
QuantLib::Real notional () const override
 Return the current notional in npvCurrency. See individual sub-classes for the precise definition. More...
 
std::string notionalCurrency () const override
 
std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
 Add underlying index names. More...
 
const string & settlement () const
 Settlement Type can be set to "Cash" for NDF. Default value is "Physical". More...
 
Serialisation
virtual void fromXML (XMLNode *node) override
 
virtual XMLNodetoXML (XMLDocument &doc) const override
 
- Public Member Functions inherited from Trade
 Trade ()
 Default constructor. More...
 
 Trade (const string &tradeType, const Envelope &env=Envelope(), const TradeActions &ta=TradeActions())
 Base class constructor. More...
 
virtual ~Trade ()
 Default destructor. More...
 
virtual void build (const QuantLib::ext::shared_ptr< EngineFactory > &)=0
 
virtual std::map< std::string, RequiredFixings::FixingDatesfixings (const QuantLib::Date &settlementDate=QuantLib::Date()) const
 
const RequiredFixingsrequiredFixings () const
 
virtual std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const
 
void reset ()
 Reset trade, clear all base class data. This does not reset accumulated timings for this trade. More...
 
void resetPricingStats (const std::size_t numberOfPricings=0, const boost::timer::nanosecond_type cumulativePricingTime=0)
 Reset accumulated timings to given values. More...
 
string & id ()
 Set the trade id. More...
 
void setEnvelope (const Envelope &envelope)
 Set the envelope with counterparty and portfolio info. More...
 
void setAdditionalData (const std::map< std::string, boost::any > &additionalData)
 
TradeActionstradeActions ()
 Set the trade actions. More...
 
const string & id () const
 
const string & tradeType () const
 
const Envelopeenvelope () const
 
const set< string > & portfolioIds () const
 
const TradeActionstradeActions () const
 
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument () const
 
const std::vector< QuantLib::Leg > & legs () const
 
const std::vector< string > & legCurrencies () const
 
const std::vector< bool > & legPayers () const
 
const string & npvCurrency () const
 
const Date & maturity () const
 
virtual bool isExpired (const Date &d)
 
const string & issuer () const
 
template<typename T >
additionalDatum (const std::string &tag) const
 returns any additional datum. More...
 
const std::string & sensitivityTemplate () const
 
void validate () const
 Utility to validate that everything that needs to be set in this base class is actually set. More...
 
virtual bool hasCashflows () const
 
boost::timer::nanosecond_type getCumulativePricingTime () const
 Get cumulative timing spent on pricing. More...
 
std::size_t getNumberOfPricings () const
 Get number of pricings. More...
 
- Public Member Functions inherited from XMLSerializable
virtual ~XMLSerializable ()
 
virtual void fromXML (XMLNode *node)=0
 
virtual XMLNodetoXML (XMLDocument &doc) const =0
 
void fromFile (const std::string &filename)
 
void toFile (const std::string &filename) const
 
void fromXMLString (const std::string &xml)
 Parse from XML string. More...
 
std::string toXMLString () const
 Parse from XML string. More...
 

Inspectors

vector< LegDatalegData_
 
string settlement_
 
bool isXCCY_ = false
 
bool isResetting_
 
Size notionalTakenFromLeg_
 
const vector< LegData > & legData () const
 
const std::map< std::string, boost::any > & additionalData () const override
 returns all additional data returned by the trade once built More...
 
virtual QuantLib::ext::shared_ptr< LegDatacreateLegData () const
 

Additional Inherited Members

- Protected Member Functions inherited from Trade
Date addPremiums (std::vector< QuantLib::ext::shared_ptr< Instrument > > &instruments, std::vector< Real > &multipliers, const Real tradeMultiplier, const PremiumData &premiumData, const Real premiumMultiplier, const Currency &tradeCurrency, const QuantLib::ext::shared_ptr< EngineFactory > &factory, const string &configuration)
 
void setLegBasedAdditionalData (const Size legNo, Size resultLegId=Null< Size >()) const
 
void setSensitivityTemplate (const EngineBuilder &builder)
 
void setSensitivityTemplate (const std::string &id)
 
- Protected Attributes inherited from Trade
string tradeType_
 
QuantLib::ext::shared_ptr< InstrumentWrapperinstrument_
 
std::vector< QuantLib::Leg > legs_
 
std::vector< string > legCurrencies_
 
std::vector< boollegPayers_
 
string npvCurrency_
 
QuantLib::Real notional_
 
string notionalCurrency_
 
Date maturity_
 
string issuer_
 
string sensitivityTemplate_
 
bool sensitivityTemplateSet_ = false
 
std::size_t savedNumberOfPricings_ = 0
 
boost::timer::nanosecond_type savedCumulativePricingTime_ = 0
 
RequiredFixings requiredFixings_
 
std::map< std::string, boost::any > additionalData_
 

Detailed Description

Serializable Swap, Single and Cross Currency.

Definition at line 36 of file swap.hpp.

Constructor & Destructor Documentation

◆ Swap() [1/4]

Swap ( const string  swapType = "Swap")

Default constructor.

Definition at line 39 of file swap.hpp.

39: Trade(swapType) {}
Trade()
Default constructor.
Definition: trade.hpp:59

◆ Swap() [2/4]

Swap ( const Envelope env,
const string  swapType = "Swap" 
)

Definition at line 41 of file swap.hpp.

41: Trade(swapType, env) {}

◆ Swap() [3/4]

Swap ( const Envelope env,
const vector< LegData > &  legData,
const string  swapType = "Swap",
const std::string  settlement = "Physical" 
)

Constructor with vector of LegData.

Definition at line 44 of file swap.hpp.

46 : Trade(swapType, env), legData_(legData), settlement_(settlement) {}
string settlement_
Definition: swap.hpp:83
vector< LegData > legData_
Definition: swap.hpp:82
+ Here is the call graph for this function:

◆ Swap() [4/4]

Swap ( const Envelope env,
const LegData leg0,
const LegData leg1,
const string  swapType = "Swap",
const std::string  settlement = "Physical" 
)

Constructor with two legs.

Definition at line 49 of file swap.hpp.

51 : Trade(swapType, env), legData_({leg0, leg1}), settlement_(settlement) {}

Member Function Documentation

◆ build()

void build ( const QuantLib::ext::shared_ptr< EngineFactory > &  engineFactory)
overridevirtual

Build QuantLib/QuantExt instrument, link pricing engine.

Implements Trade.

Reimplemented in CrossCurrencySwap, EquitySwap, InflationSwap, and ForwardRateAgreement.

Definition at line 45 of file swap.cpp.

45 {
46 DLOG("Swap::build() called for trade " << id());
47
49
50 QL_REQUIRE(legData_.size() >= 1, "Swap must have at least 1 leg");
51 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
52
53 // allow minor currencies in case first leg is equity
54 Currency currency = parseCurrencyWithMinors(legData_[0].currency());
55 string ccy_str = currency.code();
56
57 Size numLegs = legData_.size();
58 legPayers_ = vector<bool>(numLegs);
59 std::vector<QuantLib::Currency> currencies(numLegs);
60 std::vector<QuantLib::Currency> currenciesForMcSimulation;
61 legs_.resize(numLegs);
62
63 isXCCY_ = false;
64 isResetting_ = false;
65
66 for (Size i = 0; i < numLegs; ++i) {
67 // allow minor currencies for Equity legs as some exchanges trade in these, e.g LSE in pence - GBX or GBp
68 // minor currencies on other legs will fail here
69 if (legData_[i].legType() == "Equity")
70 currencies[i] = parseCurrencyWithMinors(legData_[i].currency());
71 else
72 currencies[i] = parseCurrency(legData_[i].currency());
73
74 if (currencies[i] != currency)
75 isXCCY_ = true;
76 isResetting_ = isResetting_ || (!legData_[i].isNotResetXCCY());
77 }
78
79
80 // Check if there is indexing is used, need to collect all underlying currrencies
81 // for AMC simulations, such a trade needs to be treated a x-ccy swap with both leg paying
82 // one currency.
83 auto addUnique = [](vector<Currency>& currencies, Currency ccy) {
84 if (std::find(currencies.begin(), currencies.end(), ccy) ==
85 currencies.end()) {
86 currencies.push_back(ccy);
87 }
88 };
89
90 for (Size i = 0; i < numLegs; ++i) {
91 addUnique(currenciesForMcSimulation, currencies[i]);
92 vector<Indexing> indexings = legData_[i].indexing();
93 if (!indexings.empty() && indexings.front().hasData()) {
94 Indexing indexing = indexings.front();
95 if (boost::starts_with(indexing.index(), "FX-")) {
96 auto index = parseFxIndex(indexing.index());
97 addUnique(currenciesForMcSimulation, index->targetCurrency());
98 addUnique(currenciesForMcSimulation, index->sourceCurrency());
99 }
100 }
101 }
102 isXCCY_ = isXCCY_ || currenciesForMcSimulation.size() > 1;
103 static std::set<std::string> eligibleForXbs = {"Fixed", "Floating"};
104 bool useXbsCurves = true;
105 for(Size i=0;i<numLegs;++i) {
106 useXbsCurves = useXbsCurves && (eligibleForXbs.find(legData_[i].legType()) != eligibleForXbs.end());
107 }
108
109 // The npv currency, notional currency and current notional are taken from the first leg that
110 // appears in the XML that has a notional. If no such leg exists the notional currency
111 // and current notional are left empty and the npv currency is set to the first leg's currency
112
115 const LegData& d = legData_[notionalTakenFromLeg_];
116 if (!d.notionals().empty())
117 break;
118 }
119
120 if (notionalTakenFromLeg_ == legData_.size()) {
121 ALOG("no suitable leg found to set notional, set to null and notionalCurrency to empty string");
122 notional_ = Null<Real>();
124 // parse for currency in case first leg is Equity, we only want the major currency for NPV
125 npvCurrency_ = parseCurrencyWithMinors(legData_.front().currency()).code();
126 } else {
127 if (legData_[notionalTakenFromLeg_].schedule().hasData()) {
128 Schedule schedule = makeSchedule(legData_[notionalTakenFromLeg_].schedule());
129 auto notional =
130 buildScheduledVectorNormalised(legData_[notionalTakenFromLeg_].notionals(),
131 legData_[notionalTakenFromLeg_].notionalDates(), schedule, 0.0);
132 Date today = Settings::instance().evaluationDate();
133 auto d = std::upper_bound(schedule.dates().begin(), schedule.dates().end(), today);
134 // forward starting => take first notional
135 // on or after last schedule date => zero notional
136 // in between => notional of current period
137 if (d == schedule.dates().begin())
138 notional_ = notional.at(0);
139 else if (d == schedule.dates().end())
140 notional_ = 0.0;
141 else
142 notional_ = notional.at(std::distance(schedule.dates().begin(), d) - 1);
143 } else {
144 notional_ = legData_[notionalTakenFromLeg_].notionals().at(0);
145 }
146 // parse for currency in case leg is Equity, we only want the major currency for NPV and Notional
147 notionalCurrency_ = parseCurrencyWithMinors(legData_[notionalTakenFromLeg_].currency()).code();
148 npvCurrency_ = parseCurrencyWithMinors(legData_[notionalTakenFromLeg_].currency()).code();
149 DLOG("Notional is " << notional_ << " " << notionalCurrency_);
150 }
151
152 Currency npvCcy = parseCurrency(npvCurrency_);
153 DLOG("npv currency is " << npvCurrency_);
154
155 QuantLib::ext::shared_ptr<EngineBuilder> builder =
156 isXCCY_ ? engineFactory->builder("CrossCurrencySwap") : engineFactory->builder("Swap");
157 auto configuration = builder->configuration(MarketContext::pricing);
158
159 for (Size i = 0; i < numLegs; ++i) {
160 legPayers_[i] = legData_[i].isPayer();
161 auto legBuilder = engineFactory->legBuilder(legData_[i].legType());
162 legs_[i] = legBuilder->buildLeg(legData_[i], engineFactory, requiredFixings_, configuration, Null<Date>(),
163 useXbsCurves);
164 DLOG("Swap::build(): currency[" << i << "] = " << currencies[i]);
165
166 // add notional leg, if applicable
167 auto leg = buildNotionalLeg(legData_[i], legs_[i], requiredFixings_, engineFactory->market(), configuration);
168 applyIndexing(leg, legData_[i], engineFactory, requiredFixings_, Null<Date>(), useXbsCurves);
169 if (!leg.empty()) {
170 legs_.push_back(leg);
171 legPayers_.push_back(legPayers_[i]);
172 currencies.push_back(currencies[i]);
173 }
174 } // for legs
175
176 if (isXCCY_) {
177 QuantLib::ext::shared_ptr<QuantExt::CurrencySwap> swap(
178 new QuantExt::CurrencySwap(legs_, legPayers_, currencies, settlement_ == "Physical", isResetting_));
179 QuantLib::ext::shared_ptr<CrossCurrencySwapEngineBuilderBase> swapBuilder =
180 QuantLib::ext::dynamic_pointer_cast<CrossCurrencySwapEngineBuilderBase>(builder);
181 QL_REQUIRE(swapBuilder, "No Builder found for CrossCurrencySwap " << id());
182 swap->setPricingEngine(swapBuilder->engine(currenciesForMcSimulation, npvCcy));
183 setSensitivityTemplate(*swapBuilder);
184 // take the first legs currency as the npv currency (arbitrary choice)
185 instrument_.reset(new VanillaInstrument(swap));
186 } else {
187 QuantLib::ext::shared_ptr<QuantLib::Swap> swap(new QuantLib::Swap(legs_, legPayers_));
188 QuantLib::ext::shared_ptr<SwapEngineBuilderBase> swapBuilder =
189 QuantLib::ext::dynamic_pointer_cast<SwapEngineBuilderBase>(builder);
190 QL_REQUIRE(swapBuilder, "No Builder found for Swap " << id());
191 swap->setPricingEngine(swapBuilder->engine(npvCcy, envelope().additionalField("discount_curve", false),
192 envelope().additionalField("security_spread", false)));
193 setSensitivityTemplate(*swapBuilder);
194 instrument_.reset(new VanillaInstrument(swap));
195 }
196
197 DLOG("Set instrument wrapper");
198
199 // set Leg Currencies
200 legCurrencies_ = vector<string>(currencies.size());
201 for (Size i = 0; i < currencies.size(); i++)
202 legCurrencies_[i] = currencies[i].code();
203
204 // set maturity
205 maturity_ = Date::minDate();
206 Date startDate = Date::maxDate();
207 for (auto const& l : legs_) {
208 if (!l.empty()) {
209 maturity_ = std::max(maturity_, l.back()->date());
210 startDate = std::min(startDate, l.front()->date());
211 QuantLib::ext::shared_ptr<Coupon> coupon = QuantLib::ext::dynamic_pointer_cast<Coupon>(l.front());
212 if (coupon)
213 startDate = std::min(startDate, coupon->accrualStartDate());
214 }
215 }
216
217 additionalData_["startDate"] = to_string(startDate);
218}
bool isResetting_
Definition: swap.hpp:87
bool isXCCY_
Definition: swap.hpp:84
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
Definition: swap.cpp:267
Size notionalTakenFromLeg_
Definition: swap.hpp:88
virtual void setIsdaTaxonomyFields()
Definition: swap.cpp:220
string npvCurrency_
Definition: trade.hpp:201
std::vector< bool > legPayers_
Definition: trade.hpp:200
std::vector< string > legCurrencies_
Definition: trade.hpp:199
std::vector< QuantLib::Leg > legs_
Definition: trade.hpp:198
QuantLib::Real notional_
Definition: trade.hpp:202
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
const Envelope & envelope() const
Definition: trade.hpp:135
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
string notionalCurrency_
Definition: trade.hpp:203
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
QuantLib::ext::shared_ptr< FxIndex > parseFxIndex(const string &s, const Handle< Quote > &fxSpot, const Handle< YieldTermStructure > &sourceYts, const Handle< YieldTermStructure > &targetYts, const bool useConventions)
Convert std::string to QuantExt::FxIndex.
Currency parseCurrencyWithMinors(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:310
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
vector< T > buildScheduledVectorNormalised(const vector< T > &values, const vector< string > &dates, const Schedule &schedule, const T &defaultValue, const bool checkAllValuesAppearInResult=false)
Definition: legdata.hpp:1139
void applyIndexing(Leg &leg, const LegData &data, const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, RequiredFixings &requiredFixings, const QuantLib::Date &openEndDateReplacement, const bool useXbsCurves)
Definition: legdata.cpp:2633
Leg buildNotionalLeg(const LegData &data, const Leg &leg, RequiredFixings &requiredFixings, const QuantLib::ext::shared_ptr< Market > &market, const std::string &configuration)
Definition: legdata.cpp:2729
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setIsdaTaxonomyFields()

void setIsdaTaxonomyFields ( )
virtual

Reimplemented in EquitySwap, and InflationSwap.

Definition at line 220 of file swap.cpp.

220 {
221 // ISDA taxonomy
222 additionalData_["isdaAssetClass"] = string("Interest Rate");
223 additionalData_["isdaBaseProduct"] = string(isXCCY_ ? "Cross Currency" : "IR Swap");
224 additionalData_["isdaSubProduct"] = isdaSubProductSwap(id(), legData_);
225 additionalData_["isdaTransaction"] = string("");
226}
std::string isdaSubProductSwap(const std::string &tradeId, const vector< LegData > &legData)
Definition: swap.cpp:323
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ notional()

QuantLib::Real notional ( ) const
overridevirtual

Return the current notional in npvCurrency. See individual sub-classes for the precise definition.

Reimplemented from Trade.

Definition at line 267 of file swap.cpp.

267 {
268 // try to get the notional from the additional results of the instrument
269 try {
270 return instrument_->qlInstrument(true)->result<Real>("currentNotional");
271 } catch (const std::exception& e) {
272 WLOG("swap engine does not provide current notional: " << e.what() << ", using fallback");
273 // Try getting current notional from coupons
274 if (notionalTakenFromLeg_ < legs_.size()) {
275 Real n = currentNotional(legs_[notionalTakenFromLeg_]);
276 if (fabs(n) > QL_EPSILON) {
277 return n;
278 }
279 }
280 // else return the face value
281 WLOG("swap does not provide coupon notionals, using face value");
282 return notional_;
283 }
284}
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
Real currentNotional(const Leg &leg)
Definition: legdata.cpp:2435
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ notionalCurrency()

std::string notionalCurrency ( ) const
overridevirtual

Reimplemented from Trade.

Definition at line 286 of file swap.cpp.

286 {
287 // try to get the notional ccy from the additional results of the instrument
288 try {
289 return instrument_->qlInstrument(true)->result<std::string>("notionalCurrency");
290 } catch (const std::exception& e) {
291 if (strcmp(e.what(), "notionalCurrency not provided"))
292 WLOG("swap engine does not provide notional ccy: " << e.what() << ", using fallback");
293 return notionalCurrency_;
294 }
295}
+ Here is the caller graph for this function:

◆ underlyingIndices()

map< AssetClass, set< string > > underlyingIndices ( const QuantLib::ext::shared_ptr< ReferenceDataManager > &  referenceDataManager = nullptr) const
overridevirtual

Add underlying index names.

Reimplemented from Trade.

Definition at line 298 of file swap.cpp.

298 {
299
300 map<AssetClass, set<string>> result;
301 for (const auto& ld : legData_) {
302 for (auto ind : ld.indices()) {
303 // only handle equity and commodity for now
304 if (ind.substr(0, 5) != "COMM-" && ind.substr(0, 3) != "EQ-")
305 continue;
306
307 QuantLib::ext::shared_ptr<Index> index = parseIndex(ind);
308
309 if (auto ei = QuantLib::ext::dynamic_pointer_cast<EquityIndex2>(index)) {
310 result[AssetClass::EQ].insert(ei->name());
311 } else if (auto ci = QuantLib::ext::dynamic_pointer_cast<QuantExt::CommodityIndex>(index)) {
312 result[AssetClass::COM].insert(ci->name());
313 }
314 }
315 }
316
317 if (auto s = envelope().additionalField("security_spread", false); !s.empty())
318 result[AssetClass::BOND] = {s};
319
320 return result;
321}
QuantLib::ext::shared_ptr< Index > parseIndex(const string &s)
Convert std::string to QuantLib::Index.
+ Here is the call graph for this function:

◆ settlement()

const string & settlement ( ) const

Settlement Type can be set to "Cash" for NDF. Default value is "Physical".

Definition at line 64 of file swap.hpp.

64{ return settlement_; }
+ Here is the caller graph for this function:

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Reimplemented in ForwardRateAgreement.

Definition at line 361 of file swap.cpp.

361 {
362 Trade::fromXML(node);
363 legData_.clear();
364 XMLNode* swapNode = XMLUtils::getChildNode(node, tradeType() + "Data");
365 // backwards compatibility
366 if (swapNode == nullptr) {
367 swapNode = XMLUtils::getChildNode(node, "SwapData");
368 }
369 QL_REQUIRE(swapNode, "Swap::fromXML(): expected '" << tradeType() << "Data'"
370 << (tradeType() == "Swap" ? "" : " or 'SwapData'"));
371
372 settlement_ = XMLUtils::getChildValue(swapNode, "Settlement", false);
373 if (settlement_ == "")
374 settlement_ = "Physical";
375
376 vector<XMLNode*> nodes = XMLUtils::getChildrenNodes(swapNode, "LegData");
377 for (Size i = 0; i < nodes.size(); i++) {
378 auto ld = createLegData();
379 ld->fromXML(nodes[i]);
380 legData_.push_back(*QuantLib::ext::static_pointer_cast<LegData>(ld));
381 }
382}
virtual QuantLib::ext::shared_ptr< LegData > createLegData() const
Definition: swap.cpp:384
const string & tradeType() const
Definition: trade.hpp:133
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
+ Here is the call graph for this function:

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Reimplemented in ForwardRateAgreement.

Definition at line 386 of file swap.cpp.

386 {
387 XMLNode* node = Trade::toXML(doc);
388 XMLNode* swapNode = doc.allocNode(tradeType() + "Data");
389 XMLUtils::appendNode(node, swapNode);
390
391 if (settlement_ == "Cash")
392 XMLUtils::addChild(doc, swapNode, "Settlement", settlement_);
393 for (Size i = 0; i < legData_.size(); i++)
394 XMLUtils::appendNode(swapNode, legData_[i].toXML(doc));
395 return node;
396}
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: swap.cpp:386
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ legData()

const vector< LegData > & legData ( ) const

Definition at line 74 of file swap.hpp.

74{ return legData_; }
+ Here is the caller graph for this function:

◆ additionalData()

const std::map< std::string, boost::any > & additionalData ( ) const
overridevirtual

returns all additional data returned by the trade once built

Reimplemented from Trade.

Definition at line 228 of file swap.cpp.

228 {
229 Size numLegs = legData_.size();
230 // use the build time as of date to determine current notionals
231 QuantLib::ext::shared_ptr<QuantLib::Swap> swap = QuantLib::ext::dynamic_pointer_cast<QuantLib::Swap>(instrument_->qlInstrument());
232 QuantLib::ext::shared_ptr<QuantExt::CurrencySwap> cswap = QuantLib::ext::dynamic_pointer_cast<QuantExt::CurrencySwap>(instrument_->qlInstrument());
233 std::map<std::string, Real> legNpv; // by currency
234 for (Size i = 0; i < numLegs; ++i) {
235 string legID = to_string(i+1);
236 additionalData_["legType[" + legID + "]"] = legData_[i].legType();
237 additionalData_["isPayer[" + legID + "]"] = legData_[i].isPayer();
238 additionalData_["notionalCurrency[" + legID + "]"] = legData_[i].currency();
239 if (!isXCCY_) {
240 if (swap)
241 additionalData_["legNPV[" + legID + "]"] = swap->legNPV(i);
242 else
243 ALOG("single currency swap underlying instrument not set, skip leg npv reporting");
244 }
245 else {
246 if (cswap) {
247 // The currency swap has more legs than the swap wrapper (additional notional legs), so aggregate by currency
248 Real legNpv = 0;
249 Real legNpvInCcy = 0;
250 for (Size j = 0; j < cswap->legs().size(); ++j) {
251 if (cswap->legCurrency(j).code() == legData_[i].currency()) {
252 legNpv += cswap->legNPV(j);
253 legNpvInCcy += cswap->inCcyLegNPV(j);
254 }
255 }
256 additionalData_["legNPV[" + legID + "]"] = legNpv;
257 additionalData_["legNPVCCY[" + legID + "]"] = legNpvInCcy;
258 }
259 else
260 ALOG("cross currency swap underlying instrument not set, skip leg npv reporting");
261 }
263 }
264 return additionalData_;
265}
void setLegBasedAdditionalData(const Size legNo, Size resultLegId=Null< Size >()) const
Definition: trade.cpp:153
Size size(const ValueType &v)
Definition: value.cpp:145
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createLegData()

QuantLib::ext::shared_ptr< LegData > createLegData ( ) const
protectedvirtual

Definition at line 384 of file swap.cpp.

384{ return QuantLib::ext::make_shared<LegData>(); }
+ Here is the caller graph for this function:

Member Data Documentation

◆ legData_

vector<LegData> legData_
protected

Definition at line 82 of file swap.hpp.

◆ settlement_

string settlement_
protected

Definition at line 83 of file swap.hpp.

◆ isXCCY_

bool isXCCY_ = false
protected

Definition at line 84 of file swap.hpp.

◆ isResetting_

bool isResetting_
private

Definition at line 87 of file swap.hpp.

◆ notionalTakenFromLeg_

Size notionalTakenFromLeg_
private

Definition at line 88 of file swap.hpp.