43 {
44
45 CONSOLEW(
"PNL Explain: Build Market");
48
49 CONSOLEW(
"PNL Explain: Build Portfolio");
52
53 auto pnlexplainAnalytic =
static_cast<PnlExplainAnalytic*
>(
analytic());
54 QL_REQUIRE(pnlexplainAnalytic, "Analytic must be of type PnlExplainAnalytic");
55
57 pnlAnalytic->runAnalytic(loader);
58 auto pnlReport = pnlAnalytic->reports().at("PNL").at("pnl");
59
61
62
63
64 inputs_->setSensiThreshold(QuantLib::Null<QuantLib::Real>());
65 sensiAnalytic->runAnalytic(loader, {"SENSITIVITY"});
66
67 auto sensireport = sensiAnalytic->reports().at("SENSITIVITY").at("sensitivity");
69 ext::shared_ptr<SensitivityStream> ss = ext::make_shared<SensitivityReportStream>(sensiAnalytic->reports().at("SENSITIVITY").at("sensitivity"));
70 ss = ext::make_shared<FilteredSensitivityStream>(ss, 1e-6);
71
72 auto sensiReports = sensiAnalytic->reports();
73
74 QuantLib::ext::shared_ptr<ore::data::AdjustmentFactors> adjFactors;
75 if (auto adjLoader = QuantLib::ext::dynamic_pointer_cast<AdjustedInMemoryLoader>(loader))
76 adjFactors = QuantLib::ext::make_shared<ore::data::AdjustmentFactors>(adjLoader->adjustmentFactors());
77
78
79 set<Date> pnlDates;
80 pnlDates.insert(
inputs_->asof());
81 pnlDates.insert(
inputs_->mporDate());
83
84 QuantLib::ext::shared_ptr<HistoricalScenarioGenerator> scenarios;
85 if (!
inputs_->historicalScenarioReader()) {
86
87 auto pnlImpl = static_cast<PnlAnalyticImpl*>(pnlAnalytic->impl().get());
88 QL_REQUIRE(pnlImpl, "Impl must of type PNLAnalyticImpl");
89 auto t0Scenario = pnlImpl->t0Scenario();
90 auto t1Scenario = pnlImpl->t1Scenario();
91
92 analytic()->
reports()[
label_][
"pnl_scenario_t0"] = pnlAnalytic->reports().at(
"PNL").at(
"pnl_scenario_t0");
93 analytic()->
reports()[
label_][
"pnl_scenario_t1"] = pnlAnalytic->reports().at(
"PNL").at(
"pnl_scenario_t1");
94
95 vector<QuantLib::ext::shared_ptr<ore::analytics::Scenario>> histScens = {t0Scenario, t1Scenario};
96
97 QuantLib::ext::shared_ptr<HistoricalScenarioLoader> scenarioLoader =
98 QuantLib::ext::make_shared<HistoricalScenarioLoader>(histScens, pnlDates);
99
100 scenarios = QuantLib::ext::make_shared<HistoricalScenarioGenerator>(
101 scenarioLoader, QuantLib::ext::make_shared<SimpleScenarioFactory>(), adjFactors,
102 ReturnConfiguration(), "hs_");
103 } else {
105 analytic()->configurations().simMarketParams,
106 analytic()->configurations().todaysMarketParams);
107 }
108
109 auto simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
111 *
analytic()->configurations().curveConfig, *
analytic()->configurations().todaysMarketParams,
true,
true,
112 false,
false, *
inputs_->iborFallbackConfig());
113 simMarket->scenarioGenerator() = scenarios;
114 scenarios->baseScenario() = simMarket->baseScenario();
115
116 ext::shared_ptr<ScenarioShiftCalculator> shiftCalculator = ext::make_shared<ScenarioShiftCalculator>(
117 analytic()->configurations().sensiScenarioData,
analytic()->configurations().simMarketParams);
118
119 std::unique_ptr<MarketRiskReport::SensiRunArgs> sensiArgs =
120 std::make_unique<MarketRiskReport::SensiRunArgs>(ss, shiftCalculator);
121
122 auto pnlExplainReport =
123 ext::make_shared<PnlExplainReport>(
inputs_->baseCurrency(),
analytic()->portfolio(),
inputs_->portfolioFilter(),
124 period, pnlReport, scenarios, std::move(sensiArgs), nullptr, nullptr, true);
125
126 LOG(
"Call PNL Explain calculation");
127 CONSOLEW(
"Risk: PNL Explain Calculation");
128 ext::shared_ptr<MarketRiskReport::Reports> reports = ext::make_shared<MarketRiskReport::Reports>();
129 QuantLib::ext::shared_ptr<InMemoryReport> pnlExplainOutput = QuantLib::ext::make_shared<InMemoryReport>();
130 reports->add(pnlReport);
131
132 pnlExplainReport->calculate(reports);
135}
Analytic * analytic() const
std::string label_
label for logging purposes primarily
QuantLib::ext::shared_ptr< T > dependentAnalytic(const std::string &key) const
analytic_reports & reports()
Result reports.
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
virtual void buildPortfolio()
static const string defaultConfiguration
QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > buildHistoricalScenarioGenerator(const QuantLib::ext::shared_ptr< HistoricalScenarioReader > &hsr, const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > &adjFactors, const TimePeriod &period, Calendar calendar, Size mporDays, const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simParams, const QuantLib::ext::shared_ptr< TodaysMarketParameters > &marketParams, const bool overlapping)