Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
enginefactory.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/enginefactory.hpp
20 \brief Pricing Engine Factory
21 \ingroup tradedata
22*/
23
24#pragma once
25
31
33
34#include <ql/pricingengine.hpp>
35
36#include <ql/shared_ptr.hpp>
37
38#include <map>
39#include <set>
40#include <vector>
41
42namespace ore {
43namespace data {
46using std::map;
47using std::pair;
48using std::set;
49using std::string;
50using std::tuple;
51
52class Trade;
53class LegBuilder;
54class ReferenceDataManager;
55
56/*! Market configuration contexts. Note that there is only one pricing context.
57 If several are needed (for different trade types, different collateral
58 currencies etc.), several engine factories should be set up for each such
59 portfolio subset. */
61
62//! Base PricingEngine Builder class for a specific model and engine
63/*!
64 * The EngineBuilder is responsible for building pricing engines for a specific
65 * Model and Engine.
66 *
67 * Each builder should implement a method with a signature
68 * @code
69 * QuantLib::ext::shared_ptr<PricingEngine> engine (...);
70 * @endcode
71 * The exact parameters of each method can vary depending on the type of engine.
72 *
73 * An EngineBuilder can cache engines and return the same PricingEngine multiple
74 * times, alternatively the Builder can build a unique PricingEngine each time it
75 * is called.
76 *
77 * For example a swap engine builder can have the interface
78 * @code
79 * QuantLib::ext::shared_ptr<PricingEngine> engine (const Currency&);
80 * @endcode
81 * and so returns the same (cached) engine every time it is asked for a particular
82 * currency.
83 *
84 * The interface of each type of engine builder can be different, then there can
85 * be further sub-classes for different models and engines.
86 *
87 * EngineBuilders are registered in an EngineFactory, multiple engine builders for
88 * the same trade type can be registered with the EngineFactory and it will select
89 * the appropriate one based on configuration.
90 *
91 * Each EngineBuilder must return it's Model and Engine.
92
93 \ingroup tradedata
94 */
96public:
97 /*! Constructor that takes a model and engine name
98 * @param model the model name
99 * @param engine the engine name
100 * @param tradeTypes a set of trade types
101 */
102 EngineBuilder(const string& model, const string& engine, const set<string>& tradeTypes)
104
105 //! Virtual destructor
106 virtual ~EngineBuilder() {}
107
108 //! Return the model name
109 const string& model() const { return model_; }
110 //! Return the engine name
111 const string& engine() const { return engine_; }
112 //! Return the possible trade types
113 const set<string>& tradeTypes() const { return tradeTypes_; }
114
115 //! Return a configuration (or the default one if key not found)
116 const string& configuration(const MarketContext& key) {
117 if (configurations_.count(key) > 0) {
118 return configurations_.at(key);
119 } else {
121 }
122 }
123
124 //! reset the builder (e.g. clear cache)
125 virtual void reset() {}
126
127 //! Initialise this Builder with the market and parameters to use
128 /*! This method should not be called directly, it is called by the EngineFactory
129 * before it is returned.
130 */
131 void init(const QuantLib::ext::shared_ptr<Market> market, const map<MarketContext, string>& configurations,
132 const map<string, string>& modelParameters, const map<string, string>& engineParameters,
133 const std::map<std::string, std::string>& globalParameters = {}) {
134 market_ = market;
135 configurations_ = configurations;
136 modelParameters_ = modelParameters;
137 engineParameters_ = engineParameters;
138 globalParameters_ = globalParameters;
139 }
140
141 //! return model builders
142 const set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>>& modelBuilders() const { return modelBuilders_; }
143
144 /*! retrieve engine parameter p, first look for p_qualifier, if this does not exist fall back to p */
145 std::string engineParameter(const std::string& p, const std::vector<std::string>& qualifiers = {},
146 const bool mandatory = true, const std::string& defaultValue = "") const;
147 /*! retrieve model parameter p, first look for p_qualifier, if this does not exist fall back to p */
148 std::string modelParameter(const std::string& p, const std::vector<std::string>& qualifiers = {},
149 const bool mandatory = true, const std::string& defaultValue = "") const;
150
151protected:
152 string model_;
153 string engine_;
154 set<string> tradeTypes_;
155 QuantLib::ext::shared_ptr<Market> market_;
156 map<MarketContext, string> configurations_;
157 map<string, string> modelParameters_;
158 map<string, string> engineParameters_;
159 std::map<std::string, std::string> globalParameters_;
160 set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>> modelBuilders_;
161};
162
163//! Delegating Engine Builder
164/* Special interface to consolidate different trade builders for one product. See AsianOption for a use case. */
166public:
168 virtual QuantLib::ext::shared_ptr<ore::data::Trade> build(const ore::data::Trade*,
169 const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) = 0;
170 virtual std::string effectiveTradeType() const = 0;
171};
172
173//! Engine/ Leg Builder Factory - notice that both engine and leg builders are allowed to maintain a state
174class EngineBuilderFactory : public QuantLib::Singleton<EngineBuilderFactory, std::integral_constant<bool, true>> {
175 std::vector<std::function<QuantLib::ext::shared_ptr<EngineBuilder>()>> engineBuilderBuilders_;
176 std::vector<std::function<QuantLib::ext::shared_ptr<EngineBuilder>(const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
177 const std::vector<Date>& grid)>>
179 std::vector<std::function<QuantLib::ext::shared_ptr<EngineBuilder>(const QuantLib::ext::shared_ptr<ore::data::ModelCG>& model,
180 const std::vector<Date>& grid)>>
182 std::vector<std::function<QuantLib::ext::shared_ptr<LegBuilder>()>> legBuilderBuilders_;
183 mutable boost::shared_mutex mutex_;
184
185public:
186 void addEngineBuilder(const std::function<QuantLib::ext::shared_ptr<EngineBuilder>()>& builder,
187 const bool allowOverwrite = false);
189 const std::function<QuantLib::ext::shared_ptr<EngineBuilder>(const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
190 const std::vector<Date>& grid)>& builder,
191 const bool allowOverwrite = false);
193 const std::function<QuantLib::ext::shared_ptr<EngineBuilder>(const QuantLib::ext::shared_ptr<ore::data::ModelCG>& model,
194 const std::vector<Date>& grid)>& builder,
195 const bool allowOverwrite = false);
196 void addLegBuilder(const std::function<QuantLib::ext::shared_ptr<LegBuilder>()>& builder,
197 const bool allowOverwrite = false);
198
199 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> generateEngineBuilders() const;
200 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>>
201 generateAmcEngineBuilders(const QuantLib::ext::shared_ptr<QuantExt::CrossAssetModel>& cam,
202 const std::vector<Date>& grid) const;
203 std::vector<QuantLib::ext::shared_ptr<EngineBuilder>>
204 generateAmcCgEngineBuilders(const QuantLib::ext::shared_ptr<ore::data::ModelCG>& model,
205 const std::vector<Date>& grid) const;
206 std::vector<QuantLib::ext::shared_ptr<LegBuilder>> generateLegBuilders() const;
207};
208
209//! Pricing Engine Factory class
210/*! A Pricing Engine Factory is used when building a portfolio, it provides
211 * QuantLib::PricingEngines to each of the Trade objects.
212 *
213 * An Engine Factory is configured on two levels, both from EngineData.
214 * The first level is the type of builder (Model and Engine) to use for each
215 * trade type, so given a trade type one asks the factory for a builder and
216 * then the builder for a PricingEngine. Each builder must be registered with
217 * the factory and then the configuration defines which builder to use.
218 *
219 * Secondly, the factory maintains builder specific parameters for each Model
220 * and Engine.
221
222 \ingroup tradedata
223 */
225public:
226 //! Create an engine factory
227 EngineFactory( //! Configuration data
228 const QuantLib::ext::shared_ptr<EngineData>& data,
229 //! The market that is passed to each builder
230 const QuantLib::ext::shared_ptr<Market>& market,
231 //! The market configurations that are passed to each builder
232 const map<MarketContext, string>& configurations = std::map<MarketContext, string>(),
233 //! reference data
234 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData = nullptr,
235 //! ibor fallback config
237 //! additional engine builders
238 const std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> extraEngineBuilders = {},
239 //! additional engine builders may overwrite existing builders with same key
240 const bool allowOverwrite = false);
241
242 //! Return the market used by this EngineFactory
243 const QuantLib::ext::shared_ptr<Market>& market() const { return market_; };
244 //! Return the market configurations used by this EngineFactory
245 const map<MarketContext, string>& configurations() const { return configurations_; };
246 //! Return a configuration (or the default one if key not found)
247 const string& configuration(const MarketContext& key) {
248 if (configurations_.count(key) > 0) {
249 return configurations_.at(key);
250 } else {
252 }
253 }
254 //! Return the EngineData parameters
255 const QuantLib::ext::shared_ptr<EngineData> engineData() const { return engineData_; };
256 //! Register a builder with the factory
257 void registerBuilder(const QuantLib::ext::shared_ptr<EngineBuilder>& builder, const bool allowOverwrite = false);
258 //! Return the reference data used by this EngineFactory
259 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData() const { return referenceData_; };
260 //! Return the ibor fallback config
262
263 //! Get a builder by trade type
264 /*! This will look up configured model/engine for that trade type
265 the returned builder can be cast to the type required for the tradeType.
266
267 The factory will call EngineBuilder::init() before returning it.
268 */
269 QuantLib::ext::shared_ptr<EngineBuilder> builder(const string& tradeType);
270
271 //! Register a leg builder with the factory
272 void registerLegBuilder(const QuantLib::ext::shared_ptr<LegBuilder>& legBuilder, const bool allowOverwrite = false);
273
274 //! Get a leg builder by leg type
275 QuantLib::ext::shared_ptr<LegBuilder> legBuilder(const string& legType);
276
277 //! Add a set of default engine and leg builders
278 void addDefaultBuilders();
279 //! Add a set of default engine and leg builders, overwrite existing builders with same key if specified
280 void addExtraBuilders(const std::vector<QuantLib::ext::shared_ptr<EngineBuilder>> extraEngineBuilders,
281 const std::vector<QuantLib::ext::shared_ptr<LegBuilder>> extraLegBuilders,
282 const bool allowOverwrite = false);
283
284 //! Clear all builders
285 void clear() {
286 builders_.clear();
287 legBuilders_.clear();
288 }
289
290 //! return model builders
291 set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>> modelBuilders() const;
292
293private:
294 QuantLib::ext::shared_ptr<Market> market_;
295 QuantLib::ext::shared_ptr<EngineData> engineData_;
296 map<MarketContext, string> configurations_;
297 map<tuple<string, string, set<string>>, QuantLib::ext::shared_ptr<EngineBuilder>> builders_;
298 map<string, QuantLib::ext::shared_ptr<LegBuilder>> legBuilders_;
299 QuantLib::ext::shared_ptr<ReferenceDataManager> referenceData_;
301};
302
303//! Leg builder
304class RequiredFixings;
306public:
307 LegBuilder(const string& legType) : legType_(legType) {}
308 virtual ~LegBuilder() {}
309 virtual Leg buildLeg(const LegData& data, const QuantLib::ext::shared_ptr<EngineFactory>&, RequiredFixings& requiredFixings,
310 const string& configuration, const QuantLib::Date& openEndDateReplacement = Null<Date>(),
311 const bool useXbsCurves = false) const = 0;
312 const string& legType() const { return legType_; }
313
314private:
315 const string legType_;
316};
317
318} // namespace data
319} // namespace ore
Delegating Engine Builder.
virtual QuantLib::ext::shared_ptr< ore::data::Trade > build(const ore::data::Trade *, const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory)=0
virtual std::string effectiveTradeType() const =0
Engine/ Leg Builder Factory - notice that both engine and leg builders are allowed to maintain a stat...
std::vector< std::function< QuantLib::ext::shared_ptr< LegBuilder >()> > legBuilderBuilders_
void addEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >()> &builder, const bool allowOverwrite=false)
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateAmcEngineBuilders(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid) const
void addAmcEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid)> &builder, const bool allowOverwrite=false)
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateEngineBuilders() const
std::vector< QuantLib::ext::shared_ptr< LegBuilder > > generateLegBuilders() const
void addAmcCgEngineBuilder(const std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid)> &builder, const bool allowOverwrite=false)
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >()> > engineBuilderBuilders_
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > &cam, const std::vector< Date > &grid)> > amcEngineBuilderBuilders_
std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > generateAmcCgEngineBuilders(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid) const
std::vector< std::function< QuantLib::ext::shared_ptr< EngineBuilder >(const QuantLib::ext::shared_ptr< ore::data::ModelCG > &model, const std::vector< Date > &grid)> > amcCgEngineBuilderBuilders_
void addLegBuilder(const std::function< QuantLib::ext::shared_ptr< LegBuilder >()> &builder, const bool allowOverwrite=false)
Base PricingEngine Builder class for a specific model and engine.
QuantLib::ext::shared_ptr< Market > market_
virtual ~EngineBuilder()
Virtual destructor.
const string & engine() const
Return the engine name.
const set< string > & tradeTypes() const
Return the possible trade types.
map< string, string > engineParameters_
const string & model() const
Return the model name.
void init(const QuantLib::ext::shared_ptr< Market > market, const map< MarketContext, string > &configurations, const map< string, string > &modelParameters, const map< string, string > &engineParameters, const std::map< std::string, std::string > &globalParameters={})
Initialise this Builder with the market and parameters to use.
const set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > & modelBuilders() const
return model builders
std::string modelParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
virtual void reset()
reset the builder (e.g. clear cache)
map< MarketContext, string > configurations_
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
EngineBuilder(const string &model, const string &engine, const set< string > &tradeTypes)
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders_
std::map< std::string, std::string > globalParameters_
map< string, string > modelParameters_
Pricing Engine Factory class.
const QuantLib::ext::shared_ptr< Market > & market() const
Return the market used by this EngineFactory.
QuantLib::ext::shared_ptr< Market > market_
const IborFallbackConfig & iborFallbackConfig() const
Return the ibor fallback config.
const map< MarketContext, string > & configurations() const
Return the market configurations used by this EngineFactory.
QuantLib::ext::shared_ptr< EngineBuilder > builder(const string &tradeType)
Get a builder by trade type.
map< string, QuantLib::ext::shared_ptr< LegBuilder > > legBuilders_
QuantLib::ext::shared_ptr< LegBuilder > legBuilder(const string &legType)
Get a leg builder by leg type.
QuantLib::ext::shared_ptr< EngineData > engineData_
set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders() const
return model builders
void addDefaultBuilders()
Add a set of default engine and leg builders.
map< MarketContext, string > configurations_
const QuantLib::ext::shared_ptr< EngineData > engineData() const
Return the EngineData parameters.
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
void clear()
Clear all builders.
IborFallbackConfig iborFallbackConfig_
QuantLib::ext::shared_ptr< ReferenceDataManager > referenceData_
const QuantLib::ext::shared_ptr< ReferenceDataManager > & referenceData() const
Return the reference data used by this EngineFactory.
void addExtraBuilders(const std::vector< QuantLib::ext::shared_ptr< EngineBuilder > > extraEngineBuilders, const std::vector< QuantLib::ext::shared_ptr< LegBuilder > > extraLegBuilders, const bool allowOverwrite=false)
Add a set of default engine and leg builders, overwrite existing builders with same key if specified.
map< tuple< string, string, set< string > >, QuantLib::ext::shared_ptr< EngineBuilder > > builders_
void registerBuilder(const QuantLib::ext::shared_ptr< EngineBuilder > &builder, const bool allowOverwrite=false)
Register a builder with the factory.
void registerLegBuilder(const QuantLib::ext::shared_ptr< LegBuilder > &legBuilder, const bool allowOverwrite=false)
Register a leg builder with the factory.
static IborFallbackConfig defaultConfig()
virtual Leg buildLeg(const LegData &data, const QuantLib::ext::shared_ptr< EngineFactory > &, RequiredFixings &requiredFixings, const string &configuration, const QuantLib::Date &openEndDateReplacement=Null< Date >(), const bool useXbsCurves=false) const =0
const string & legType() const
LegBuilder(const string &legType)
Serializable object holding leg data.
Definition: legdata.hpp:844
static const string defaultConfiguration
Default configuration label.
Definition: market.hpp:296
Trade base class.
Definition: trade.hpp:55
A class to hold pricing engine parameters.
ibor fallback configuration
leg data model and serialization
@ data
Definition: log.hpp:77
Base Market class.
interface for model against which a script can be run
Serializable Credit Default Swap.
Definition: namespaces.docs:23