Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equitydoubletouchoption.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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 ORE is free software: you can redistribute it and/or modify it
8 under the terms of the Modified BSD License. You should have received a
9 copy of the license along with this program.
10 The license is also available online at <http://opensourcerisk.org>
11 This program is distributed on the basis that it will form a useful
12 contribution to risk analytics and model standardisation, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
15*/
16
21
22#include <boost/make_shared.hpp>
27#include <ql/errors.hpp>
28#include <ql/exercise.hpp>
29#include <ql/instruments/barrieroption.hpp>
30#include <ql/instruments/compositeinstrument.hpp>
31#include <ql/instruments/swap.hpp>
32#include <ql/instruments/vanillaoption.hpp>
34
35using namespace QuantLib;
36
37namespace ore {
38namespace data {
39
41 const EquityUnderlying& equityUnderlying, string payoffCurrency,
42 double payoffAmount, string startDate, string calendar)
43 : ore::data::Trade("EquityDoubleTouchOption", env),
44 EquitySingleAssetDerivative("", env, equityUnderlying),
45 option_(option),
46 barrier_(barrier), startDate_(startDate), calendar_(calendar), payoffAmount_(payoffAmount),
47 payoffCurrency_(payoffCurrency) {
48 DoubleBarrier::Type barrierType = parseDoubleBarrierType(barrier_.type());
49 switch (barrierType) {
50 case DoubleBarrier::KnockIn:
51 type_ = "KnockIn";
52 break;
53 case DoubleBarrier::KnockOut:
54 type_ = "KnockOut";
55 break;
56 default:
57 QL_FAIL("unsupported barrier type " << barrierType);
58 }
59}
60
61void EquityDoubleTouchOption::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
62
63 // ISDA taxonomy
64 additionalData_["isdaAssetClass"] = string("Equity");
65 additionalData_["isdaBaseProduct"] = string("Other");
66 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
67 // skip the transaction level mapping for now
68 additionalData_["isdaTransaction"] = string("");
69
70 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
71 Date start = ore::data::parseDate(startDate_);
72 Calendar cal = ore::data::parseCalendar(calendar_);
73
74 QL_REQUIRE(tradeActions().empty(), "TradeActions not supported for FxOption");
75 QL_REQUIRE(option_.exerciseDates().size() == 1, "Invalid number of exercise dates");
76 QL_REQUIRE(barrier_.levels().size() == 2, "Invalid number of barrier levels");
77 QL_REQUIRE(barrier_.style().empty() || barrier_.style() == "American", "Only american barrier style suppported");
78
79 // Parse trade data
80 string assetName = equityName();
81 Currency ccy = parseCurrency(payoffCurrency_);
82 Date expiryDate = parseDate(option_.exerciseDates().front());
83 DoubleBarrier::Type barrierType = parseDoubleBarrierType(barrier_.type());
84 bool payoffAtExpiry = option_.payoffAtExpiry();
85 double rebate = barrier_.rebate();
86 Position::Type positionType = parsePositionType(option_.longShort());
87
88 QL_REQUIRE(rebate == 0, "Rebates not supported for EquityDoubleTouchOptions");
89 if (payoffAtExpiry == false) {
90 payoffAtExpiry = true;
91 DLOG("Payoff at hit not yet supported for EquityDoubleTouchOptions, setting to payoff at expiry");
92 }
93
94 Real levelLow = barrier_.levels()[0].value();
95 Real levelHigh = barrier_.levels()[1].value();
96 QL_REQUIRE(levelLow < levelHigh, "barrier levels are not in ascending order");
97
98
99 // create payoff and exercise, as well as leg of underlying instrument
100 QuantLib::ext::shared_ptr<StrikedTypePayoff> payoff(new CashOrNothingPayoff(Option::Call, (levelLow + levelHigh) / 2, 1.0));
101 Leg leg;
102
103 leg.push_back(QuantLib::ext::shared_ptr<CashFlow>(new SimpleCashFlow(1.0, expiryDate)));
104
105 QuantLib::ext::shared_ptr<Exercise> exercise = QuantLib::ext::make_shared<EuropeanExercise>(expiryDate);
106
107 QuantLib::ext::shared_ptr<Instrument> doubleTouch =
108 QuantLib::ext::make_shared<DoubleBarrierOption>(barrierType, levelLow, levelHigh, 0.0, payoff, exercise);
109 QuantLib::ext::shared_ptr<Instrument> underlying = QuantLib::ext::make_shared<Swap>(Leg(), leg);
110
111
112
113 QuantLib::ext::shared_ptr<QuantExt::EquityIndex2> eqIndex = engineFactory->market()->equityCurve(assetName).currentLink();
114
115 // set pricing engines
116 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
117 QL_REQUIRE(builder, "No builder found for " << tradeType_);
118 QuantLib::ext::shared_ptr<EquityDoubleTouchOptionEngineBuilder> eqDoubleTouchOptBuilder =
119 QuantLib::ext::dynamic_pointer_cast<EquityDoubleTouchOptionEngineBuilder>(builder);
120 doubleTouch->setPricingEngine(eqDoubleTouchOptBuilder->engine(assetName, ccy));
121 setSensitivityTemplate(*eqDoubleTouchOptBuilder);
122 if (type_ == "KnockIn") {
123 // if a knock-in option is triggered it becomes a simple forward cashflow
124 // which we price as a swap
125 builder = engineFactory->builder("Swap");
126 QL_REQUIRE(builder, "No builder found for Swap");
127 QuantLib::ext::shared_ptr<SwapEngineBuilderBase> swapBuilder =
128 QuantLib::ext::dynamic_pointer_cast<SwapEngineBuilderBase>(builder);
129 underlying->setPricingEngine(swapBuilder->engine(parseCurrency(payoffCurrency_), std::string(), std::string()));
130 }
131
132 bool isLong = (positionType == Position::Long) ? true : false;
133
134 std::vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
135 std::vector<Real> additionalMultipliers;
136 Date lastPremiumDate = addPremiums(
137 additionalInstruments, additionalMultipliers, (isLong ? 1.0 : -1.0) * payoffAmount_, option_.premiumData(),
138 isLong ? -1.0 : 1.0, ccy, engineFactory, builder->configuration(MarketContext::pricing));
139
140 Handle<Quote> spot = market->equitySpot(assetName);
141 instrument_ = QuantLib::ext::make_shared<DoubleBarrierOptionWrapper>(
142 doubleTouch, isLong, expiryDate, false, underlying, barrierType, spot, levelLow, levelHigh, 0, ccy, start, eqIndex, cal,
143 payoffAmount_, payoffAmount_, additionalInstruments, additionalMultipliers);
147 maturity_ = std::max(lastPremiumDate, expiryDate);
148
149 if (start != Date()) {
150 for (Date d = start; d <= expiryDate; d = eqIndex->fixingCalendar().advance(d, 1 * Days))
151 requiredFixings_.addFixingDate(d, "EQ-" + assetName, expiryDate);
152 }
153
154 additionalData_["payoffAmount"] = payoffAmount_;
155 additionalData_["payoffCurrency"] = payoffCurrency_;
156}
157
158bool EquityDoubleTouchOption::checkBarrier(Real spot, Barrier::Type type, Real barrier) {
159 switch (type) {
160 case Barrier::DownIn:
161 case Barrier::DownOut:
162 return spot <= barrier;
163 case Barrier::UpIn:
164 case Barrier::UpOut:
165 return spot >= barrier;
166 default:
167 QL_FAIL("unknown barrier type " << type);
168 }
169}
170
172 Trade::fromXML(node);
173 XMLNode* eqNode = XMLUtils::getChildNode(node, "EquityDoubleTouchOptionData");
174 QL_REQUIRE(eqNode, "No EquityDoubleTouchOptionData Node");
175 option_.fromXML(XMLUtils::getChildNode(eqNode, "OptionData"));
176 barrier_.fromXML(XMLUtils::getChildNode(eqNode, "BarrierData"));
177 DoubleBarrier::Type barrierType = parseDoubleBarrierType(barrier_.type());
178 switch (barrierType) {
179 case DoubleBarrier::KnockIn:
180 type_ = "KnockIn";
181 break;
182 case DoubleBarrier::KnockOut:
183 type_ = "KnockOut";
184 break;
185 default:
186 QL_FAIL("unsupported barrier type " << barrierType);
187 }
188
189 XMLNode* tmp = XMLUtils::getChildNode(eqNode, "Underlying");
190 if (!tmp)
191 tmp = XMLUtils::getChildNode(eqNode, "Name");
193 payoffCurrency_ = XMLUtils::getChildValue(eqNode, "PayoffCurrency", true);
194 startDate_ = XMLUtils::getChildValue(eqNode, "StartDate", false);
195 calendar_ = XMLUtils::getChildValue(eqNode, "Calendar", false);
196 payoffAmount_ = XMLUtils::getChildValueAsDouble(eqNode, "PayoffAmount", true);
197}
198
200 XMLNode* node = Trade::toXML(doc);
201 XMLNode* eqNode = doc.allocNode("EquityDoubleTouchOptionData");
202 XMLUtils::appendNode(node, eqNode);
203 XMLUtils::appendNode(eqNode, option_.toXML(doc));
204 XMLUtils::appendNode(eqNode, barrier_.toXML(doc));
206 XMLUtils::addChild(doc, eqNode, "PayoffCurrency", payoffCurrency_);
207 XMLUtils::addChild(doc, eqNode, "PayoffAmount", payoffAmount_);
208 if (startDate_ != "")
209 XMLUtils::addChild(doc, eqNode, "StartDate", startDate_);
210 if (calendar_ != "")
211 XMLUtils::addChild(doc, eqNode, "Calendar", calendar_);
212
213 return node;
214}
215
216} // namespace data
217} // namespace oreplus
Wrapper for option instruments, tracks whether option has been exercised or not.
Engine builder for Swaps.
Serializable obejct holding barrier data.
Definition: barrierdata.hpp:34
const std::string & type() const
Definition: barrierdata.hpp:46
virtual void fromXML(ore::data::XMLNode *node) override
Definition: barrierdata.cpp:25
double rebate() const
Definition: barrierdata.hpp:47
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: barrierdata.cpp:49
std::vector< ore::data::TradeBarrier > levels() const
Definition: barrierdata.hpp:50
const std::string & style() const
Definition: barrierdata.hpp:51
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
bool checkBarrier(Real spot, Barrier::Type type, Real level)
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
Base class for all single asset Equity Derivaties.
void fromXML(XMLNode *node) override
Definition: underlying.cpp:81
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:102
Serializable object holding option data.
Definition: optiondata.hpp:42
const string & longShort() const
Definition: optiondata.hpp:70
virtual void fromXML(XMLNode *node) override
Definition: optiondata.cpp:32
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: optiondata.cpp:86
const bool & payoffAtExpiry() const
Definition: optiondata.hpp:75
const PremiumData & premiumData() const
Definition: optiondata.hpp:83
const vector< string > & exerciseDates() const
Definition: optiondata.hpp:76
void addFixingDate(const QuantLib::Date &fixingDate, const std::string &indexName, const QuantLib::Date &payDate=Date::maxDate(), const bool alwaysAddIfPaysOnSettlement=false, const bool mandatoryFixing=true)
Trade base class.
Definition: trade.hpp:55
TradeActions & tradeActions()
Set the trade actions.
Definition: trade.hpp:126
string npvCurrency_
Definition: trade.hpp:201
QuantLib::Real notional_
Definition: trade.hpp:202
virtual void fromXML(XMLNode *node) override
Definition: trade.cpp:34
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)
Definition: trade.cpp:58
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
RequiredFixings requiredFixings_
Definition: trade.hpp:223
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
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.
EQ Double One-Touch/No-Touch Option data model and serialization.
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
DoubleBarrier::Type parseDoubleBarrierType(const std::string &s)
Convert std::string to QuantLib::DoubleBarrierType.
Definition: parsers.cpp:1061
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
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
Map text representations to QuantLib/QuantExt types.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Calendar calendar
Definition: utilities.cpp:441
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.