Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
Portfolio Class Reference

Serializable portfolio. More...

#include <ored/portfolio/portfolio.hpp>

+ Inheritance diagram for Portfolio:
+ Collaboration diagram for Portfolio:

Public Member Functions

 Portfolio (bool buildFailedTrades=true, bool ignoreTradeBuildFail=false)
 Default constructor. More...
 
void add (const QuantLib::ext::shared_ptr< Trade > &trade)
 Add a trade to the portfolio. More...
 
bool has (const string &id)
 Check if a trade id is already in the portfolio. More...
 
QuantLib::ext::shared_ptr< Tradeget (const std::string &id) const
 
void clear ()
 Clear the portfolio. More...
 
void reset ()
 Reset all trade data. More...
 
QuantLib::Size size () const
 Portfolio size. More...
 
bool empty () const
 
void fromXML (XMLNode *node) override
 XMLSerializable interface. More...
 
XMLNodetoXML (XMLDocument &doc) const override
 
bool remove (const std::string &tradeID)
 Remove specified trade from the portfolio. More...
 
void removeMatured (const QuantLib::Date &asof)
 Remove matured trades from portfolio for a given date, each removal is logged with an Alert. More...
 
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. More...
 
QuantLib::Date maturity () const
 Calculates the maturity of the portfolio. More...
 
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades () const
 Return the map tradeId -> trade. More...
 
std::set< std::string > ids () const
 Build a set of tradeIds. More...
 
std::map< std::string, std::string > nettingSetMap () const
 Build a map from trade Ids to NettingSet. More...
 
std::set< std::string > counterparties () const
 Build a set of all counterparties in the portfolio. More...
 
std::map< std::string, std::set< std::string > > counterpartyNettingSets () const
 Build a map from counterparty to NettingSet. More...
 
std::set< std::string > portfolioIds () const
 Compute set of portfolios. More...
 
bool hasNettingSetDetails () const
 Check if at least one trade in the portfolio uses the NettingSetDetails node, and not just NettingSetId. More...
 
bool buildFailedTrades () const
 Does this portfolio build failed trades? More...
 
bool ignoreTradeBuildFail () const
 Keep trade in the portfolio even after build fail. More...
 
std::map< std::string, RequiredFixings::FixingDatesfixings (const QuantLib::Date &settlementDate=QuantLib::Date()) const
 
std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr)
 
std::set< std::string > underlyingIndices (AssetClass assetClass, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr)
 
- Public Member Functions inherited from XMLSerializable
virtual ~XMLSerializable ()
 
virtual void fromXML (XMLNode *node)=0
 
virtual XMLNodetoXML (XMLDocument &doc) const =0
 
void fromFile (const std::string &filename)
 
void toFile (const std::string &filename) const
 
void fromXMLString (const std::string &xml)
 Parse from XML string. More...
 
std::string toXMLString () const
 Parse from XML string. More...
 

Private Attributes

bool buildFailedTrades_
 
bool ignoreTradeBuildFail_
 
std::map< std::string, QuantLib::ext::shared_ptr< Trade > > trades_
 
std::map< AssetClass, std::set< std::string > > underlyingIndicesCache_
 

Detailed Description

Serializable portfolio.

Definition at line 43 of file portfolio.hpp.

Constructor & Destructor Documentation

◆ Portfolio()

Portfolio ( bool  buildFailedTrades = true,
bool  ignoreTradeBuildFail = false 
)
explicit

Default constructor.

Definition at line 46 of file portfolio.hpp.

bool ignoreTradeBuildFail() const
Keep trade in the portfolio even after build fail.
Definition: portfolio.hpp:114
bool buildFailedTrades() const
Does this portfolio build failed trades?
Definition: portfolio.hpp:111

Member Function Documentation

◆ add()

void add ( const QuantLib::ext::shared_ptr< Trade > &  trade)

Add a trade to the portfolio.

Definition at line 186 of file portfolio.cpp.

186 {
187 QL_REQUIRE(!has(trade->id()), "Attempted to add a trade to the portfolio with an id, which already exists.");
189 trades_[trade->id()] = trade;
190}
std::map< std::string, QuantLib::ext::shared_ptr< Trade > > trades_
Definition: portfolio.hpp:133
std::map< AssetClass, std::set< std::string > > underlyingIndicesCache_
Definition: portfolio.hpp:134
bool has(const string &id)
Check if a trade id is already in the portfolio.
Definition: portfolio.cpp:192
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has()

bool has ( const string &  id)

Check if a trade id is already in the portfolio.

