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

Serializable FX Forward. More...

#include <ored/portfolio/fxforward.hpp>

+ Inheritance diagram for FxForward:
+ Collaboration diagram for FxForward:

Public Member Functions

 FxForward ()
 Default constructor. More...
 
 FxForward (const Envelope &env, const string &maturityDate, const string &boughtCurrency, double boughtAmount, const string &soldCurrency, double soldAmount, const string &settlement="Physical", const string &fxIndex="", const string &payDate="")
 Constructor. More...
 
bool isExpired (const Date &d) override
 
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 & maturityDate () const
 
const string & boughtCurrency () const
 
double boughtAmount () const
 
const string & soldCurrency () const
 
double soldAmount () const
 
const string & settlement () const
 Settlement Type can be set to "Cash" for NDF. Default value is "Physical". More...
 
const string & fxIndex () const
 
const string & paymentDate () const
 
- 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
 
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 maturityDate_
 
string boughtCurrency_
 
double boughtAmount_
 
string soldCurrency_
 
double soldAmount_
 
string settlement_
 
string payCurrency_
 
string fxIndex_
 
string payDate_
 
string payLag_
 
string payCalendar_
 
string payConvention_
 
bool includeSettlementDateFlows_
 
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 Forward.

Definition at line 36 of file fxforward.hpp.

Constructor & Destructor Documentation

◆ FxForward() [1/2]

FxForward ( )

Default constructor.

Definition at line 39 of file fxforward.hpp.

39: Trade("FxForward"), boughtAmount_(0.0), soldAmount_(0.0) {}
Trade()
Default constructor.
Definition: trade.hpp:59

◆ FxForward() [2/2]

FxForward ( const Envelope env,
const string &  maturityDate,
const string &  boughtCurrency,
double  boughtAmount,
const string &  soldCurrency,
double  soldAmount,
const string &  settlement = "Physical",
const string &  fxIndex = "",
const string &  payDate = "" 
)

Constructor.

Definition at line 41 of file fxforward.hpp.

44 : Trade("FxForward", env), maturityDate_(maturityDate), boughtCurrency_(boughtCurrency),
45 boughtAmount_(boughtAmount), soldCurrency_(soldCurrency), soldAmount_(soldAmount), settlement_(settlement),
Date maturityDate() const
boost::shared_ptr< QuantExt::FxIndex > fxIndex() const
Date payDate() const
boost::shared_ptr< FxIndex > fxIndex_
+ Here is the call graph for this function:

Member Function Documentation

◆ isExpired()

bool isExpired ( const Date &  d)
overridevirtual

Reimplemented from Trade.

+ Here is the call graph for this function:

◆ build()

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

Build QuantLib/QuantExt instrument, link pricing engine.

Implements Trade.

Definition at line 33 of file fxforward.cpp.

