QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
instrument.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file instrument.hpp
22 \brief Abstract instrument class
23*/
24
25#ifndef quantlib_instrument_hpp
26#define quantlib_instrument_hpp
27
29#include <ql/pricingengine.hpp>
30#include <ql/utilities/null.hpp>
31#include <ql/time/date.hpp>
32#include <ql/any.hpp>
33#include <map>
34#include <string>
35
36namespace QuantLib {
37
38 //! Abstract instrument class
39 /*! This class is purely abstract and defines the interface of concrete
40 instruments which will be derived from this one.
41
42 \test observability of class instances is checked.
43 */
44 class Instrument : public LazyObject {
45 public:
46 class results;
47 Instrument();
48 //! \name Inspectors
49 //@{
50
51 //! returns the net present value of the instrument.
52 Real NPV() const;
53 //! returns the error estimate on the NPV when available.
54 Real errorEstimate() const;
55 //! returns the date the net present value refers to.
56 const Date& valuationDate() const;
57
58 //! returns any additional result returned by the pricing engine.
59 template <typename T> T result(const std::string& tag) const;
60 //! returns all additional result returned by the pricing engine.
61 const std::map<std::string, ext::any>& additionalResults() const;
62
63 //! returns whether the instrument might have value greater than zero.
64 virtual bool isExpired() const = 0;
65 //@}
66 //! \name Modifiers
67 //@{
68 //! set the pricing engine to be used.
69 /*! \warning calling this method will have no effects in
70 case the <b>performCalculation</b> method
71 was overridden in a derived class.
72 */
73 void setPricingEngine(const ext::shared_ptr<PricingEngine>&);
74 //@}
75 /*! When a derived argument structure is defined for an
76 instrument, this method should be overridden to fill
77 it. This is mandatory in case a pricing engine is used.
78 */
79 virtual void setupArguments(PricingEngine::arguments*) const;
80 /*! When a derived result structure is defined for an
81 instrument, this method should be overridden to read from
82 it. This is mandatory in case a pricing engine is used.
83 */
84 virtual void fetchResults(const PricingEngine::results*) const;
85 protected:
86 //! \name Calculations
87 //@{
88 void calculate() const override;
89 /*! This method must leave the instrument in a consistent
90 state when the expiration condition is met.
91 */
92 virtual void setupExpired() const;
93 /*! In case a pricing engine is <b>not</b> used, this
94 method must be overridden to perform the actual
95 calculations and set any needed results. In case
96 a pricing engine is used, the default implementation
97 can be used.
98 */
99 void performCalculations() const override;
100 //@}
101 /*! \name Results
102 The value of this attribute and any other that derived
103 classes might declare must be set during calculation.
104 */
105 //@{
108 mutable std::map<std::string, ext::any> additionalResults_;
109 //@}
110 ext::shared_ptr<PricingEngine> engine_;
111 };
112
114 public:
115 void reset() override {
118 additionalResults.clear();
119 }
123 std::map<std::string, ext::any> additionalResults;
124 };
125
126
127 // inline definitions
128
129 inline void Instrument::calculate() const {
130 if (!calculated_) {
131 if (isExpired()) {
132 setupExpired();
133 calculated_ = true;
134 } else {
136 }
137 }
138 }
139
140 inline void Instrument::setupExpired() const {
141 NPV_ = errorEstimate_ = 0.0;
143 additionalResults_.clear();
144 }
145
147 QL_REQUIRE(engine_, "null pricing engine");
148 engine_->reset();
149 setupArguments(engine_->getArguments());
150 engine_->getArguments()->validate();
151 engine_->calculate();
152 fetchResults(engine_->getResults());
153 }
154
156 const PricingEngine::results* r) const {
157 const auto* results = dynamic_cast<const Instrument::results*>(r);
158 QL_ENSURE(results != nullptr, "no results returned from pricing engine");
159
160 NPV_ = results->value;
163
165 }
166
167 inline Real Instrument::NPV() const {
168 calculate();
169 QL_REQUIRE(NPV_ != Null<Real>(), "NPV not provided");
170 return NPV_;
171 }
172
174 calculate();
176 "error estimate not provided");
177 return errorEstimate_;
178 }
179
180 inline const Date& Instrument::valuationDate() const {
181 calculate();
183 "valuation date not provided");
184 return valuationDate_;
185 }
186
187 template <class T>
188 inline T Instrument::result(const std::string& tag) const {
189 calculate();
190 std::map<std::string, ext::any>::const_iterator value =
191 additionalResults_.find(tag);
192 QL_REQUIRE(value != additionalResults_.end(),
193 tag << " not provided");
194 return ext::any_cast<T>(value->second);
195 }
196
197 inline const std::map<std::string, ext::any>&
199 calculate();
200 return additionalResults_;
201 }
202
203}
204
205#endif
Maps any to either the boost or std implementation.
Concrete date class.
Definition: date.hpp:125
std::map< std::string, ext::any > additionalResults
Definition: instrument.hpp:123
Abstract instrument class.
Definition: instrument.hpp:44
const std::map< std::string, ext::any > & additionalResults() const
returns all additional result returned by the pricing engine.
Definition: instrument.hpp:198
void performCalculations() const override
Definition: instrument.hpp:146
std::map< std::string, ext::any > additionalResults_
Definition: instrument.hpp:108
T result(const std::string &tag) const
returns any additional result returned by the pricing engine.
Definition: instrument.hpp:188
Real NPV() const
returns the net present value of the instrument.
Definition: instrument.hpp:167
void calculate() const override
Definition: instrument.hpp:129
virtual bool isExpired() const =0
returns whether the instrument might have value greater than zero.
virtual void fetchResults(const PricingEngine::results *) const
Definition: instrument.hpp:155
Real errorEstimate() const
returns the error estimate on the NPV when available.
Definition: instrument.hpp:173
ext::shared_ptr< PricingEngine > engine_
Definition: instrument.hpp:110
const Date & valuationDate() const
returns the date the net present value refers to.
Definition: instrument.hpp:180
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
virtual void setupArguments(PricingEngine::arguments *) const
Definition: instrument.cpp:45
virtual void setupExpired() const
Definition: instrument.hpp:140
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
template class providing a null value for a given type.
Definition: null.hpp:76
date- and time-related classes, typedefs and enumerations
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
framework for calculation on demand and result caching
Definition: any.hpp:35
null values
ext::shared_ptr< YieldTermStructure > r
Base class for pricing engines.