Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensitivityfilestream.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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
23#include <ql/errors.hpp>
24
25#include <boost/algorithm/string.hpp>
26
30using std::getline;
31using std::string;
32using std::vector;
33
34namespace ore {
35namespace analytics {
36
37void SensitivityInputStream::setStream(std::istream* stream) {
38 stream_ = stream;
39}
40
42 // Get the next valid SensitivityRecord
43 string line;
44 while (getline(*stream_, line)) {
45 // Update the current line number
46 ++lineNo_;
47
48 // Trim leading and trailing space
49 boost::trim(line);
50
51 // If line is empty or a comment line, skip to next
52 if (line.empty() || boost::starts_with(line, comment_))
53 continue;
54
55 // Try to parse line in to a SensitivityRecord
56 DLOG("Processing line number " << lineNo_ << ": " << line);
57 vector<string> entries;
58 boost::split(
59 entries, line, [this](char c) { return c == delim_; }, boost::token_compress_off);
60 return processRecord(entries);
61 }
62
63 // If we get to here, no more lines to process so return empty record
64 return SensitivityRecord();
65}
66
68 // Reset to beginning of file and line number
69 stream_->clear();
70 stream_->seekg(0, std::ios::beg);
71 lineNo_ = 0;
72}
73
74SensitivityRecord SensitivityInputStream::processRecord(const vector<string>& entries) const {
75
76 QL_REQUIRE(entries.size() == 10, "On line number " << lineNo_ << ": A sensitivity record needs 10 entries");
77
79 sr.tradeId = entries[0];
80 sr.isPar = parseBool(entries[1]);
81
82 auto p = deconstructFactor(entries[2]);
83 sr.key_1 = p.first;
84 sr.desc_1 = p.second;
85 tryParseReal(entries[3], sr.shift_1);
86
87 p = deconstructFactor(entries[4]);
88 sr.key_2 = p.first;
89 sr.desc_2 = p.second;
90 tryParseReal(entries[5], sr.shift_2);
91
92 sr.currency = entries[6];
93 sr.baseNpv = parseReal(entries[7]);
94 sr.delta = parseReal(entries[8]);
95 tryParseReal(entries[9], sr.gamma); // might be #N/A, if not computed
96
97 return sr;
98}
99
100SensitivityFileStream::SensitivityFileStream(const string& fileName, char delim, const string& comment)
101 : SensitivityInputStream(delim, comment) {
102
103 // set file name
104 file_ = new std::ifstream(fileName);
105 QL_REQUIRE(file_->is_open(), "error opening file " << fileName);
106 LOG("The file " << fileName << " has been opened for streaming");
107
108 // pass stream to function set stream
110}
111
113 // Close the file if still open
114 if (file_->is_open()) {
115 file_->close();
116 }
117 LOG("The file stream has been closed");
118}
119
120SensitivityBufferStream::SensitivityBufferStream(const std::string& buffer, char delim, const std::string& comment)
121 : SensitivityInputStream(delim, comment) {
122 std::stringstream* stream = new std::stringstream(buffer);
123 setStream(stream);
124}
125
126} // namespace analytics
127} // namespace ore
SensitivityBufferStream(const std::string &buffer, char delim=',', const std::string &comment="#")
Constructor providing path to csv file fileName.
SensitivityFileStream(const std::string &fileName, char delim=',', const std::string &comment="#")
Constructor providing path to csv file fileName.
Class for streaming SensitivityRecords from csv file.
QuantLib::Size lineNo_
Keep track of line number for messages.
std::istream * stream_
Handle on the stram.
std::string comment_
Csv file comment string.
SensitivityRecord processRecord(const std::vector< std::string > &entries) const
Create a record from a collection of strings.
void reset() override
Resets the stream so that SensitivityRecord objects can be streamed again.
SensitivityRecord next() override
Returns the next SensitivityRecord in the stream.
bool tryParseReal(const string &s, QuantLib::Real &result)
bool parseBool(const string &s)
Real parseReal(const string &s)
#define LOG(text)
#define DLOG(text)
pair< RiskFactorKey, string > deconstructFactor(const string &factor)
Class for streaming SensitivityRecords from file.
Shift scenario generation.