Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
multiccycompositeinstrument.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
20
21namespace QuantExt {
22using namespace QuantLib;
23
24void MultiCcyCompositeInstrument::add(const ext::shared_ptr<Instrument>& instrument, Real multiplier,
25 const Handle<Quote>& fx) {
26 components_.push_back(std::make_tuple(instrument, multiplier, fx));
27 registerWith(instrument);
28 registerWith(fx);
29 update();
30 // When we ask for the NPV of an expired composite, the
31 // components are not recalculated and thus wouldn't forward
32 // later notifications according to the default behavior of
33 // LazyObject instances. This means that even if the
34 // evaluation date changes so that the composite is no longer
35 // expired, the instrument wouldn't be notified and thus it
36 // wouldn't recalculate. To avoid this, we override the
37 // default behavior of the components.
38 instrument->alwaysForwardNotifications();
39}
40
41void MultiCcyCompositeInstrument::subtract(const ext::shared_ptr<Instrument>& instrument, Real multiplier,
42 const Handle<Quote>& fx) {
43 add(instrument, -multiplier, fx);
44}
45
47 for (const_iterator i = components_.cbegin(); i != components_.end(); ++i) {
48 if (!std::get<0>(*i)->isExpired())
49 return false;
50 }
51 return true;
52}
53
55 NPV_ = 0.0;
56 additionalResults_.clear();
57 Size counter = 0;
58 for (const_iterator i = components_.cbegin(); i != components_.end(); ++i, ++counter) {
59 // npv += npv * fx * multiplier
60 NPV_ += std::get<1>(*i) * std::get<2>(*i)->value() * std::get<0>(*i)->NPV();
61 for (auto const& k : std::get<0>(*i)->additionalResults()) {
62 additionalResults_[k.first + "_" + std::to_string(counter)] = k.second;
63 }
64 additionalResults_["__multiplier_" + std::to_string(counter)] = std::get<1>(*i);
65 additionalResults_["__fx_conversion_" + std::to_string(counter)] = std::get<2>(*i)->value();
66 }
67}
68
70 for (const_iterator i = components_.cbegin(); i != components_.end(); ++i) {
71 std::get<0>(*i)->deepUpdate();
72 }
73 update();
74}
75
76} // namespace QuantExt
std::list< component >::const_iterator const_iterator
void add(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0, const Handle< Quote > &fx=Handle< Quote >(QuantLib::ext::make_shared< SimpleQuote >(1.0)))
adds an instrument to the composite
void subtract(const ext::shared_ptr< Instrument > &instrument, Real multiplier=1.0, const Handle< Quote > &fx=Handle< Quote >(QuantLib::ext::make_shared< SimpleQuote >(1.0)))
shorts an instrument from the composite