33 {
34
35 // ISDA taxonomy
36 additionalData_["isdaAssetClass"] = string("Foreign Exchange");
37 additionalData_["isdaBaseProduct"] = string(settlement_ == "Cash" ? "NDF" : "Forward");
38 additionalData_["isdaSubProduct"] = string("");
39 additionalData_["isdaTransaction"] = string("");
40
41 additionalData_["soldCurrency"] = soldCurrency_;
42 additionalData_["boughtCurrency"] = boughtCurrency_;
43 additionalData_["soldAmount"] = soldAmount_;
44 additionalData_["boughtAmount"] = boughtAmount_;
45 additionalData_["valueDate"] = maturityDate_;
46 additionalData_["settlement"] = settlement_;
47
48 // If you Buy EURUSD forward, then you buy EUR and sell USD.
49 // EUR = foreign, USD = Domestic.
50 // You pay in USD, so the Domestic / Sold ccy is the "payer" currency
51 Currency boughtCcy = parseCurrency(boughtCurrency_);
52 Currency soldCcy = parseCurrency(soldCurrency_);
53
54 Currency payCcy;
55 if (payCurrency_.empty()) {
56 // If settlement currency is not set, set it to the domestic currency.
57 payCcy = soldCcy;
58 } else {
59 payCcy = parseCurrency(payCurrency_);
60 QL_REQUIRE(payCcy == boughtCcy || payCcy == soldCcy,
61 "Settlement currency must be either " << boughtCcy.code() << " or " << soldCcy.code() << ".");
62 }
63
64 npvCurrency_ = payCcy.code();
65
66 // The notional and notional ccy will be set in the engine anyway,
67 // but we also set this here as a default/fallback in case the engine builder fails.
68 if (settlement_ == "Physical") {
71 } else {
72 // for cash settled forwards we take the notional from the settlement ccy leg
75 }
76
78
79 // Derive settlement date from payment data parameters
80 Date payDate;
81 if (payDate_.empty()) {
82 Natural conventionalLag = 0;
83 Calendar conventionalCalendar = NullCalendar();
84 BusinessDayConvention conventionalBdc = Unadjusted;
85 if (!fxIndex_.empty() && settlement_ == "Cash") {
86 std::tie(conventionalLag, conventionalCalendar, conventionalBdc) =
87 getFxIndexConventions(fxIndex_.empty() ? boughtCurrency_ + soldCurrency_ : fxIndex_);
88 }
89 PaymentLag paymentLag;
90 if (payLag_.empty())
91 paymentLag = conventionalLag;
92 else
93 paymentLag = parsePaymentLag(payLag_);
94 Period payLag = boost::apply_visitor(PaymentLagPeriod(), paymentLag);
95 Calendar payCalendar = payCalendar_.empty() ? conventionalCalendar : parseCalendar(payCalendar_);
96 BusinessDayConvention payConvention =
97 payConvention_.empty() ? conventionalBdc : parseBusinessDayConvention(payConvention_);
98 payDate = payCalendar.advance(maturityDate, payLag, payConvention);
99 } else {
101 QL_REQUIRE(payDate >= maturityDate, "FX Forward settlement date should equal or exceed the maturity date.");
102 }
103
104 additionalData_["payDate"] = payDate;
105 maturity_ = std::max(payDate, maturityDate);
106 QuantLib::ext::shared_ptr<QuantExt::FxIndex> fxIndex;
107
108 Date fixingDate;
109 if (settlement_ == "Cash") {
110 // We allow for an empty fxIndex if maturiytDate == payDate in order not to break trades that were previously
111 // pricing - we should really require fxIndex whenever cash settlement is specified. If the fxIndex is not
112 // given in this case, we assume that the current FX Spot rate is used to determine the settlement amount.
113 if (maturityDate <= payDate && !fxIndex_.empty()) {
114 Currency nonPayCcy = payCcy == boughtCcy ? soldCcy : boughtCcy;
115 fxIndex = buildFxIndex(fxIndex_, nonPayCcy.code(), payCcy.code(), engineFactory->market(),
116 engineFactory->configuration(MarketContext::pricing));
117 // We also allow for an effective fixing date > payDate in order not to break trades that were preivously
118 // pricing - this should be an error as well. If this is the case we assume that the current FX Spot
119 // rate is used to determine the settlement amount as above.
120 fixingDate = fxIndex->fixingCalendar().adjust(maturityDate);
121 if (fixingDate <= payDate) {
123 }
124 } else {
125 QL_REQUIRE(maturityDate >= payDate,
126 "FX settlement index must be specified for non-deliverable forward if value date ("
127 << maturityDate << ") < payDate (" << payDate << ")");
128 }
129 }
130 if (fixingDate != Date()) {
131 if (fixingDate <= payDate)
132 additionalData_["fixingDate"] = fixingDate;
133 else
134 additionalData_["adjustedValueDate"] = fixingDate;
135 }
136
137 QL_REQUIRE(tradeActions().empty(), "TradeActions not supported for FxForward");
138
139 DLOG("Build FxForward with maturity date " << QuantLib::io::iso_date(maturityDate) << " and pay date "
140 << QuantLib::io::iso_date(payDate));
141
142 // get pricing engine builder
143 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
144 QL_REQUIRE(builder, "No builder found for " << tradeType_);
145 QuantLib::ext::shared_ptr<FxForwardEngineBuilderBase> fxBuilder =
146 QuantLib::ext::dynamic_pointer_cast<FxForwardEngineBuilderBase>(builder);
147
148 string tmp = fxBuilder->engineParameter("includeSettlementDateFlows", {}, false, "");
149 includeSettlementDateFlows_ = tmp == "" ? false : parseBool(tmp);
150
151 QuantLib::ext::shared_ptr<QuantLib::Instrument> instrument =
152 QuantLib::ext::make_shared<QuantExt::FxForward>(boughtAmount_, boughtCcy, soldAmount_, soldCcy, maturityDate, false,
153 settlement_ == "Physical", payDate, payCcy, fixingDate, fxIndex,
154 includeSettlementDateFlows_);
155 instrument_.reset(new VanillaInstrument(instrument));
156
157 // set pricing engine
158 instrument_->qlInstrument()->setPricingEngine(fxBuilder->engine(boughtCcy, soldCcy));
159 setSensitivityTemplate(*fxBuilder);
160
161 // Set up Legs
162 legs_ = {{QuantLib::ext::make_shared<SimpleCashFlow>(boughtAmount_, payDate)},
163 {QuantLib::ext::make_shared<SimpleCashFlow>(soldAmount_, payDate)}};
165 legPayers_ = {false, true};
166}
Currency payCcy() const
bool includeSettlementDateFlows_
Definition: fxforward.hpp:87
void addFixingDate(const QuantLib::Date &fixingDate, const std::string &indexName, const QuantLib::Date &payDate=Date::maxDate(), const bool alwaysAddIfPaysOnSettlement=false, const bool mandatoryFixing=true)
TradeActions & tradeActions()
Set the trade actions.
Definition: trade.hpp:126
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
RequiredFixings requiredFixings_
Definition: trade.hpp:223
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument() const
Definition: trade.hpp:141
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
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
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
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
PaymentLag parsePaymentLag(const string &s)
Convert text to PaymentLag.
Definition: parsers.cpp:628
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
std::tuple< Natural, Calendar, BusinessDayConvention > getFxIndexConventions(const string &index)
Definition: marketdata.cpp:160
boost::variant< QuantLib::Period, QuantLib::Natural > PaymentLag
Definition: types.hpp:32
QuantLib::ext::shared_ptr< QuantExt::FxIndex > buildFxIndex(const string &fxIndex, const string &domestic, const string &foreign, const QuantLib::ext::shared_ptr< Market > &market, const string &configuration, bool useXbsCurves)
Definition: marketdata.cpp:137
+ 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 175 of file fxforward.cpp.

