Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
PnlExplainReport Class Reference

#include <orea/engine/pnlexplainreport.hpp>

+ Inheritance diagram for PnlExplainReport:
+ Collaboration diagram for PnlExplainReport:

Classes

struct  PnlExplainResults
 

Public Member Functions

 PnlExplainReport (const std::string &baseCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const std::string &portfolioFilter, boost::optional< ore::data::TimePeriod > period, const QuantLib::ext::shared_ptr< Report > &pnlReport=nullptr, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen=nullptr, std::unique_ptr< SensiRunArgs > sensiArgs=nullptr, std::unique_ptr< FullRevalArgs > fullRevalArgs=nullptr, std::unique_ptr< MultiThreadArgs > multiThreadArgs=nullptr, const bool requireTradePnl=false)
 
- Public Member Functions inherited from MarketRiskReport
 MarketRiskReport (const std::string &calculationCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const std::string &portfolioFilter, boost::optional< ore::data::TimePeriod > period, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen=nullptr, std::unique_ptr< SensiRunArgs > sensiArgs=nullptr, std::unique_ptr< FullRevalArgs > fullRevalArgs=nullptr, std::unique_ptr< MultiThreadArgs > multiThreadArgs=nullptr, const bool breakdown=false, const bool requireTradePnl=false)
 
virtual ~MarketRiskReport ()
 
virtual void initialise ()
 
void initSimMarket ()
 Method to init simMarket_ for multi-threaded ctors. More...
 
virtual void calculate (const QuantLib::ext::shared_ptr< Reports > &report)
 
void enableCubeWrite (const std::string &cubeDir, const std::string &cubeFilename)
 
- Public Member Functions inherited from ProgressReporter
 ProgressReporter ()
 
