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

Serializable FX Swap. More...

#include <ored/portfolio/fxswap.hpp>

+ Inheritance diagram for FxSwap:
+ Collaboration diagram for FxSwap:

Public Member Functions

 FxSwap ()
 Default constructor. More...
 
 FxSwap (const Envelope &env, const string &nearDate, const string &farDate, const string &nearBoughtCurrency, double nearBoughtAmount, const string &nearSoldCurrency, double nearSoldAmount, double farBoughtAmount, double farSoldAmount, const string &settlement="Physical")
 Constructor. More...
 
void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 Build QuantLib/QuantExt instrument, link pricing engine. More...
 
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
 
Inspectors
const string & nearDate () const
 
const string & farDate () const
 
const string & nearBoughtCurrency () const
 
double nearBoughtAmount () const
 
const string & nearSoldCurrency () const
 
double nearSoldAmount () const
 
double farBoughtAmount () const
 
double farSoldAmount () const
 
const string & settlement () const
 Settlement Type can be set to "Cash" for NDF. Default value is "Physical". More...
 
- 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...
 
virtual const std::map< std::string, boost::any > & additionalData () const
 returns all additional data returned by the trade once built 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...
 

Serialisation

string nearDate_
 
string farDate_
 
string nearBoughtCurrency_
 
double nearBoughtAmount_
 
string nearSoldCurrency_
 
double nearSoldAmount_
 
double farBoughtAmount_
 
double farSoldAmount_
 
string settlement_
 
QuantLib::ext::shared_ptr< QuantLib::Instrument > instNear_
 
QuantLib::ext::shared_ptr< QuantLib::Instrument > instFar_
 
virtual void fromXML (XMLNode *node) override
 
virtual XMLNodetoXML (XMLDocument &doc) const override
 

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 FX Swap.

Definition at line 36 of file fxswap.hpp.

Constructor & Destructor Documentation

◆ FxSwap() [1/2]

FxSwap ( )

Default constructor.

Definition at line 39 of file fxswap.hpp.

40 : Trade("FxSwap"), nearBoughtAmount_(0.0), nearSoldAmount_(0.0), farBoughtAmount_(0.0), farSoldAmount_(0.0) {}
double farBoughtAmount_
Definition: fxswap.hpp:84
double farSoldAmount_
Definition: fxswap.hpp:85
double nearBoughtAmount_
Definition: fxswap.hpp:81
double nearSoldAmount_
Definition: fxswap.hpp:83
Trade()
Default constructor.
Definition: trade.hpp:59

◆ FxSwap() [2/2]

FxSwap ( const Envelope env,
const string &  nearDate,
const string &  farDate,
const string &  nearBoughtCurrency,
double  nearBoughtAmount,
const string &  nearSoldCurrency,
double  nearSoldAmount,
double  farBoughtAmount,
double  farSoldAmount,
const string &  settlement = "Physical" 
)

Constructor.

Definition at line 42 of file fxswap.hpp.

const string & nearDate() const
Definition: fxswap.hpp:60
string settlement_
Definition: fxswap.hpp:86
string nearDate_
Definition: fxswap.hpp:78
string nearSoldCurrency_
Definition: fxswap.hpp:82
string nearBoughtCurrency_
Definition: fxswap.hpp:80
const string & settlement() const
Settlement Type can be set to "Cash" for NDF. Default value is "Physical".
Definition: fxswap.hpp:69
double nearBoughtAmount() const
Definition: fxswap.hpp:63
double nearSoldAmount() const
Definition: fxswap.hpp:65
const string & nearBoughtCurrency() const
Definition: fxswap.hpp:62
string farDate_
Definition: fxswap.hpp:79
double farBoughtAmount() const
Definition: fxswap.hpp:66
double farSoldAmount() const
Definition: fxswap.hpp:67
const string & farDate() const
Definition: fxswap.hpp:61
const string & nearSoldCurrency() const
Definition: fxswap.hpp:64

Member Function Documentation

◆ build()

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

Build QuantLib/QuantExt instrument, link pricing engine.

Constructs a composite pricing engine of two FX forward pricing engines. One with the near amounts as notionals, the other with the far amounts. NPV is the total npv of these trades.

Implements Trade.

Definition at line 34 of file fxswap.cpp.

