Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
equitydigitaloption.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
17#include <boost/make_shared.hpp>
23#include <ql/errors.hpp>
24#include <ql/exercise.hpp>
25#include <ql/instruments/compositeinstrument.hpp>
26#include <ql/instruments/payoffs.hpp>
27#include <ql/instruments/vanillaoption.hpp>
28
29using namespace QuantLib;
30
31namespace ore {
32namespace data {
33
34void EquityDigitalOption::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
35
36 // ISDA taxonomy
37 additionalData_["isdaAssetClass"] = string("Equity");
38 additionalData_["isdaBaseProduct"] = string("Option");
39 additionalData_["isdaSubProduct"] = string("Price Return Basic Performance");
40 // skip the transaction level mapping for now
41 additionalData_["isdaTransaction"] = string("");
42
43 // Only European Vanilla supported for now
44 QL_REQUIRE(option_.style() == "European", "Option Style unknown: " << option_.style());
45 QL_REQUIRE(option_.exerciseDates().size() == 1, "Invalid number of exercise dates");
46 QL_REQUIRE(option_.payoffAtExpiry() == true, "PayoffAtExpiry must be True for EquityDigitalOption");
47 QL_REQUIRE(tradeActions().empty(), "TradeActions not supported for EquityDigitalOption");
48 QL_REQUIRE(strike_ > 0.0 && strike_ != Null<Real>(), "Invalid strike " << strike_);
49 QL_REQUIRE(payoffAmount_ > 0.0 && payoffAmount_ != Null<Real>(), "Invalid payoff amount " << payoffAmount_);
50 QL_REQUIRE(payoffCurrency_ != "", "PayoffCurrency is missing");
51
52 // Currency
53 Currency ccy = parseCurrency(payoffCurrency_);
54
55 // Asset Name
56 string assetName = equityName();
57
58 // Payoff Type
59 Option::Type type = parseOptionType(option_.callPut());
60
61 // Set up the CashOrNothing
62 Real strike = strike_;
63 QuantLib::ext::shared_ptr<StrikedTypePayoff> payoff(new CashOrNothingPayoff(type, strike, payoffAmount_));
64
65 // Exercise
66 Date expiryDate = parseDate(option_.exerciseDates().front());
67 QuantLib::ext::shared_ptr<Exercise> exercise = QuantLib::ext::make_shared<EuropeanExercise>(expiryDate);
68
69 // QL does not have an EquityDigitalOption, so we add a vanilla one here and wrap
70 // it in a composite.
71 QuantLib::ext::shared_ptr<Instrument> vanilla = QuantLib::ext::make_shared<VanillaOption>(payoff, exercise);
72
73 // set pricing engines
74 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(tradeType_);
75 QL_REQUIRE(builder, "No builder found for " << tradeType_);
76 QuantLib::ext::shared_ptr<EquityDigitalOptionEngineBuilder> eqOptBuilder =
77 QuantLib::ext::dynamic_pointer_cast<EquityDigitalOptionEngineBuilder>(builder);
78 vanilla->setPricingEngine(eqOptBuilder->engine(assetName, ccy));
79 setSensitivityTemplate(*eqOptBuilder);
80
81 Position::Type positionType = parsePositionType(option_.longShort());
82 Real bsInd = (positionType == QuantLib::Position::Long ? 1.0 : -1.0);
83 Real mult = bsInd;
84
85 std::vector<QuantLib::ext::shared_ptr<Instrument>> additionalInstruments;
86 std::vector<Real> additionalMultipliers;
87 Date lastPremiumDate =
88 addPremiums(additionalInstruments, additionalMultipliers, mult * quantity_, option_.premiumData(), -bsInd, ccy,
89 engineFactory, eqOptBuilder->configuration(MarketContext::pricing));
90
91 instrument_ = QuantLib::ext::shared_ptr<InstrumentWrapper>(
92 new VanillaInstrument(vanilla, mult*quantity_, additionalInstruments, additionalMultipliers));
93
97 maturity_ = std::max(lastPremiumDate, expiryDate);
98
99 additionalData_["payoffAmount"] = payoffAmount_;
100 additionalData_["payoffCurrency"] = payoffCurrency_;
101}
102
104 Trade::fromXML(node);
105 XMLNode* eqNode = XMLUtils::getChildNode(node, "EquityDigitalOptionData");
106 QL_REQUIRE(eqNode, "No EquityDigitalOptionData Node");
107 option_.fromXML(XMLUtils::getChildNode(eqNode, "OptionData"));
108 strike_ = XMLUtils::getChildValueAsDouble(eqNode, "Strike", true);
109 payoffCurrency_ = XMLUtils::getChildValue(eqNode, "PayoffCurrency", true);
110 payoffAmount_ = XMLUtils::getChildValueAsDouble(eqNode, "PayoffAmount", true);
111 XMLNode* tmp = XMLUtils::getChildNode(eqNode, "Underlying");
112 if (!tmp)
113 tmp = XMLUtils::getChildNode(eqNode, "Name");
115 quantity_ = XMLUtils::getChildValueAsDouble(eqNode, "Quantity", true);
116}
117
119 XMLNode* node = Trade::toXML(doc);
120 XMLNode* eqNode = doc.allocNode("EquityDigitalOptionData");
121 XMLUtils::appendNode(node, eqNode);
122
123 XMLUtils::appendNode(eqNode, option_.toXML(doc));
124 XMLUtils::addChild(doc, eqNode, "Strike", strike_);
125 XMLUtils::addChild(doc, eqNode, "PayoffCurrency", payoffCurrency_);
126 XMLUtils::addChild(doc, eqNode, "PayoffAmount", payoffAmount_);
128 XMLUtils::addChild(doc, eqNode, "Quantity", quantity_);
129
130 return node;
131}
132} // namespace data
133} // namespace oreplus
Engine builder for equity options.
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
Build QuantLib/QuantExt instrument, link pricing engine.
void fromXML(XMLNode *node) override
Definition: underlying.cpp:81
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:102
const string & callPut() const
Definition: optiondata.hpp:71
const string & longShort() const
Definition: optiondata.hpp:70
const string & style() const
Definition: optiondata.hpp:74
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
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
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
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 Digital 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
Position::Type parsePositionType(const std::string &s)
Convert text to QuantLib::Position::Type.
Definition: parsers.cpp:404
Option::Type parseOptionType(const std::string &s)
Convert text to QuantLib::Option::Type.
Definition: parsers.cpp:481
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23