void registerProgressIndicator (const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
 
void unregisterProgressIndicator (const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
 
void unregisterAllProgressIndicators ()
 
void updateProgress (const unsigned long progress, const unsigned long total, const std::string &detail="")
 
void resetProgress ()
 
const std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > & progressIndicators () const
 

Protected Member Functions

void createReports (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) 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 addPnlCalculators (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) 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
 
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 closeReports (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports) override
 
- Protected Member Functions inherited from MarketRiskReport
virtual void initialiseRiskGroups ()
 Method for shared initialisation. More...
 
virtual void registerProgressIndicators ()
 
virtual void createReports (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)=0
 
virtual bool runTradeDetail (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
 
virtual QuantLib::ext::shared_ptr< ScenarioFiltercreateScenarioFilter (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup)
 
virtual void reset (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup)
 
virtual bool runTradeRiskGroup (const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual bool disablesAll (const QuantLib::ext::shared_ptr< ScenarioFilter > &filter) const
 
virtual void updateFilter (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< ScenarioFilter > &filter)
 update any filters required More...
 
virtual std::string portfolioId (const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) const
 
virtual std::string tradeGroupKey (const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) const
 
virtual ore::data::TimePeriod covariancePeriod () const
 
virtual void addPnlCalculators (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
 
virtual void handleSensiResults (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &report, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup)
 
virtual void handleFullRevalResults (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup)
 
virtual bool includeDeltaMargin (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual bool includeGammaMargin (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual bool runFullReval (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual bool generateCube (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual std::string cubeFilePath (const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup) const
 
virtual std::vector< ore::data::TimePeriodtimePeriods ()
 
virtual void writeReports (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup)
 
virtual void closeReports (const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
 

Private Attributes

std::map< std::string, PnlExplainResultsresults_
 
QuantLib::ext::shared_ptr< ReportpnlReport_
 
QuantLib::Size pnlReportColumnSize_
 

Additional Inherited Members

- Protected Attributes inherited from MarketRiskReport
bool sensiBased_ = false
 
bool fullReval_ = false
 
std::string calculationCurrency_
 
QuantLib::ext::shared_ptr< Portfolioportfolio_
 
std::string portfolioFilter_
 
boost::optional< ore::data::TimePeriodperiod_
 
QuantLib::ext::shared_ptr< HistoricalScenarioGeneratorhisScenGen_
 
std::unique_ptr< SensiRunArgssensiArgs_
 
std::unique_ptr< FullRevalArgsfullRevalArgs_
 
std::unique_ptr< MultiThreadArgsmultiThreadArgs_
 
bool breakdown_ = false
 
bool requireTradePnl_ = false
 
QuantLib::ext::shared_ptr< MarketRiskGroupBaseContainerriskGroups_
 
QuantLib::ext::shared_ptr< TradeGroupBaseContainertradeGroups_
 
std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > tradeIdGroups_
 
std::set< std::pair< std::string, QuantLib::Size > > tradeIdIdxPairs_
 
std::vector< std::string > tradeIds_
 
std::map< RiskFactorKey, QuantLib::Real > deltas_
 
std::map< std::pair< RiskFactorKey, RiskFactorKey >, QuantLib::Real > gammas_
 
QuantLib::Matrix covarianceMatrix_
 
bool writePnl_ = false
 
std::vector< QuantLib::ext::shared_ptr< PNLCalculator > > pnlCalculators_
 
QuantLib::ext::shared_ptr< QuantExt::CovarianceSalvagesalvage_
 
bool includeDeltaMargin_ = true
 
bool includeGammaMargin_ = true
 
QuantLib::ext::shared_ptr< ore::data::EngineFactoryfactory_
 
QuantLib::ext::shared_ptr< ore::analytics::HistoricalPnlGeneratorhistPnlGen_
 
QuantLib::ext::shared_ptr< HistoricalSensiPnlCalculatorsensiPnlCalculator_
 

Detailed Description

Definition at line 24 of file pnlexplainreport.hpp.

Constructor & Destructor Documentation

◆ PnlExplainReport()

PnlExplainReport ( const std::string &  baseCurrency,
const QuantLib::ext::shared_ptr< Portfolio > &  portfolio,
const std::string &  portfolioFilter,
boost::optional< ore::data::TimePeriod period,
const QuantLib::ext::shared_ptr< Report > &  pnlReport = nullptr,
const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &  hisScenGen = nullptr,
std::unique_ptr< SensiRunArgs sensiArgs = nullptr,
std::unique_ptr< FullRevalArgs fullRevalArgs = nullptr,
std::unique_ptr< MultiThreadArgs multiThreadArgs = nullptr,
const bool  requireTradePnl = false 
)

Definition at line 52 of file pnlexplainreport.hpp.

60 : MarketRiskReport(baseCurrency, portfolio, portfolioFilter, period, hisScenGen, std::move(sensiArgs), std::move(fullRevalArgs),
61 std::move(multiThreadArgs), true, requireTradePnl), pnlReport_(pnlReport) {
62 sensiBased_ = true;
63 }
MarketRiskReport(const std::string &calculationCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const std::string &portfolioFilter, boost::optional< ore::data::TimePeriod > period, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen=nullptr, std::unique_ptr< SensiRunArgs > sensiArgs=nullptr, std::unique_ptr< FullRevalArgs > fullRevalArgs=nullptr, std::unique_ptr< MultiThreadArgs > multiThreadArgs=nullptr, const bool breakdown=false, const bool requireTradePnl=false)
QuantLib::ext::shared_ptr< Report > pnlReport_

Member Function Documentation

◆ createReports()

void createReports ( const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &  reports)
overrideprotectedvirtual

Implements MarketRiskReport.

Definition at line 81 of file pnlexplainreport.cpp.

81 {
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");
87
88 pnlReportColumnSize_ = report->columns();
89
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)
112 ;
113}

◆ handleSensiResults()

void handleSensiResults ( const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &  report,
const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &  riskGroup,
const QuantLib::ext::shared_ptr< TradeGroupBase > &  tradeGroup 
)
overrideprotectedvirtual

Reimplemented from MarketRiskReport.

Definition at line 115 of file pnlexplainreport.cpp.

117 {
118}

◆ addPnlCalculators()

void addPnlCalculators ( const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &  reports)
overrideprotectedvirtual

Reimplemented from MarketRiskReport.

Definition at line 120 of file pnlexplainreport.cpp.

120 {
121 pnlCalculators_.push_back(QuantLib::ext::make_shared<PNLCalculator>(period_.get()));
122}
std::vector< QuantLib::ext::shared_ptr< PNLCalculator > > pnlCalculators_
boost::optional< ore::data::TimePeriod > period_

◆ writeReports()

void writeReports ( const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &  reports,
const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &  riskGroup,
const QuantLib::ext::shared_ptr< TradeGroupBase > &  tradeGroup 
)
overrideprotectedvirtual

Reimplemented from MarketRiskReport.

Definition at line 124 of file pnlexplainreport.cpp.

126 {
127
128 std::vector<QuantLib::Real> sensiPnls = pnlCalculators_[0]->pnls();
129 std::vector<QuantLib::Real> foSensiPnls = pnlCalculators_[0]->foPnls();
130 TradePnLStore tradeSensiPnls = pnlCalculators_[0]->tradePnls();
131 TradePnLStore foTradeSensiPnls = pnlCalculators_[0]->foTradePnls();
132
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;
138
139 auto pId = portfolioId(tradeGroup);
140 auto it = results_.find(pId);
141 if (it == results_.end())
142 results_[pId] = PnlExplainResults();
143
144 auto mrg = ext::dynamic_pointer_cast<MarketRiskGroup>(riskGroup);
145 QL_REQUIRE(mrg, "Require a group of type MarketRiskGroup");
146
147 populateResults(mrg, results_[pId], deltaPnl, gammaPnl, pnl);
148
149 if (runTradeDetail(reports)) {
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");
154 QL_REQUIRE(tradeSensiPnls[0].size() == tradeIds_.size(),
155 "PNLExplainReport::writeReports - tradeSensiPNLs do not match portfolio size");
156 QL_REQUIRE(foTradeSensiPnls[0].size() == tradeIds_.size(),
157 "PNLExplainReport::writeReports - foTradeSensiPNLs do not match portfolio size");
158
159 for (QuantLib::Size i = 0; i < tradeIds_.size(); i++) {
160 auto tId = tradeIds_.at(i);
161 Real tpnl = tradeSensiPnls[0].at(i);
162 Real tdeltaPnl = foTradeSensiPnls[0].at(i);
163 Real tgammaPnl = tpnl - tdeltaPnl;
164 auto itt = results_.find(tId);
165 if (itt == results_.end())
166 results_[tId] = PnlExplainResults();
167
168 populateResults(mrg, results_[tId], tdeltaPnl, tgammaPnl, tpnl);
169 }
170 }
171}
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)
std::map< std::string, PnlExplainResults > results_
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)
+ Here is the call graph for this function:

◆ includeDeltaMargin()

bool includeDeltaMargin ( const QuantLib::ext::shared_ptr< ore::analytics::MarketRiskGroupBase > &  riskGroup) const
overrideprotected

Definition at line 173 of file pnlexplainreport.cpp.

174 {
175 return true;
176}

◆ includeGammaMargin()

bool includeGammaMargin ( const QuantLib::ext::shared_ptr< ore::analytics::MarketRiskGroupBase > &  riskGroup) const
overrideprotected

Definition at line 177 of file pnlexplainreport.cpp.

178 {
179 return true;
180}

◆ closeReports()

void closeReports ( const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &  reports)
overrideprotectedvirtual

Reimplemented from MarketRiskReport.

Definition at line 182 of file pnlexplainreport.cpp.

182 {
183 QuantLib::ext::shared_ptr<InMemoryReport> report =
184 QuantLib::ext::dynamic_pointer_cast<ore::data::InMemoryReport>(reports->reports().at(0));
185
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);
190 if (r == results_.end()) {
191 StructuredAnalyticsWarningMessage("Pnl Explain", "Failed to generate Pnl Explain Records",
192 "Could not find Pnl Explain record for trade ID: " + tradeId)
193 .log();
194 } else {
195 report->next();
196 report->jumpToColumn(pnlReportColumnSize_);
197 report->add(r->second.pnl)
198 .add(r->second.delta)
199 .add(r->second.gamma)
200 .add(r->second.vega)
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);
219 }
220 }
221
223}
virtual void closeReports(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports)
+ Here is the call graph for this function:

Member Data Documentation

◆ results_

std::map<std::string, PnlExplainResults> results_
private

Definition at line 81 of file pnlexplainreport.hpp.

◆ pnlReport_

QuantLib::ext::shared_ptr<Report> pnlReport_
private

Definition at line 82 of file pnlexplainreport.hpp.

◆ pnlReportColumnSize_

QuantLib::Size pnlReportColumnSize_
private

Definition at line 83 of file pnlexplainreport.hpp.