Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scriptedtrade.hpp
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
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/scriptedtrade.hpp
20 \brief scripted trade data model
21 \ingroup portfolio
22*/
23
24#pragma once
25
29
30namespace ore {
31namespace data {
32
34public:
35 enum class Type { Value, Array, Derived };
36 // default ctor
38 // single value cor
39 ScriptedTradeEventData(const std::string& name, const std::string& date)
40 : type_(Type::Value), name_(name), value_(date) {}
41 // array ctor
44 // derived schedule ctor
45 ScriptedTradeEventData(const std::string& name, const std::string& baseSchedule, const std::string& shift,
46 const std::string& calendar, const std::string& convention)
49
50 virtual void fromXML(XMLNode* node) override;
51 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
52
53 Type type() const { return type_; }
54 const std::string& name() const { return name_; }
55 const std::string& value() const { return value_; }
56 const ScheduleData& schedule() const { return schedule_; }
57 const std::string& baseSchedule() const { return baseSchedule_; }
58 const std::string& shift() const { return shift_; }
59 const std::string& calendar() const { return calendar_; }
60 const std::string& convention() const { return convention_; }
61 const bool hasData();
62
63private:
65 std::string name_;
66 std::string value_; // single value
67 ScheduleData schedule_; // schedule data
68 std::string baseSchedule_; // derived schedule
69 std::string shift_;
70 std::string calendar_;
71 std::string convention_;
72};
73
75public:
76 // node name ctor
77 explicit ScriptedTradeValueTypeData(const std::string& nodeName) : nodeName_(nodeName), isArray_(false) {}
78 // single value cor
79 ScriptedTradeValueTypeData(const std::string& nodeName, const std::string& name, const std::string& value)
80 : nodeName_(nodeName), isArray_(false), name_(name), value_(value) {}
81 // array ctor
82 ScriptedTradeValueTypeData(const std::string& nodeName, const std::string& name,
83 const std::vector<std::string>& values)
84 : nodeName_(nodeName), isArray_(true), name_(name), values_(values) {}
85
86 virtual void fromXML(XMLNode* node) override;
87 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
88
89 bool isArray() const { return isArray_; }
90 const std::string& name() const { return name_; }
91 const std::string& value() const { return value_; }
92 const std::vector<std::string>& values() const { return values_; }
93
94private:
95 std::string nodeName_;
97 std::string name_;
98 std::string value_;
99 std::vector<std::string> values_;
100};
101
103public:
105 public:
107 NewScheduleData(const std::string& name, const std::string& operation,
108 const std::vector<std::string>& sourceSchedules)
110 virtual void fromXML(XMLNode* node) override;
111 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
112 const std::string& name() const { return name_; }
113 const std::string& operation() const { return operation_; }
114 const std::vector<std::string>& sourceSchedules() const { return sourceSchedules_; }
115
116 private:
117 std::string name_;
118 std::string operation_;
119 std::vector<std::string> sourceSchedules_;
120 };
121
123 public:
125 CalibrationData(const std::string& index, const std::vector<std::string>& strikes)
127 virtual void fromXML(XMLNode* node) override;
128 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
129 const std::string& index() const { return index_; }
130 const std::vector<string>& strikes() const { return strikes_; }
131
132 private:
133 std::string index_;
134 std::vector<std::string> strikes_;
135 };
136
137 // default ctor
139 // ctor taking data
140 ScriptedTradeScriptData(const std::string& code, const std::string& npv,
141 const std::vector<std::pair<std::string, std::string>>& results,
142 const std::vector<std::string>& schedulesEligibleForCoarsening,
143 const std::vector<NewScheduleData>& newSchedules = {},
144 const std::vector<CalibrationData>& calibrationSpec = {},
145 const std::vector<std::string>& stickyCloseOutStates = {},
146 const std::vector<std::string>& conditionalExpectationModelStates = {})
150 formatCode();
151 }
152
153 virtual void fromXML(XMLNode* node) override;
154 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
155
156 const std::string& code() const { return code_; }
157 const std::string& npv() const { return npv_; }
158 // a result is given by a name and a context variable, e.g. ("notionalCurrency", "PayCcy")
159 const std::vector<std::pair<std::string, std::string>>& results() const { return results_; }
160 const std::vector<std::string>& schedulesEligibleForCoarsening() const { return schedulesEligibleForCoarsening_; }
161 const std::vector<NewScheduleData>& newSchedules() const { return newSchedules_; }
162 const std::vector<CalibrationData>& calibrationSpec() const { return calibrationSpec_; }
163 const std::vector<std::string>& stickyCloseOutStates() const { return stickyCloseOutStates_; }
164 const std::vector<std::string>& conditionalExpectationModelStates() const {
166 }
167
168private:
169 void formatCode();
170 std::string code_;
171 std::string npv_;
172 std::vector<std::pair<std::string, std::string>> results_;
173 std::vector<std::string> schedulesEligibleForCoarsening_;
174 std::vector<NewScheduleData> newSchedules_;
175 std::vector<CalibrationData> calibrationSpec_;
176 std::vector<std::string> stickyCloseOutStates_;
177 std::vector<std::string> conditionalExpectationModelStates_;
178};
179
181public:
187
188 virtual void fromXML(XMLNode* node) override;
189 virtual XMLNode* toXML(ore::data::XMLDocument& doc) const override;
190
191 bool has(const std::string& scriptName, const std::string& purpose, const bool fallBackOnEmptyPurpose = true) const;
192
193 // get (product tag, script data)
194 std::pair<std::string, ScriptedTradeScriptData> get(const std::string& scriptName, const std::string& purpose,
195 const bool fallBackOnEmptyPurpose = true) const;
196
197private:
198 // scriptName => ( productTag, purpose => script )
199 std::map<std::string, std::pair<std::string, std::map<std::string, ScriptedTradeScriptData>>> scripts_;
200};
201
202class ScriptedTrade : public Trade {
203public:
204 // ctor taking a trade type and an envelope
205 ScriptedTrade(const std::string& tradeType = "ScriptedTrade", const Envelope& env = Envelope())
206 : Trade(tradeType, env) {}
207 // ctor taking data and an explicit script + product tag
208 ScriptedTrade(const Envelope& env, const std::vector<ScriptedTradeEventData>& events,
209 const std::vector<ScriptedTradeValueTypeData>& numbers,
210 const std::vector<ScriptedTradeValueTypeData>& indices,
211 const std::vector<ScriptedTradeValueTypeData>& currencies,
212 const std::vector<ScriptedTradeValueTypeData>& daycounters,
213 const std::map<std::string, ScriptedTradeScriptData>& script, const std::string& productTag,
214 const std::string& tradeType = "ScriptedTrade")
217 // ctor taking data and a reference to a script in the script library
218 ScriptedTrade(const Envelope& env, const std::vector<ScriptedTradeEventData>& events,
219 const std::vector<ScriptedTradeValueTypeData>& numbers,
220 const std::vector<ScriptedTradeValueTypeData>& indices,
221 const std::vector<ScriptedTradeValueTypeData>& currencies,
222 const std::vector<ScriptedTradeValueTypeData>& daycounters, const std::string& scriptName,
223 const std::string& tradeType = "ScriptedTrade")
226
227 // clear data members specific to ScriptedTrade, e.g. called in fromXML()
228 void clear();
229
230 // Trade interface
231 void build(const QuantLib::ext::shared_ptr<EngineFactory>&) override;
232 QuantLib::Real notional() const override;
233 std::string notionalCurrency() const override;
234 void fromXML(XMLNode* node) override;
235 XMLNode* toXML(ore::data::XMLDocument& doc) const override;
236
237 // build and incorporate provided premium data
238 void build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory, const PremiumData& premiumData,
239 const Real premiumMultiplier);
240
241 // underlying asset names
242 std::map<ore::data::AssetClass, std::set<std::string>>
243 underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager = nullptr) const override;
244
245 // Add ISDA taxonomy classification to additional data
246 virtual void setIsdaTaxonomyFields();
247
248 // Inspectors
249 const std::vector<ScriptedTradeEventData>& events() const { return events_; }
250 const std::vector<ScriptedTradeValueTypeData>& numbers() const { return numbers_; }
251 const std::vector<ScriptedTradeValueTypeData>& indices() const { return indices_; }
252 const std::vector<ScriptedTradeValueTypeData>& currencies() const { return currencies_; }
253 const std::vector<ScriptedTradeValueTypeData>& daycounters() const { return daycounters_; }
254 const std::map<std::string, ScriptedTradeScriptData>& script() const { return script_; }
255 const std::string& productTag() const { return productTag_; }
256 const std::string& scriptName() const { return scriptName_; }
257 const std::string& simmProductClass() const { return simmProductClass_; }
258 const std::string& scheduleProductClass() const { return scheduleProductClass_; }
259
260 // get script for purpose, possibly fall back on script with empty purpose
261 const ScriptedTradeScriptData& script(const std::string& purpose, const bool fallBackOnEmptyPurpose = true) const;
262
263protected:
264 // data
265 std::vector<ScriptedTradeEventData> events_;
266 std::vector<ScriptedTradeValueTypeData> numbers_;
267 std::vector<ScriptedTradeValueTypeData> indices_;
268 std::vector<ScriptedTradeValueTypeData> currencies_;
269 std::vector<ScriptedTradeValueTypeData> daycounters_;
270 // either we have a script + product tag ...
271 std::map<std::string, ScriptedTradeScriptData> script_;
272 std::string productTag_;
273 // .. or a script name referencing a script in the library
274 std::string scriptName_;
275
276 // set in build()
277 std::string simmProductClass_;
279};
280
281class ScriptLibraryStorage : public QuantLib::Singleton<ScriptLibraryStorage, std::integral_constant<bool, true>> {
283 mutable boost::shared_mutex mutex_;
284
285public:
286 const ScriptLibraryData& get() const;
287 void set(const ScriptLibraryData& data);
288 void set(ScriptLibraryData&& data);
289 void clear();
290};
291
292} // namespace data
293} // namespace ore
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding premium data.
Definition: premiumdata.hpp:37
Serializable schedule data.
Definition: schedule.hpp:202
ScriptLibraryData & operator=(const ScriptLibraryData &d)
std::pair< std::string, ScriptedTradeScriptData > get(const std::string &scriptName, const std::string &purpose, const bool fallBackOnEmptyPurpose=true) const
std::map< std::string, std::pair< std::string, std::map< std::string, ScriptedTradeScriptData > > > scripts_
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
bool has(const std::string &scriptName, const std::string &purpose, const bool fallBackOnEmptyPurpose=true) const
void set(const ScriptLibraryData &data)
const ScriptLibraryData & get() const
const std::string & convention() const
const ScheduleData & schedule() const
const std::string & name() const
const std::string & value() const
ScriptedTradeEventData(const std::string &name, const ScheduleData &schedule)
ScriptedTradeEventData(const std::string &name, const std::string &baseSchedule, const std::string &shift, const std::string &calendar, const std::string &convention)
const std::string & shift() const
virtual void fromXML(XMLNode *node) override
const std::string & baseSchedule() const
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
ScriptedTradeEventData(const std::string &name, const std::string &date)
const std::string & calendar() const
std::string notionalCurrency() const override
const std::string & productTag() const
const std::map< std::string, ScriptedTradeScriptData > & script() const
std::vector< ScriptedTradeValueTypeData > daycounters_
std::vector< ScriptedTradeEventData > events_
const std::string & scriptName() const
std::vector< ScriptedTradeValueTypeData > currencies_
const std::string & scheduleProductClass() const
std::map< ore::data::AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const override
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
const std::vector< ScriptedTradeValueTypeData > & indices() const
void fromXML(XMLNode *node) override
const std::vector< ScriptedTradeValueTypeData > & daycounters() const
const std::vector< ScriptedTradeValueTypeData > & numbers() const
XMLNode * toXML(ore::data::XMLDocument &doc) const override
ScriptedTrade(const std::string &tradeType="ScriptedTrade", const Envelope &env=Envelope())
std::vector< ScriptedTradeValueTypeData > indices_
const std::string & simmProductClass() const
virtual void setIsdaTaxonomyFields()
std::vector< ScriptedTradeValueTypeData > numbers_
const std::vector< ScriptedTradeValueTypeData > & currencies() const
ScriptedTrade(const Envelope &env, const std::vector< ScriptedTradeEventData > &events, const std::vector< ScriptedTradeValueTypeData > &numbers, const std::vector< ScriptedTradeValueTypeData > &indices, const std::vector< ScriptedTradeValueTypeData > &currencies, const std::vector< ScriptedTradeValueTypeData > &daycounters, const std::string &scriptName, const std::string &tradeType="ScriptedTrade")
std::map< std::string, ScriptedTradeScriptData > script_
ScriptedTrade(const Envelope &env, const std::vector< ScriptedTradeEventData > &events, const std::vector< ScriptedTradeValueTypeData > &numbers, const std::vector< ScriptedTradeValueTypeData > &indices, const std::vector< ScriptedTradeValueTypeData > &currencies, const std::vector< ScriptedTradeValueTypeData > &daycounters, const std::map< std::string, ScriptedTradeScriptData > &script, const std::string &productTag, const std::string &tradeType="ScriptedTrade")
void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
const std::vector< ScriptedTradeEventData > & events() const
const std::vector< string > & strikes() const
CalibrationData(const std::string &index, const std::vector< std::string > &strikes)
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
NewScheduleData(const std::string &name, const std::string &operation, const std::vector< std::string > &sourceSchedules)
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
const std::vector< std::string > & sourceSchedules() const
const std::vector< std::string > & stickyCloseOutStates() const
const std::string & code() const
std::vector< std::string > conditionalExpectationModelStates_
std::vector< CalibrationData > calibrationSpec_
std::vector< NewScheduleData > newSchedules_
const std::vector< std::string > & conditionalExpectationModelStates() const
const std::string & npv() const
const std::vector< CalibrationData > & calibrationSpec() const
std::vector< std::string > schedulesEligibleForCoarsening_
virtual void fromXML(XMLNode *node) override
std::vector< std::pair< std::string, std::string > > results_
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
const std::vector< NewScheduleData > & newSchedules() const
const std::vector< std::pair< std::string, std::string > > & results() const
std::vector< std::string > stickyCloseOutStates_
ScriptedTradeScriptData(const std::string &code, const std::string &npv, const std::vector< std::pair< std::string, std::string > > &results, const std::vector< std::string > &schedulesEligibleForCoarsening, const std::vector< NewScheduleData > &newSchedules={}, const std::vector< CalibrationData > &calibrationSpec={}, const std::vector< std::string > &stickyCloseOutStates={}, const std::vector< std::string > &conditionalExpectationModelStates={})
const std::vector< std::string > & schedulesEligibleForCoarsening() const
std::vector< std::string > values_
ScriptedTradeValueTypeData(const std::string &nodeName, const std::string &name, const std::string &value)
const std::string & name() const
const std::vector< std::string > & values() const
const std::string & value() const
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
ScriptedTradeValueTypeData(const std::string &nodeName)
ScriptedTradeValueTypeData(const std::string &nodeName, const std::string &name, const std::vector< std::string > &values)
Trade base class.
Definition: trade.hpp:55
const string & tradeType() const
Definition: trade.hpp:133
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
Base class for all serializable classes.
Definition: xmlutils.hpp:101
@ data
Definition: log.hpp:77
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
Serializable Credit Default Swap.
Definition: namespaces.docs:23
trade schedule data model and serialization
base trade data model and serialization
Trade Factory.