Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
historicalscenariofilereader.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
20
23#include <ql/errors.hpp>
24
28using std::string;
29using namespace QuantLib;
30
31namespace ore {
32namespace analytics {
33
35 const QuantLib::ext::shared_ptr<ScenarioFactory>& scenarioFactory)
36 : scenarioFactory_(scenarioFactory), file_(fileName, true), finished_(false) {
37
38 // Do some checks
39 QL_REQUIRE(file_.fields().size() >= 4, "Need at least 4 columns in the file " << fileName);
40 QL_REQUIRE(file_.fields()[0] == "Date", "First column must be 'Date' in the file " << fileName);
41 QL_REQUIRE(file_.fields()[1] == "Scenario", "Second column should be 'Scenario' in the file " << fileName);
42 QL_REQUIRE(file_.fields()[2] == "Numeraire", "Third column should be 'Numeraire' in the file " << fileName);
43
44 // Populate the risk factor keys vector
45 keys_.reserve(file_.fields().size() - 3);
46 for (Size k = 3; k < file_.fields().size(); ++k) {
47 keys_.push_back(parseRiskFactorKey(file_.fields()[k]));
48 }
49}
50
52 // Close the file
53 file_.close();
54 LOG("The file has been closed");
55}
56
58 finished_ = file_.next() ? false : true;
59 return !finished_;
60}
61
63 if (finished_) {
64 return Null<Date>();
65 } else {
66 return parseDate(file_.get("Date"));
67 }
68}
69
70QuantLib::ext::shared_ptr<ore::analytics::Scenario> HistoricalScenarioFileReader::scenario() const {
71 if (finished_) {
72 return nullptr;
73 } else {
74 Date date = parseDate(file_.get("Date"));
75 Real numeraire = parseReal(file_.get("Numeraire"));
76 TLOG("Creating scenario for date " << io::iso_date(date));
77 QuantLib::ext::shared_ptr<Scenario> scenario =
78 scenarioFactory_->buildScenario(date, true, std::string(), numeraire);
79 Real value;
80 for (Size k = 0; k < keys_.size(); ++k) {
82 scenario->add(keys_[k], value);
83 }
84 return scenario;
85 }
86}
87
88} // namespace analytics
89} // namespace ore
ore::data::CSVFileReader file_
Handle on the csv file.
HistoricalScenarioFileReader(const std::string &fileName, const QuantLib::ext::shared_ptr< ScenarioFactory > &scenarioFactory)
QuantLib::ext::shared_ptr< Scenario > scenario() const override
Return the current scenario if reader is still valid and nullptr otherwise.
bool finished_
Flag indicating if the reader has no more scenarios to read.
std::vector< RiskFactorKey > keys_
The risk factor keys of the scenarios in the file.
QuantLib::Date date() const override
Return the current scenario's date if reader is still valid and Null<Date>() otherwise.
QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory_
Scenario factory.
bool next() override
Return true if there is another Scenario to read and move to it.
void close() override
const std::vector< std::string > & fields() const
std::string get(const std::string &field) const
SafeStack< ValueType > value
bool tryParseReal(const string &s, QuantLib::Real &result)
Date parseDate(const string &s)
Real parseReal(const string &s)
Class for reading historical scenarios from file.
#define LOG(text)
#define TLOG(text)
RiskFactorKey parseRiskFactorKey(const string &str)
Definition: scenario.cpp:183
Size size(const ValueType &v)