QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
compositeinstrument.cpp
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) 2006 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
21
22namespace QuantLib {
23
24 void CompositeInstrument::add(const ext::shared_ptr<Instrument>& instrument, Real multiplier) {
25 QL_REQUIRE(instrument, "null instrument provided");
26 components_.emplace_back(instrument, multiplier);
27 registerWith(instrument);
28 update();
29 // When we ask for the NPV of an expired composite, the
30 // components are not recalculated and thus wouldn't forward
31 // later notifications according to the default behavior of
32 // LazyObject instances. This means that even if the
33 // evaluation date changes so that the composite is no longer
34 // expired, the instrument wouldn't be notified and thus it
35 // wouldn't recalculate. To avoid this, we override the
36 // default behavior of the components.
37 instrument->alwaysForwardNotifications();
38 }
39
41 const ext::shared_ptr<Instrument>& instrument, Real multiplier) {
42 add(instrument, -multiplier);
43 }
44
46 for (const auto& component : components_) {
47 if (!component.first->isExpired())
48 return false;
49 }
50 return true;
51 }
52
54 NPV_ = 0.0;
55 for (const auto& component : components_) {
56 NPV_ += component.second * component.first->NPV();
57 }
58 }
59
61 for (const_iterator i=components_.begin(); i!=components_.end(); ++i) {
62 i->first->deepUpdate();
63 }
64 update();
65 }
66
67}
68
std::pair< ext::shared_ptr< Instrument >, Real > component
void performCalculations() const override
void add(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0)
adds an instrument to the composite
bool isExpired() const override
returns whether the instrument might have value greater than zero.
std::list< component >::const_iterator const_iterator
void subtract(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0)
shorts an instrument from the composite
std::list< component > components_
void update() override
Definition: lazyobject.hpp:188
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Composite instrument class.
#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
Definition: any.hpp:35