175 {
176 // try to get the notional from the additional results of the instrument
177 try {
178 return instrument_->qlInstrument(true)->result<Real>("currentNotional");
179 } catch (const std::exception& e) {
180 if (strcmp(e.what(), "currentNotional not provided"))
181 ALOG("error when retrieving notional: " << e.what());
182 }
183 // if not provided, return original/fallback amount
184 return notional_;
185}
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
+ Here is the caller graph for this function:

◆ notionalCurrency()

std::string notionalCurrency ( ) const
overridevirtual

Reimplemented from Trade.

Definition at line 187 of file fxforward.cpp.

187 {
188 // try to get the notional ccy from the additional results of the instrument
189 try {
190 return instrument_->qlInstrument(true)->result<std::string>("notionalCurrency");
191 } catch (const std::exception& e) {
192 if (strcmp(e.what(), "notionalCurrency not provided"))
193 ALOG("error when retrieving notional ccy: " << e.what());
194 }
195 // if not provided, return original/fallback value
196 return notionalCurrency_;
197}
+ Here is the caller graph for this function:

◆ maturityDate()

const string & maturityDate ( ) const

Definition at line 57 of file fxforward.hpp.

57{ return maturityDate_; }

◆ boughtCurrency()

const string & boughtCurrency ( ) const

Definition at line 58 of file fxforward.hpp.

58{ return boughtCurrency_; }

◆ boughtAmount()

double boughtAmount ( ) const

Definition at line 59 of file fxforward.hpp.

59{ return boughtAmount_; }

◆ soldCurrency()

const string & soldCurrency ( ) const

Definition at line 60 of file fxforward.hpp.

60{ return soldCurrency_; }

◆ soldAmount()

double soldAmount ( ) const

Definition at line 61 of file fxforward.hpp.

61{ return soldAmount_; }

◆ settlement()

const string & settlement ( ) const

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

Definition at line 63 of file fxforward.hpp.

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

◆ fxIndex()

const string & fxIndex ( ) const

Definition at line 64 of file fxforward.hpp.

64{ return fxIndex_; }

◆ paymentDate()

const string & paymentDate ( ) const

Definition at line 65 of file fxforward.hpp.

65{ return payDate_; }

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 199 of file fxforward.cpp.

