Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
portfolio.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 portfolio/portfolio.hpp
20 \brief Portfolio class
21 \ingroup portfolio
22*/
23
24#pragma once
25
26#include <boost/make_shared.hpp>
27#include <ql/shared_ptr.hpp>
30#include <ql/time/date.hpp>
31#include <ql/types.hpp>
32#include <vector>
33
34namespace ore {
35namespace data {
36
37class ReferenceDataManager;
38
39//! Serializable portfolio
40/*!
41 \ingroup portfolio
42*/
43class Portfolio : public XMLSerializable {
44public:
45 //! Default constructor
46 explicit Portfolio(bool buildFailedTrades = true, bool ignoreTradeBuildFail = false)
48
49 //! Add a trade to the portfolio
50 void add(const QuantLib::ext::shared_ptr<Trade>& trade);
51
52 //! Check if a trade id is already in the portfolio
53 bool has(const string& id);
54
55 /*! Get a Trade with the given \p id from the portfolio
56
57 \remark returns a `nullptr` if no trade found with the given \p id
58 */
59 QuantLib::ext::shared_ptr<Trade> get(const std::string& id) const;
60
61 //! Clear the portfolio
62 void clear();
63
64 //! Reset all trade data
65 void reset();
66
67 //! Portfolio size
68 QuantLib::Size size() const { return trades_.size(); }
69
70 bool empty() const { return trades_.empty(); }
71
72 //! XMLSerializable interface
73 void fromXML(XMLNode* node) override;
74 XMLNode* toXML(XMLDocument& doc) const override;
75
76 //! Remove specified trade from the portfolio
77 bool remove(const std::string& tradeID);
78
79 //! Remove matured trades from portfolio for a given date, each removal is logged with an Alert
80 void removeMatured(const QuantLib::Date& asof);
81
82 //! Call build on all trades in the portfolio, the context is included in error messages
83 void build(const QuantLib::ext::shared_ptr<EngineFactory>&, const std::string& context = "unspecified",
84 const bool emitStructuredError = true);
85
86 //! Calculates the maturity of the portfolio
87 QuantLib::Date maturity() const;
88
89 //! Return the map tradeId -> trade
90 const std::map<std::string, QuantLib::ext::shared_ptr<Trade>>& trades() const;
91
92 //! Build a set of tradeIds
93 std::set<std::string> ids() const;
94
95 //! Build a map from trade Ids to NettingSet
96 std::map<std::string, std::string> nettingSetMap() const;
97
98 //! Build a set of all counterparties in the portfolio
99 std::set<std::string> counterparties() const;
100
101 //! Build a map from counterparty to NettingSet
102 std::map<std::string, std::set<std::string>> counterpartyNettingSets() const;
103
104 //! Compute set of portfolios
105 std::set<std::string> portfolioIds() const;
106
107 //! Check if at least one trade in the portfolio uses the NettingSetDetails node, and not just NettingSetId
108 bool hasNettingSetDetails() const;
109
110 //! Does this portfolio build failed trades?
111 bool buildFailedTrades() const { return buildFailedTrades_; }
112
113 //! Keep trade in the portfolio even after build fail
115
116 /*! Return the fixings that will be requested in order to price every Trade in this Portfolio given
117 the \p settlementDate. The map key is the ORE name of the index and the map value is the set of fixing dates.
118
119 \warning This method will return an empty map if the Portfolio has not been built.
120 */
121 std::map<std::string, RequiredFixings::FixingDates>
122 fixings(const QuantLib::Date& settlementDate = QuantLib::Date()) const;
123
124 /*! Returns the names of the underlying instruments for each asset class */
125 std::map<AssetClass, std::set<std::string>>
126 underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager = nullptr);
127 std::set<std::string>
128 underlyingIndices(AssetClass assetClass,
129 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager = nullptr);
130
131private:
133 std::map<std::string, QuantLib::ext::shared_ptr<Trade>> trades_;
134 std::map<AssetClass, std::set<std::string>> underlyingIndicesCache_;
135};
136
137std::pair<QuantLib::ext::shared_ptr<Trade>, bool> buildTrade(
138 QuantLib::ext::shared_ptr<Trade>& trade,
139 const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory,
140 const std::string& context, const bool ignoreTradeBuildFail,
141 const bool buildFailedTrades, const bool emitStructuredError);
142
143} // namespace data
144} // namespace ore
Serializable portfolio.
Definition: portfolio.hpp:43
QuantLib::Date maturity() const
Calculates the maturity of the portfolio.
Definition: portfolio.cpp:147
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Definition: portfolio.cpp:162
std::set< std::string > counterparties() const
Build a set of all counterparties in the portfolio.
Definition: portfolio.cpp:171
void add(const QuantLib::ext::shared_ptr< Trade > &trade)
Add a trade to the portfolio.
Definition: portfolio.cpp:186
std::map< std::string, RequiredFixings::FixingDates > fixings(const QuantLib::Date &settlementDate=QuantLib::Date()) const
Definition: portfolio.cpp:220
QuantLib::Size size() const
Portfolio size.
Definition: portfolio.hpp:68
void removeMatured(const QuantLib::Date &asof)
Remove matured trades from portfolio for a given date, each removal is logged with an Alert.
Definition: portfolio.cpp:111
std::map< std::string, QuantLib::ext::shared_ptr< Trade > > trades_
Definition: portfolio.hpp:133
bool empty() const
Definition: portfolio.hpp:70
std::map< std::string, std::string > nettingSetMap() const
Build a map from trade Ids to NettingSet.
Definition: portfolio.cpp:164
bool ignoreTradeBuildFail() const
Keep trade in the portfolio even after build fail.
Definition: portfolio.hpp:114
void fromXML(XMLNode *node) override
XMLSerializable interface.
Definition: portfolio.cpp:50
void build(const QuantLib::ext::shared_ptr< EngineFactory > &, const std::string &context="unspecified", const bool emitStructuredError=true)
Call build on all trades in the portfolio, the context is included in error messages.
Definition: portfolio.cpp:122
std::set< std::string > portfolioIds() const
Compute set of portfolios.
Definition: portfolio.cpp:202
XMLNode * toXML(XMLDocument &doc) const override
Definition: portfolio.cpp:99
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr)
Definition: portfolio.cpp:234
bool hasNettingSetDetails() const
Check if at least one trade in the portfolio uses the NettingSetDetails node, and not just NettingSet...
Definition: portfolio.cpp:210
bool remove(const std::string &tradeID)
Remove specified trade from the portfolio.
Definition: portfolio.cpp:106
bool buildFailedTrades() const
Does this portfolio build failed trades?
Definition: portfolio.hpp:111
std::map< AssetClass, std::set< std::string > > underlyingIndicesCache_
Definition: portfolio.hpp:134
Portfolio(bool buildFailedTrades=true, bool ignoreTradeBuildFail=false)
Default constructor.
Definition: portfolio.hpp:46
void clear()
Clear the portfolio.
Definition: portfolio.cpp:39
void reset()
Reset all trade data.
Definition: portfolio.cpp:44
std::map< std::string, std::set< std::string > > counterpartyNettingSets() const
Build a map from counterparty to NettingSet.
Definition: portfolio.cpp:178
bool has(const string &id)
Check if a trade id is already in the portfolio.
Definition: portfolio.cpp:192
QuantLib::ext::shared_ptr< Trade > get(const std::string &id) const
Definition: portfolio.cpp:194
std::set< std::string > ids() const
Build a set of tradeIds.
Definition: portfolio.cpp:155
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
Base class for all serializable classes.
Definition: xmlutils.hpp:101
Pricing Engine Factory.
@ data
Definition: log.hpp:77
std::pair< QuantLib::ext::shared_ptr< Trade >, bool > buildTrade(QuantLib::ext::shared_ptr< Trade > &trade, const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, const std::string &context, const bool ignoreTradeBuildFail, const bool buildFailedTrades, const bool emitStructuredError)
Definition: portfolio.cpp:269
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Trade Factory.