Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
instrumentwrapper.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/instrumentwrapper.hpp
20 \brief Base class for wrapper of QL instrument, used to store "state" of trade under each scenario
21 \ingroup tradedata
22*/
23
24#pragma once
25
26#include <ql/instrument.hpp>
27#include <ql/time/date.hpp>
28
29#include <vector>
30
31#include <boost/timer/timer.hpp>
32
33namespace ore {
34namespace data {
35
36using QuantLib::Real;
37
38//! Instrument Wrapper
39/*!
40 Wrap Instrument base class
41 Derived classes should
42 - store instrument "state" for each scenario
43 - adjust the instrument pricing formula to account for state
44
45 \ingroup tradedata
46*/
48public:
50 InstrumentWrapper(const QuantLib::ext::shared_ptr<QuantLib::Instrument>& inst, const Real multiplier = 1.0,
51 const std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>>& additionalInstruments =
52 std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>>(),
53 const std::vector<Real>& additionalMultipliers = std::vector<Real>());
54
55 virtual ~InstrumentWrapper() {}
56
57 //! Initialise with the given date grid
58 virtual void initialise(const std::vector<QuantLib::Date>& dates) = 0;
59
60 //! reset is called every time a new path is about to be priced.
61 /*! For path dependent Wrappers, this is when internal state should be reset
62 */
63 virtual void reset() = 0;
64
65 //! Return the NPV of this instrument
66 virtual QuantLib::Real NPV() const = 0;
67
68 //! Return the additional results of this instrument
69 virtual const std::map<std::string, boost::any>& additionalResults() const = 0;
70
71 QuantLib::Real additionalInstrumentsNPV() const;
72
73 //! call update on enclosed instrument(s)
74 virtual void updateQlInstruments();
75
76 //! is it an Option?
77 virtual bool isOption();
78
79 //! Inspectors
80 //@{
81 /*! The "QuantLib" instrument
82 Pass true if you trigger a calculation on the returned instrument and want to record
83 the timing for that calculation. If in doubt whether a calculation is triggered, pass false. */
84 QuantLib::ext::shared_ptr<QuantLib::Instrument> qlInstrument(const bool calculate = false) const;
85
86 /*! The multiplier */
87 Real multiplier() const;
88
89 /*! multiplier to be applied on top of multiplier(), e.g. -1 for short options */
90 virtual Real multiplier2() const;
91
92 /*! additional instruments */
93 const std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>>& additionalInstruments() const;
94
95 /*! multipliers for additional instruments */
96 const std::vector<Real>& additionalMultipliers() const;
97 //@}
98
99 //! Get cumulative timing spent on pricing
100 boost::timer::nanosecond_type getCumulativePricingTime() const;
101
102 //! Get number of pricings
103 std::size_t getNumberOfPricings() const;
104
105 //! Reset pricing statistics
106 void resetPricingStats() const;
107
108protected:
109 QuantLib::ext::shared_ptr<QuantLib::Instrument> instrument_;
111 std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>> additionalInstruments_;
112 std::vector<Real> additionalMultipliers_;
113
114 // all NPV calls to be logged in the timings should go through this method
115 Real getTimedNPV(const QuantLib::ext::shared_ptr<QuantLib::Instrument>& instr) const;
116
117 mutable std::size_t numberOfPricings_ = 0;
118 mutable boost::timer::nanosecond_type cumulativePricingTime_ = 0;
119};
120
121//! Vanilla Instrument Wrapper
122/*!
123 Derived from InstrumentWrapper
124 Used for any non path-dependent trades
125
126 \ingroup tradedata
127*/
129public:
130 VanillaInstrument(const QuantLib::ext::shared_ptr<QuantLib::Instrument>& inst, const Real multiplier = 1.0,
131 const std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>>& additionalInstruments =
132 std::vector<QuantLib::ext::shared_ptr<QuantLib::Instrument>>(),
133 const std::vector<Real>& additionalMultipliers = std::vector<Real>());
134
135 void initialise(const std::vector<QuantLib::Date>&) override{};
136 void reset() override {}
137
138 QuantLib::Real NPV() const override;
139 const std::map<std::string, boost::any>& additionalResults() const override;
140};
141
142} // namespace data
143} // namespace ore
const std::vector< Real > & additionalMultipliers() const
virtual Real multiplier2() const
virtual void reset()=0
reset is called every time a new path is about to be priced.
std::size_t getNumberOfPricings() const
Get number of pricings.
virtual QuantLib::Real NPV() const =0
Return the NPV of this instrument.
virtual bool isOption()
is it an Option?
virtual void updateQlInstruments()
call update on enclosed instrument(s)
virtual void initialise(const std::vector< QuantLib::Date > &dates)=0
Initialise with the given date grid.
const std::vector< QuantLib::ext::shared_ptr< QuantLib::Instrument > > & additionalInstruments() const
std::vector< QuantLib::ext::shared_ptr< QuantLib::Instrument > > additionalInstruments_
virtual const std::map< std::string, boost::any > & additionalResults() const =0
Return the additional results of this instrument.
boost::timer::nanosecond_type cumulativePricingTime_
QuantLib::Real additionalInstrumentsNPV() const
boost::timer::nanosecond_type getCumulativePricingTime() const
Get cumulative timing spent on pricing.
Real getTimedNPV(const QuantLib::ext::shared_ptr< QuantLib::Instrument > &instr) const
std::vector< Real > additionalMultipliers_
void resetPricingStats() const
Reset pricing statistics.
QuantLib::ext::shared_ptr< QuantLib::Instrument > instrument_
QuantLib::ext::shared_ptr< QuantLib::Instrument > qlInstrument(const bool calculate=false) const
Inspectors.
Vanilla Instrument Wrapper.
QuantLib::Real NPV() const override
Return the NPV of this instrument.
const std::map< std::string, boost::any > & additionalResults() const override
Return the additional results of this instrument.
void initialise(const std::vector< QuantLib::Date > &) override
Initialise with the given date grid.
void reset() override
reset is called every time a new path is about to be priced.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23