34 {
35
36 // ISDA taxonomy
37 additionalData_["isdaAssetClass"] = string("Foreign Exchange");
38 additionalData_["isdaBaseProduct"] = string(settlement_ == "Cash" ? "NDF" : "Forward");
39 additionalData_["isdaSubProduct"] = string("");
40 additionalData_["isdaTransaction"] = string("");
41
42 Currency nearBoughtCcy = data::parseCurrency(nearBoughtCurrency_);
43 Currency nearSoldCcy = data::parseCurrency(nearSoldCurrency_);
47
51
52 try {
53 DLOG("FxSwap::build() called for trade " << id());
54 //QuantLib::ext::shared_ptr<QuantLib::Instrument> instNear;
55 // builds two fxforwards and sums the npvs
56 // so that both npvs are in the same currency, the value of the first forward is taken to be the negative of the
57 // counterparty's npv
58 // npv_total= -npv1+npv2
59 instNear_.reset(
60 new QuantExt::FxForward(nearSoldAmount_, nearSoldCcy, nearBoughtAmount_, nearBoughtCcy, nearDate, false));
61 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder("FxForward");
62 QL_REQUIRE(builder, "No builder found for " << tradeType_);
63 QuantLib::ext::shared_ptr<FxForwardEngineBuilderBase> fxBuilder =
64 QuantLib::ext::dynamic_pointer_cast<FxForwardEngineBuilderBase>(builder);
65 instNear_->setPricingEngine(fxBuilder->engine(nearSoldCcy, nearBoughtCcy));
66 setSensitivityTemplate(*fxBuilder);
67 instFar_.reset(
68 new QuantExt::FxForward(farBoughtAmount_, nearSoldCcy, farSoldAmount_, nearBoughtCcy, farDate, false));
69 instFar_->setPricingEngine(fxBuilder->engine(nearSoldCcy, nearBoughtCcy));
70
71 DLOG("FxSwap::build(): Near NPV = " << instNear_->NPV());
72 DLOG("FxSwap::build(): Far NPV = " << instFar_->NPV());
73 // TODO: cannot use a CompositeInstrument
74 QuantLib::ext::shared_ptr<CompositeInstrument> composite(new CompositeInstrument());
75 composite->add(instNear_, -1.0);
76 composite->add(instFar_, 1.0);
77 instrument_.reset(new VanillaInstrument(composite));
78
79 } catch (std::exception&) {
80 instrument_.reset();
81 throw;
82 }
83 // Set up Legs
84 legs_.clear();
85 legs_.resize(4);
86 legCurrencies_.resize(4);
87 legPayers_.resize(4);
88 legs_[0].push_back(QuantLib::ext::shared_ptr<CashFlow>(new SimpleCashFlow(nearBoughtAmount_, nearDate)));
89 legs_[1].push_back(QuantLib::ext::shared_ptr<CashFlow>(new SimpleCashFlow(nearSoldAmount_, nearDate)));
90 legs_[2].push_back(QuantLib::ext::shared_ptr<CashFlow>(new SimpleCashFlow(farBoughtAmount_, farDate)));
91 legs_[3].push_back(QuantLib::ext::shared_ptr<CashFlow>(new SimpleCashFlow(farSoldAmount_, farDate)));
92
97
98 legPayers_[0] = false;
99 legPayers_[1] = true;
100 legPayers_[2] = false;
101 legPayers_[3] = true;
102
103 additionalData_["farSoldCurrency"] = nearBoughtCurrency_;
104 additionalData_["farBoughtCurrency"] = nearSoldCurrency_;
105 additionalData_["farSoldAmount"] = farSoldAmount_;
106 additionalData_["farBoughtAmount"] = farBoughtAmount_;
107 additionalData_["nearSoldCurrency"] = nearSoldCurrency_;
108 additionalData_["nearBoughtCurrency"] = nearBoughtCurrency_;
109 additionalData_["nearSoldAmount"] = nearSoldAmount_;
110 additionalData_["nearBoughtAmount"] = nearBoughtAmount_;
111
112 DLOG("FxSwap leg 0: " << nearDate_ << " " << legs_[0][0]->amount());
113 DLOG("FxSwap leg 1: " << nearDate_ << " " << legs_[1][0]->amount());
114 DLOG("FxSwap leg 2: " << farDate_ << " " << legs_[2][0]->amount());
115 DLOG("FxSwap leg 3: " << farDate_ << " " << legs_[3][0]->amount());
116}
QuantLib::ext::shared_ptr< QuantLib::Instrument > instFar_
Definition: fxswap.hpp:87
QuantLib::ext::shared_ptr< QuantLib::Instrument > instNear_
Definition: fxswap.hpp:87
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
string tradeType_
Definition: trade.hpp:196
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
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
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
+ Here is the call 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 119 of file fxswap.cpp.

119 {
120 // try to get the notional from the additional results of the instrument
121 try {
122 return instFar_->result<Real>("currentNotional");
123 } catch (const std::exception& e) {
124 if (strcmp(e.what(), "currentNotional not provided"))
125 ALOG("error when retrieving notional: " << e.what());
126 }
127 // if not provided, return original/fallback amount
128 return notional_;
129}
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544

◆ notionalCurrency()

std::string notionalCurrency ( ) const
overridevirtual

Reimplemented from Trade.

Definition at line 131 of file fxswap.cpp.

131 {
132 // try to get the notional ccy from the additional results of the instrument
133 try {
134 return instFar_->result<std::string>("notionalCurrency");
135 } catch (const std::exception& e) {
136 if (strcmp(e.what(), "notionalCurrency not provided"))
137 ALOG("error when retrieving notional ccy: " << e.what());
138 }
139 // if not provided, return original/fallback value
140 return notionalCurrency_;
141}