Definition at line 192 of file portfolio.cpp.

192{ return trades_.find(id) != trades_.end(); }
+ Here is the caller graph for this function:

◆ get()

QuantLib::ext::shared_ptr< Trade > get ( const std::string &  id) const

Get a Trade with the given id from the portfolio

Remarks
returns a nullptr if no trade found with the given id

Definition at line 194 of file portfolio.cpp.

194 {
195 auto it = trades_.find(id);
196 if (it != trades_.end())
197 return it->second;
198 else
199 return nullptr;
200}
+ Here is the caller graph for this function:

◆ clear()

void clear ( )

Clear the portfolio.

Definition at line 39 of file portfolio.cpp.

39 {
40 trades_.clear();
42}
+ Here is the caller graph for this function:

◆ reset()

void reset ( )

Reset all trade data.

Definition at line 44 of file portfolio.cpp.

44 {
45 LOG("Reset portfolio of size " << trades_.size());
46 for (auto [id, t] : trades_)
47 t->reset();
48}
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552

◆ size()

QuantLib::Size size ( ) const

Portfolio size.

Definition at line 68 of file portfolio.hpp.

68{ return trades_.size(); }
+ Here is the caller graph for this function:

◆ empty()

bool empty ( ) const

Definition at line 70 of file portfolio.hpp.

70{ return trades_.empty(); }

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

XMLSerializable interface.

Implements XMLSerializable.

Definition at line 50 of file portfolio.cpp.

50 {
51 XMLUtils::checkNode(node, "Portfolio");
52 vector<XMLNode*> nodes = XMLUtils::getChildrenNodes(node, "Trade");
53 for (Size i = 0; i < nodes.size(); i++) {
54 string tradeType = XMLUtils::getChildValue(nodes[i], "TradeType", true);
55
56 // Get the id attribute
57 string id = XMLUtils::getAttribute(nodes[i], "id");
58 QL_REQUIRE(id != "", "No id attribute in Trade Node");
59 DLOG("Parsing trade id:" << id);
60
61 QuantLib::ext::shared_ptr<Trade> trade;
62 bool failedToLoad = true;
63 try {
64 trade = TradeFactory::instance().build(tradeType);
65 trade->fromXML(nodes[i]);
66 trade->id() = id;
67 add(trade);
68 DLOG("Added Trade " << id << " (" << trade->id() << ")"
69 << " type:" << tradeType);
70 failedToLoad = false;
71 } catch (std::exception& ex) {
72 StructuredTradeErrorMessage(id, tradeType, "Error parsing Trade XML", ex.what()).log();
73 }
74
75 // If trade loading failed, then insert a dummy trade with same id and envelope
76 if (failedToLoad && buildFailedTrades_) {
77 try {
78 trade = TradeFactory::instance().build("Failed");
79 // this loads only type, id and envelope, but type will be set to the original trade's type
80 trade->fromXML(nodes[i]);
81 // create a dummy trade of type "Dummy"
82 QuantLib::ext::shared_ptr<FailedTrade> failedTrade = QuantLib::ext::make_shared<FailedTrade>();
83 // copy id and envelope
84 failedTrade->id() = id;
85 failedTrade->setUnderlyingTradeType(tradeType);
86 failedTrade->setEnvelope(trade->envelope());
87 // and add it to the portfolio
88 add(failedTrade);
89 WLOG("Added trade id " << failedTrade->id() << " type " << failedTrade->tradeType()
90 << " for original trade type " << trade->tradeType());
91 } catch (std::exception& ex) {
92 StructuredTradeErrorMessage(id, tradeType, "Error parsing type and envelope", ex.what()).log();
93 }
94 }
95 }
96 LOG("Finished Parsing XML doc");
97}
void add(const QuantLib::ext::shared_ptr< Trade > &trade)
Add a trade to the portfolio.
Definition: portfolio.cpp:186
static string getAttribute(XMLNode *node, const string &attrName)
Definition: xmlutils.cpp:419
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
+ Here is the call graph for this function:

◆ toXML()

XMLNode * toXML ( XMLDocument doc) const
overridevirtual

Implements XMLSerializable.

Definition at line 99 of file portfolio.cpp.

99 {
100 XMLNode* node = doc.allocNode("Portfolio");
101 for (auto& t : trades_)
102 XMLUtils::appendNode(node, t.second->toXML(doc));
103 return node;
104}
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
+ Here is the call graph for this function:

◆ remove()

bool remove ( const std::string &  tradeID)

Remove specified trade from the portfolio.

