Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
trade.hpp
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/*! \file ored/portfolio/trade.hpp
20 \brief base trade data model and serialization
21 \ingroup portfolio
22*/
23
24#pragma once
25
32#include <ored/portfolio/tradefactory.hpp> // just convenience so that client code needs to include trade.hpp only
33
35
36#include <ql/cashflow.hpp>
37#include <ql/instrument.hpp>
38#include <ql/time/date.hpp>
39
40namespace ore {
41namespace data {
44using QuantLib::Date;
45using std::string;
46
47//! Trade base class
48/*! Instrument interface to pricing and risk applications
49 Derived classes should
50 - contain additional serializable data classes
51 - implement a build() function that parses data and constructs QuantLib
52 and QuantExt objects
53 \ingroup portfolio
54*/
55class Trade : public XMLSerializable {
56public:
57
58 //! Default constructor
59 Trade() {}
60
61 //! Base class constructor
62 Trade(const string& tradeType, const Envelope& env = Envelope(), const TradeActions& ta = TradeActions())
64 reset();
65 }
66
67 //! Default destructor
68 virtual ~Trade() {}
69
70 /*! Build QuantLib/QuantExt instrument, link pricing engine. If build() is called multiple times, reset() should
71 be called between these calls. */
72 virtual void build(const QuantLib::ext::shared_ptr<EngineFactory>&) = 0;
73
74 /*! Return the fixings that will be requested in order to price this Trade given the \p settlementDate.
75
76
77
78 If the \p settlementDate is not provided, the current evaluation date is taken as the settlement date.
79 If a Trade does not have any fixings, this method will return an empty map.
80 The map key is the ORE name of the index and the map value is the set of fixing dates.
81
82 \warning This method will return an empty map if the Trade has not been built.
83 */
84 virtual std::map<std::string, RequiredFixings::FixingDates>
85 fixings(const QuantLib::Date& settlementDate = QuantLib::Date()) const {
86 return requiredFixings_.fixingDatesIndices(settlementDate);
87 }
88
89 /*! Return the full required fixing information */
91
92 virtual std::map<AssetClass, std::set<std::string>>
93 underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager = nullptr) const {
94 return {};
95 }
96
97 //! \name Serialisation
98 //@{
99 virtual void fromXML(XMLNode* node) override;
100 virtual XMLNode* toXML(XMLDocument& doc) const override;
101 //@}
102
103 //! Reset trade, clear all base class data. This does not reset accumulated timings for this trade.
104 void reset();
105
106 //! Reset accumulated timings to given values
107 void resetPricingStats(const std::size_t numberOfPricings = 0,
108 const boost::timer::nanosecond_type cumulativePricingTime = 0) {
109 savedNumberOfPricings_ = numberOfPricings;
110 savedCumulativePricingTime_ = cumulativePricingTime;
111 if (instrument_ != nullptr)
112 instrument_->resetPricingStats();
113 }
114
115 //! \name Setters
116 //@{
117 //! Set the trade id
118 string& id() { return id_; }
119
120 //! Set the envelope with counterparty and portfolio info
121 void setEnvelope(const Envelope& envelope);
122
123 void setAdditionalData(const std::map<std::string, boost::any>& additionalData);
124
125 //! Set the trade actions
127 //@}
128
129 //! \name Inspectors
130 //@{
131 const string& id() const { return id_; }
132
133 const string& tradeType() const { return tradeType_; }
134
135 const Envelope& envelope() const { return envelope_; }
136
137 const set<string>& portfolioIds() const { return envelope().portfolioIds(); }
138
139 const TradeActions& tradeActions() const { return tradeActions_; }
140
141 const QuantLib::ext::shared_ptr<InstrumentWrapper>& instrument() const { return instrument_; }
142
143 const std::vector<QuantLib::Leg>& legs() const { return legs_; }
144
145 const std::vector<string>& legCurrencies() const { return legCurrencies_; }
146
147 const std::vector<bool>& legPayers() const { return legPayers_; }
148
149 const string& npvCurrency() const { return npvCurrency_; }
150
151 //! Return the current notional in npvCurrency. See individual sub-classes for the precise definition
152 // of notional, for exotic trades this may not be what you expect.
153 virtual QuantLib::Real notional() const { return notional_; }
154
155 virtual string notionalCurrency() const { return notionalCurrency_; }
156
157 const Date& maturity() const { return maturity_; }
158
159 virtual bool isExpired(const Date& d) { return d >= maturity_; }
160
161 const string& issuer() const { return issuer_; }
162
163 //! returns any additional datum.
164 template <typename T> T additionalDatum(const std::string& tag) const;
165 //! returns all additional data returned by the trade once built
166 const virtual std::map<std::string,boost::any>& additionalData() const;
167
168 /*! returns the sensi template, e.g. "IR_Analytical" for this trade,
169 this is only available after build() has been called */
170 const std::string& sensitivityTemplate() const;
171 //@}
172
173 //! \name Utility
174 //@{
175 //! Utility to validate that everything that needs to be set in this base class is actually set
176 void validate() const;
177
178 /*! Utility method indicating if the trade has cashflows for the cashflow report. The default implementation
179 returns \c true so that a trade is automatically considered when cashflows are being written. To prevent a
180 trade from being asked for its cashflows, the method can be overridden to return \c false.
181 */
182 virtual bool hasCashflows() const { return true; }
183 //@}
184
185 //! Get cumulative timing spent on pricing
186 boost::timer::nanosecond_type getCumulativePricingTime() const {
187 return savedCumulativePricingTime_ + (instrument_ != nullptr ? instrument_->getCumulativePricingTime() : 0);
188 }
189
190 //! Get number of pricings
191 std::size_t getNumberOfPricings() const {
192 return savedNumberOfPricings_ + (instrument_ != nullptr ? instrument_->getNumberOfPricings() : 0);
193 }
194
195protected:
196 string tradeType_; // class name of the derived class
197 QuantLib::ext::shared_ptr<InstrumentWrapper> instrument_;
198 std::vector<QuantLib::Leg> legs_;
199 std::vector<string> legCurrencies_;
200 std::vector<bool> legPayers_;
202 QuantLib::Real notional_;
205 string issuer_;
208
209 std::size_t savedNumberOfPricings_ = 0;
210 boost::timer::nanosecond_type savedCumulativePricingTime_ = 0;
211
212 // Utility to add premiums such that they are taken into account in pricing and cash flow projection.
213 // For example, an option premium flow is not covered by the underlying option instrument in
214 // QuantLib and needs to be represented separately. This is done by inserting it as an additional instrument
215 // into the InstrumentWrapper. This utility creates the additional instrument. The actual insertion into the
216 // instrument wrapper is done in the individual trade builders when they instantiate the InstrumentWrapper.
217 // The returned date is the latest premium payment date added.
218 Date addPremiums(std::vector<QuantLib::ext::shared_ptr<Instrument>>& instruments, std::vector<Real>& multipliers,
219 const Real tradeMultiplier, const PremiumData& premiumData, const Real premiumMultiplier,
220 const Currency& tradeCurrency, const QuantLib::ext::shared_ptr<EngineFactory>& factory,
221 const string& configuration);
222
224 mutable std::map<std::string,boost::any> additionalData_;
225
226 /* sets additional data based on given internal legNo (0, 1, ...), the result leg id is derived from this
227 as "legNo + 1", i.e. starting with 1 (1, 2, ...). The result leg id can be overwriten using the second
228 parameter resultLegId. */
229 void setLegBasedAdditionalData(const Size legNo, Size resultLegId = Null<Size>()) const;
230
231 /* sets the sensitivity template for this trade */
232 void setSensitivityTemplate(const EngineBuilder& builder);
233 void setSensitivityTemplate(const std::string& id);
234
235private:
236 string id_;
239};
240
241template <class T>
242inline T Trade::additionalDatum(const std::string& tag) const {
243 std::map<std::string,boost::any>::const_iterator value =
244 additionalData_.find(tag);
245 QL_REQUIRE(value != additionalData_.end(),
246 tag << " not provided");
247 return boost::any_cast<T>(value->second);
248}
249
250} // namespace data
251} // namespace ore
Base PricingEngine Builder class for a specific model and engine.
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
const set< string > & portfolioIds() const
Definition: envelope.hpp:103
Serializable object holding premium data.
Definition: premiumdata.hpp:37
std::map< std::string, FixingDates > fixingDatesIndices(const QuantLib::Date &settlementDate=QuantLib::Date()) const
Serializable object holding generic trade actions.
Trade base class.
Definition: trade.hpp:55
TradeActions & tradeActions()
Set the trade actions.
Definition: trade.hpp:126
const string & issuer() const
Definition: trade.hpp:161
string npvCurrency_
Definition: trade.hpp:201
void setEnvelope(const Envelope &envelope)
Set the envelope with counterparty and portfolio info.
Definition: trade.cpp:122
virtual bool isExpired(const Date &d)
Definition: trade.hpp:159
const std::string & sensitivityTemplate() const
Definition: trade.cpp:305
const string & id() const
Definition: trade.hpp:131
void resetPricingStats(const std::size_t numberOfPricings=0, const boost::timer::nanosecond_type cumulativePricingTime=0)
Reset accumulated timings to given values.
Definition: trade.hpp:107
std::vector< bool > legPayers_
Definition: trade.hpp:200
string sensitivityTemplate_
Definition: trade.hpp:206
std::vector< string > legCurrencies_
Definition: trade.hpp:199
std::size_t getNumberOfPricings() const
Get number of pricings.
Definition: trade.hpp:191
string issuer_
Definition: trade.hpp:205
T additionalDatum(const std::string &tag) const
returns any additional datum.
Definition: trade.hpp:242
const TradeActions & tradeActions() const
Definition: trade.hpp:139
const std::vector< QuantLib::Leg > & legs() const
Definition: trade.hpp:143
const Date & maturity() const
Definition: trade.hpp:157
const set< string > & portfolioIds() const
Definition: trade.hpp:137
std::vector< QuantLib::Leg > legs_
Definition: trade.hpp:198
TradeActions tradeActions_
Definition: trade.hpp:238
const RequiredFixings & requiredFixings() const
Definition: trade.hpp:90
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
const std::vector< string > & legCurrencies() const
Definition: trade.hpp:145
string & id()
Set the trade id.
Definition: trade.hpp:118
string tradeType_
Definition: trade.hpp:196
bool sensitivityTemplateSet_
Definition: trade.hpp:207
virtual const std::map< std::string, boost::any > & additionalData() const
returns all additional data returned by the trade once built
Definition: trade.cpp:151
const std::vector< bool > & legPayers() const
Definition: trade.hpp:147
void setAdditionalData(const std::map< std::string, boost::any > &additionalData)
Definition: trade.cpp:126
virtual ~Trade()
Default destructor.
Definition: trade.hpp:68
virtual bool hasCashflows() const
Definition: trade.hpp:182
RequiredFixings requiredFixings_
Definition: trade.hpp:223
void validate() const
Utility to validate that everything that needs to be set in this base class is actually set.
Definition: trade.cpp:104
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument() const
Definition: trade.hpp:141
virtual QuantLib::Real notional() const
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
Definition: trade.hpp:153
const string & npvCurrency() const
Definition: trade.hpp:149
const Envelope & envelope() const
Definition: trade.hpp:135
boost::timer::nanosecond_type savedCumulativePricingTime_
Definition: trade.hpp:210
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
Envelope envelope_
Definition: trade.hpp:237
boost::timer::nanosecond_type getCumulativePricingTime() const
Get cumulative timing spent on pricing.
Definition: trade.hpp:186
void reset()
Reset trade, clear all base class data. This does not reset accumulated timings for this trade.
Definition: trade.cpp:130
virtual std::map< std::string, RequiredFixings::FixingDates > fixings(const QuantLib::Date &settlementDate=QuantLib::Date()) const
Definition: trade.hpp:85
Trade(const string &tradeType, const Envelope &env=Envelope(), const TradeActions &ta=TradeActions())
Base class constructor.
Definition: trade.hpp:62
virtual std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const
Definition: trade.hpp:93
Trade()
Default constructor.
Definition: trade.hpp:59
std::size_t savedNumberOfPricings_
Definition: trade.hpp:209
string notionalCurrency_
Definition: trade.hpp:203
virtual string notionalCurrency() const
Definition: trade.hpp:155
const string & tradeType() const
Definition: trade.hpp:133
void setLegBasedAdditionalData(const Size legNo, Size resultLegId=Null< Size >()) const
Definition: trade.cpp:153
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
virtual void build(const QuantLib::ext::shared_ptr< EngineFactory > &)=0
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
Base class for all serializable classes.
Definition: xmlutils.hpp:101
SafeStack< ValueType > value
Pricing Engine Factory.
trade envelope data model and serialization
Logic for calculating required fixing dates on legs.
Base class for wrapper of QL instrument, used to store "state" of trade under each scenario.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
premium data
Trade Factory.