◆ nearDate()

const string & nearDate ( ) const

Definition at line 60 of file fxswap.hpp.

60{ return nearDate_; }
+ Here is the caller graph for this function:

◆ farDate()

const string & farDate ( ) const

Definition at line 61 of file fxswap.hpp.

61{ return farDate_; }
+ Here is the caller graph for this function:

◆ nearBoughtCurrency()

const string & nearBoughtCurrency ( ) const

Definition at line 62 of file fxswap.hpp.

62{ return nearBoughtCurrency_; }

◆ nearBoughtAmount()

double nearBoughtAmount ( ) const

Definition at line 63 of file fxswap.hpp.

63{ return nearBoughtAmount_; }

◆ nearSoldCurrency()

const string & nearSoldCurrency ( ) const

Definition at line 64 of file fxswap.hpp.

64{ return nearSoldCurrency_; }

◆ nearSoldAmount()

double nearSoldAmount ( ) const

Definition at line 65 of file fxswap.hpp.

65{ return nearSoldAmount_; }

◆ farBoughtAmount()

double farBoughtAmount ( ) const

Definition at line 66 of file fxswap.hpp.

66{ return farBoughtAmount_; }

◆ farSoldAmount()

double farSoldAmount ( ) const

Definition at line 67 of file fxswap.hpp.

67{ return farSoldAmount_; }

◆ settlement()

const string & settlement ( ) const

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

Definition at line 69 of file fxswap.hpp.

69{ return settlement_; }

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 143 of file fxswap.cpp.

143 {
144 Trade::fromXML(node);
145 XMLNode* fxNode = XMLUtils::getChildNode(node, "FxSwapData");
146 nearDate_ = XMLUtils::getChildValue(fxNode, "NearDate", true);
147 farDate_ = XMLUtils::getChildValue(fxNode, "FarDate", true);
148 nearBoughtCurrency_ = XMLUtils::getChildValue(fxNode, "NearBoughtCurrency", true);
149 nearSoldCurrency_ = XMLUtils::getChildValue(fxNode, "NearSoldCurrency", true);
150 nearBoughtAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "NearBoughtAmount", true);
151 nearSoldAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "NearSoldAmount", true);
152 farBoughtAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "FarBoughtAmount", true);
153 farSoldAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "FarSoldAmount", true);
154 settlement_ = XMLUtils::getChildValue(fxNode, "Settlement", false);
155 if (settlement_ == "")
156 settlement_ = "Physical";
157}
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
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
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.

Definition at line 159 of file fxswap.cpp.

159 {
160 XMLNode* node = Trade::toXML(doc);
161 XMLNode* fxNode = doc.allocNode("FxSwapData");
162 XMLUtils::appendNode(node, fxNode);
163 XMLUtils::addChild(doc, fxNode, "NearDate", nearDate_);
164 XMLUtils::addChild(doc, fxNode, "FarDate", farDate_);
165 XMLUtils::addChild(doc, fxNode, "NearBoughtCurrency", nearBoughtCurrency_);
166 XMLUtils::addChild(doc, fxNode, "NearBoughtAmount", nearBoughtAmount_);
167 XMLUtils::addChild(doc, fxNode, "NearSoldCurrency", nearSoldCurrency_);
168 XMLUtils::addChild(doc, fxNode, "NearSoldAmount", nearSoldAmount_);
169 XMLUtils::addChild(doc, fxNode, "FarBoughtAmount", farBoughtAmount_);
170 XMLUtils::addChild(doc, fxNode, "FarSoldAmount", farSoldAmount_);
171 XMLUtils::addChild(doc, fxNode, "Settlement", settlement_);
172
173 return node;
174}
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
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
+ Here is the call graph for this function:

Member Data Documentation

◆ nearDate_

string nearDate_
private

Definition at line 78 of file fxswap.hpp.

◆ farDate_

string farDate_
private

Definition at line 79 of file fxswap.hpp.

◆ nearBoughtCurrency_

string nearBoughtCurrency_
private

Definition at line 80 of file fxswap.hpp.

◆ nearBoughtAmount_

double nearBoughtAmount_
private

Definition at line 81 of file fxswap.hpp.

◆ nearSoldCurrency_

string nearSoldCurrency_
private

Definition at line 82 of file fxswap.hpp.

◆ nearSoldAmount_

double nearSoldAmount_
private

Definition at line 83 of file fxswap.hpp.

◆ farBoughtAmount_

double farBoughtAmount_
private

Definition at line 84 of file fxswap.hpp.

◆ farSoldAmount_

double farSoldAmount_
private

Definition at line 85 of file fxswap.hpp.

◆ settlement_

string settlement_
private

Definition at line 86 of file fxswap.hpp.

◆ instNear_

QuantLib::ext::shared_ptr<QuantLib::Instrument> instNear_
private

Definition at line 87 of file fxswap.hpp.

◆ instFar_

QuantLib::ext::shared_ptr<QuantLib::Instrument> instFar_
private

Definition at line 87 of file fxswap.hpp.