Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crifrecord.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
19#include <boost/algorithm/string.hpp>
20#include <boost/assign.hpp>
21#include <boost/bimap.hpp>
23
24namespace ore {
25namespace analytics {
26
27using std::ostream;
28using std::string;
29
30// Following bimaps ease the conversion from enum to string and back
31// It also puts in one place a mapping from string to enum value
32
33// custom comparator for bimaps
34struct string_cmp {
35 bool operator()(const string& lhs, const string& rhs) const {
36 return ((boost::to_lower_copy(lhs)) < (boost::to_lower_copy(rhs)));
37 }
38};
39
40// Ease the notation below
41template <typename T> using bm = boost::bimap<T, boost::bimaps::set_of<std::string, string_cmp>>;
42
43// Initialise the bimaps
44const bm<CrifRecord::RiskType> riskTypeMap = boost::assign::list_of<bm<CrifRecord::RiskType>::value_type>(
45 CrifRecord::RiskType::Commodity, "Risk_Commodity")
46 (CrifRecord::RiskType::CommodityVol, "Risk_CommodityVol")
47 (CrifRecord::RiskType::CreditNonQ, "Risk_CreditNonQ")
48 (CrifRecord::RiskType::CreditQ, "Risk_CreditQ")
49 (CrifRecord::RiskType::CreditVol, "Risk_CreditVol")
50 (CrifRecord::RiskType::CreditVolNonQ, "Risk_CreditVolNonQ")
51 (CrifRecord::RiskType::Equity, "Risk_Equity")
52 (CrifRecord::RiskType::EquityVol, "Risk_EquityVol")
53 (CrifRecord::RiskType::FX, "Risk_FX")
54 (CrifRecord::RiskType::FXVol, "Risk_FXVol")
55 (CrifRecord::RiskType::Inflation, "Risk_Inflation")
56 (CrifRecord::RiskType::IRCurve, "Risk_IRCurve")
57 (CrifRecord::RiskType::IRVol, "Risk_IRVol")
58 (CrifRecord::RiskType::InflationVol, "Risk_InflationVol")
59 (CrifRecord::RiskType::BaseCorr, "Risk_BaseCorr")
60 (CrifRecord::RiskType::XCcyBasis, "Risk_XCcyBasis")
61 (CrifRecord::RiskType::ProductClassMultiplier, "Param_ProductClassMultiplier")
62 (CrifRecord::RiskType::AddOnNotionalFactor, "Param_AddOnNotionalFactor")
63 (CrifRecord::RiskType::Notional, "Notional")
64 (CrifRecord::RiskType::AddOnFixedAmount, "Param_AddOnFixedAmount")
65 (CrifRecord::RiskType::PV,"PV") // IM Schedule
66 (CrifRecord::RiskType::GIRR_DELTA, "GIRR_DELTA")
67 (CrifRecord::RiskType::GIRR_VEGA, "GIRR_VEGA")
68 (CrifRecord::RiskType::GIRR_CURV, "GIRR_CURV")
69 (CrifRecord::RiskType::CSR_NS_DELTA, "CSR_NS_DELTA")
70 (CrifRecord::RiskType::CSR_NS_VEGA, "CSR_NS_VEGA")
71 (CrifRecord::RiskType::CSR_NS_CURV, "CSR_NS_CURV")
72 (CrifRecord::RiskType::CSR_SNC_DELTA, "CSR_SNC_DELTA")
73 (CrifRecord::RiskType::CSR_SNC_VEGA, "CSR_SNC_VEGA")
74 (CrifRecord::RiskType::CSR_SNC_CURV, "CSR_SNC_CURV")
75 (CrifRecord::RiskType::CSR_SC_DELTA, "CSR_SC_DELTA")
76 (CrifRecord::RiskType::CSR_SC_VEGA, "CSR_SC_VEGA")
77 (CrifRecord::RiskType::CSR_SC_CURV, "CSR_SC_CURV")
78 (CrifRecord::RiskType::EQ_DELTA, "EQ_DELTA")
79 (CrifRecord::RiskType::EQ_VEGA, "EQ_VEGA")
80 (CrifRecord::RiskType::EQ_CURV, "EQ_CURV")
81 (CrifRecord::RiskType::COMM_DELTA, "COMM_DELTA")
82 (CrifRecord::RiskType::COMM_VEGA, "COMM_VEGA")
83 (CrifRecord::RiskType::COMM_CURV, "COMM_CURV")
84 (CrifRecord::RiskType::FX_DELTA, "FX_DELTA")
85 (CrifRecord::RiskType::FX_VEGA, "FX_VEGA")
86 (CrifRecord::RiskType::FX_CURV, "FX_CURV")
87 (CrifRecord::RiskType::DRC_NS, "DRC_NS")
88 (CrifRecord::RiskType::DRC_SNC, "DRC_SNC")
89 (CrifRecord::RiskType::DRC_SC, "DRC_SC")
90 (CrifRecord::RiskType::RRAO_1_PERCENT, "RRAO_1_PERCENT")
91 (CrifRecord::RiskType::RRAO_01_PERCENT, "RRAO_01_PERCENT")
92 (CrifRecord::RiskType::Empty, "")
93 (CrifRecord::RiskType::All, "All");
94
95const bm<CrifRecord::ProductClass> productClassMap = boost::assign::list_of<bm<CrifRecord::ProductClass>::value_type>(
96 CrifRecord::ProductClass::RatesFX, "RatesFX")(CrifRecord::ProductClass::Rates, "Rates")(
97 CrifRecord::ProductClass::FX, "FX")(CrifRecord::ProductClass::Credit, "Credit")(
98 CrifRecord::ProductClass::Equity, "Equity")(CrifRecord::ProductClass::Commodity, "Commodity")(
99 CrifRecord::ProductClass::Other, "Other")(CrifRecord::ProductClass::Empty, "")(
100 CrifRecord::ProductClass::All, "All")(CrifRecord::ProductClass::AddOnNotionalFactor, "AddOnNotionalFactor")(
101 CrifRecord::ProductClass::AddOnFixedAmount, "AddOnFixedAmount");
102
103
104ostream& operator<<(ostream& out, const CrifRecord::RiskType& rt) {
105 QL_REQUIRE(riskTypeMap.left.count(rt) > 0,
106 "Risk type (" << static_cast<int>(rt) << ") not a valid CrifRecord::RiskType");
107 return out << riskTypeMap.left.at(rt);
108}
109
110ostream& operator<<(ostream& out, const CrifRecord::ProductClass& pc) {
111 QL_REQUIRE(productClassMap.left.count(pc) > 0,
112 "Product class (" << static_cast<int>(pc) << ") not a valid CrifRecord::ProductClass");
113 return out << productClassMap.left.at(pc);
114}
115
116
118 for (auto it = riskTypeMap.right.begin(); it != riskTypeMap.right.end(); it++) {
119 if (boost::to_lower_copy(rt) == boost::to_lower_copy(it->first))
120 return it->second;
121 }
122
123 // If we reach this point, then the risk type provided was not found
124 QL_FAIL("Risk type string " << rt << " does not correspond to a valid CrifRecord::RiskType");
125}
126
128 for (auto it = productClassMap.right.begin(); it != productClassMap.right.end(); it++) {
129 if (boost::to_lower_copy(pc) == boost::to_lower_copy(it->first))
130 return it->second;
131 }
132
133 // If we reach this point, then the product class provided was not found
134 QL_FAIL("Product class string " << pc << " does not correspond to a valid CrifRecord::ProductClass");
135}
136
137std::ostream& operator<<(std::ostream& out, const CrifRecord::CurvatureScenario& scenario){
138 switch(scenario){
140 out << "CurvatureDown";
141 break;
143 out << "CurvatureUp";
144 break;
145 default:
146 out << "";
147 break;
148 }
149 return out;
150}
151
153 if (scenario == "CurvatureDown") {
155 } else if (scenario == "CurvatureUp") {
157 } else {
159 }
160}
161
162std::vector<std::set<std::string>> CrifRecord::additionalHeaders = {};
163
164ostream& operator<<(ostream& out, const CrifRecord& cr) {
166 if (n.empty()) {
167 out << "[" << cr.tradeId << ", " << cr.portfolioId << ", " << cr.productClass << ", " << cr.riskType
168 << ", " << cr.qualifier << ", " << cr.bucket << ", " << cr.label1 << ", " << cr.label2 << ", "
169 << cr.amountCurrency << ", " << cr.amount << ", " << cr.amountUsd;
170 } else {
171 out << "[" << cr.tradeId << ", [" << n << "], " << cr.productClass << ", " << cr.riskType << ", "
172 << cr.qualifier << ", " << cr.bucket << ", " << cr.label1 << ", " << cr.label2 << ", "
173 << cr.amountCurrency << ", " << cr.amount << ", " << cr.amountUsd;
174 }
175
176 if (!cr.collectRegulations.empty())
177 out << ", collect_regulations=" << cr.collectRegulations;
178 if (!cr.postRegulations.empty())
179 out << ", post_regulations=" << cr.postRegulations;
180
181 out << "]";
182
183 return out;
184}
185
187 switch (riskType) {
188 case RiskType::Commodity:
189 case RiskType::CommodityVol:
190 case RiskType::CreditNonQ:
191 case RiskType::CreditQ:
192 case RiskType::CreditVol:
193 case RiskType::CreditVolNonQ:
194 case RiskType::Equity:
195 case RiskType::EquityVol:
196 case RiskType::FX:
197 case RiskType::FXVol:
198 case RiskType::Inflation:
199 case RiskType::IRCurve:
200 case RiskType::IRVol:
201 case RiskType::InflationVol:
202 case RiskType::BaseCorr:
203 case RiskType::XCcyBasis:
204 case RiskType::ProductClassMultiplier:
205 case RiskType::AddOnNotionalFactor:
206 case RiskType::Notional:
207 case RiskType::AddOnFixedAmount:
208 case RiskType::PV:
209 return RecordType::SIMM;
210 case RiskType::GIRR_DELTA:
211 case RiskType::GIRR_VEGA:
212 case RiskType::GIRR_CURV:
213 case RiskType::CSR_NS_DELTA:
214 case RiskType::CSR_NS_VEGA:
215 case RiskType::CSR_NS_CURV:
216 case RiskType::CSR_SNC_DELTA:
217 case RiskType::CSR_SNC_VEGA:
218 case RiskType::CSR_SNC_CURV:
219 case RiskType::CSR_SC_DELTA:
220 case RiskType::CSR_SC_VEGA:
221 case RiskType::CSR_SC_CURV:
222 case RiskType::EQ_DELTA:
223 case RiskType::EQ_VEGA:
224 case RiskType::EQ_CURV:
225 case RiskType::COMM_DELTA:
226 case RiskType::COMM_VEGA:
227 case RiskType::COMM_CURV:
228 case RiskType::FX_DELTA:
229 case RiskType::FX_VEGA:
230 case RiskType::FX_CURV:
231 case RiskType::DRC_NS:
232 case RiskType::DRC_SNC:
233 case RiskType::DRC_SC:
234 case RiskType::RRAO_1_PERCENT:
235 case RiskType::RRAO_01_PERCENT:
236 return RecordType::FRTB;
237 case RiskType::All:
238 case RiskType::Empty:
239 return RecordType::Generic;
240 default:
241 QL_FAIL("Unexpected RiskType " << riskType);
242 }
243}
244
245} // namespace analytics
246} // namespace ore
Struct for holding a CRIF record.
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
const bm< CrifRecord::ProductClass > productClassMap
Definition: crifrecord.cpp:95
boost::bimap< T, boost::bimaps::set_of< string, string_cmp > > bm
Definition: riskfilter.cpp:42
CrifRecord::CurvatureScenario parseFrtbCurvatureScenario(const std::string &scenario)
Definition: crifrecord.cpp:152
CrifRecord::RiskType parseRiskType(const string &rt)
Definition: crifrecord.cpp:117
const bm< MarketRiskConfiguration::RiskType > riskTypeMap
Definition: riskfilter.cpp:53
CrifRecord::ProductClass parseProductClass(const string &pc)
Definition: crifrecord.cpp:127
RecordType type() const
Definition: crifrecord.cpp:186
static std::vector< std::set< std::string > > additionalHeaders
Definition: crifrecord.hpp:369
NettingSetDetails nettingSetDetails
Definition: crifrecord.hpp:156
CurvatureScenario
There are two entries for curvature risk in frtb, a up and down shift.
Definition: crifrecord.hpp:126