27 QuantLib::Real deltaPnl, QuantLib::Real gammaPnl, QuantLib::Real pnl) {
28 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::All) {
29 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
30 result.
delta = deltaPnl;
31 result.
gamma = gammaPnl;
32 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
34 else if (mrg->riskType() == MarketRiskConfiguration::RiskType::All)
37 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::InterestRate) {
38 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
41 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
44 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::Equity) {
45 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
48 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
51 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::FX) {
52 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
55 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
58 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::Inflation) {
59 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
62 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
65 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::Credit) {
66 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
69 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
72 if (mrg->riskClass() == MarketRiskConfiguration::RiskClass::Commodity) {
73 if (mrg->riskType() == MarketRiskConfiguration::RiskType::DeltaGamma) {
76 }
else if (mrg->riskType() == MarketRiskConfiguration::RiskType::Vega)
82 QL_REQUIRE(reports->reports().size() == 1,
"We should only report for PNL Explain");
83 QL_REQUIRE(reports->reports().size() == 1,
"We should only report for PNL Explain");
84 QuantLib::ext::shared_ptr<InMemoryReport> report =
85 QuantLib::ext::dynamic_pointer_cast<ore::data::InMemoryReport>(reports->reports().at(0));
86 QL_REQUIRE(report,
"PNL report must be an InMemoryReport");
90 report->addColumn(
"ScenarioPnl",
double(), 6)
91 .addColumn(
"TotalDelta",
double(), 6)
92 .addColumn(
"TotalGamma",
double(), 6)
93 .addColumn(
"TotalVega",
double(), 6)
94 .addColumn(
"IrDelta",
double(), 6)
95 .addColumn(
"IrGamma",
double(), 6)
96 .addColumn(
"IrVega",
double(), 6)
97 .addColumn(
"EqDelta",
double(), 6)
98 .addColumn(
"EqGamma",
double(), 6)
99 .addColumn(
"EqVega",
double(), 6)
100 .addColumn(
"FxDelta",
double(), 6)
101 .addColumn(
"FxGamma",
double(), 6)
102 .addColumn(
"FxVega",
double(), 6)
103 .addColumn(
"InfDelta",
double(), 6)
104 .addColumn(
"InfGamma",
double(), 6)
105 .addColumn(
"InfVega",
double(), 6)
106 .addColumn(
"CreditDelta",
double(), 6)
107 .addColumn(
"CreditGamma",
double(), 6)
108 .addColumn(
"CreditVega",
double(), 6)
109 .addColumn(
"CommDelta",
double(), 6)
110 .addColumn(
"CommGamma",
double(), 6)
111 .addColumn(
"CommVega",
double(), 6)
116 const QuantLib::ext::shared_ptr<MarketRiskGroupBase>& riskGroup,
117 const QuantLib::ext::shared_ptr<TradeGroupBase>& tradeGroup) {
125 const QuantLib::ext::shared_ptr<MarketRiskGroupBase>& riskGroup,
126 const QuantLib::ext::shared_ptr<TradeGroupBase>& tradeGroup) {
129 std::vector<QuantLib::Real> foSensiPnls =
pnlCalculators_[0]->foPnls();
133 QL_REQUIRE(sensiPnls.size() == 1,
"PNLExplainReport::writeReports - should have exactly 1 sensi pnl");
134 QL_REQUIRE(foSensiPnls.size() == 1,
"PNLExplainReport::writeReports - should have exactly 1 fo sensi pnl");
135 Real pnl = sensiPnls[0];
136 Real deltaPnl = foSensiPnls[0];
137 Real gammaPnl = pnl - deltaPnl;
144 auto mrg = ext::dynamic_pointer_cast<MarketRiskGroup>(riskGroup);
145 QL_REQUIRE(mrg,
"Require a group of type MarketRiskGroup");
150 QL_REQUIRE(tradeSensiPnls.size() == 1,
151 "PNLExplainReport::writeReports - should have exactly 1 sensi pnl for each trade");
152 QL_REQUIRE(foTradeSensiPnls.size() == 1,
153 "PNLExplainReport::writeReports - should have exactly 1 fo sensi pnl for each trade");
155 "PNLExplainReport::writeReports - tradeSensiPNLs do not match portfolio size");
157 "PNLExplainReport::writeReports - foTradeSensiPNLs do not match portfolio size");
159 for (QuantLib::Size i = 0; i <
tradeIds_.size(); i++) {
161 Real tpnl = tradeSensiPnls[0].at(i);
162 Real tdeltaPnl = foTradeSensiPnls[0].at(i);
163 Real tgammaPnl = tpnl - tdeltaPnl;
174 const QuantLib::ext::shared_ptr<ore::analytics::MarketRiskGroupBase>& riskGroup)
const {
178 const QuantLib::ext::shared_ptr<ore::analytics::MarketRiskGroupBase>& riskGroup)
const {
183 QuantLib::ext::shared_ptr<InMemoryReport> report =
184 QuantLib::ext::dynamic_pointer_cast<ore::data::InMemoryReport>(reports->reports().at(0));
186 const vector<ore::data::Report::ReportType>& tradeIds = report->data(0);
187 for (Size j = 0; j < tradeIds.size(); j++) {
188 string tradeId = QuantLib::ext::get<std::string>(tradeIds[j]);
189 const auto& r =
results_.find(tradeId);
192 "Could not find Pnl Explain record for trade ID: " + tradeId)
197 report->add(r->second.pnl)
198 .add(r->second.delta)
199 .add(r->second.gamma)
201 .add(r->second.irDelta)
202 .add(r->second.irGamma)
203 .add(r->second.irVega)
204 .add(r->second.eqDelta)
205 .add(r->second.eqGamma)
206 .add(r->second.eqVega)
207 .add(r->second.fxDelta)
208 .add(r->second.fxGamma)
209 .add(r->second.fxVega)
210 .add(r->second.infDelta)
211 .add(r->second.infGamma)
212 .add(r->second.infVega)
213 .add(r->second.creditDelta)
214 .add(r->second.creditGamma)
215 .add(r->second.creditVega)
216 .add(r->second.comDelta)
217 .add(r->second.comGamma)
218 .add(r->second.comVega);
virtual void closeReports(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
std::vector< QuantLib::ext::shared_ptr< PNLCalculator > > pnlCalculators_
boost::optional< ore::data::TimePeriod > period_
virtual std::string portfolioId(const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) const
std::vector< std::string > tradeIds_
virtual bool runTradeDetail(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
void createReports(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) override
void closeReports(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) override
void addPnlCalculators(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) override
std::map< std::string, PnlExplainResults > results_
QuantLib::Size pnlReportColumnSize_
bool includeDeltaMargin(const QuantLib::ext::shared_ptr< ore::analytics::MarketRiskGroupBase > &riskGroup) const override
bool includeGammaMargin(const QuantLib::ext::shared_ptr< ore::analytics::MarketRiskGroupBase > &riskGroup) const override
void writeReports(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) override
void handleSensiResults(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &report, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) override
void populateResults(QuantLib::ext::shared_ptr< MarketRiskGroup > mrg, PnlExplainReport::PnlExplainResults &result, QuantLib::Real deltaPnl, QuantLib::Real gammaPnl, QuantLib::Real pnl)
std::vector< std::vector< QuantLib::Real > > TradePnLStore
Size size(const ValueType &v)
QuantLib::Real creditDelta
QuantLib::Real creditVega
QuantLib::Real creditGamma
Class for structured analytics warnings.