199 {
200 Trade::fromXML(node);
201 XMLNode* fxNode = XMLUtils::getChildNode(node, "FxForwardData");
202 QL_REQUIRE(fxNode, "No FxForwardData Node");
203 maturityDate_ = XMLUtils::getChildValue(fxNode, "ValueDate", true);
204 boughtCurrency_ = XMLUtils::getChildValue(fxNode, "BoughtCurrency", true);
205 soldCurrency_ = XMLUtils::getChildValue(fxNode, "SoldCurrency", true);
206 boughtAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "BoughtAmount", true);
207 soldAmount_ = XMLUtils::getChildValueAsDouble(fxNode, "SoldAmount", true);
208 settlement_ = XMLUtils::getChildValue(fxNode, "Settlement", false);
209 if (settlement_ == "")
210 settlement_ = "Physical";
211
212 if (XMLNode* settlementDataNode = XMLUtils::getChildNode(fxNode, "SettlementData")) {
213 payCurrency_ = XMLUtils::getChildValue(settlementDataNode, "Currency", false);
214 fxIndex_ = XMLUtils::getChildValue(settlementDataNode, "FXIndex", false);
215 payDate_ = XMLUtils::getChildValue(settlementDataNode, "Date", false);
216
217 if (payDate_.empty()) {
218 if (XMLNode* rulesNode = XMLUtils::getChildNode(settlementDataNode, "Rules")) {
219 payLag_ = XMLUtils::getChildValue(rulesNode, "PaymentLag", false);
220 payCalendar_ = XMLUtils::getChildValue(rulesNode, "PaymentCalendar", false);
221 payConvention_ = XMLUtils::getChildValue(rulesNode, "PaymentConvention", false);
222 }
223 }
224 }
225}
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 227 of file fxforward.cpp.

227 {
228 XMLNode* node = Trade::toXML(doc);
229 XMLNode* fxNode = doc.allocNode("FxForwardData");
230 XMLUtils::appendNode(node, fxNode);
231 XMLUtils::addChild(doc, fxNode, "ValueDate", maturityDate_);
232 XMLUtils::addChild(doc, fxNode, "BoughtCurrency", boughtCurrency_);
233 XMLUtils::addChild(doc, fxNode, "BoughtAmount", boughtAmount_);
234 XMLUtils::addChild(doc, fxNode, "SoldCurrency", soldCurrency_);
235 XMLUtils::addChild(doc, fxNode, "SoldAmount", soldAmount_);
236 XMLUtils::addChild(doc, fxNode, "Settlement", settlement_);
237
238 XMLNode* settlementDataNode = doc.allocNode("SettlementData");
239 XMLUtils::appendNode(fxNode, settlementDataNode);
240
241 if (!payCurrency_.empty())
242 XMLUtils::addChild(doc, settlementDataNode, "Currency", payCurrency_);
243 if (!fxIndex_.empty())
244 XMLUtils::addChild(doc, settlementDataNode, "FXIndex", fxIndex_);
245 if (!payDate_.empty()) {
246 XMLUtils::addChild(doc, settlementDataNode, "Date", payDate_);
247 } else {
248 XMLNode* rulesNode = doc.allocNode("Rules");
249 XMLUtils::appendNode(settlementDataNode, rulesNode);
250 if (!payLag_.empty())
251 XMLUtils::addChild(doc, rulesNode, "PaymentLag", payLag_);
252 if (!payCalendar_.empty())
253 XMLUtils::addChild(doc, rulesNode, "PaymentCalendar", payCalendar_);
254 if (!payConvention_.empty())
255 XMLUtils::addChild(doc, rulesNode, "PaymentConvention", payConvention_);
256 }
257
258 return node;
259}
+ Here is the call graph for this function:

Member Data Documentation

◆ maturityDate_

string maturityDate_
private

Definition at line 75 of file fxforward.hpp.

◆ boughtCurrency_

string boughtCurrency_
private

Definition at line 76 of file fxforward.hpp.

◆ boughtAmount_

double boughtAmount_
private

Definition at line 77 of file fxforward.hpp.

◆ soldCurrency_

string soldCurrency_
private

Definition at line 78 of file fxforward.hpp.

◆ soldAmount_

double soldAmount_
private

Definition at line 79 of file fxforward.hpp.

◆ settlement_

string settlement_
private

Definition at line 80 of file fxforward.hpp.

◆ payCurrency_

string payCurrency_
private

Definition at line 81 of file fxforward.hpp.

◆ fxIndex_

string fxIndex_
private

Definition at line 82 of file fxforward.hpp.

◆ payDate_

string payDate_
private

Definition at line 83 of file fxforward.hpp.

◆ payLag_

string payLag_
private

Definition at line 84 of file fxforward.hpp.

◆ payCalendar_

string payCalendar_
private

Definition at line 85 of file fxforward.hpp.

◆ payConvention_

string payConvention_
private

Definition at line 86 of file fxforward.hpp.

◆ includeSettlementDateFlows_

bool includeSettlementDateFlows_
private

Definition at line 87 of file fxforward.hpp.