@@ -42,7 +42,6 @@ void Portfolio::clear() {
4242}
4343
4444void 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+
181184Date 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
227229bool 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 {
0 commit comments