Skip to content

Commit 7689ea7

Browse files
pcaspersfarahkhashman
authored andcommitted
QPR-13654 move built flag to trade level
1 parent 63f1731 commit 7689ea7

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

OREData/ored/portfolio/portfolio.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ void Portfolio::clear() {
4242
}
4343

4444
void Portfolio::reset() {
45-
isBuilt_ = false;
4645
LOG("Reset portfolio of size " << trades_.size());
4746
for (auto [id, t] : trades_)
4847
t->reset();
@@ -175,9 +174,13 @@ void Portfolio::build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFact
175174
.log();
176175
}
177176
QL_REQUIRE(trades_.size() > 0, "Portfolio does not contain any built trades, context is '" + context + "'");
178-
isBuilt_ = true;
179177
}
180178

179+
bool Portfolio::isBuilt() const {
180+
return std::all_of(trades_.begin(), trades_.end(), [](const auto& s) { return s.second->isBuilt(); });
181+
}
182+
183+
181184
Date Portfolio::maturity() const {
182185
QL_REQUIRE(trades_.size() > 0, "Cannot get maturity of an empty portfolio");
183186
Date mat = Date::minDate();
@@ -221,7 +224,6 @@ void Portfolio::add(const QuantLib::ext::shared_ptr<Trade>& trade) {
221224
QL_REQUIRE(!has(trade->id()), "Attempted to add a trade to the portfolio with an id, which already exists.");
222225
underlyingIndicesCache_.clear();
223226
trades_[trade->id()] = trade;
224-
isBuilt_ = false;
225227
}
226228

227229
bool Portfolio::has(const string& id) { return trades_.find(id) != trades_.end(); }
@@ -308,6 +310,7 @@ std::pair<QuantLib::ext::shared_ptr<Trade>, bool> buildTrade(QuantLib::ext::shar
308310
try {
309311
trade->reset();
310312
trade->build(engineFactory);
313+
trade->setBuilt();
311314
TLOG("Required Fixings for trade " << trade->id() << ":");
312315
TLOGGERSTREAM(trade->requiredFixings());
313316
return std::make_pair(nullptr, true);
@@ -326,6 +329,7 @@ std::pair<QuantLib::ext::shared_ptr<Trade>, bool> buildTrade(QuantLib::ext::shar
326329
failed->setEnvelope(trade->envelope());
327330
failed->build(engineFactory);
328331
failed->resetPricingStats(trade->getNumberOfPricings(), trade->getCumulativePricingTime());
332+
failed->setBuilt();
329333
LOG("Built failed trade with id " << failed->id());
330334
return std::make_pair(failed, false);
331335
} else {

OREData/ored/portfolio/portfolio.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Portfolio : public XMLSerializable {
8787
const bool emitStructuredError = true);
8888

8989
//! if the portfolio has been built
90-
bool isBuilt() const { return isBuilt_; }
90+
bool isBuilt() const;
9191

9292
//! Calculates the maturity of the portfolio
9393
QuantLib::Date maturity() const;
@@ -138,7 +138,6 @@ class Portfolio : public XMLSerializable {
138138
bool buildFailedTrades_, ignoreTradeBuildFail_;
139139
std::map<std::string, QuantLib::ext::shared_ptr<Trade>> trades_;
140140
std::map<AssetClass, std::set<std::string>> underlyingIndicesCache_;
141-
bool isBuilt_ = false;
142141
};
143142

144143
std::pair<QuantLib::ext::shared_ptr<Trade>, bool> buildTrade(

OREData/ored/portfolio/trade.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ void Trade::reset() {
300300
savedNumberOfPricings_ += instrument_->getNumberOfPricings();
301301
savedCumulativePricingTime_ += instrument_->getCumulativePricingTime();
302302
}
303+
// reset build status
304+
setBuilt(false);
303305
// reset members
304306
instrument_ = QuantLib::ext::shared_ptr<InstrumentWrapper>();
305307
legs_.clear();

OREData/ored/portfolio/trade.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ class Trade : public XMLSerializable {
246246
const std::string& configuration,
247247
const bool includePastCashflows) const;
248248

249+
/* set build status, this flag is maintained in buildTrade() and Trade::reset(), i.e. _not_ in Trade::build() */
250+
void setBuilt(const bool b = true) const { isBuilt_ = b; }
251+
252+
/* get build status */
253+
bool isBuilt() const { return isBuilt_; }
254+
249255
protected:
250256
string tradeType_; // class name of the derived class
251257
QuantLib::ext::shared_ptr<InstrumentWrapper> instrument_;
@@ -294,6 +300,7 @@ class Trade : public XMLSerializable {
294300
string id_;
295301
Envelope envelope_;
296302
TradeActions tradeActions_;
303+
mutable bool isBuilt_ = false;
297304
};
298305

299306
template <class T>

0 commit comments

Comments
 (0)