@@ -407,11 +407,29 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
407407
408408 LOG (" Run simulation..." );
409409
410+ for (size_t i = 0 ; i < sgd->getGrid ()->dates ().size (); i++){
411+ std::cout << i << " Date " << sgd->getGrid ()->dates ()[i] << std::endl;
412+ }
413+
414+ for (size_t i = 0 ; i < sgd->getGrid ()->times ().size (); i++) {
415+ std::cout << i << " time " << sgd->getGrid ()->times ()[i] << std::endl;
416+ }
417+
418+ for (size_t i = 0 ; i < sgd->getGrid ()->isCloseOutDate ().size (); i++) {
419+ std::cout << i << " isCloseOutDate " << sgd->getGrid ()->isCloseOutDate ()[i] << std::endl;
420+ }
421+
422+ for (size_t i = 0 ; i < sgd->getGrid ()->isValuationDate ().size (); i++) {
423+ std::cout << i << " isValuationDate " << sgd->getGrid ()->isValuationDate ()[i] << std::endl;
424+ }
425+
410426 // set up vectors indicating valuation times, close-out times and all times
411427
412428 std::vector<bool > allTimes (pathTimes.size (), true );
413429 std::vector<bool > valuationTimes (pathTimes.size ()), closeOutTimes (pathTimes.size ());
414430 for (Size i = 0 ; i < pathTimes.size (); ++i) {
431+ std::cout << pathTimes[i] << " " << sgd->getGrid ()->isValuationDate ()[i] << " "
432+ << sgd->getGrid ()->isCloseOutDate ()[i] << std::endl;
415433 valuationTimes[i] = sgd->getGrid ()->isValuationDate ()[i];
416434 closeOutTimes[i] = sgd->getGrid ()->isCloseOutDate ()[i];
417435 }
@@ -449,12 +467,16 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
449467 } else {
450468 // with close-out lag, fill depth 0 with valuation date npvs, depth 1 with (inflated) close-out npvs
451469 if (sgd->withMporStickyDate ()) {
470+ std::cout << " Simulate with sticky date the valuationTimes" << std::endl;
452471 // sticky date mpor mode. simulate the valuation times...
453472 auto res = simulatePathInterface2 (amcCalculators[j], pathTimes, paths, valuationTimes, false ,
454473 tradeLabel[j], tradeType[j]);
455474 // ... and then the close-out times, but times moved to the valuation times
475+ std::cout << " Simulate with sticky date the closeOutTimes" << std::endl;
476+
456477 auto resLag = simulatePathInterface2 (amcCalculators[j], pathTimes, paths, closeOutTimes, true ,
457478 tradeLabel[j], tradeType[j]);
479+ std::cout << " Finished closeOutTime simulation" << std::endl;
458480 Real v = outputCube->getT0 (tradeId[j], 0 );
459481 outputCube->setT0 (v +
460482 res[0 ].at (0 ) * fx (fxBuffer, currencyIndex[j], 0 , 0 ) *
@@ -463,33 +485,39 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
463485 resFee[0 ][0 ],
464486 tradeId[j], 0 );
465487 int dateIndex = -1 ;
466- std::map<QuantLib::Date, std::pair< double , size_t >> dateIndexCache ;
488+ std::map<QuantLib::Date, std::queue<std::tuple<QuantLib::Date, double , size_t >>> closeOutDateToValuationDate ;
467489 for (Size k = 0 ; k < sgd->getGrid ()->dates ().size (); ++k) {
490+
468491 Real t = sgd->getGrid ()->timeGrid ()[k + 1 ];
469-
492+ std::cout << k << " " << t << std::endl;
470493 if (sgd->getGrid ()->isCloseOutDate ()[k]) {
471494 Date closeOutDate = sgd->getGrid ()->dates ()[k];
472- Date valuationDate = sgd->getGrid ()->valuationDateFromCloseOutDate (closeOutDate);
473- auto dateIndexIt = dateIndexCache.find (valuationDate);
474- QL_REQUIRE (dateIndexIt != dateIndexCache.end (),
475- " The valuation date (" << ore::data::to_string (valuationDate)
476- << " ) needs to before the corresponding close out date ("
477- << ore::data::to_string (closeOutDate) << " )" );
478- auto [timeValueDate, timeIndexValueDate] = dateIndexIt->second ;
495+ auto dateIndexIt = closeOutDateToValuationDate.find (closeOutDate);
496+ QL_REQUIRE (dateIndexIt != closeOutDateToValuationDate.end () && !dateIndexIt->second .empty (),
497+ " The valuation date needs to before the corresponding close out date" );
498+ Date valuationDate;
499+ double valuationTime;
500+ size_t valuationIndex;
501+ std::tie (valuationDate, valuationTime, valuationIndex) = dateIndexIt->second .front ();
502+ dateIndexIt->second .pop ();
503+ std::cout << " Is closeOut date: " << to_string (closeOutDate) << " for valuation Date " << to_string (valuationDate) << std::endl;
479504 for (Size i = 0 ; i < outputCube->samples (); ++i) {
480- Real v = outputCube->get (tradeId[j], dateIndex , i, 1 );
505+ Real v = outputCube->get (tradeId[j], valuationIndex , i, 1 );
481506 outputCube->set (
482507 v +
483508 resLag[dateIndex + 1 ][i] * fx (fxBuffer, currencyIndex[j], k + 1 , i) *
484- num (model, irStateBuffer, currencyIndex[j], k + 1 , timeValueDate , i) *
509+ num (model, irStateBuffer, currencyIndex[j], k + 1 , valuationTime , i) *
485510 effectiveMultiplier[j] +
486511 resFee[dateIndex + 1 ][i],
487- tradeId[j], timeIndexValueDate , i, 1 );
512+ tradeId[j], valuationIndex , i, 1 );
488513 }
489514 }
490515 if (sgd->getGrid ()->isValuationDate ()[k]) {
491516 Date valuationDate = sgd->getGrid ()->dates ()[k];
492- dateIndexCache[valuationDate] = std::make_pair (t, ++dateIndex);
517+ Date closeOutDate = sgd->getGrid ()->closeOutDateFromValuationDate (valuationDate);
518+ std::cout << " Is valuationDate date: " << to_string (valuationDate) << " with Index " << to_string (dateIndex + 1 )
519+ << " and closeOutDate " << to_string (closeOutDate) << std::endl;
520+ closeOutDateToValuationDate[closeOutDate].push (std::make_tuple (valuationDate, t, ++dateIndex));
493521 for (Size i = 0 ; i < outputCube->samples (); ++i) {
494522 Real v = outputCube->get (tradeId[j], dateIndex, i, 0 );
495523 outputCube->set (v +
@@ -510,32 +538,34 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
510538 numRatio (model, irStateBuffer, currencyIndex[j], 0 , 0.0 , 0 ) *
511539 effectiveMultiplier[j],
512540 tradeId[j], 0 );
513- std::map<QuantLib::Date, std::pair<double , size_t >> dateIndexCache;
541+ std::map<QuantLib::Date, std::queue<std::tuple<QuantLib::Date, double , size_t >>>
542+ closeOutDateToValuationDate;
514543 int dateIndex = -1 ;
515544 for (Size k = 1 ; k < res.size (); ++k) {
516545 Real t = sgd->getGrid ()->timeGrid ()[k];
517546 if (sgd->getGrid ()->isCloseOutDate ()[k - 1 ]) {
518547 Date closeOutDate = sgd->getGrid ()->dates ()[k - 1 ];
519- Date valuationDate = sgd-> getGrid ()-> valuationDateFromCloseOutDate (closeOutDate);
520- auto dateIndexIt = dateIndexCache. find (valuationDate);
521- QL_REQUIRE (dateIndexIt != dateIndexCache. end (),
522- " The valuation date ( " << ore::data::to_string ( valuationDate)
523- << " ) needs to before the corresponding close out date ( "
524- << ore::data::to_string (closeOutDate) << " ) " ) ;
525- auto [_, timeIndexValueDate] = dateIndexIt->second ;
548+ auto dateIndexIt = closeOutDateToValuationDate. find (closeOutDate);
549+ QL_REQUIRE ( dateIndexIt != closeOutDateToValuationDate. end () && !dateIndexIt-> second . empty (),
550+ " The valuation date needs to before the corresponding close out date " );
551+ Date valuationDate;
552+ double valuationTime;
553+ size_t valuationIndex ;
554+ std::tie (valuationDate, valuationTime, valuationIndex) = dateIndexIt->second . front () ;
526555 for (Size i = 0 ; i < outputCube->samples (); ++i) {
527- Real v = outputCube->get (tradeId[j], dateIndex , i, 1 );
556+ Real v = outputCube->get (tradeId[j], valuationIndex , i, 1 );
528557 outputCube->set (v +
529558 res[k][i] * fx (fxBuffer, currencyIndex[j], k, i) *
530559 num (model, irStateBuffer, currencyIndex[j], k, t, i) *
531560 effectiveMultiplier[j] +
532561 resFee[k][i],
533- tradeId[j], timeIndexValueDate , i, 1 );
562+ tradeId[j], valuationIndex , i, 1 );
534563 }
535564 }
536565 if (sgd->getGrid ()->isValuationDate ()[k - 1 ]) {
537566 Date valuationDate = sgd->getGrid ()->dates ()[k - 1 ];
538- dateIndexCache[valuationDate] = std::make_pair (t, ++dateIndex);
567+ Date closeOutDate = sgd->getGrid ()->closeOutDateFromValuationDate (valuationDate);
568+ closeOutDateToValuationDate[closeOutDate].push (std::make_tuple (valuationDate, t, ++dateIndex));
539569 for (Size i = 0 ; i < outputCube->samples (); ++i) {
540570 Real v = outputCube->get (tradeId[j], dateIndex, i, 0 );
541571 outputCube->set (v +
0 commit comments