Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fxswap.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
23#include <ql/cashflows/simplecashflow.hpp>
24#include <ql/instruments/compositeinstrument.hpp>
26
28using namespace QuantLib;
29using namespace QuantExt;
30
31namespace ore {
32namespace data {
33
34void FxSwap::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
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}
117
118
119QuantLib::Real FxSwap::notional() const {
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}
130
131std::string FxSwap::notionalCurrency() const {
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}
142
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}
158
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}
175} // namespace data
176} // namespace ore
Engine builder for FX Forwards.
const string & nearDate() const
Definition: fxswap.hpp:60
double farBoughtAmount_
Definition: fxswap.hpp:84
std::string notionalCurrency() const override
Definition: fxswap.cpp:131
string settlement_
Definition: fxswap.hpp:86
double farSoldAmount_
Definition: fxswap.hpp:85
string nearDate_
Definition: fxswap.hpp:78
double nearBoughtAmount_
Definition: fxswap.hpp:81
QuantLib::ext::shared_ptr< QuantLib::Instrument > instFar_
Definition: fxswap.hpp:87
string nearSoldCurrency_
Definition: fxswap.hpp:82
string nearBoughtCurrency_
Definition: fxswap.hpp:80
QuantLib::ext::shared_ptr< QuantLib::Instrument > instNear_
Definition: fxswap.hpp:87
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
Definition: fxswap.cpp:119
virtual void fromXML(XMLNode *node) override
Definition: fxswap.cpp:143
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: fxswap.cpp:159
string farDate_
Definition: fxswap.hpp:79
double nearSoldAmount_
Definition: fxswap.hpp:83
const string & farDate() const
Definition: fxswap.hpp:61
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
Definition: fxswap.cpp:34
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
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
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
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
XML Utilities Class.
Definition: xmlutils.hpp:119
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
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
Pricing Engine Factory.
FX Swap data model and serialization.
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
Classes and functions for log message handling.
@ 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
Serializable Credit Default Swap.
Definition: namespaces.docs:23