Beispiel #1
0
        public void BuildCube(Portfolio portfolio, List <ValuationCalculator> calculators)
        {
            QLNet.Utils.QL_REQUIRE(portfolio.Size() > 0, () => "ValuationEngine: Error portfolio is empty");

            ObservationMode.Mode om = ObservationMode.Mode.Disable;
            double updateTime       = 0.0;
            double pricingTime      = 0.0;
            double fixingTime       = 0.0;
            int    samples          = 1;

            List <Date>  dates  = _dg.Dates();
            List <Trade> trades = portfolio.Trades();

            int numFRC = 0;

            // initialise state objects for each trade (required for path-dependent derivatives in particular)
            for (int i = 0; i < trades.Count; i++)
            {
                QLNet.Utils.QL_REQUIRE(trades[i].NpvCurrency() != "", () => "NPV currency not set for trade " + trades[i].Id());

                //DLOG("Initialise wrapper for trade " << trades[i]->id());
                trades[i].Instrument();// ->initialise(dates);

                // T0 values
                foreach (var calc in calculators)
                {
                    calc.CalculateT0(trades[i], i, _simMarket); //, outputCube);
                }
                // TODO: Fix me!
                if (om == ObservationMode.Mode.Unregister)
                {
                    foreach (var leg in trades[i].Legs())
                    {
                        for (int n = 0; n < leg.Count; n++)
                        {
                            FloatingRateCoupon frc = leg[n] as FloatingRateCoupon;
                            if (frc != null)
                            {
                                //frc.unregisterWith(frc.index());
                                //trades[i]->instrument()->qlInstrument()->unregisterWith(frc);
                                //// Unregister with eval dates
                                //frc->unregisterWith(Settings::instance().evaluationDate());
                                //frc->index()->unregisterWith(Settings::instance().evaluationDate());
                                //trades[i]->instrument()->qlInstrument()->unregisterWith(Settings::instance().evaluationDate());
                            }
                        }
                    }
                }
            }

            //    LOG("Total number of swaps = " << portfolio->size());
            //    LOG("Total number of FRC = " << numFRC);

            //    simMarket_->fixingManager()->initialise(portfolio);

            Stopwatch timer     = new Stopwatch();
            Stopwatch loopTimer = new Stopwatch();

            // We call Cube::samples() each time her to allow for dynamic stopping times
            // e.g. MC convergence tests
            for (int sample = 0; sample < samples; ++sample)
            {
                //updateProgress(sample, outputCube->samples());

                foreach (var trade in trades)
                {
                    //trade.Instrument().Reset();
                }

                // loop over Dates
                for (int i = 0; i < dates.Count; ++i)
                {
                    Date d = dates[i];

                    timer.Start();

                    //simMarket_->update(d);

                    // recalibrate models
                    //foreach (var b in modelBuilders_)
                    //{
                    //    if (om == ObservationMode::Mode::Disable)
                    //    {
                    //        b.second->recalculate();
                    //    }

                    //    b.second->recalibrate();
                    //}

                    updateTime += timer.ElapsedMilliseconds / 1000.0;

                    // loop over trades
                    timer.Restart();
                    for (int j = 0; j < trades.Count; ++j)
                    {
                        var trade = trades[j];

                        // We can avoid checking mode here and always call updateQlInstruments()
                        if (om == ObservationMode.Mode.Disable)
                        {
                            trade.Instrument().update(); // ->updateQlInstruments();
                        }

                        foreach (var calc in calculators)
                        {
                            calc.Calculate(trade, j, _simMarket, d, i); //, outputCube, d, i, sample);
                        }
                    }
                    pricingTime += timer.ElapsedMilliseconds / 1000.0;
                }

                timer.Restart();
                //simMarket_->fixingManager()->reset();
                fixingTime += timer.ElapsedMilliseconds / 1000.0;
            }

            //simMarket_->reset();
            //updateProgress(outputCube->samples(), outputCube->samples());
            //LOG("ValuationEngine completed: loop " << setprecision(2) << loopTimer.elapsed() << " sec, "
            //                                               << "pricing " << pricingTime << " sec, "
            //                                               << "update " << updateTime << " sec "
            //                                               << "fixing " << fixingTime);
        }
Beispiel #2
0
        public override void Update(Date d)
        {
            QLNet.Utils.QL_REQUIRE(_scenarioGenerator != null, () => "ScenarioSimMarket::update: no scenario generator set");

            ObservationMode.Mode om = ObservationMode.Mode.Disable;
            //if (om == ObservationMode.Disable)
            //    ObservableSettings::instance().disableUpdates(false);
            //else if (om == ObservationMode.Defer)
            //    ObservableSettings::instance().disableUpdates(true);

            Scenario scenario = _scenarioGenerator.Next(d);

            QLNet.Utils.QL_REQUIRE(scenario.AsOf() == d, () => "Invalid Scenario date " + scenario.AsOf() + ", expected " + d);

            _numeraire = scenario.GetNumeraire();

            if (d != Settings.evaluationDate())
            {
                Settings.setEvaluationDate(d);
            }
            else if (om == ObservationMode.Mode.Unregister)
            {
                // Due to some of the notification chains having been unregistered,
                // it is possible that some lazy objects might be missed in the case
                // that the evaluation date has not been updated. Therefore, we
                // manually kick off an observer notification from this level.
                // We have unit regression tests in OREAnalyticsTestSuite to ensure
                // the various ObservationMode settings return the anticipated results.

                //QLNet.IObservable obs = Settings.evaluationDate();
                //obs.notifyObservers();
            }

            ApplyScenario(scenario);

            // Observation Mode - key to update these before fixings are set
            if (om == ObservationMode.Mode.Disable)
            {
                Refresh();
                //ObservableSettings::instance().enableUpdates();
            }
            else if (om == ObservationMode.Mode.Defer)
            {
                //ObservableSettings::instance().enableUpdates();
            }

            // Apply fixings as historical fixings. Must do this before we populate ASD
            //_fixingManager.update(d);

            //if (asd_)
            //{
            //    // add additional scenario data to the given container, if required
            //    for (auto i : parameters_->additionalScenarioDataIndices())
            //    {
            //        boost::shared_ptr<QuantLib::Index> index;
            //        try
            //        {
            //            index = *iborIndex(i);
            //        }
            //        catch (...) {
            //    }
            //    try
            //    {
            //        index = *swapIndex(i);
            //    }
            //    catch (...) {
            //    }
            //    QL_REQUIRE(index != nullptr, "ScenarioSimMarket::update() index " << i << " not found in sim market");
            //    asd_->set(index->fixing(d), AggregationScenarioDataType::IndexFixing, i);
            //    }

            //    for (auto c : parameters_->additionalScenarioDataCcys())
            //    {
            //        if (c != parameters_->baseCcy())
            //            asd_->set(fxSpot(c + parameters_->baseCcy())->value(), AggregationScenarioDataType::FXSpot, c);
            //    }

            //    asd_->set(numeraire_, AggregationScenarioDataType::Numeraire);

            //    asd_->next();
        }