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
20#include <ql/instruments/compositeinstrument.hpp>
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(args: instrument, args&: multiplier);
27 registerWith(h: 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
40 void CompositeInstrument::subtract(
41 const ext::shared_ptr<Instrument>& instrument, Real multiplier) {
42 add(instrument, multiplier: -multiplier);
43 }
44
45 bool CompositeInstrument::isExpired() const {
46 for (const auto& component : components_) {
47 if (!component.first->isExpired())
48 return false;
49 }
50 return true;
51 }
52
53 void CompositeInstrument::performCalculations() const {
54 NPV_ = 0.0;
55 for (const auto& component : components_) {
56 NPV_ += component.second * component.first->NPV();
57 }
58 }
59
60 void CompositeInstrument::deepUpdate() {
61 for (const_iterator i=components_.begin(); i!=components_.end(); ++i) {
62 i->first->deepUpdate();
63 }
64 update();
65 }
66
67}
68
69

source code of quantlib/ql/instruments/compositeinstrument.cpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.