Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
imscheduleanalytic.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
22
23using namespace ore::data;
24using namespace boost::filesystem;
25
26namespace ore {
27namespace analytics {
28
29void IMScheduleAnalytic::loadCrifRecords(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
30 QL_REQUIRE(inputs_, "Inputs not set");
31 QL_REQUIRE(!inputs_->crif().empty(), "CRIF loader does not contain any records");
32
33 crif_ = inputs_->crif();
36
37 // Keep record of which netting sets have SEC and CFTC
38 map<string, bool> hasSECCache, hasCFTCCache;
39 for (const CrifRecord& cr : crif_) {
40 const NettingSetDetails& nsd = cr.nettingSetDetails;
41
42 for (const SimmConfiguration::SimmSide& side : {SimmConfiguration::SimmSide::Call, SimmConfiguration::SimmSide::Post}) {
43 const string& crifRegs = side == SimmConfiguration::SimmSide::Call ? cr.collectRegulations : cr.postRegulations;
44 for (const string& reg : {"SEC", "CFTC"}) {
45 map<string, bool>& regCache = reg == "SEC" ? hasSECCache : hasCFTCCache;
46 auto& hasRegMap = reg == "SEC" ? hasSEC_ : hasCFTC_;
47
48 if (hasRegMap[side].find(nsd) == hasRegMap[side].end()) {
49 bool hasReg = false;
50 if (regCache.find(crifRegs) != regCache.end()) {
51 hasReg = regCache.at(crifRegs);
52 } else {
53 set<string> regs = parseRegulationString(crifRegs);
54 hasReg = regs.find(reg) != regs.end();
55 regCache[crifRegs] = hasReg;
56 }
57 if (hasReg)
58 hasRegMap[side].insert(nsd);
59 }
60 }
61 }
62 }
63}
64
65void IMScheduleAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
66 const std::set<std::string>& runTypes) {
67
68 if (!analytic()->match(runTypes))
69 return;
70
71 LOG("IMScheduleAnalytic::runAnalytic called");
72
73 analytic()->buildMarket(loader, false);
74
75 auto imAnalytic = static_cast<IMScheduleAnalytic*>(analytic());
76 QL_REQUIRE(imAnalytic, "Analytic must be of type IMScheduleAnalytic");
77
78
79 imAnalytic->loadCrifRecords(loader);
80
81 // Calculate IMSchedule
82 LOG("Calculating Schedule IM")
83 auto imSchedule = QuantLib::ext::make_shared<IMScheduleCalculator>(
84 imAnalytic->crif(), inputs_->simmResultCurrency(), analytic()->market(),
85 true, inputs_->enforceIMRegulations(), false, imAnalytic->hasSEC(),
86 imAnalytic->hasCFTC());
87 imAnalytic->setImSchedule(imSchedule);
88
89 Real fxSpotReport = 1.0;
90 if (!inputs_->simmReportingCurrency().empty()) {
91 fxSpotReport = analytic()
92 ->market()
93 ->fxRate(inputs_->simmResultCurrency() + inputs_->simmReportingCurrency())
94 ->value();
95 DLOG("SIMM reporting currency is " << inputs_->simmReportingCurrency() << " with fxSpot "
96 << fxSpotReport);
97 }
98
99 QuantLib::ext::shared_ptr<InMemoryReport> imScheduleSummaryReport = QuantLib::ext::make_shared<InMemoryReport>();
100 QuantLib::ext::shared_ptr<InMemoryReport> imScheduleTradeReport = QuantLib::ext::make_shared<InMemoryReport>();
101
102 // Populate the trade-level IM Schedule report
103 LOG("Generating Schedule IM reports")
104 ore::analytics::ReportWriter(inputs_->reportNaString())
105 .writeIMScheduleTradeReport(imSchedule->imScheduleTradeResults(), imScheduleTradeReport,
106 imAnalytic->hasNettingSetDetails());
107
108 // Populate the netting set level IM Schedule report
109 ore::analytics::ReportWriter(inputs_->reportNaString())
110 .writeIMScheduleSummaryReport(imSchedule->finalImScheduleSummaryResults(), imScheduleSummaryReport,
111 imAnalytic->hasNettingSetDetails(), inputs_->simmResultCurrency(),
112 inputs_->simmReportingCurrency(), fxSpotReport);
113
114 LOG("Schedule IM reports generated");
115 MEM_LOG;
116
117 analytic()->reports()["IM_SCHEDULE"]["im_schedule"] = imScheduleSummaryReport;
118 analytic()->reports()["IM_SCHEDULE"]["im_schedule_trade"] = imScheduleTradeReport;
119}
120
121} // namespace analytics
122} // namespace ore
Analytic * analytic() const
Definition: analytic.hpp:193
QuantLib::ext::shared_ptr< InputParameters > inputs_
Definition: analytic.hpp:216
analytic_reports & reports()
Result reports.
Definition: analytic.hpp:131
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
Definition: analytic.cpp:178
const QuantLib::ext::shared_ptr< ore::data::Market > & market() const
Definition: analytic.hpp:117
QuantLib::ext::shared_ptr< InputParameters > inputs_
contains all the input parameters for the run
Definition: analytic.hpp:156
bool hasNettingSetDetails() const
Check if netting set details are used anywhere, instead of just the netting set ID.
Definition: crif.cpp:342
void fillAmountUsd(const QuantLib::ext::shared_ptr< ore::data::Market > market)
Definition: crif.cpp:351
std::map< SimmConfiguration::SimmSide, std::set< ore::data::NettingSetDetails > > hasSEC_
virtual void loadCrifRecords(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
Load CRIF from external source, override to generate CRIF from the input portfolio.
std::map< SimmConfiguration::SimmSide, std::set< ore::data::NettingSetDetails > > hasCFTC_
void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
Write ORE outputs to reports.
virtual void writeIMScheduleTradeReport(const std::map< std::string, std::vector< IMScheduleCalculator::IMScheduleTradeData > > &tradeResults, const QuantLib::ext::shared_ptr< ore::data::Report > report, const bool hasNettingSetDetails=false)
virtual void writeIMScheduleSummaryReport(const std::map< SimmConfiguration::SimmSide, std::map< NettingSetDetails, std::pair< std::string, IMScheduleResults > > > &finalResultsMap, const QuantLib::ext::shared_ptr< Report > report, const bool hasNettingSetDetails=false, const std::string &simmResultCcy="", const std::string &reportCcy="", QuantLib::Real fxSpot=1.0, QuantLib::Real outputThreshold=0.005)
SimmSide
Enum indicating the relevant side of the SIMM calculation.
ORE IM Schedule Analytic.
#define MEM_LOG
#define LOG(text)
#define DLOG(text)
set< string > parseRegulationString(const string &regsString, const set< string > &valueIfEmpty)
Reads a string containing regulations applicable for a given CRIF record.
A Class to write ORE outputs to reports.
SIMM configuration interface.