Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fxoption.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
19#include <boost/make_shared.hpp>
31#include <ql/errors.hpp>
32#include <ql/exercise.hpp>
33#include <ql/instruments/compositeinstrument.hpp>
34#include <ql/instruments/vanillaoption.hpp>
35
36using namespace QuantLib;
37
38namespace ore {
39namespace data {
40
41void FxOption::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
42
43 // ISDA taxonomy
44 additionalData_["isdaAssetClass"] = string("Foreign Exchange");
45 additionalData_["isdaBaseProduct"] = string("Vanilla Option");
46 additionalData_["isdaSubProduct"] = string("");
47 additionalData_["isdaTransaction"] = string("");
48
49 additionalData_["boughtCurrency"] = assetName_;
50 additionalData_["boughtAmount"] = quantity_;
51 additionalData_["soldCurrency"] = currency_;
52 additionalData_["soldAmount"] = quantity_ * strike_.value();
53
54 QuantLib::Date today = Settings::instance().evaluationDate();
55 const QuantLib::ext::shared_ptr<Market>& market = engineFactory->market();
56
57 // If automatic exercise, check that we have a non-empty FX index string, parse it and attach curves from market.
59
60 QL_REQUIRE(!fxIndex_.empty(),
61 "FX option trade " << id() << " has automatic exercise so the FXIndex node needs to be populated.");
62
63 // The strike is the number of units of sold currency (currency_) per unit of bought currency (assetName_).
64 // So, the convention here is that the sold currency is domestic and the bought currency is foreign.
65 // Note: intentionally use null calendar and 0 day fixing lag here because we will ask the FX index for its
66 // value on the expiry date without adjustment.
68 engineFactory->configuration(MarketContext::pricing));
69
70 // Populate the external index name so that fixings work.
72 }
73
74 const ext::optional<OptionPaymentData>& opd = option_.paymentData();
76 QuantLib::Date paymentDate = expiryDate_;
77 if (option_.settlement() == "Physical" && opd) {
78 if (opd->rulesBased()) {
79 const Calendar& cal = opd->calendar();
80 QL_REQUIRE(cal != Calendar(), "Need a non-empty calendar for rules based payment date.");
81 paymentDate = cal.advance(expiryDate_, opd->lag(), Days, opd->convention());
82 } else {
83 if (opd->dates().size() > 1)
85 id(), tradeType(), "Trade build", "Found more than 1 payment date. The first one will be used.")
86 .log();
87 paymentDate = opd->dates().front();
88 }
89
90 QL_REQUIRE(paymentDate >= expiryDate_, "Settlement date must be greater than or equal to expiry date.");
91
92 if (expiryDate_ <= today) {
93 // building an fx forward instrument instead of the option
94 Date fixingDate;
95 Currency boughtCcy = parseCurrency(assetName_);
96 Currency soldCcy = parseCurrency(currency_);
97 ext::shared_ptr<QuantLib::Instrument> instrument =
98 ext::make_shared<QuantExt::FxForward>(quantity_, boughtCcy, soldAmount(), soldCcy, maturity_, false,
99 true, paymentDate, soldCcy, fixingDate);
101 if (option_.exerciseData()) {
102 if (option_.exerciseData()->date() <= expiryDate_) {
103 // option is exercised
104 // fxforward flow are flows from trade data
105 legs_ = {{ext::make_shared<SimpleCashFlow>(quantity_, paymentDate)},
106 {ext::make_shared<SimpleCashFlow>(soldAmount(), paymentDate)}};
108 legPayers_ = {false, true};
109 } else
110 QL_REQUIRE(option_.exerciseData()->date() <= expiryDate_,
111 "Trade build error, exercise after option expiry is not allowed");
112 } else {
113 // option not exercised
114 // set flows = 0
115 legs_ = {};
116 }
119 }
120 // Build the trade using the shared functionality in the base class.
121 VanillaOptionTrade::build(engineFactory);
123 } else {
124 VanillaOptionTrade::build(engineFactory);
125 }
126}
127
130 XMLNode* fxNode = XMLUtils::getChildNode(node, "FxOptionData");
131 QL_REQUIRE(fxNode, "No FxOptionData Node");
132 option_.fromXML(XMLUtils::getChildNode(fxNode, "OptionData"));
133 assetName_ = XMLUtils::getChildValue(fxNode, "BoughtCurrency", true);
134 currency_ = XMLUtils::getChildValue(fxNode, "SoldCurrency", true);
135 double boughtAmount = XMLUtils::getChildValueAsDouble(fxNode, "BoughtAmount", true);
136 double soldAmount = XMLUtils::getChildValueAsDouble(fxNode, "SoldAmount", true);
139 fxIndex_ = XMLUtils::getChildValue(fxNode, "FXIndex", false);
140 QL_REQUIRE(boughtAmount > 0.0, "positive BoughtAmount required");
141 QL_REQUIRE(soldAmount > 0.0, "positive SoldAmount required");
142}
143
145 // TODO: Should call parent class to xml?
146 XMLNode* node = Trade::toXML(doc);
147 XMLNode* fxNode = doc.allocNode("FxOptionData");
148 XMLUtils::appendNode(node, fxNode);
149
150 XMLUtils::appendNode(fxNode, option_.toXML(doc));
151 XMLUtils::addChild(doc, fxNode, "BoughtCurrency", boughtCurrency());
152 XMLUtils::addChild(doc, fxNode, "BoughtAmount", boughtAmount());
153 XMLUtils::addChild(doc, fxNode, "SoldCurrency", soldCurrency());
154 XMLUtils::addChild(doc, fxNode, "SoldAmount", soldAmount());
155
156 if (!fxIndex_.empty())
157 XMLUtils::addChild(doc, fxNode, "FXIndex", fxIndex_);
158
159 return node;
160}
161} // namespace data
162} // namespace ore
Engine builder for FX Forwards.
Engine builder for FX Options.
double soldAmount() const
Definition: fxoption.hpp:59
const string & soldCurrency() const
Definition: fxoption.hpp:58
virtual void fromXML(XMLNode *node) override
Definition: fxoption.cpp:128
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: fxoption.cpp:144
std::string fxIndex_
If the option has automatic exercise, need an FX index for settlement.
Definition: fxoption.hpp:71
const string & boughtCurrency() const
Definition: fxoption.hpp:56
double boughtAmount() const
Definition: fxoption.hpp:57
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
Definition: fxoption.cpp:41
void log() const
generate Boost log record to pass to corresponding sinks
Definition: log.cpp:491
const string & settlement() const
Definition: optiondata.hpp:81
virtual void fromXML(XMLNode *node) override
Definition: optiondata.cpp:32
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: optiondata.cpp:86
bool isAutomaticExercise() const
Automatic exercise assumed false if not explicitly provided.
Definition: optiondata.hpp:117
const boost::optional< OptionPaymentData > & paymentData() const
Definition: optiondata.hpp:93
const boost::optional< OptionExerciseData > & exerciseData() const
Definition: optiondata.hpp:92
const vector< string > & exerciseDates() const
Definition: optiondata.hpp:76
Utility classes for Structured warnings, contains the Trade ID and Type.
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
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: trade.cpp:46
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument() const
Definition: trade.hpp:141
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
const string & tradeType() const
Definition: trade.hpp:133
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
QuantLib::Real value() const
Vanilla Instrument Wrapper.
QuantLib::Date expiryDate_
Store the option expiry date.
QuantLib::Date paymentDate_
Store the (optional) payment date.
const QuantLib::Date paymentDate() const
QuantLib::Date forwardDate_
Store the (optional) forward date.
std::string indexName_
Hold the external index name if needed e.g. in the case of an FX index.
QuantLib::ext::shared_ptr< QuantLib::Index > index_
An index is needed if the option is to be automatically exercised on expiry.
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
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 Forward data model and serialization.
FX Option 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
leg data model and serialization
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
market data related utilties
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
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
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
Classes for structured trade warnings.