Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crifloader.hpp
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/*! \file orea/simm/crifloader.hpp
20 \brief Class for loading CRIF records
21*/
22
23#pragma once
24
25#include <map>
26#include <orea/simm/crif.hpp>
30#include <tuple>
31
32#include <ql/types.hpp>
33
34namespace ore {
35namespace analytics {
36
37/*! A class for loading CRIF records. The records are aggregated and stored in a
38 SimmNetSensitivities object so that they can later be used in a SIMM calculation
39*/
41public:
42 /*! Constructor
43 We set the trade ID to an empty string if we are going to be netting at portfolio level.
44 This is the default. To override this the flag \p keepTradeId may be set to true.
45 */
46 CrifLoader(const QuantLib::ext::shared_ptr<SimmConfiguration>& configuration,
47 const std::vector<std::set<std::string>>& additionalHeaders = {},
48 bool updateMapper = false, bool aggregateTrades = true)
49 : configuration_(configuration), additionalHeaders_(additionalHeaders), updateMapper_(updateMapper),
50 aggregateTrades_(aggregateTrades) {}
51
52 /*! Destructor */
53 virtual ~CrifLoader() {}
54
55 virtual Crif loadCrif() {
56 auto crif = loadCrifImpl();
57 if (updateMapper_ && configuration_->bucketMapper() != nullptr) {
58 configuration_->bucketMapper()->updateFromCrif(crif);
59 }
60 return crif;
61 }
62
63 //! SIMM configuration getter
64 const QuantLib::ext::shared_ptr<SimmConfiguration>& simmConfiguration() { return configuration_; }
65
66protected:
67 virtual Crif loadCrifImpl() = 0;
68
69 void addRecordToCrif(Crif& crif, CrifRecord&& recordToAdd) const;
70
71 //! Check if the record is a valid Simm Crif Record
72 void validateSimmRecord(const CrifRecord& cr) const;
73 //! Override currency codes
74 void currencyOverrides(CrifRecord& crifRecord) const;
75 //! update bucket mappings
76 void updateMapping(const CrifRecord& cr) const;
77
78 //! Simm configuration that is used during loading of CRIF records
79 QuantLib::ext::shared_ptr<SimmConfiguration> configuration_;
80
81 //! Defines accepted column headers, beyond required and optional headers, see crifloader.cpp
82 std::vector<std::set<std::string>> additionalHeaders_;
83
84 /*! If true, the SIMM configuration's bucket mapper is updated during the
85 CRIF loading with the mapping from SIMM qualifier to SIMM bucket. This is
86 useful when consuming CRIF files from elsewhere in that it allows for
87 using the mapping that is already present in the external file.
88 */
90
91 /*! If true, aggregate over trade ids */
93
94 //! Map giving required CRIF file headers and their allowable alternatives
95 static std::map<QuantLib::Size, std::set<std::string>> requiredHeaders;
96
97 //! Map giving optional CRIF file headers and their allowable alternatives
98 static std::map<QuantLib::Size, std::set<std::string>> optionalHeaders;
99};
100
102public:
103 StringStreamCrifLoader(const QuantLib::ext::shared_ptr<SimmConfiguration>& configuration,
104 const std::vector<std::set<std::string>>& additionalHeaders = {}, bool updateMapper = false,
105 bool aggregateTrades = true, char eol = '\n', char delim = '\t', char quoteChar = '\0',
106 char escapeChar = '\\', const std::string& nullString = "#N/A");
107
108protected:
109 Crif loadCrifImpl() override { return loadFromStream(stream()); }
110
111 //! Core CRIF loader from generic istream
112 Crif loadFromStream(std::stringstream&& stream);
113
114 virtual std::stringstream stream() const = 0;
115 /*! Internal map from known index of CRIF record member to file column
116 For example, give trade ID an index of 0 and find the column index of
117 trade ID in the CRIF file e.g. n. The map entry would be [0, n]
118 */
119 std::map<QuantLib::Size, QuantLib::Size> columnIndex_;
120
121
122 std::map<QuantLib::Size, std::set<std::string>> additionalHeadersIndexMap_;
123
124 //! Process the elements of a header line of a CRIF file
125 void processHeader(const std::vector<std::string>& headers);
126
127 /*! Process a line of a CRIF file and return true if valid line
128 or false if an invalid line
129 */
130 bool process(const std::vector<std::string>& entries, QuantLib::Size maxIndex, QuantLib::Size currentLine, Crif& result);
131 char eol_;
132 char delim_;
135 std::string nullString_;
136};
137
139public:
140 CsvFileCrifLoader(const std::string& filename, const QuantLib::ext::shared_ptr<SimmConfiguration>& configuration,
141 const std::vector<std::set<std::string>>& additionalHeaders = {},
142 bool updateMapper = false, bool aggregateTrades = true, char eol = '\n', char delim = '\t',
143 char quoteChar = '\0', char escapeChar = '\\', const std::string& nullString = "#N/A")
144 : StringStreamCrifLoader(configuration, additionalHeaders, updateMapper, aggregateTrades, eol, delim, quoteChar,
145 escapeChar, nullString),
146 filename_(filename) {}
147
148protected:
149 std::string filename_;
150 std::stringstream stream() const override;
151};
152
154public:
155 CsvBufferCrifLoader(const std::string& buffer, const QuantLib::ext::shared_ptr<SimmConfiguration>& configuration,
156 const std::vector<std::set<std::string>>& additionalHeaders = {},
157 bool updateMapper = false, bool aggregateTrades = true, char eol = '\n', char delim = '\t',
158 char quoteChar = '\0', char escapeChar = '\\', const std::string& nullString = "#N/A")
159 : StringStreamCrifLoader(configuration, additionalHeaders, updateMapper, aggregateTrades, eol, delim, quoteChar,
160 escapeChar, nullString),
161 buffer_(buffer) {}
162
163protected:
164 std::string buffer_;
165 std::stringstream stream() const override;
166};
167
168} // namespace analytics
169} // namespace ore
void currencyOverrides(CrifRecord &crifRecord) const
Override currency codes.
Definition: crifloader.cpp:145
static std::map< QuantLib::Size, std::set< std::string > > requiredHeaders
Map giving required CRIF file headers and their allowable alternatives.
Definition: crifloader.hpp:95
QuantLib::ext::shared_ptr< SimmConfiguration > configuration_
Simm configuration that is used during loading of CRIF records.
Definition: crifloader.hpp:79
void updateMapping(const CrifRecord &cr) const
update bucket mappings
Definition: crifloader.cpp:186
static std::map< QuantLib::Size, std::set< std::string > > optionalHeaders
Map giving optional CRIF file headers and their allowable alternatives.
Definition: crifloader.hpp:98
virtual Crif loadCrifImpl()=0
CrifLoader(const QuantLib::ext::shared_ptr< SimmConfiguration > &configuration, const std::vector< std::set< std::string > > &additionalHeaders={}, bool updateMapper=false, bool aggregateTrades=true)
Definition: crifloader.hpp:46
void validateSimmRecord(const CrifRecord &cr) const
Check if the record is a valid Simm Crif Record.
Definition: crifloader.cpp:112
const QuantLib::ext::shared_ptr< SimmConfiguration > & simmConfiguration()
SIMM configuration getter.
Definition: crifloader.hpp:64
std::vector< std::set< std::string > > additionalHeaders_
Defines accepted column headers, beyond required and optional headers, see crifloader....
Definition: crifloader.hpp:82
void addRecordToCrif(Crif &crif, CrifRecord &&recordToAdd) const
Definition: crifloader.cpp:95
std::stringstream stream() const override
Definition: crifloader.cpp:227
CsvBufferCrifLoader(const std::string &buffer, const QuantLib::ext::shared_ptr< SimmConfiguration > &configuration, const std::vector< std::set< std::string > > &additionalHeaders={}, bool updateMapper=false, bool aggregateTrades=true, char eol='\n', char delim='\t', char quoteChar='\0', char escapeChar='\\', const std::string &nullString="#N/A")
Definition: crifloader.hpp:155
CsvFileCrifLoader(const std::string &filename, const QuantLib::ext::shared_ptr< SimmConfiguration > &configuration, const std::vector< std::set< std::string > > &additionalHeaders={}, bool updateMapper=false, bool aggregateTrades=true, char eol='\n', char delim='\t', char quoteChar='\0', char escapeChar='\\', const std::string &nullString="#N/A")
Definition: crifloader.hpp:140
std::stringstream stream() const override
Definition: crifloader.cpp:216
std::map< QuantLib::Size, QuantLib::Size > columnIndex_
Definition: crifloader.hpp:119
bool process(const std::vector< std::string > &entries, QuantLib::Size maxIndex, QuantLib::Size currentLine, Crif &result)
Definition: crifloader.cpp:336
virtual std::stringstream stream() const =0
std::map< QuantLib::Size, std::set< std::string > > additionalHeadersIndexMap_
Definition: crifloader.hpp:122
Crif loadFromStream(std::stringstream &&stream)
Core CRIF loader from generic istream.
Definition: crifloader.cpp:233
void processHeader(const std::vector< std::string > &headers)
Process the elements of a header line of a CRIF file.
Definition: crifloader.cpp:284
Struct for holding CRIF records.
SIMM configuration interface.