Definition at line 106 of file portfolio.cpp.

106 {
108 return trades_.erase(tradeID) > 0;
109}

◆ removeMatured()

void removeMatured ( const QuantLib::Date &  asof)

Remove matured trades from portfolio for a given date, each removal is logged with an Alert.

Definition at line 111 of file portfolio.cpp.

111 {
112 for (auto it = trades_.begin(); it != trades_.end(); /* manual */) {
113 if ((*it).second->isExpired(asof)) {
114 StructuredTradeWarningMessage((*it).second, "", "Trade is Matured").log();
115 it=trades_.erase(it);
116 } else {
117 ++it;
118 }
119 }
120}
+ Here is the call graph for this function:

◆ build()

void build ( const QuantLib::ext::shared_ptr< EngineFactory > &  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 at line 122 of file portfolio.cpp.

123 {
124 LOG("Building Portfolio of size " << trades_.size() << " for context = '" << context << "'");
125 auto trade = trades_.begin();
126 Size initialSize = trades_.size();
127 Size failedTrades = 0;
128 while (trade != trades_.end()) {
129 auto [ft, success] = buildTrade((*trade).second, engineFactory, context, ignoreTradeBuildFail(),
130 buildFailedTrades(), emitStructuredError);
131 if (success) {
132 ++trade;
133 } else if (ft) {
134 (*trade).second = ft;
135 ++failedTrades;
136 ++trade;
137 } else {
138 trade = trades_.erase(trade);
139 }
140 }
141 LOG("Built Portfolio. Initial size = " << initialSize << ", size now " << trades_.size() << ", built "
142 << failedTrades << " failed trades, context is " + context);
143
144 QL_REQUIRE(trades_.size() > 0, "Portfolio does not contain any built trades, context is '" + context + "'");
145}
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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ maturity()

Date maturity ( ) const

Calculates the maturity of the portfolio.

Definition at line 147 of file portfolio.cpp.

147 {
148 QL_REQUIRE(trades_.size() > 0, "Cannot get maturity of an empty portfolio");
149 Date mat = Date::minDate();
150 for (const auto& t : trades_)
151 mat = std::max(mat, t.second->maturity());
152 return mat;
153}

◆ trades()

const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades ( ) const

Return the map tradeId -> trade.

Definition at line 162 of file portfolio.cpp.

162{ return trades_; }
+ Here is the caller graph for this function:

◆ ids()

set< string > ids ( ) const

Build a set of tradeIds.

Definition at line 155 of file portfolio.cpp.

155 {
156 set<string> ids;
157 for (const auto& [tradeId, _] : trades_)
158 ids.insert(tradeId);
159 return ids;
160}
std::set< std::string > ids() const
Build a set of tradeIds.
Definition: portfolio.cpp:155
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nettingSetMap()

map< string, string > nettingSetMap ( ) const

Build a map from trade Ids to NettingSet.

Definition at line 164 of file portfolio.cpp.

164 {
165 map<string, string> nettingSetMap;
166 for (const auto& t : trades_)
167 nettingSetMap[t.second->id()] = t.second->envelope().nettingSetId();
168 return nettingSetMap;
169}
std::map< std::string, std::string > nettingSetMap() const
Build a map from trade Ids to NettingSet.
Definition: portfolio.cpp:164
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ counterparties()

std::set< std::string > counterparties ( ) const

Build a set of all counterparties in the portfolio.

Definition at line 171 of file portfolio.cpp.

171 {
172 set<string> counterparties;
173 for (const auto& t : trades_)
174 counterparties.insert(t.second->envelope().counterparty());
175 return counterparties;
176}
std::set< std::string > counterparties() const
Build a set of all counterparties in the portfolio.
Definition: portfolio.cpp:171
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ counterpartyNettingSets()

map< string, set< string > > counterpartyNettingSets ( ) const

Build a map from counterparty to NettingSet.

Definition at line 178 of file portfolio.cpp.

178 {
179 map<string, set<string>> cpNettingSets;
180 for (const auto& [tradeId, trade] : trades()) {
181 cpNettingSets[trade->envelope().counterparty()].insert(trade->envelope().nettingSetId());
182 }
183 return cpNettingSets;
184}
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Definition: portfolio.cpp:162
+ Here is the call graph for this function:

◆ portfolioIds()

std::set< std::string > portfolioIds ( ) const

Compute set of portfolios.

Definition at line 202 of file portfolio.cpp.

202 {
203 std::set<std::string> portfolioIds;
204 for (const auto& [tradeId, trade] : trades()) {
205 portfolioIds.insert(trade->portfolioIds().begin(), trade->portfolioIds().end());
206 }
207 return portfolioIds;
208}
std::set< std::string > portfolioIds() const
Compute set of portfolios.
Definition: portfolio.cpp:202
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasNettingSetDetails()

bool hasNettingSetDetails ( ) const

Check if at least one trade in the portfolio uses the NettingSetDetails node, and not just NettingSetId.

Definition at line 210 of file portfolio.cpp.

210 {
211 bool hasNettingSetDetails = false;
212 for (const auto& t : trades_)
213 if (!t.second->envelope().nettingSetDetails().emptyOptionalFields()) {
215 break;
216 }
218}
bool hasNettingSetDetails() const
Check if at least one trade in the portfolio uses the NettingSetDetails node, and not just NettingSet...
Definition: portfolio.cpp:210
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buildFailedTrades()

bool buildFailedTrades ( ) const

Does this portfolio build failed trades?

Definition at line 111 of file portfolio.hpp.

111{ return buildFailedTrades_; }
+ Here is the caller graph for this function:

◆ ignoreTradeBuildFail()

bool ignoreTradeBuildFail ( ) const

Keep trade in the portfolio even after build fail.

Definition at line 114 of file portfolio.hpp.

114{ return ignoreTradeBuildFail_; }
+ Here is the caller graph for this function:

◆ fixings()

map< string, RequiredFixings::FixingDates > fixings ( const QuantLib::Date &  settlementDate = QuantLib::Date()) const

Return the fixings that will be requested in order to price every Trade in this Portfolio given the settlementDate. The map key is the ORE name of the index and the map value is the set of fixing dates.

Warning:
This method will return an empty map if the Portfolio has not been built.

Definition at line 220 of file portfolio.cpp.

220 {
221 map<string, RequiredFixings::FixingDates> result;
222 for (const auto& t : trades_) {
223 auto fixings = t.second->fixings(settlementDate);
224 for (const auto& [index, fixingDates] : fixings) {
225 if (!fixingDates.empty()) {
226 result[index].addDates(fixingDates);
227 }
228 }
229 }
230 return result;
231}
std::map< std::string, RequiredFixings::FixingDates > fixings(const QuantLib::Date &settlementDate=QuantLib::Date()) const
Definition: portfolio.cpp:220
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ underlyingIndices() [1/2]

std::map< AssetClass, std::set< std::string > > underlyingIndices ( const QuantLib::ext::shared_ptr< ReferenceDataManager > &  referenceDataManager = nullptr)

Returns the names of the underlying instruments for each asset class

Definition at line 234 of file portfolio.cpp.

234 {
235
236 if (!underlyingIndicesCache_.empty())
238
239 map<AssetClass, std::set<std::string>> result;
240
241 for (const auto& t : trades_) {
242 try {
243 auto underlyings = t.second->underlyingIndices(referenceDataManager);
244 for (const auto& kv : underlyings) {
245 result[kv.first].insert(kv.second.begin(), kv.second.end());
246 }
247 } catch (const std::exception& e) {
248 StructuredTradeErrorMessage(t.second->id(), t.second->tradeType(), "Error retrieving underlying indices",
249 e.what())
250 .log();
251 }
252 }
255}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ underlyingIndices() [2/2]

std::set< std::string > underlyingIndices ( AssetClass  assetClass,
const QuantLib::ext::shared_ptr< ReferenceDataManager > &  referenceDataManager = nullptr 
)

Definition at line 258 of file portfolio.cpp.

259 {
260
261 std::map<AssetClass, std::set<std::string>> indices = underlyingIndices(referenceDataManager);
262 auto it = indices.find(assetClass);
263 if (it != indices.end()) {
264 return it->second;
265 }
266 return std::set<std::string>();
267}
std::map< AssetClass, std::set< std::string > > underlyingIndices(const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr)
Definition: portfolio.cpp:234
+ Here is the call graph for this function:

Member Data Documentation

◆ buildFailedTrades_

bool buildFailedTrades_
private

Definition at line 132 of file portfolio.hpp.

◆ ignoreTradeBuildFail_

bool ignoreTradeBuildFail_
private

Definition at line 132 of file portfolio.hpp.

◆ trades_

std::map<std::string, QuantLib::ext::shared_ptr<Trade> > trades_
private

Definition at line 133 of file portfolio.hpp.

◆ underlyingIndicesCache_

std::map<AssetClass, std::set<std::string> > underlyingIndicesCache_
private

Definition at line 134 of file portfolio.hpp.