QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
instrument.hpp
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
25#ifndef quantlib_instrument_hpp
26#define quantlib_instrument_hpp
27
28#include <ql/patterns/lazyobject.hpp>
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
39
44 class Instrument : public LazyObject {
45 public:
46 class results;
47 Instrument();
49
50
52 Real NPV() const;
54 Real errorEstimate() const;
56 const Date& valuationDate() const;
57
59 template <typename T> T result(const std::string& tag) const;
61 const std::map<std::string, ext::any>& additionalResults() const;
62
64 virtual bool isExpired() const = 0;
66
68
73 void setPricingEngine(const ext::shared_ptr<PricingEngine>&);
75
79 virtual void setupArguments(PricingEngine::arguments*) const;
84 virtual void fetchResults(const PricingEngine::results*) const;
85 protected:
87
88 void calculate() const override;
92 virtual void setupExpired() const;
99 void performCalculations() const override;
101
108 mutable std::map<std::string, ext::any> additionalResults_;
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();
175 QL_REQUIRE(errorEstimate_ != Null<Real>(),
176 "error estimate not provided");
177 return errorEstimate_;
178 }
179
180 inline const Date& Instrument::valuationDate() const {
181 calculate();
182 QL_REQUIRE(valuationDate_ != Date(),
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
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
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35